style modernisation using helpers for file name manip

This commit is contained in:
Basile Burg 2016-01-18 07:24:12 +01:00
parent bc295d3939
commit 5e2a1b8290
12 changed files with 82 additions and 52 deletions

View File

@ -48,9 +48,15 @@ type
function isNotNil: boolean; function isNotNil: boolean;
end; end;
// sugar for strings
TStringHelper = type helper for string TStringHelper = type helper for string
function isEmpty: boolean; function isEmpty: boolean;
function isNotEmpty: boolean; function isNotEmpty: boolean;
function extractFileName: string;
function extractFileExt: string;
function extractFilePath: string;
function fileExists: boolean;
function dirExists: boolean;
end; end;
(** (**
@ -331,6 +337,31 @@ begin
exit(self <> ''); exit(self <> '');
end; end;
function TStringHelper.extractFileName: string;
begin
exit(sysutils.extractFileName(self));
end;
function TStringHelper.extractFileExt: string;
begin
exit(sysutils.extractFileExt(self));
end;
function TStringHelper.extractFilePath: string;
begin
exit(sysutils.extractFilePath(self));
end;
function TStringHelper.fileExists: boolean;
begin
exit(sysutils.FileExists(self));
end;
function TStringHelper.dirExists: boolean;
begin
exit(sysutils.DirectoryExists(self));
end;
{$IFDEF LINUX} {$IFDEF LINUX}
constructor TCheckedAsyncProcess.Create(aOwner: TComponent); constructor TCheckedAsyncProcess.Create(aOwner: TComponent);
begin begin
@ -399,7 +430,7 @@ begin
str1.WriteComponent(aComp); str1.WriteComponent(aComp);
str1.Position := 0; str1.Position := 0;
ObjectBinaryToText(str1,str2); ObjectBinaryToText(str1,str2);
ForceDirectories(ExtractFilePath(aFilename)); ForceDirectories(aFilename.extractFilePath);
str2.SaveToFile(aFilename); str2.SaveToFile(aFilename);
finally finally
str1.Free; str1.Free;
@ -777,16 +808,16 @@ begin
if ext.isEmpty then if ext.isEmpty then
anExeName += exeExt; anExeName += exeExt;
//full path already specified //full path already specified
if FileExists(anExeName) and (not FileExists(ExtractFileName(anExeName))) then if anExeName.fileExists and (not anExeName.extractFileName.fileExists) then
exit(anExeName); exit(anExeName);
// //
env := sysutils.GetEnvironmentVariable('PATH'); env := sysutils.GetEnvironmentVariable('PATH');
// maybe in current dir // maybe in current dir
if FileExists(anExeName) then if anExeName.fileExists then
env += PathSeparator + GetCurrentDir; env += PathSeparator + GetCurrentDir;
{$IFNDEF CEBUILD} {$IFNDEF CEBUILD}
if Application <> nil then if Application <> nil then
env += PathSeparator + ExtractFileDir(ExtractFilePath(application.ExeName)); env += PathSeparator + ExtractFileDir(application.ExeName.ExtractFilePath);
{$ENDIF} {$ENDIF}
exit(ExeSearch(anExeName, env)); exit(ExeSearch(anExeName, env));
end; end;
@ -950,7 +981,7 @@ begin
try try
sink.Assign(someFiles); sink.Assign(someFiles);
for i := sink.Count-1 downto 0 do for i := sink.Count-1 downto 0 do
if (not FileExists(sink.Strings[i])) and (not DirectoryExists(sink.Strings[i])) then if (not sink.Strings[i].fileExists) and (not DirectoryExists(sink.Strings[i])) then
sink.Delete(i); sink.Delete(i);
// folders count // folders count
cnt := 256; cnt := 256;

View File

@ -144,7 +144,7 @@ begin
try try
for i:= 0 to fProj.sourcesCount-1 do for i:= 0 to fProj.sourcesCount-1 do
begin begin
fold := extractFilePath(fProj.sourceAbsolute(i)); fold := fProj.sourceAbsolute(i).extractFilePath;
if folds.IndexOf(fold) = -1 then if folds.IndexOf(fold) = -1 then
folds.Add(fold); folds.Add(fold);
end; end;

View File

@ -443,7 +443,7 @@ begin
fTokList.Clear; fTokList.Clear;
fErrList.Clear; fErrList.Clear;
end; end;
if md.isEmpty then md := extractFileName(fDoc.fileName); if md.isEmpty then md := fDoc.fileName.extractFileName;
pageControl.currentPage.Caption := md; pageControl.currentPage.Caption := md;
end; end;
end; end;
@ -481,7 +481,7 @@ begin
fErrList.Clear; fErrList.Clear;
end; end;
if md.isEmpty then if md.isEmpty then
md := extractFileName(fDoc.fileName); md := fDoc.fileName.extractFileName;
pageControl.currentPage.Caption := md; pageControl.currentPage.Caption := md;
end; end;
{$ENDREGION} {$ENDREGION}

View File

@ -189,7 +189,7 @@ begin
case fType of case fType of
ptFile: ptFile:
with TOpenDialog.create(nil) do try with TOpenDialog.create(nil) do try
InitialDir := ExtractFileName(GetValue); InitialDir := GetValue.extractFileName;
FileName := GetValue; FileName := GetValue;
if Execute then SetValue(FileName); if Execute then SetValue(FileName);
finally finally

View File

@ -438,13 +438,13 @@ begin
filename := ini; filename := ini;
if Execute then if Execute then
begin begin
if not fileExists(filename) then if not filename.fileExists then
List.Selected.SubItems[0] := extractFilePath(filename) List.Selected.SubItems[0] := filename.extractFilePath
else else
begin begin
List.Selected.SubItems[0] := filename; List.Selected.SubItems[0] := filename;
if (List.Selected.Caption.isEmpty) or (List.Selected.Caption = notav) then if (List.Selected.Caption.isEmpty) or (List.Selected.Caption = notav) then
List.Selected.Caption := ChangeFileExt(extractFileName(filename), ''); List.Selected.Caption := ChangeFileExt(filename.extractFileName, '');
end; end;
end; end;
finally finally

View File

@ -1470,7 +1470,7 @@ object CEMainForm: TCEMainForm
OnCloseQuery = FormCloseQuery OnCloseQuery = FormCloseQuery
OnDropFiles = FormDropFiles OnDropFiles = FormDropFiles
ShowHint = True ShowHint = True
LCLVersion = '1.6.0.1' LCLVersion = '1.6.0.2'
Visible = False Visible = False
object mainMenu: TMainMenu object mainMenu: TMainMenu
Images = imgList Images = imgList

View File

@ -1077,9 +1077,8 @@ begin
getprocInputHandler.addProcess(nil); getprocInputHandler.addProcess(nil);
end; end;
killProcess(fRunProc); killProcess(fRunProc);
if fileExists(fname) then if fname.fileExists and (fname.extractFilePath = GetTempDir(false)) then
if ExtractFilePath(fname) = GetTempDir(false) then sysutils.DeleteFile(fname);
sysutils.DeleteFile(fname);
end; end;
procedure TCEMainForm.SaveLastDocsAndProj; procedure TCEMainForm.SaveLastDocsAndProj;
@ -1504,7 +1503,7 @@ begin
if not fileExists(fProjectInterface.filename) then exit; if not fileExists(fProjectInterface.filename) then exit;
// //
DockMaster.GetAnchorSite(fExplWidg).Show; DockMaster.GetAnchorSite(fExplWidg).Show;
fExplWidg.expandPath(extractFilePath(fProjectInterface.filename)); fExplWidg.expandPath(fProjectInterface.filename.extractFilePath);
end; end;
procedure TCEMainForm.actFileNewExecute(Sender: TObject); procedure TCEMainForm.actFileNewExecute(Sender: TObject);
@ -1856,7 +1855,7 @@ begin
dmdproc.Options := [poUsePipes, poStderrToOutPut]; dmdproc.Options := [poUsePipes, poStderrToOutPut];
dmdproc.Executable := 'dmd'; dmdproc.Executable := 'dmd';
dmdproc.Parameters.Add(fDoc.fileName); dmdproc.Parameters.Add(fDoc.fileName);
dmdproc.Parameters.Add('-J' + ExtractFilePath(fDoc.fileName)); dmdproc.Parameters.Add('-J' + fDoc.fileName.extractFilePath);
dmdproc.Parameters.AddText(fRunnableSw); dmdproc.Parameters.AddText(fRunnableSw);
CommandToList(firstlineFlags, extraArgs); CommandToList(firstlineFlags, extraArgs);
dmdproc.Parameters.AddStrings(extraArgs); dmdproc.Parameters.AddStrings(extraArgs);
@ -1934,7 +1933,7 @@ begin
if not fileExists(fDoc.fileName) then exit; if not fileExists(fDoc.fileName) then exit;
// //
DockMaster.GetAnchorSite(fExplWidg).Show; DockMaster.GetAnchorSite(fExplWidg).Show;
fExplWidg.expandPath(extractFilePath(fDoc.fileName)); fExplWidg.expandPath(fDoc.fileName.extractFilePath);
end; end;
procedure TCEMainForm.actProjCompileExecute(Sender: TObject); procedure TCEMainForm.actProjCompileExecute(Sender: TObject);
@ -2045,7 +2044,7 @@ begin
DockMaster.GetAnchorSite(fWidgList.widget[i]).Close; DockMaster.GetAnchorSite(fWidgList.widget[i]).Close;
end; end;
// //
forceDirectory(extractFilePath(aFilename)); forceDirectory(aFilename.extractFilePath);
xcfg := TXMLConfigStorage.Create(aFilename + '.tmp', false); xcfg := TXMLConfigStorage.Create(aFilename + '.tmp', false);
try try
DockMaster.SaveLayoutToConfig(xcfg); DockMaster.SaveLayoutToConfig(xcfg);
@ -2088,7 +2087,7 @@ begin
for i := 0 to lst.Count-1 do for i := 0 to lst.Count-1 do
begin begin
itm := TMenuItem.Create(self); itm := TMenuItem.Create(self);
itm.Caption := extractFileName(lst.Strings[i]); itm.Caption := lst.Strings[i].extractFileName;
itm.Caption := stripFileExt(itm.Caption); itm.Caption := stripFileExt(itm.Caption);
itm.OnClick := @layoutMnuItemClick; itm.OnClick := @layoutMnuItemClick;
itm.ImageIndex := 32; itm.ImageIndex := 32;
@ -2112,8 +2111,8 @@ begin
if not InputQuery('New layout name', '', fname) then if not InputQuery('New layout name', '', fname) then
exit; exit;
// //
fname := extractFileName(fname); fname := fname.extractFileName;
if extractFileExt(fname) <> '.xml' then if fname.extractFileExt <> '.xml' then
fname += '.xml'; fname += '.xml';
layoutSaveToFile(getCoeditDocPath + 'layouts' + DirectorySeparator + fname); layoutSaveToFile(getCoeditDocPath + 'layouts' + DirectorySeparator + fname);
layoutUpdateMenu; layoutUpdateMenu;

View File

@ -320,7 +320,7 @@ begin
lstFiles.BeginUpdate; lstFiles.BeginUpdate;
for fname in aList do for fname in aList do
begin begin
itemText := extractFileName(fname); itemText := fname.extractFileName;
if noFilter or AnsiContainsText(itemText,lstFilter.Filter) then if noFilter or AnsiContainsText(itemText,lstFilter.Filter) then
begin begin
itm := lstFiles.Items.Add; itm := lstFiles.Items.Add;
@ -484,7 +484,7 @@ begin
listFolders(lst, PString(aRoot.Data)^ + directorySeparator); listFolders(lst, PString(aRoot.Data)^ + directorySeparator);
for fold in lst do for fold in lst do
begin begin
itm := Tree.Items.AddChild(aRoot, extractFileName(fold)); itm := Tree.Items.AddChild(aRoot, fold.extractFileName);
itm.Data := NewStr(fold); itm.Data := NewStr(fold);
itm.ImageIndex := 1; itm.ImageIndex := 1;
itm.SelectedIndex := 1; itm.SelectedIndex := 1;

View File

@ -228,7 +228,7 @@ begin
fFilename := aValue; fFilename := aValue;
oldBase := fBasePath; oldBase := fBasePath;
fBasePath := extractFilePath(fFilename); fBasePath := fFilename.extractFilePath;
// //
for i:= 0 to fSrcs.Count-1 do for i:= 0 to fSrcs.Count-1 do
begin begin
@ -414,7 +414,7 @@ begin
begin begin
abs := expandFilenameEx(fBasePath, rel); abs := expandFilenameEx(fBasePath, rel);
if ex_files.IndexOf(abs) = -1 then if ex_files.IndexOf(abs) = -1 then
if ex_folds.IndexOf(ExtractFilePath(abs)) = -1 if ex_folds.IndexOf(abs.extractFilePath) = -1
then aList.Add(abs); // note: process.inc ln 249. double quotes are added if there's a space. then aList.Add(abs); // note: process.inc ln 249. double quotes are added if there's a space.
end; end;
// libraries: an asterisk in list selects all the entries // libraries: an asterisk in list selects all the entries
@ -493,7 +493,7 @@ var
dirHint := fSrcs.Strings[i]; dirHint := fSrcs.Strings[i];
while (dirHint[1] = '.') or (dirHint[1] = DirectorySeparator) do while (dirHint[1] = '.') or (dirHint[1] = DirectorySeparator) do
dirHint := dirHint[2..length(dirHint)]; dirHint := dirHint[2..length(dirHint)];
ini := extractFilePath(fFilename); ini := fFilename.extractFilePath;
if not selectDirectory( format('select the folder (that contains "%s")',[dirHint]), ini, newdir) then if not selectDirectory( format('select the folder (that contains "%s")',[dirHint]), ini, newdir) then
exit; exit;
for i := 0 to fSrcs.Count-1 do for i := 0 to fSrcs.Count-1 do
@ -524,11 +524,11 @@ var
// //
opendlg := TOpenDialog.Create(nil); opendlg := TOpenDialog.Create(nil);
try try
opendlg.InitialDir := extractFilePath(fFilename); opendlg.InitialDir := fFilename.extractFilePath;
opendlg.FileName := fSrcs[i]; opendlg.FileName := fSrcs[i];
if opendlg.execute then if opendlg.execute then
begin begin
if ExtractFileName(oldsrc) <> ExtractFileName(opendlg.filename) then if oldsrc.extractFileName <> opendlg.filename.extractFileName then
if dlgOkCancel('the filenames are different, replace the old file ?') <> mrOk then if dlgOkCancel('the filenames are different, replace the old file ?') <> mrOk then
continue; continue;
fSrcs[i] := ExtractRelativepath(fBasePath, opendlg.Filename); fSrcs[i] := ExtractRelativepath(fBasePath, opendlg.Filename);
@ -625,10 +625,10 @@ begin
else if Sources.Count > 0 then else if Sources.Count > 0 then
begin begin
// ideally, main() should be searched for, when project binaryKind is executable // ideally, main() should be searched for, when project binaryKind is executable
fOutputFilename := extractFilename(Sources.Strings[0]); fOutputFilename := Sources.Strings[0].extractFileName;
fOutputFilename := stripFileExt(fOutputFilename); fOutputFilename := stripFileExt(fOutputFilename);
if FileExists(fileName) then if fileName.fileExists then
fOutputFilename := extractFilePath(fileName) + fOutputFilename fOutputFilename := fileName.extractFilePath + fOutputFilename
else else
fOutputFilename := GetTempDir(false) + fOutputFilename; fOutputFilename := GetTempDir(false) + fOutputFilename;
// force extension // force extension
@ -701,7 +701,7 @@ begin
msgs.clearByData(self as ICECommonProject); msgs.clearByData(self as ICECommonProject);
subjProjCompiling(fProjectSubject, Self); subjProjCompiling(fProjectSubject, Self);
// //
prjpath := extractFilePath(fFileName); prjpath := fFileName.extractFilePath;
oldCwd := GetCurrentDir; oldCwd := GetCurrentDir;
SetCurrentDir(prjpath); SetCurrentDir(prjpath);
// //
@ -789,7 +789,7 @@ begin
if fRunner.CurrentDirectory.isEmpty then if fRunner.CurrentDirectory.isEmpty then
begin begin
fRunnerOldCwd := GetCurrentDir; fRunnerOldCwd := GetCurrentDir;
cwd := extractFilePath(fRunner.Executable); cwd := fRunner.Executable.extractFilePath;
chDir(cwd); chDir(cwd);
fRunner.CurrentDirectory := cwd; fRunner.CurrentDirectory := cwd;
end; end;

View File

@ -28,7 +28,7 @@ inherited CEProjectInspectWidget: TCEProjectInspectWidget
Align = alClient Align = alClient
AutoExpand = True AutoExpand = True
BorderSpacing.Around = 2 BorderSpacing.Around = 2
DefaultItemHeight = 16 DefaultItemHeight = 18
Images = imgList Images = imgList
ReadOnly = True ReadOnly = True
RightClickSelect = True RightClickSelect = True

View File

@ -243,9 +243,9 @@ begin
// //
with TOpenDialog.Create(nil) do with TOpenDialog.Create(nil) do
try try
if FileExists(fLastFileOrFolder) then if fLastFileOrFolder.fileExists then
InitialDir := ExtractFilePath(fLastFileOrFolder) InitialDir := fLastFileOrFolder.extractFilePath
else if DirectoryExists(fLastFileOrFolder) then else if fLastFileOrFolder.dirExists then
InitialDir := fLastFileOrFolder; InitialDir := fLastFileOrFolder;
filter := DdiagFilter; filter := DdiagFilter;
if execute then if execute then
@ -268,12 +268,12 @@ begin
if fProject.isNil then exit; if fProject.isNil then exit;
// //
dir := ''; dir := '';
if FileExists(fLastFileOrFolder) then if fLastFileOrFolder.fileExists then
dir := extractFilePath(fLastFileOrFolder) dir := fLastFileOrFolder.extractFilePath
else if DirectoryExists(fLastFileOrFolder) then else if fLastFileOrFolder.dirExists then
dir := fLastFileOrFolder dir := fLastFileOrFolder
else if fileExists(fProject.fileName) then else if fProject.fileName.fileExists then
dir := extractFilePath(fProject.fileName); dir := fProject.fileName.extractFilePath;
if selectDirectory('sources', dir, dir, true, 0) then if selectDirectory('sources', dir, dir, true, 0) then
begin begin
fProject.beginUpdate; fProject.beginUpdate;
@ -306,12 +306,12 @@ begin
i := fProject.Sources.IndexOf(fname); i := fProject.Sources.IndexOf(fname);
if i = -1 then exit; if i = -1 then exit;
fname := fProject.sourceAbsolute(i); fname := fProject.sourceAbsolute(i);
dir := extractFilePath(fname); dir := fname.extractFilePath;
if not DirectoryExists(dir) then exit; if not dir.dirExists then exit;
// //
fProject.beginUpdate; fProject.beginUpdate;
for i:= fProject.Sources.Count-1 downto 0 do for i:= fProject.Sources.Count-1 downto 0 do
if extractFilePath(fProject.sourceAbsolute(i)) = dir then if fProject.sourceAbsolute(i).extractFilePath = dir then
fProject.Sources.Delete(i); fProject.Sources.Delete(i);
fProject.endUpdate; fProject.endUpdate;
end; end;

View File

@ -167,14 +167,14 @@ begin
// //
// application // application
fSymbols[CAF] := Application.ExeName; fSymbols[CAF] := Application.ExeName;
fSymbols[CAP] := ExtractFilePath(fSymbols[CAF]); fSymbols[CAP] := fSymbols[CAF].extractFilePath;
// document // document
if hasDoc then if hasDoc then
begin begin
if not fileExists(fDoc.fileName) then if not fileExists(fDoc.fileName) then
fDoc.saveTempFile; fDoc.saveTempFile;
fSymbols[CFF] := fDoc.fileName; fSymbols[CFF] := fDoc.fileName;
fSymbols[CFP] := ExtractFilePath(fDoc.fileName); fSymbols[CFP] := fDoc.fileName.extractFilePath;
if fDoc.Identifier.isNotEmpty then if fDoc.Identifier.isNotEmpty then
fSymbols[CI] := fDoc.Identifier; fSymbols[CI] := fDoc.Identifier;
end; end;
@ -183,8 +183,8 @@ begin
begin begin
fname := fProjInterface.filename; fname := fProjInterface.filename;
fSymbols[CPF] := fname; fSymbols[CPF] := fname;
fSymbols[CPP] := ExtractFilePath(fSymbols[CPF]); fSymbols[CPP] := fSymbols[CPF].extractFilePath;
fSymbols[CPN] := stripFileExt(extractFileName(fSymbols[CPF])); fSymbols[CPN] := stripFileExt(fSymbols[CPF].extractFileName);
fSymbols[CPO] := fProjInterface.outputFilename; fSymbols[CPO] := fProjInterface.outputFilename;
if fProjInterface.sourcesCount <> 0 then if fProjInterface.sourcesCount <> 0 then
begin begin
@ -193,7 +193,7 @@ begin
for i := 0 to fProjInterface.sourcesCount-1 do for i := 0 to fProjInterface.sourcesCount-1 do
begin begin
fname := fProjInterface.sourceAbsolute(i); fname := fProjInterface.sourceAbsolute(i);
if not isEditable(ExtractFileExt(fname)) then if not isEditable(fname.extractFileExt) then
continue; continue;
str.Add(fname); str.Add(fname);
end; end;