mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-05-14 18:52:51 +02:00
FileUtil: inline AppendPathDelim, TrimFilename, CleanAndExpandFilename and CleanAndExpandDirectory
to their LazFileUtils couterparts. git-svn-id: trunk@41297 -
This commit is contained in:
parent
d79f871ec1
commit
c990f3d7c8
@ -318,183 +318,12 @@ end;
|
||||
|
||||
function AppendPathDelim(const Path: string): string;
|
||||
begin
|
||||
if (Path<>'') and (Path[length(Path)]<>PathDelim) then
|
||||
Result:=Path+PathDelim
|
||||
else
|
||||
Result:=Path;
|
||||
Result := LazFileUtils.AppendPathDelim(Path);
|
||||
end;
|
||||
|
||||
function TrimFilename(const AFilename: string): string;
|
||||
// trim double path delims, heading and trailing spaces
|
||||
// and special dirs . and ..
|
||||
|
||||
function FilenameIsTrimmed(const TheFilename: string): boolean;
|
||||
var
|
||||
l: Integer;
|
||||
i: Integer;
|
||||
begin
|
||||
Result:=false;
|
||||
if TheFilename='' then begin
|
||||
Result:=true;
|
||||
exit;
|
||||
end;
|
||||
// check heading spaces
|
||||
if TheFilename[1]=' ' then exit;
|
||||
// check trailing spaces
|
||||
l:=length(TheFilename);
|
||||
if TheFilename[l]=' ' then exit;
|
||||
i:=1;
|
||||
while i<=l do begin
|
||||
case TheFilename[i] of
|
||||
|
||||
PathDelim:
|
||||
// check for double path delimiter
|
||||
if (i<l) and (TheFilename[i+1]=PathDelim) then exit;
|
||||
|
||||
'.':
|
||||
if (i=1) or (TheFilename[i-1]=PathDelim) then begin
|
||||
// check for . directories
|
||||
if ((i<l) and (TheFilename[i+1]=PathDelim)) or ((i=l) and (i>1)) then exit;
|
||||
// check for .. directories
|
||||
if (i<l) and (TheFilename[i+1]='.')
|
||||
and ((i+1=l) or ((i+2<=l) and (TheFilename[i+2]=PathDelim))) then exit;
|
||||
end;
|
||||
|
||||
end;
|
||||
inc(i);
|
||||
end;
|
||||
Result:=true;
|
||||
end;
|
||||
|
||||
var SrcPos, DestPos, l, DirStart: integer;
|
||||
c: char;
|
||||
MacroPos: LongInt;
|
||||
begin
|
||||
Result:=AFilename;
|
||||
if FilenameIsTrimmed(Result) then exit;
|
||||
|
||||
l:=length(AFilename);
|
||||
SrcPos:=1;
|
||||
DestPos:=1;
|
||||
|
||||
// skip trailing spaces
|
||||
while (l>=1) and (AFilename[l]=' ') do dec(l);
|
||||
|
||||
// skip heading spaces
|
||||
while (SrcPos<=l) and (AFilename[SrcPos]=' ') do inc(SrcPos);
|
||||
|
||||
// trim double path delims and special dirs . and ..
|
||||
while (SrcPos<=l) do begin
|
||||
c:=AFilename[SrcPos];
|
||||
// check for double path delims
|
||||
if (c=PathDelim) then begin
|
||||
inc(SrcPos);
|
||||
{$IFDEF WINDOWS}
|
||||
if (DestPos>2)
|
||||
{$ELSE}
|
||||
if (DestPos>1)
|
||||
{$ENDIF}
|
||||
and (Result[DestPos-1]=PathDelim) then begin
|
||||
// skip second PathDelim
|
||||
continue;
|
||||
end;
|
||||
Result[DestPos]:=c;
|
||||
inc(DestPos);
|
||||
continue;
|
||||
end;
|
||||
// check for special dirs . and ..
|
||||
if (c='.') then begin
|
||||
if (SrcPos<l) then begin
|
||||
if (AFilename[SrcPos+1]=PathDelim)
|
||||
and ((DestPos=1) or (AFilename[SrcPos-1]=PathDelim)) then begin
|
||||
// special dir ./
|
||||
// -> skip
|
||||
inc(SrcPos,2);
|
||||
continue;
|
||||
end else if (AFilename[SrcPos+1]='.')
|
||||
and (SrcPos+1=l) or (AFilename[SrcPos+2]=PathDelim) then
|
||||
begin
|
||||
// special dir ..
|
||||
// 1. .. -> keep
|
||||
// 2. /.. -> skip .., keep /
|
||||
// 3. C:.. -> keep
|
||||
// 4. C:\.. -> skip .., keep C:\
|
||||
// 5. \\.. -> skip .., keep \\
|
||||
// 6. xxx../.. -> keep
|
||||
// 7. xxxdir$Macro/.. -> keep
|
||||
// 8. xxxdir/.. -> trim dir and skip ..
|
||||
if DestPos=1 then begin
|
||||
// 1. .. -> keep
|
||||
end else if (DestPos=2) and (Result[1]=PathDelim) then begin
|
||||
// 2. /.. -> skip .., keep /
|
||||
inc(SrcPos,2);
|
||||
continue;
|
||||
{$IFDEF WINDOWS}
|
||||
end else if (DestPos=3) and (Result[2]=':')
|
||||
and (Result[1] in ['a'..'z','A'..'Z']) then begin
|
||||
// 3. C:.. -> keep
|
||||
end else if (DestPos=4) and (Result[2]=':') and (Result[3]=PathDelim)
|
||||
and (Result[1] in ['a'..'z','A'..'Z']) then begin
|
||||
// 4. C:\.. -> skip .., keep C:\
|
||||
inc(SrcPos,2);
|
||||
continue;
|
||||
end else if (DestPos=3) and (Result[1]=PathDelim)
|
||||
and (Result[2]=PathDelim) then begin
|
||||
// 5. \\.. -> skip .., keep \\
|
||||
inc(SrcPos,2);
|
||||
continue;
|
||||
{$ENDIF}
|
||||
end else if (DestPos>1) and (Result[DestPos-1]=PathDelim) then begin
|
||||
if (DestPos>3)
|
||||
and (Result[DestPos-2]='.') and (Result[DestPos-3]='.')
|
||||
and ((DestPos=4) or (Result[DestPos-4]=PathDelim)) then begin
|
||||
// 6. ../.. -> keep
|
||||
end else begin
|
||||
// 7. xxxdir/.. -> trim dir and skip ..
|
||||
DirStart:=DestPos-2;
|
||||
while (DirStart>1) and (Result[DirStart-1]<>PathDelim) do
|
||||
dec(DirStart);
|
||||
MacroPos:=DirStart;
|
||||
while MacroPos<DestPos do begin
|
||||
if (Result[MacroPos]='$')
|
||||
and (Result[MacroPos+1] in ['(','a'..'z','A'..'Z']) then begin
|
||||
// 8. directory contains a macro -> keep
|
||||
break;
|
||||
end;
|
||||
inc(MacroPos);
|
||||
end;
|
||||
if MacroPos=DestPos then begin
|
||||
DestPos:=DirStart;
|
||||
inc(SrcPos,2);
|
||||
continue;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end else begin
|
||||
// special dir . at end of filename
|
||||
if DestPos=1 then begin
|
||||
Result:='.';
|
||||
exit;
|
||||
end else begin
|
||||
// skip
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
// copy directory
|
||||
repeat
|
||||
Result[DestPos]:=c;
|
||||
inc(DestPos);
|
||||
inc(SrcPos);
|
||||
if (SrcPos>l) then break;
|
||||
c:=AFilename[SrcPos];
|
||||
if c=PathDelim then break;
|
||||
until false;
|
||||
end;
|
||||
// trim result
|
||||
if DestPos<=length(AFilename) then
|
||||
SetLength(Result,DestPos-1);
|
||||
Result := LazFileUtils.TrimFilename(AFileName);
|
||||
end;
|
||||
|
||||
function ExtractFileNameWithoutExt(const AFilename: string): string;
|
||||
@ -700,7 +529,7 @@ end;
|
||||
------------------------------------------------------------------------------}
|
||||
function CleanAndExpandFilename(const Filename: string): string;
|
||||
begin
|
||||
Result:=ExpandFileNameUTF8(TrimFileName(Filename));
|
||||
Result := LazFileutils.CleanAndExpandFilename(FileName);
|
||||
end;
|
||||
|
||||
{------------------------------------------------------------------------------
|
||||
@ -708,7 +537,7 @@ end;
|
||||
------------------------------------------------------------------------------}
|
||||
function CleanAndExpandDirectory(const Filename: string): string;
|
||||
begin
|
||||
Result:=AppendPathDelim(CleanAndExpandFilename(Filename));
|
||||
Result := LazFileUtils.CleanAndExpandDirectory(FileName);
|
||||
end;
|
||||
|
||||
function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string
|
||||
|
@ -110,9 +110,9 @@ function CompareFileExt(const Filename, Ext: string): integer; overload;
|
||||
function FilenameIsPascalUnit(const Filename: string): boolean;
|
||||
function AppendPathDelim(const Path: string): string;
|
||||
function ChompPathDelim(const Path: string): string;
|
||||
function TrimFilename(const AFilename: string): string;
|
||||
function CleanAndExpandFilename(const Filename: string): string;
|
||||
function CleanAndExpandDirectory(const Filename: string): string;
|
||||
function TrimFilename(const AFilename: string): string; inline;
|
||||
function CleanAndExpandFilename(const Filename: string): string; inline;
|
||||
function CleanAndExpandDirectory(const Filename: string): string; inline;
|
||||
function CreateAbsoluteSearchPath(const SearchPath, BaseDirectory: string): string;
|
||||
function CreateRelativePath(const Filename, BaseDirectory: string;
|
||||
UsePointDirectory: boolean = false): string;
|
||||
|
Loading…
Reference in New Issue
Block a user