12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301 |
- {
- Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
- This module provides some basic file/dir handling utils and classes
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit cfileutl;
- {$i fpcdefs.inc}
- {$define usedircache}
- interface
- uses
- {$ifdef hasunix}
- Baseunix,unix,
- {$endif hasunix}
- {$ifdef win32}
- Windows,
- {$endif win32}
- {$if defined(go32v2) or defined(watcom)}
- Dos,
- {$endif}
- {$IFNDEF USE_FAKE_SYSUTILS}
- SysUtils,
- {$ELSE}
- fksysutl,
- {$ENDIF}
- GlobType,
- CUtils,CClasses,
- Systems;
- type
- TCachedDirectory = class(TFPHashObject)
- private
- FDirectoryEntries : TFPHashList;
- FCached : Boolean;
- procedure FreeDirectoryEntries;
- function GetItemAttr(const AName: TCmdStr): byte;
- function TryUseCache: boolean;
- procedure ForceUseCache;
- procedure Reload;
- public
- constructor Create(AList:TFPHashObjectList;const AName:TCmdStr);
- destructor destroy;override;
- function FileExists(const AName:TCmdStr):boolean;
- function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
- function DirectoryExists(const AName:TCmdStr):boolean;
- property DirectoryEntries:TFPHashList read FDirectoryEntries;
- end;
- TCachedSearchRec = record
- Name : TCmdStr;
- Attr : byte;
- Pattern : TCmdStr;
- CachedDir : TCachedDirectory;
- EntryIndex : longint;
- end;
- PCachedDirectoryEntry = ^TCachedDirectoryEntry;
- TCachedDirectoryEntry = record
- RealName: TCmdStr;
- Attr : longint;
- end;
- TDirectoryCache = class
- private
- FDirectories : TFPHashObjectList;
- function GetDirectory(const ADir:TCmdStr):TCachedDirectory;
- public
- constructor Create;
- destructor destroy;override;
- function FileExists(const AName:TCmdStr):boolean;
- function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
- function DirectoryExists(const AName:TCmdStr):boolean;
- function FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
- function FindNext(var Res:TCachedSearchRec):boolean;
- function FindClose(var Res:TCachedSearchRec):boolean;
- end;
- TSearchPathList = class(TCmdStrList)
- procedure AddPath(s:TCmdStr;addfirst:boolean);overload;
- procedure AddPath(SrcPath,s:TCmdStr;addfirst:boolean);overload;
- procedure AddList(list:TSearchPathList;addfirst:boolean);
- function FindFile(const f : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- end;
- function bstoslash(const s : TCmdStr) : TCmdStr;
- {Gives the absolute path to the current directory}
- function GetCurrentDir:TCmdStr;
- {Gives the relative path to the current directory,
- with a trailing dir separator. E. g. on unix ./ }
- function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
- function path_absolute(const s : TCmdStr) : boolean;
- Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
- Function FileExists (const F : TCmdStr;allowcache:boolean) : Boolean;
- function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- Function RemoveDir(d:TCmdStr):boolean;
- Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
- function FixFileName(const s:TCmdStr):TCmdStr;
- function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
- function TargetFixFileName(const s:TCmdStr):TCmdStr;
- procedure SplitBinCmd(const s:TCmdStr;var bstr: TCmdStr;var cstr:TCmdStr);
- function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- { function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;}
- function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- function GetShortName(const n:TCmdStr):TCmdStr;
- procedure InitFileUtils;
- procedure DoneFileUtils;
- { * Since native Amiga commands can't handle Unix-style relative paths used by the compiler,
- and some GNU tools, Unix2AmigaPath is needed to handle such situations (KB) * }
- {$IF DEFINED(MORPHOS) OR DEFINED(AMIGA)}
- { * PATHCONV is implemented in the Amiga/MorphOS system unit * }
- {$WARNING TODO Amiga: implement PathConv() in System unit, which works with AnsiString}
- function Unix2AmigaPath(path: ShortString): ShortString; external name 'PATHCONV';
- {$ELSE}
- function Unix2AmigaPath(path: String): String;{$IFDEF USEINLINE}inline;{$ENDIF}
- {$ENDIF}
- implementation
- uses
- Comphook,
- Globals;
- {$undef AllFilesMaskIsInRTL}
- {$if (FPC_VERSION > 2)}
- {$define AllFilesMaskIsInRTL}
- {$endif FPC_VERSION}
- {$if (FPC_VERSION = 2) and (FPC_RELEASE > 2)}
- {$define AllFilesMaskIsInRTL}
- {$endif}
- {$if (FPC_VERSION = 2) and (FPC_RELEASE = 2) and (FPC_PATCH > 0)}
- {$define AllFilesMaskIsInRTL}
- {$endif}
- {$ifndef AllFilesMaskIsInRTL}
- {$if defined(go32v2) or defined(watcom)}
- const
- AllFilesMask = '*.*';
- {$else}
- const
- AllFilesMask = '*';
- {$endif not (go32v2 or watcom)}
- {$endif not AllFilesMaskIsInRTL}
- var
- DirCache : TDirectoryCache;
- {$IF NOT (DEFINED(MORPHOS) OR DEFINED(AMIGA))}
- { Stub function for Unix2Amiga Path conversion functionality, only available in
- Amiga/MorphOS RTL. I'm open for better solutions. (KB) }
- function Unix2AmigaPath(path: String): String;{$IFDEF USEINLINE}inline;{$ENDIF}
- begin
- Unix2AmigaPath:=path;
- end;
- {$ENDIF}
- {****************************************************************************
- TCachedDirectory
- ****************************************************************************}
- constructor TCachedDirectory.create(AList:TFPHashObjectList;const AName:TCmdStr);
- begin
- inherited create(AList,AName);
- FDirectoryEntries:=TFPHashList.Create;
- FCached:=False;
- end;
- destructor TCachedDirectory.destroy;
- begin
- FreeDirectoryEntries;
- FDirectoryEntries.Free;
- inherited destroy;
- end;
- function TCachedDirectory.TryUseCache:boolean;
- begin
- Result:=True;
- if FCached then
- exit;
- if not current_settings.disabledircache then
- ForceUseCache
- else
- Result:=False;
- end;
- procedure TCachedDirectory.ForceUseCache;
- begin
- if not FCached then
- begin
- FCached:=True;
- Reload;
- end;
- end;
- procedure TCachedDirectory.FreeDirectoryEntries;
- var
- i: Integer;
- begin
- if not(tf_files_case_aware in source_info.flags) then
- exit;
- for i := 0 to DirectoryEntries.Count-1 do
- dispose(PCachedDirectoryEntry(DirectoryEntries[i]));
- end;
- function TCachedDirectory.GetItemAttr(const AName: TCmdStr): byte;
- var
- entry: PCachedDirectoryEntry;
- begin
- if not(tf_files_case_sensitive in source_info.flags) then
- if (tf_files_case_aware in source_info.flags) then
- begin
- entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(AName)));
- if assigned(entry) then
- Result:=entry^.Attr
- else
- Result:=0;
- end
- else
- Result:=PtrUInt(DirectoryEntries.Find(Lower(AName)))
- else
- Result:=PtrUInt(DirectoryEntries.Find(AName));
- end;
- procedure TCachedDirectory.Reload;
- var
- dir : TSearchRec;
- entry : PCachedDirectoryEntry;
- begin
- FreeDirectoryEntries;
- DirectoryEntries.Clear;
- if findfirst(IncludeTrailingPathDelimiter(Name)+AllFilesMask,faAnyFile or faDirectory,dir) = 0 then
- begin
- repeat
- if ((dir.attr and faDirectory)<>faDirectory) or
- ((dir.Name<>'.') and
- (dir.Name<>'..')) then
- begin
- { Force Archive bit so the attribute always has a value. This is needed
- to be able to see the difference in the directoryentries lookup if a file
- exists or not }
- Dir.Attr:=Dir.Attr or faArchive;
- if not(tf_files_case_sensitive in source_info.flags) then
- if (tf_files_case_aware in source_info.flags) then
- begin
- new(entry);
- entry^.RealName:=Dir.Name;
- entry^.Attr:=Dir.Attr;
- DirectoryEntries.Add(Lower(Dir.Name),entry)
- end
- else
- DirectoryEntries.Add(Lower(Dir.Name),Pointer(Ptrint(Dir.Attr)))
- else
- DirectoryEntries.Add(Dir.Name,Pointer(Ptrint(Dir.Attr)));
- end;
- until findnext(dir) <> 0;
- end;
- findclose(dir);
- end;
- function TCachedDirectory.FileExists(const AName:TCmdStr):boolean;
- var
- Attr : Longint;
- begin
- if not TryUseCache then
- begin
- { prepend directory name again }
- result:=cfileutl.FileExists(Name+AName,false);
- exit;
- end;
- Attr:=GetItemAttr(AName);
- if Attr<>0 then
- Result:=((Attr and faDirectory)=0)
- else
- Result:=false;
- end;
- function TCachedDirectory.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
- var
- entry : PCachedDirectoryEntry;
- begin
- if (tf_files_case_aware in source_info.flags) then
- begin
- if not TryUseCache then
- begin
- Result:=FileExistsNonCase(path,fn,false,FoundName);
- exit;
- end;
- entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(ExtractFileName(fn))));
- if assigned(entry) and
- (entry^.Attr<>0) and
- ((entry^.Attr and faDirectory) = 0) then
- begin
- FoundName:=ExtractFilePath(path+fn)+entry^.RealName;
- Result:=true
- end
- else
- Result:=false;
- end
- else
- { should not be called in this case, use plain FileExists }
- Result:=False;
- end;
- function TCachedDirectory.DirectoryExists(const AName:TCmdStr):boolean;
- var
- Attr : Longint;
- begin
- if not TryUseCache then
- begin
- Result:=PathExists(Name+AName,false);
- exit;
- end;
- Attr:=GetItemAttr(AName);
- if Attr<>0 then
- Result:=((Attr and faDirectory)=faDirectory)
- else
- Result:=false;
- end;
- {****************************************************************************
- TDirectoryCache
- ****************************************************************************}
- constructor TDirectoryCache.create;
- begin
- inherited create;
- FDirectories:=TFPHashObjectList.Create(true);
- end;
- destructor TDirectoryCache.destroy;
- begin
- FDirectories.Free;
- inherited destroy;
- end;
- function TDirectoryCache.GetDirectory(const ADir:TCmdStr):TCachedDirectory;
- var
- CachedDir : TCachedDirectory;
- DirName : TCmdStr;
- begin
- if ADir='' then
- DirName:='.'+source_info.DirSep
- else
- DirName:=ADir;
- CachedDir:=TCachedDirectory(FDirectories.Find(DirName));
- if not assigned(CachedDir) then
- CachedDir:=TCachedDirectory.Create(FDirectories,DirName);
- Result:=CachedDir;
- end;
- function TDirectoryCache.FileExists(const AName:TCmdStr):boolean;
- var
- CachedDir : TCachedDirectory;
- begin
- Result:=false;
- CachedDir:=GetDirectory(ExtractFilePath(AName));
- if assigned(CachedDir) then
- Result:=CachedDir.FileExists(ExtractFileName(AName));
- end;
- function TDirectoryCache.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
- var
- CachedDir : TCachedDirectory;
- begin
- Result:=false;
- CachedDir:=GetDirectory(ExtractFilePath(path+fn));
- if assigned(CachedDir) then
- Result:=CachedDir.FileExistsCaseAware(path,fn,FoundName);
- end;
- function TDirectoryCache.DirectoryExists(const AName:TCmdStr):boolean;
- var
- CachedDir : TCachedDirectory;
- begin
- Result:=false;
- CachedDir:=GetDirectory(ExtractFilePath(AName));
- if assigned(CachedDir) then
- Result:=CachedDir.DirectoryExists(ExtractFileName(AName));
- end;
- function TDirectoryCache.FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
- begin
- Res.Pattern:=ExtractFileName(APattern);
- Res.CachedDir:=GetDirectory(ExtractFilePath(APattern));
- Res.CachedDir.ForceUseCache;
- Res.EntryIndex:=0;
- if assigned(Res.CachedDir) then
- Result:=FindNext(Res)
- else
- Result:=false;
- end;
- function TDirectoryCache.FindNext(var Res:TCachedSearchRec):boolean;
- var
- entry: PCachedDirectoryEntry;
- begin
- if Res.EntryIndex<Res.CachedDir.DirectoryEntries.Count then
- begin
- if (tf_files_case_aware in source_info.flags) then
- begin
- entry:=Res.CachedDir.DirectoryEntries[Res.EntryIndex];
- Res.Name:=entry^.RealName;
- Res.Attr:=entry^.Attr;
- end
- else
- begin
- Res.Name:=Res.CachedDir.DirectoryEntries.NameOfIndex(Res.EntryIndex);
- Res.Attr:=PtrUInt(Res.CachedDir.DirectoryEntries[Res.EntryIndex]);
- end;
- inc(Res.EntryIndex);
- Result:=true;
- end
- else
- Result:=false;
- end;
- function TDirectoryCache.FindClose(var Res:TCachedSearchRec):boolean;
- begin
- { nothing todo }
- result:=true;
- end;
- {****************************************************************************
- Utils
- ****************************************************************************}
- function bstoslash(const s : TCmdStr) : TCmdStr;
- {
- return TCmdStr s with all \ changed into /
- }
- var
- i : longint;
- begin
- setlength(bstoslash,length(s));
- for i:=1to length(s) do
- if s[i]='\' then
- bstoslash[i]:='/'
- else
- bstoslash[i]:=s[i];
- end;
- {Gives the absolute path to the current directory}
- var
- CachedCurrentDir : TCmdStr;
- function GetCurrentDir:TCmdStr;
- begin
- if CachedCurrentDir='' then
- begin
- GetDir(0,CachedCurrentDir);
- CachedCurrentDir:=FixPath(CachedCurrentDir,false);
- end;
- result:=CachedCurrentDir;
- end;
- {Gives the relative path to the current directory,
- with a trailing dir separator. E. g. on unix ./ }
- function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
- begin
- if systeminfo.system <> system_powerpc_macos then
- CurDirRelPath:= '.'+systeminfo.DirSep
- else
- CurDirRelPath:= ':'
- end;
- function path_absolute(const s : TCmdStr) : boolean;
- {
- is path s an absolute path?
- }
- begin
- result:=false;
- {$if defined(unix)}
- if (length(s)>0) and (s[1] in AllowDirectorySeparators) then
- result:=true;
- {$elseif defined(amiga) or defined(morphos)}
- (* An Amiga path is absolute, if it has a volume/device name in it (contains ":"),
- otherwise it's always a relative path, no matter if it starts with a directory
- separator or not. (KB) *)
- if (length(s)>0) and (Pos(':',s) <> 0) then
- result:=true;
- {$elseif defined(macos)}
- if IsMacFullPath(s) then
- result:=true;
- {$elseif defined(netware)}
- if (Pos (DriveSeparator, S) <> 0) or
- ((Length (S) > 0) and (S [1] in AllowDirectorySeparators)) then
- result:=true;
- {$elseif defined(win32) or defined(win64) or defined(go32v2) or defined(os2) or defined(watcom)}
- if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
- (* The following check for non-empty AllowDriveSeparators assumes that all
- other platforms supporting drives and not handled as exceptions above
- should work with DOS-like paths, i.e. use absolute paths with one letter
- for drive followed by path separator *)
- ((length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
- result:=true;
- {$else}
- if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
- (* The following check for non-empty AllowDriveSeparators assumes that all
- other platforms supporting drives and not handled as exceptions above
- should work with DOS-like paths, i.e. use absolute paths with one letter
- for drive followed by path separator *)
- ((AllowDriveSeparators <> []) and (length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
- result:=true;
- {$endif unix}
- end;
- Function FileExists ( Const F : TCmdStr;allowcache:boolean) : Boolean;
- begin
- {$ifdef usedircache}
- if allowcache then
- Result:=DirCache.FileExists(F)
- else
- {$endif usedircache}
- Result:=SysUtils.FileExists(F);
- if do_checkverbosity(V_Tried) then
- begin
- if Result then
- do_comment(V_Tried,'Searching file '+F+'... found')
- else
- do_comment(V_Tried,'Searching file '+F+'... not found');
- end;
- end;
- function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- var
- fn2 : TCmdStr;
- begin
- result:=false;
- if tf_files_case_sensitive in source_info.flags then
- begin
- {
- Search order for case sensitive systems:
- 1. NormalCase
- 2. lowercase
- 3. UPPERCASE
- }
- FoundFile:=path+fn;
- If FileExists(FoundFile,allowcache) then
- begin
- result:=true;
- exit;
- end;
- fn2:=Lower(fn);
- if fn2<>fn then
- begin
- FoundFile:=path+fn2;
- If FileExists(FoundFile,allowcache) then
- begin
- result:=true;
- exit;
- end;
- end;
- fn2:=Upper(fn);
- if fn2<>fn then
- begin
- FoundFile:=path+fn2;
- If FileExists(FoundFile,allowcache) then
- begin
- result:=true;
- exit;
- end;
- end;
- end
- else
- if tf_files_case_aware in source_info.flags then
- begin
- {
- Search order for case aware systems:
- 1. NormalCase
- }
- {$ifdef usedircache}
- if allowcache then
- begin
- result:=DirCache.FileExistsCaseAware(path,fn,fn2);
- if result then
- begin
- FoundFile:=fn2;
- exit;
- end;
- end
- else
- {$endif usedircache}
- begin
- FoundFile:=path+fn;
- If FileExists(FoundFile,allowcache) then
- begin
- { don't know the real name in this case }
- result:=true;
- exit;
- end;
- end;
- end
- else
- begin
- { None case sensitive only lowercase }
- FoundFile:=path+Lower(fn);
- If FileExists(FoundFile,allowcache) then
- begin
- result:=true;
- exit;
- end;
- end;
- { Set foundfile to something useful }
- FoundFile:=fn;
- end;
- Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
- Var
- i: longint;
- hs : TCmdStr;
- begin
- if F = '' then
- begin
- result := true;
- exit;
- end;
- hs := ExpandFileName(F);
- I := Pos (DriveSeparator, hs);
- if (hs [Length (hs)] = DirectorySeparator) and
- (((I = 0) and (Length (hs) > 1)) or (I <> Length (hs) - 1)) then
- Delete (hs, Length (hs), 1);
- {$ifdef usedircache}
- if allowcache then
- Result:=DirCache.DirectoryExists(hs)
- else
- {$endif usedircache}
- Result:=SysUtils.DirectoryExists(hs);
- end;
- Function RemoveDir(d:TCmdStr):boolean;
- begin
- if d[length(d)]=source_info.DirSep then
- Delete(d,length(d),1);
- {$I-}
- rmdir(d);
- {$I+}
- RemoveDir:=(ioresult=0);
- end;
- Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
- var
- i, L : longint;
- P: PChar;
- begin
- Result := s;
- L := Length(Result);
- if L=0 then
- exit;
- { Fix separator }
- P := @Result[1];
- for i:=0 to L-1 do
- begin
- if p^ in ['/','\'] then
- p^:=source_info.DirSep;
- inc(p);
- end;
- { Fix ending / }
- if (L>0) and (Result[L]<>source_info.DirSep) and
- (Result[L]<>DriveSeparator) then
- Result:=Result+source_info.DirSep; { !still results in temp AnsiString }
- { Remove ./ }
- if (not allowdot) and ((Length(Result)=2) and (Result[1]='.') and (Result[2] = source_info.DirSep)) then
- begin
- Result:='';
- Exit;
- end;
- { return }
- if not ((tf_files_case_aware in source_info.flags) or
- (tf_files_case_sensitive in source_info.flags)) then
- Result := lower(Result);
- end;
- {Actually the version in macutils.pp could be used,
- but that would not work for crosscompiling, so this is a slightly modified
- version of it.}
- function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
- function GetVolumeIdentifier: TCmdStr;
- begin
- GetVolumeIdentifier := '{Boot}'
- (*
- if mpw then
- GetVolumeIdentifier := '{Boot}'
- else
- GetVolumeIdentifier := macosBootVolumeName;
- *)
- end;
- var
- slashPos, oldpos, newpos, oldlen, maxpos: Longint;
- begin
- oldpos := 1;
- slashPos := Pos('/', path);
- if (slashPos <> 0) then {its a unix path}
- begin
- if slashPos = 1 then
- begin {its a full path}
- oldpos := 2;
- TranslatePathToMac := GetVolumeIdentifier;
- end
- else {its a partial path}
- TranslatePathToMac := ':';
- end
- else
- begin
- slashPos := Pos('\', path);
- if (slashPos <> 0) then {its a dos path}
- begin
- if slashPos = 1 then
- begin {its a full path, without drive letter}
- oldpos := 2;
- TranslatePathToMac := GetVolumeIdentifier;
- end
- else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
- begin
- oldpos := 4;
- TranslatePathToMac := GetVolumeIdentifier;
- end
- else {its a partial path}
- TranslatePathToMac := ':';
- end;
- end;
- if (slashPos <> 0) then {its a unix or dos path}
- begin
- {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
- newpos := Length(TranslatePathToMac);
- oldlen := Length(path);
- SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
- {prepended plus length of path.}
- maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
- {There is never a slash in the beginning, because either it was an absolute path, and then the}
- {drive and slash was removed, or it was a relative path without a preceding slash.}
- while oldpos <= oldlen do
- begin
- {Check if special dirs, ./ or ../ }
- if path[oldPos] = '.' then
- if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
- begin
- if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
- begin
- {It is "../" or ".." translates to ":" }
- if newPos = maxPos then
- begin {Shouldn't actually happen, but..}
- Exit('');
- end;
- newPos := newPos + 1;
- TranslatePathToMac[newPos] := ':';
- oldPos := oldPos + 3;
- continue; {Start over again}
- end;
- end
- else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
- begin
- {It is "./" or "." ignor it }
- oldPos := oldPos + 2;
- continue; {Start over again}
- end;
- {Collect file or dir name}
- while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
- begin
- if newPos = maxPos then
- begin {Shouldn't actually happen, but..}
- Exit('');
- end;
- newPos := newPos + 1;
- TranslatePathToMac[newPos] := path[oldPos];
- oldPos := oldPos + 1;
- end;
- {When we come here there is either a slash or we are at the end.}
- if (oldpos <= oldlen) then
- begin
- if newPos = maxPos then
- begin {Shouldn't actually happen, but..}
- Exit('');
- end;
- newPos := newPos + 1;
- TranslatePathToMac[newPos] := ':';
- oldPos := oldPos + 1;
- end;
- end;
- SetLength(TranslatePathToMac, newpos);
- end
- else if (path = '.') then
- TranslatePathToMac := ':'
- else if (path = '..') then
- TranslatePathToMac := '::'
- else
- TranslatePathToMac := path; {its a mac path}
- end;
- function FixFileName(const s:TCmdStr):TCmdStr;
- var
- i : longint;
- begin
- if source_info.system = system_powerpc_MACOS then
- FixFileName:= TranslatePathToMac(s, true)
- else
- if (tf_files_case_aware in source_info.flags) or
- (tf_files_case_sensitive in source_info.flags) then
- begin
- setlength(FixFileName,length(s));
- for i:=1 to length(s) do
- begin
- case s[i] of
- '/','\' :
- FixFileName[i]:=source_info.dirsep;
- else
- FixFileName[i]:=s[i];
- end;
- end;
- end
- else
- begin
- setlength(FixFileName,length(s));
- for i:=1 to length(s) do
- begin
- case s[i] of
- '/','\' :
- FixFileName[i]:=source_info.dirsep;
- 'A'..'Z' :
- FixFileName[i]:=char(byte(s[i])+32);
- else
- FixFileName[i]:=s[i];
- end;
- end;
- end;
- end;
- Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
- var
- i : longint;
- begin
- { Fix separator }
- for i:=1 to length(s) do
- if s[i] in ['/','\'] then
- s[i]:=target_info.DirSep;
- { Fix ending / }
- if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
- (s[length(s)]<>':') then
- s:=s+target_info.DirSep;
- { Remove ./ }
- if (not allowdot) and (s='.'+target_info.DirSep) then
- s:='';
- { return }
- if (tf_files_case_aware in target_info.flags) or
- (tf_files_case_sensitive in target_info.flags) then
- TargetFixPath:=s
- else
- TargetFixPath:=Lower(s);
- end;
- function TargetFixFileName(const s:TCmdStr):TCmdStr;
- var
- i : longint;
- begin
- if target_info.system = system_powerpc_MACOS then
- TargetFixFileName:= TranslatePathToMac(s, true)
- else
- if (tf_files_case_aware in target_info.flags) or
- (tf_files_case_sensitive in target_info.flags) then
- begin
- setlength(TargetFixFileName,length(s));
- for i:=1 to length(s) do
- begin
- case s[i] of
- '/','\' :
- TargetFixFileName[i]:=target_info.dirsep;
- else
- TargetFixFileName[i]:=s[i];
- end;
- end;
- end
- else
- begin
- setlength(TargetFixFileName,length(s));
- for i:=1 to length(s) do
- begin
- case s[i] of
- '/','\' :
- TargetFixFileName[i]:=target_info.dirsep;
- 'A'..'Z' :
- TargetFixFileName[i]:=char(byte(s[i])+32);
- else
- TargetFixFileName[i]:=s[i];
- end;
- end;
- end;
- end;
- procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
- var
- i : longint;
- begin
- i:=pos(' ',s);
- if i>0 then
- begin
- bstr:=Copy(s,1,i-1);
- cstr:=Copy(s,i+1,length(s)-i);
- end
- else
- begin
- bstr:=s;
- cstr:='';
- end;
- end;
- procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
- begin
- AddPath('',s,AddFirst);
- end;
- procedure TSearchPathList.AddPath(SrcPath,s:TCmdStr;addfirst:boolean);
- var
- staridx,
- i,j : longint;
- prefix,
- suffix,
- CurrentDir,
- currPath : TCmdStr;
- subdirfound : boolean;
- {$ifdef usedircache}
- dir : TCachedSearchRec;
- {$else usedircache}
- dir : TSearchRec;
- {$endif usedircache}
- hp : TCmdStrListItem;
- procedure WarnNonExistingPath(const path : TCmdStr);
- begin
- if do_checkverbosity(V_Tried) then
- do_comment(V_Tried,'Path "'+path+'" not found');
- end;
- procedure AddCurrPath;
- begin
- if addfirst then
- begin
- Remove(currPath);
- Insert(currPath);
- end
- else
- begin
- { Check if already in path, then we don't add it }
- hp:=Find(currPath);
- if not assigned(hp) then
- Concat(currPath);
- end;
- end;
- begin
- if s='' then
- exit;
- { Support default macro's }
- DefaultReplacements(s);
- {$warnings off}
- if PathSeparator <> ';' then
- for i:=1 to length(s) do
- if s[i]=PathSeparator then
- s[i]:=';';
- {$warnings on}
- { get current dir }
- CurrentDir:=GetCurrentDir;
- repeat
- { get currpath }
- if addfirst then
- begin
- j:=length(s);
- while (j>0) and (s[j]<>';') do
- dec(j);
- currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
- if j=0 then
- s:=''
- else
- System.Delete(s,j,length(s)-j+1);
- end
- else
- begin
- j:=Pos(';',s);
- if j=0 then
- j:=length(s)+1;
- currPath:= TrimSpace(Copy(s,1,j-1));
- System.Delete(s,1,j);
- end;
- { fix pathname }
- DePascalQuote(currPath);
- currPath:=SrcPath+FixPath(currPath,false);
- if currPath='' then
- currPath:= CurDirRelPath(source_info)
- else
- begin
- currPath:=FixPath(ExpandFileName(currpath),false);
- if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
- begin
- {$if defined(amiga) and defined(morphos)}
- currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
- {$else}
- currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
- {$endif}
- end;
- end;
- { wildcard adding ? }
- staridx:=pos('*',currpath);
- if staridx>0 then
- begin
- prefix:=ExtractFilePath(Copy(currpath,1,staridx));
- suffix:=Copy(currpath,staridx+1,length(currpath));
- subdirfound:=false;
- {$ifdef usedircache}
- if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
- begin
- repeat
- if (dir.attr and faDirectory)<>0 then
- begin
- subdirfound:=true;
- currpath:=prefix+dir.name+suffix;
- if (suffix='') or PathExists(currpath,true) then
- begin
- hp:=Find(currPath);
- if not assigned(hp) then
- AddCurrPath;
- end;
- end;
- until not DirCache.FindNext(dir);
- end;
- DirCache.FindClose(dir);
- {$else usedircache}
- if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
- begin
- repeat
- if (dir.name<>'.') and
- (dir.name<>'..') and
- ((dir.attr and faDirectory)<>0) then
- begin
- subdirfound:=true;
- currpath:=prefix+dir.name+suffix;
- if (suffix='') or PathExists(currpath,false) then
- begin
- hp:=Find(currPath);
- if not assigned(hp) then
- AddCurrPath;
- end;
- end;
- until findnext(dir) <> 0;
- end;
- FindClose(dir);
- {$endif usedircache}
- if not subdirfound then
- WarnNonExistingPath(currpath);
- end
- else
- begin
- if PathExists(currpath,true) then
- AddCurrPath
- else
- WarnNonExistingPath(currpath);
- end;
- until (s='');
- end;
- procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
- var
- s : TCmdStr;
- hl : TSearchPathList;
- hp,hp2 : TCmdStrListItem;
- begin
- if list.empty then
- exit;
- { create temp and reverse the list }
- if addfirst then
- begin
- hl:=TSearchPathList.Create;
- hp:=TCmdStrListItem(list.first);
- while assigned(hp) do
- begin
- hl.insert(hp.Str);
- hp:=TCmdStrListItem(hp.next);
- end;
- while not hl.empty do
- begin
- s:=hl.GetFirst;
- Remove(s);
- Insert(s);
- end;
- hl.Free;
- end
- else
- begin
- hp:=TCmdStrListItem(list.first);
- while assigned(hp) do
- begin
- hp2:=Find(hp.Str);
- { Check if already in path, then we don't add it }
- if not assigned(hp2) then
- Concat(hp.Str);
- hp:=TCmdStrListItem(hp.next);
- end;
- end;
- end;
- function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- Var
- p : TCmdStrListItem;
- begin
- FindFile:=false;
- p:=TCmdStrListItem(first);
- while assigned(p) do
- begin
- result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
- if result then
- exit;
- p:=TCmdStrListItem(p.next);
- end;
- { Return original filename if not found }
- FoundFile:=f;
- end;
- function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- Var
- StartPos, EndPos, L: LongInt;
- begin
- Result:=False;
- StartPos := 1;
- L := Length(Path);
- repeat
- EndPos := StartPos;
- while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
- Inc(EndPos);
- Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
- if Result then
- Exit;
- StartPos := EndPos + 1;
- until StartPos > L;
- FoundFile:=f;
- end;
- {
- function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
- Var
- singlepathstring : TCmdStr;
- startpc,pc : pchar;
- begin
- FindFilePchar:=false;
- if Assigned (Path) then
- begin
- pc:=path;
- repeat
- startpc:=pc;
- while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
- inc(pc);
- SetLength(singlepathstring, pc-startpc);
- move(startpc^,singlepathstring[1],pc-startpc);
- singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
- result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
- if result then
- exit;
- if (pc^=#0) then
- break;
- inc(pc);
- until false;
- end;
- foundfile:=f;
- end;
- }
- function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
- var
- Path : TCmdStr;
- found : boolean;
- begin
- found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),exepath,allowcache,foundfile);
- if not found then
- begin
- {$ifdef macos}
- Path:=GetEnvironmentVariable('Commands');
- {$else}
- Path:=GetEnvironmentVariable('PATH');
- {$endif}
- found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),Path,allowcache,foundfile);
- end;
- FindExe:=found;
- end;
- function GetShortName(const n:TCmdStr):TCmdStr;
- {$ifdef win32}
- var
- hs,hs2 : TCmdStr;
- i : longint;
- {$endif}
- {$if defined(go32v2) or defined(watcom)}
- var
- hs : shortstring;
- {$endif}
- begin
- GetShortName:=n;
- {$ifdef win32}
- hs:=n+#0;
- { may become longer in case of e.g. ".a" -> "a~1" or so }
- setlength(hs2,length(hs)*2);
- i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
- if (i>0) and (i<=length(hs)*2) then
- begin
- setlength(hs2,strlen(@hs2[1]));
- GetShortName:=hs2;
- end;
- {$endif}
- {$if defined(go32v2) or defined(watcom)}
- hs:=n;
- if Dos.GetShortName(hs) then
- GetShortName:=hs;
- {$endif}
- end;
- {****************************************************************************
- Init / Done
- ****************************************************************************}
- procedure InitFileUtils;
- begin
- DirCache:=TDirectoryCache.Create;
- end;
- procedure DoneFileUtils;
- begin
- DirCache.Free;
- end;
- end.
|