lazutils: FindDefaultExecutablePath: fixed using env PATH with quotes under Windows

git-svn-id: branches/fixes_2_0@63368 -
This commit is contained in:
mattias 2020-06-17 09:55:25 +00:00
parent ded3842a35
commit 55770eb7c7
2 changed files with 45 additions and 16 deletions

View File

@ -200,8 +200,8 @@ begin
Result:=LazFileUtils.CreateAbsolutePath(Filename, BaseDirectory);
end;
function CopyFile(const SrcFilename, DestFilename: String;
Flags: TCopyFileFlags=[cffOverwriteFile]; ExceptionOnError: Boolean=False): Boolean;
function CopyFile(const SrcFilename, DestFilename: string;
Flags: TCopyFileFlags; ExceptionOnError: Boolean): boolean;
var
SrcHandle: THandle;
DestHandle: THandle;
@ -269,7 +269,8 @@ begin
end;
end;
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: Boolean; ExceptionOnError: Boolean): boolean;
function CopyFile(const SrcFilename, DestFilename: string;
PreserveTime: boolean; ExceptionOnError: Boolean): boolean;
// Flags parameter can be used for the same thing.
var
Flags: TCopyFileFlags;
@ -354,7 +355,7 @@ begin
{$ENDIF}
end;
function ReadFileToString(const Filename: String): String;
function ReadFileToString(const Filename: string): string;
var
SrcHandle: THandle;
ReadCount: LongInt;
@ -381,10 +382,10 @@ begin
end;
end;
function SearchFileInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): string;
function SearchFileInPath(const Filename, BasePath: string; SearchPath: string;
const Delimiter: string; Flags: TSearchFileInPathFlags): string;
var
p, StartPos, l: integer;
p, StartPos, l, QuoteStart: integer;
CurPath, Base: string;
begin
//debugln('[SearchFileInPath] Filename="',Filename,'" BasePath="',BasePath,'" SearchPath="',SearchPath,'" Delimiter="',Delimiter,'"');
@ -411,8 +412,27 @@ begin
l:=length(SearchPath);
while StartPos<=l do begin
p:=StartPos;
while (p<=l) and (pos(SearchPath[p],Delimiter)<1) do inc(p);
CurPath:=TrimFilename(copy(SearchPath,StartPos,p-StartPos));
while (p<=l) and (pos(SearchPath[p],Delimiter)<1) do
begin
if (SearchPath[p]='"') and (sffDequoteSearchPath in Flags) then
begin
// For example: Windows allows set path=C:\"a;b c"\d;%path%
QuoteStart:=p;
repeat
inc(p);
until (p>l) or (SearchPath[p]='"');
if p<=l then
begin
system.delete(SearchPath,p,1);
system.delete(SearchPath,QuoteStart,1);
dec(l,2);
dec(p,2);
end;
end;
inc(p);
end;
CurPath:=copy(SearchPath,StartPos,p-StartPos);
CurPath:=TrimFilename(CurPath);
if CurPath<>'' then begin
if not FilenameIsAbsolute(CurPath) then
CurPath:=Base+CurPath;
@ -584,8 +604,6 @@ end;
function FindDefaultExecutablePath(const Executable: string;
const BaseDir: string): string;
const
Flags : TSearchFileInPathFlags = [{$IFDEF Unix}sffDontSearchInBasePath{$ENDIF}];
var
Env: string;
begin
@ -600,11 +618,11 @@ begin
{$ENDIF}
end else begin
Env:=GetEnvironmentVariableUTF8('PATH');
Result:=SearchFileInPath(Executable, BaseDir, Env, PathSeparator, Flags);
Result:=SearchFileInPath(Executable, BaseDir, Env, PathSeparator, sffFindProgramInPath);
if Result<>'' then exit;
{$IFDEF Windows}
if ExtractFileExt(Executable)='' then begin
Result:=SearchFileInPath(Executable+'.exe', BaseDir, Env, PathSeparator, Flags);
Result:=SearchFileInPath(Executable+'.exe', BaseDir, Env, PathSeparator, sffFindProgramInPath);
if Result<>'' then exit;
end;
{$ENDIF}

View File

@ -77,12 +77,23 @@ function ReadFileToString(const Filename: string): string;
type
TSearchFileInPathFlag = (
sffDontSearchInBasePath, // do not search in BasePath, search only in SearchPath.
sffSearchLoUpCase
sffSearchLoUpCase,
sffFile, // must be file, not directory
sffExecutable, // file must be executable
sffDequoteSearchPath // ansi dequote
);
TSearchFileInPathFlags = set of TSearchFileInPathFlag;
const
sffFindProgramInPath = [
{$IFDEF Unix}sffDontSearchInBasePath,{$ENDIF}
{$IFDEF Windows}sffDequoteSearchPath,{$ENDIF}
sffFile,
sffExecutable
];
function SearchFileInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): string; overload;
function SearchFileInPath(const Filename, BasePath: string;
SearchPath: string; const Delimiter: string;
Flags: TSearchFileInPathFlags): string; overload;
function SearchAllFilesInPath(const Filename, BasePath, SearchPath,
Delimiter: string; Flags: TSearchFileInPathFlags): TStrings;
function FindDiskFilename(const Filename: string): string;