cfileutl.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides some basic file/dir handling utils and classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cfileutl;
  18. {$i fpcdefs.inc}
  19. {$define usedircache}
  20. interface
  21. uses
  22. {$ifdef hasunix}
  23. Baseunix,unix,
  24. {$endif hasunix}
  25. {$ifdef win32}
  26. Windows,
  27. {$endif win32}
  28. {$if defined(go32v2) or defined(watcom)}
  29. Dos,
  30. {$endif}
  31. {$IFNDEF USE_FAKE_SYSUTILS}
  32. SysUtils,
  33. {$ELSE}
  34. fksysutl,
  35. {$ENDIF}
  36. GlobType,
  37. CUtils,CClasses,
  38. Systems;
  39. type
  40. TCachedDirectory = class(TFPHashObject)
  41. private
  42. FDirectoryEntries : TFPHashList;
  43. FCached : Boolean;
  44. procedure FreeDirectoryEntries;
  45. function GetItemAttr(const AName: TCmdStr): byte;
  46. function TryUseCache: boolean;
  47. procedure ForceUseCache;
  48. procedure Reload;
  49. public
  50. constructor Create(AList:TFPHashObjectList;const AName:TCmdStr);
  51. destructor destroy;override;
  52. function FileExists(const AName:TCmdStr):boolean;
  53. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  54. function DirectoryExists(const AName:TCmdStr):boolean;
  55. property DirectoryEntries:TFPHashList read FDirectoryEntries;
  56. end;
  57. TCachedSearchRec = record
  58. Name : TCmdStr;
  59. Attr : byte;
  60. Pattern : TCmdStr;
  61. CachedDir : TCachedDirectory;
  62. EntryIndex : longint;
  63. end;
  64. PCachedDirectoryEntry = ^TCachedDirectoryEntry;
  65. TCachedDirectoryEntry = record
  66. RealName: TCmdStr;
  67. Attr : longint;
  68. end;
  69. TDirectoryCache = class
  70. private
  71. FDirectories : TFPHashObjectList;
  72. function GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  73. public
  74. constructor Create;
  75. destructor destroy;override;
  76. function FileExists(const AName:TCmdStr):boolean;
  77. function FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  78. function DirectoryExists(const AName:TCmdStr):boolean;
  79. function FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  80. function FindNext(var Res:TCachedSearchRec):boolean;
  81. function FindClose(var Res:TCachedSearchRec):boolean;
  82. end;
  83. TSearchPathList = class(TCmdStrList)
  84. procedure AddPath(s:TCmdStr;addfirst:boolean);overload;
  85. procedure AddPath(SrcPath,s:TCmdStr;addfirst:boolean);overload;
  86. procedure AddList(list:TSearchPathList;addfirst:boolean);
  87. function FindFile(const f : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  88. end;
  89. function bstoslash(const s : TCmdStr) : TCmdStr;
  90. {Gives the absolute path to the current directory}
  91. function GetCurrentDir:TCmdStr;
  92. {Gives the relative path to the current directory,
  93. with a trailing dir separator. E. g. on unix ./ }
  94. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  95. function path_absolute(const s : TCmdStr) : boolean;
  96. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  97. Function FileExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  98. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  99. Function RemoveDir(d:TCmdStr):boolean;
  100. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  101. function FixFileName(const s:TCmdStr):TCmdStr;
  102. function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  103. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  104. procedure SplitBinCmd(const s:TCmdStr;var bstr: TCmdStr;var cstr:TCmdStr);
  105. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  106. { function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;}
  107. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  108. function GetShortName(const n:TCmdStr):TCmdStr;
  109. procedure InitFileUtils;
  110. procedure DoneFileUtils;
  111. { * Since native Amiga commands can't handle Unix-style relative paths used by the compiler,
  112. and some GNU tools, Unix2AmigaPath is needed to handle such situations (KB) * }
  113. {$IF DEFINED(MORPHOS) OR DEFINED(AMIGA)}
  114. { * PATHCONV is implemented in the Amiga/MorphOS system unit * }
  115. {$WARNING TODO Amiga: implement PathConv() in System unit, which works with AnsiString}
  116. function Unix2AmigaPath(path: ShortString): ShortString; external name 'PATHCONV';
  117. {$ELSE}
  118. function Unix2AmigaPath(path: String): String;{$IFDEF USEINLINE}inline;{$ENDIF}
  119. {$ENDIF}
  120. implementation
  121. uses
  122. Comphook,
  123. Globals;
  124. {$undef AllFilesMaskIsInRTL}
  125. {$if (FPC_VERSION > 2)}
  126. {$define AllFilesMaskIsInRTL}
  127. {$endif FPC_VERSION}
  128. {$if (FPC_VERSION = 2) and (FPC_RELEASE > 2)}
  129. {$define AllFilesMaskIsInRTL}
  130. {$endif}
  131. {$if (FPC_VERSION = 2) and (FPC_RELEASE = 2) and (FPC_PATCH > 0)}
  132. {$define AllFilesMaskIsInRTL}
  133. {$endif}
  134. {$ifndef AllFilesMaskIsInRTL}
  135. {$if defined(go32v2) or defined(watcom)}
  136. const
  137. AllFilesMask = '*.*';
  138. {$else}
  139. const
  140. AllFilesMask = '*';
  141. {$endif not (go32v2 or watcom)}
  142. {$endif not AllFilesMaskIsInRTL}
  143. var
  144. DirCache : TDirectoryCache;
  145. {$IF NOT (DEFINED(MORPHOS) OR DEFINED(AMIGA))}
  146. { Stub function for Unix2Amiga Path conversion functionality, only available in
  147. Amiga/MorphOS RTL. I'm open for better solutions. (KB) }
  148. function Unix2AmigaPath(path: String): String;{$IFDEF USEINLINE}inline;{$ENDIF}
  149. begin
  150. Unix2AmigaPath:=path;
  151. end;
  152. {$ENDIF}
  153. {****************************************************************************
  154. TCachedDirectory
  155. ****************************************************************************}
  156. constructor TCachedDirectory.create(AList:TFPHashObjectList;const AName:TCmdStr);
  157. begin
  158. inherited create(AList,AName);
  159. FDirectoryEntries:=TFPHashList.Create;
  160. FCached:=False;
  161. end;
  162. destructor TCachedDirectory.destroy;
  163. begin
  164. FreeDirectoryEntries;
  165. FDirectoryEntries.Free;
  166. inherited destroy;
  167. end;
  168. function TCachedDirectory.TryUseCache:boolean;
  169. begin
  170. Result:=True;
  171. if FCached then
  172. exit;
  173. if not current_settings.disabledircache then
  174. ForceUseCache
  175. else
  176. Result:=False;
  177. end;
  178. procedure TCachedDirectory.ForceUseCache;
  179. begin
  180. if not FCached then
  181. begin
  182. FCached:=True;
  183. Reload;
  184. end;
  185. end;
  186. procedure TCachedDirectory.FreeDirectoryEntries;
  187. var
  188. i: Integer;
  189. begin
  190. if not(tf_files_case_aware in source_info.flags) then
  191. exit;
  192. for i := 0 to DirectoryEntries.Count-1 do
  193. dispose(PCachedDirectoryEntry(DirectoryEntries[i]));
  194. end;
  195. function TCachedDirectory.GetItemAttr(const AName: TCmdStr): byte;
  196. var
  197. entry: PCachedDirectoryEntry;
  198. begin
  199. if not(tf_files_case_sensitive in source_info.flags) then
  200. if (tf_files_case_aware in source_info.flags) then
  201. begin
  202. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(AName)));
  203. if assigned(entry) then
  204. Result:=entry^.Attr
  205. else
  206. Result:=0;
  207. end
  208. else
  209. Result:=PtrUInt(DirectoryEntries.Find(Lower(AName)))
  210. else
  211. Result:=PtrUInt(DirectoryEntries.Find(AName));
  212. end;
  213. procedure TCachedDirectory.Reload;
  214. var
  215. dir : TSearchRec;
  216. entry : PCachedDirectoryEntry;
  217. begin
  218. FreeDirectoryEntries;
  219. DirectoryEntries.Clear;
  220. if findfirst(IncludeTrailingPathDelimiter(Name)+AllFilesMask,faAnyFile or faDirectory,dir) = 0 then
  221. begin
  222. repeat
  223. if ((dir.attr and faDirectory)<>faDirectory) or
  224. ((dir.Name<>'.') and
  225. (dir.Name<>'..')) then
  226. begin
  227. { Force Archive bit so the attribute always has a value. This is needed
  228. to be able to see the difference in the directoryentries lookup if a file
  229. exists or not }
  230. Dir.Attr:=Dir.Attr or faArchive;
  231. if not(tf_files_case_sensitive in source_info.flags) then
  232. if (tf_files_case_aware in source_info.flags) then
  233. begin
  234. new(entry);
  235. entry^.RealName:=Dir.Name;
  236. entry^.Attr:=Dir.Attr;
  237. DirectoryEntries.Add(Lower(Dir.Name),entry)
  238. end
  239. else
  240. DirectoryEntries.Add(Lower(Dir.Name),Pointer(Ptrint(Dir.Attr)))
  241. else
  242. DirectoryEntries.Add(Dir.Name,Pointer(Ptrint(Dir.Attr)));
  243. end;
  244. until findnext(dir) <> 0;
  245. end;
  246. findclose(dir);
  247. end;
  248. function TCachedDirectory.FileExists(const AName:TCmdStr):boolean;
  249. var
  250. Attr : Longint;
  251. begin
  252. if not TryUseCache then
  253. begin
  254. { prepend directory name again }
  255. result:=cfileutl.FileExists(Name+AName,false);
  256. exit;
  257. end;
  258. Attr:=GetItemAttr(AName);
  259. if Attr<>0 then
  260. Result:=((Attr and faDirectory)=0)
  261. else
  262. Result:=false;
  263. end;
  264. function TCachedDirectory.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  265. var
  266. entry : PCachedDirectoryEntry;
  267. begin
  268. if (tf_files_case_aware in source_info.flags) then
  269. begin
  270. if not TryUseCache then
  271. begin
  272. Result:=FileExistsNonCase(path,fn,false,FoundName);
  273. exit;
  274. end;
  275. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(ExtractFileName(fn))));
  276. if assigned(entry) and
  277. (entry^.Attr<>0) and
  278. ((entry^.Attr and faDirectory) = 0) then
  279. begin
  280. FoundName:=ExtractFilePath(path+fn)+entry^.RealName;
  281. Result:=true
  282. end
  283. else
  284. Result:=false;
  285. end
  286. else
  287. { should not be called in this case, use plain FileExists }
  288. Result:=False;
  289. end;
  290. function TCachedDirectory.DirectoryExists(const AName:TCmdStr):boolean;
  291. var
  292. Attr : Longint;
  293. begin
  294. if not TryUseCache then
  295. begin
  296. Result:=PathExists(Name+AName,false);
  297. exit;
  298. end;
  299. Attr:=GetItemAttr(AName);
  300. if Attr<>0 then
  301. Result:=((Attr and faDirectory)=faDirectory)
  302. else
  303. Result:=false;
  304. end;
  305. {****************************************************************************
  306. TDirectoryCache
  307. ****************************************************************************}
  308. constructor TDirectoryCache.create;
  309. begin
  310. inherited create;
  311. FDirectories:=TFPHashObjectList.Create(true);
  312. end;
  313. destructor TDirectoryCache.destroy;
  314. begin
  315. FDirectories.Free;
  316. inherited destroy;
  317. end;
  318. function TDirectoryCache.GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  319. var
  320. CachedDir : TCachedDirectory;
  321. DirName : TCmdStr;
  322. begin
  323. if ADir='' then
  324. DirName:='.'+source_info.DirSep
  325. else
  326. DirName:=ADir;
  327. CachedDir:=TCachedDirectory(FDirectories.Find(DirName));
  328. if not assigned(CachedDir) then
  329. CachedDir:=TCachedDirectory.Create(FDirectories,DirName);
  330. Result:=CachedDir;
  331. end;
  332. function TDirectoryCache.FileExists(const AName:TCmdStr):boolean;
  333. var
  334. CachedDir : TCachedDirectory;
  335. begin
  336. Result:=false;
  337. CachedDir:=GetDirectory(ExtractFilePath(AName));
  338. if assigned(CachedDir) then
  339. Result:=CachedDir.FileExists(ExtractFileName(AName));
  340. end;
  341. function TDirectoryCache.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  342. var
  343. CachedDir : TCachedDirectory;
  344. begin
  345. Result:=false;
  346. CachedDir:=GetDirectory(ExtractFilePath(path+fn));
  347. if assigned(CachedDir) then
  348. Result:=CachedDir.FileExistsCaseAware(path,fn,FoundName);
  349. end;
  350. function TDirectoryCache.DirectoryExists(const AName:TCmdStr):boolean;
  351. var
  352. CachedDir : TCachedDirectory;
  353. begin
  354. Result:=false;
  355. CachedDir:=GetDirectory(ExtractFilePath(AName));
  356. if assigned(CachedDir) then
  357. Result:=CachedDir.DirectoryExists(ExtractFileName(AName));
  358. end;
  359. function TDirectoryCache.FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  360. begin
  361. Res.Pattern:=ExtractFileName(APattern);
  362. Res.CachedDir:=GetDirectory(ExtractFilePath(APattern));
  363. Res.CachedDir.ForceUseCache;
  364. Res.EntryIndex:=0;
  365. if assigned(Res.CachedDir) then
  366. Result:=FindNext(Res)
  367. else
  368. Result:=false;
  369. end;
  370. function TDirectoryCache.FindNext(var Res:TCachedSearchRec):boolean;
  371. var
  372. entry: PCachedDirectoryEntry;
  373. begin
  374. if Res.EntryIndex<Res.CachedDir.DirectoryEntries.Count then
  375. begin
  376. if (tf_files_case_aware in source_info.flags) then
  377. begin
  378. entry:=Res.CachedDir.DirectoryEntries[Res.EntryIndex];
  379. Res.Name:=entry^.RealName;
  380. Res.Attr:=entry^.Attr;
  381. end
  382. else
  383. begin
  384. Res.Name:=Res.CachedDir.DirectoryEntries.NameOfIndex(Res.EntryIndex);
  385. Res.Attr:=PtrUInt(Res.CachedDir.DirectoryEntries[Res.EntryIndex]);
  386. end;
  387. inc(Res.EntryIndex);
  388. Result:=true;
  389. end
  390. else
  391. Result:=false;
  392. end;
  393. function TDirectoryCache.FindClose(var Res:TCachedSearchRec):boolean;
  394. begin
  395. { nothing todo }
  396. result:=true;
  397. end;
  398. {****************************************************************************
  399. Utils
  400. ****************************************************************************}
  401. function bstoslash(const s : TCmdStr) : TCmdStr;
  402. {
  403. return TCmdStr s with all \ changed into /
  404. }
  405. var
  406. i : longint;
  407. begin
  408. setlength(bstoslash,length(s));
  409. for i:=1to length(s) do
  410. if s[i]='\' then
  411. bstoslash[i]:='/'
  412. else
  413. bstoslash[i]:=s[i];
  414. end;
  415. {Gives the absolute path to the current directory}
  416. var
  417. CachedCurrentDir : TCmdStr;
  418. function GetCurrentDir:TCmdStr;
  419. begin
  420. if CachedCurrentDir='' then
  421. begin
  422. GetDir(0,CachedCurrentDir);
  423. CachedCurrentDir:=FixPath(CachedCurrentDir,false);
  424. end;
  425. result:=CachedCurrentDir;
  426. end;
  427. {Gives the relative path to the current directory,
  428. with a trailing dir separator. E. g. on unix ./ }
  429. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  430. begin
  431. if systeminfo.system <> system_powerpc_macos then
  432. CurDirRelPath:= '.'+systeminfo.DirSep
  433. else
  434. CurDirRelPath:= ':'
  435. end;
  436. function path_absolute(const s : TCmdStr) : boolean;
  437. {
  438. is path s an absolute path?
  439. }
  440. begin
  441. result:=false;
  442. {$if defined(unix)}
  443. if (length(s)>0) and (s[1] in AllowDirectorySeparators) then
  444. result:=true;
  445. {$elseif defined(amiga) or defined(morphos)}
  446. (* An Amiga path is absolute, if it has a volume/device name in it (contains ":"),
  447. otherwise it's always a relative path, no matter if it starts with a directory
  448. separator or not. (KB) *)
  449. if (length(s)>0) and (Pos(':',s) <> 0) then
  450. result:=true;
  451. {$elseif defined(macos)}
  452. if IsMacFullPath(s) then
  453. result:=true;
  454. {$elseif defined(netware)}
  455. if (Pos (DriveSeparator, S) <> 0) or
  456. ((Length (S) > 0) and (S [1] in AllowDirectorySeparators)) then
  457. result:=true;
  458. {$elseif defined(win32) or defined(win64) or defined(go32v2) or defined(os2) or defined(watcom)}
  459. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  460. (* The following check for non-empty AllowDriveSeparators assumes that all
  461. other platforms supporting drives and not handled as exceptions above
  462. should work with DOS-like paths, i.e. use absolute paths with one letter
  463. for drive followed by path separator *)
  464. ((length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  465. result:=true;
  466. {$else}
  467. if ((length(s)>0) and (s[1] in AllowDirectorySeparators)) or
  468. (* The following check for non-empty AllowDriveSeparators assumes that all
  469. other platforms supporting drives and not handled as exceptions above
  470. should work with DOS-like paths, i.e. use absolute paths with one letter
  471. for drive followed by path separator *)
  472. ((AllowDriveSeparators <> []) and (length(s)>2) and (s[2] in AllowDriveSeparators) and (s[3] in AllowDirectorySeparators)) then
  473. result:=true;
  474. {$endif unix}
  475. end;
  476. Function FileExists ( Const F : TCmdStr;allowcache:boolean) : Boolean;
  477. begin
  478. {$ifdef usedircache}
  479. if allowcache then
  480. Result:=DirCache.FileExists(F)
  481. else
  482. {$endif usedircache}
  483. Result:=SysUtils.FileExists(F);
  484. if do_checkverbosity(V_Tried) then
  485. begin
  486. if Result then
  487. do_comment(V_Tried,'Searching file '+F+'... found')
  488. else
  489. do_comment(V_Tried,'Searching file '+F+'... not found');
  490. end;
  491. end;
  492. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  493. var
  494. fn2 : TCmdStr;
  495. begin
  496. result:=false;
  497. if tf_files_case_sensitive in source_info.flags then
  498. begin
  499. {
  500. Search order for case sensitive systems:
  501. 1. NormalCase
  502. 2. lowercase
  503. 3. UPPERCASE
  504. }
  505. FoundFile:=path+fn;
  506. If FileExists(FoundFile,allowcache) then
  507. begin
  508. result:=true;
  509. exit;
  510. end;
  511. fn2:=Lower(fn);
  512. if fn2<>fn then
  513. begin
  514. FoundFile:=path+fn2;
  515. If FileExists(FoundFile,allowcache) then
  516. begin
  517. result:=true;
  518. exit;
  519. end;
  520. end;
  521. fn2:=Upper(fn);
  522. if fn2<>fn then
  523. begin
  524. FoundFile:=path+fn2;
  525. If FileExists(FoundFile,allowcache) then
  526. begin
  527. result:=true;
  528. exit;
  529. end;
  530. end;
  531. end
  532. else
  533. if tf_files_case_aware in source_info.flags then
  534. begin
  535. {
  536. Search order for case aware systems:
  537. 1. NormalCase
  538. }
  539. {$ifdef usedircache}
  540. if allowcache then
  541. begin
  542. result:=DirCache.FileExistsCaseAware(path,fn,fn2);
  543. if result then
  544. begin
  545. FoundFile:=fn2;
  546. exit;
  547. end;
  548. end
  549. else
  550. {$endif usedircache}
  551. begin
  552. FoundFile:=path+fn;
  553. If FileExists(FoundFile,allowcache) then
  554. begin
  555. { don't know the real name in this case }
  556. result:=true;
  557. exit;
  558. end;
  559. end;
  560. end
  561. else
  562. begin
  563. { None case sensitive only lowercase }
  564. FoundFile:=path+Lower(fn);
  565. If FileExists(FoundFile,allowcache) then
  566. begin
  567. result:=true;
  568. exit;
  569. end;
  570. end;
  571. { Set foundfile to something useful }
  572. FoundFile:=fn;
  573. end;
  574. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  575. Var
  576. i: longint;
  577. hs : TCmdStr;
  578. begin
  579. if F = '' then
  580. begin
  581. result := true;
  582. exit;
  583. end;
  584. hs := ExpandFileName(F);
  585. I := Pos (DriveSeparator, hs);
  586. if (hs [Length (hs)] = DirectorySeparator) and
  587. (((I = 0) and (Length (hs) > 1)) or (I <> Length (hs) - 1)) then
  588. Delete (hs, Length (hs), 1);
  589. {$ifdef usedircache}
  590. if allowcache then
  591. Result:=DirCache.DirectoryExists(hs)
  592. else
  593. {$endif usedircache}
  594. Result:=SysUtils.DirectoryExists(hs);
  595. end;
  596. Function RemoveDir(d:TCmdStr):boolean;
  597. begin
  598. if d[length(d)]=source_info.DirSep then
  599. Delete(d,length(d),1);
  600. {$I-}
  601. rmdir(d);
  602. {$I+}
  603. RemoveDir:=(ioresult=0);
  604. end;
  605. Function FixPath(const s:TCmdStr;allowdot:boolean):TCmdStr;
  606. var
  607. i, L : longint;
  608. P: PChar;
  609. begin
  610. Result := s;
  611. L := Length(Result);
  612. if L=0 then
  613. exit;
  614. { Fix separator }
  615. P := @Result[1];
  616. for i:=0 to L-1 do
  617. begin
  618. if p^ in ['/','\'] then
  619. p^:=source_info.DirSep;
  620. inc(p);
  621. end;
  622. { Fix ending / }
  623. if (L>0) and (Result[L]<>source_info.DirSep) and
  624. (Result[L]<>DriveSeparator) then
  625. Result:=Result+source_info.DirSep; { !still results in temp AnsiString }
  626. { Remove ./ }
  627. if (not allowdot) and ((Length(Result)=2) and (Result[1]='.') and (Result[2] = source_info.DirSep)) then
  628. begin
  629. Result:='';
  630. Exit;
  631. end;
  632. { return }
  633. if not ((tf_files_case_aware in source_info.flags) or
  634. (tf_files_case_sensitive in source_info.flags)) then
  635. Result := lower(Result);
  636. end;
  637. {Actually the version in macutils.pp could be used,
  638. but that would not work for crosscompiling, so this is a slightly modified
  639. version of it.}
  640. function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
  641. function GetVolumeIdentifier: TCmdStr;
  642. begin
  643. GetVolumeIdentifier := '{Boot}'
  644. (*
  645. if mpw then
  646. GetVolumeIdentifier := '{Boot}'
  647. else
  648. GetVolumeIdentifier := macosBootVolumeName;
  649. *)
  650. end;
  651. var
  652. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  653. begin
  654. oldpos := 1;
  655. slashPos := Pos('/', path);
  656. if (slashPos <> 0) then {its a unix path}
  657. begin
  658. if slashPos = 1 then
  659. begin {its a full path}
  660. oldpos := 2;
  661. TranslatePathToMac := GetVolumeIdentifier;
  662. end
  663. else {its a partial path}
  664. TranslatePathToMac := ':';
  665. end
  666. else
  667. begin
  668. slashPos := Pos('\', path);
  669. if (slashPos <> 0) then {its a dos path}
  670. begin
  671. if slashPos = 1 then
  672. begin {its a full path, without drive letter}
  673. oldpos := 2;
  674. TranslatePathToMac := GetVolumeIdentifier;
  675. end
  676. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  677. begin
  678. oldpos := 4;
  679. TranslatePathToMac := GetVolumeIdentifier;
  680. end
  681. else {its a partial path}
  682. TranslatePathToMac := ':';
  683. end;
  684. end;
  685. if (slashPos <> 0) then {its a unix or dos path}
  686. begin
  687. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  688. newpos := Length(TranslatePathToMac);
  689. oldlen := Length(path);
  690. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  691. {prepended plus length of path.}
  692. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  693. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  694. {drive and slash was removed, or it was a relative path without a preceding slash.}
  695. while oldpos <= oldlen do
  696. begin
  697. {Check if special dirs, ./ or ../ }
  698. if path[oldPos] = '.' then
  699. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  700. begin
  701. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  702. begin
  703. {It is "../" or ".." translates to ":" }
  704. if newPos = maxPos then
  705. begin {Shouldn't actually happen, but..}
  706. Exit('');
  707. end;
  708. newPos := newPos + 1;
  709. TranslatePathToMac[newPos] := ':';
  710. oldPos := oldPos + 3;
  711. continue; {Start over again}
  712. end;
  713. end
  714. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  715. begin
  716. {It is "./" or "." ignor it }
  717. oldPos := oldPos + 2;
  718. continue; {Start over again}
  719. end;
  720. {Collect file or dir name}
  721. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  722. begin
  723. if newPos = maxPos then
  724. begin {Shouldn't actually happen, but..}
  725. Exit('');
  726. end;
  727. newPos := newPos + 1;
  728. TranslatePathToMac[newPos] := path[oldPos];
  729. oldPos := oldPos + 1;
  730. end;
  731. {When we come here there is either a slash or we are at the end.}
  732. if (oldpos <= oldlen) then
  733. begin
  734. if newPos = maxPos then
  735. begin {Shouldn't actually happen, but..}
  736. Exit('');
  737. end;
  738. newPos := newPos + 1;
  739. TranslatePathToMac[newPos] := ':';
  740. oldPos := oldPos + 1;
  741. end;
  742. end;
  743. SetLength(TranslatePathToMac, newpos);
  744. end
  745. else if (path = '.') then
  746. TranslatePathToMac := ':'
  747. else if (path = '..') then
  748. TranslatePathToMac := '::'
  749. else
  750. TranslatePathToMac := path; {its a mac path}
  751. end;
  752. function FixFileName(const s:TCmdStr):TCmdStr;
  753. var
  754. i : longint;
  755. begin
  756. if source_info.system = system_powerpc_MACOS then
  757. FixFileName:= TranslatePathToMac(s, true)
  758. else
  759. if (tf_files_case_aware in source_info.flags) or
  760. (tf_files_case_sensitive in source_info.flags) then
  761. begin
  762. setlength(FixFileName,length(s));
  763. for i:=1 to length(s) do
  764. begin
  765. case s[i] of
  766. '/','\' :
  767. FixFileName[i]:=source_info.dirsep;
  768. else
  769. FixFileName[i]:=s[i];
  770. end;
  771. end;
  772. end
  773. else
  774. begin
  775. setlength(FixFileName,length(s));
  776. for i:=1 to length(s) do
  777. begin
  778. case s[i] of
  779. '/','\' :
  780. FixFileName[i]:=source_info.dirsep;
  781. 'A'..'Z' :
  782. FixFileName[i]:=char(byte(s[i])+32);
  783. else
  784. FixFileName[i]:=s[i];
  785. end;
  786. end;
  787. end;
  788. end;
  789. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  790. var
  791. i : longint;
  792. begin
  793. { Fix separator }
  794. for i:=1 to length(s) do
  795. if s[i] in ['/','\'] then
  796. s[i]:=target_info.DirSep;
  797. { Fix ending / }
  798. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  799. (s[length(s)]<>':') then
  800. s:=s+target_info.DirSep;
  801. { Remove ./ }
  802. if (not allowdot) and (s='.'+target_info.DirSep) then
  803. s:='';
  804. { return }
  805. if (tf_files_case_aware in target_info.flags) or
  806. (tf_files_case_sensitive in target_info.flags) then
  807. TargetFixPath:=s
  808. else
  809. TargetFixPath:=Lower(s);
  810. end;
  811. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  812. var
  813. i : longint;
  814. begin
  815. if target_info.system = system_powerpc_MACOS then
  816. TargetFixFileName:= TranslatePathToMac(s, true)
  817. else
  818. if (tf_files_case_aware in target_info.flags) or
  819. (tf_files_case_sensitive in target_info.flags) then
  820. begin
  821. setlength(TargetFixFileName,length(s));
  822. for i:=1 to length(s) do
  823. begin
  824. case s[i] of
  825. '/','\' :
  826. TargetFixFileName[i]:=target_info.dirsep;
  827. else
  828. TargetFixFileName[i]:=s[i];
  829. end;
  830. end;
  831. end
  832. else
  833. begin
  834. setlength(TargetFixFileName,length(s));
  835. for i:=1 to length(s) do
  836. begin
  837. case s[i] of
  838. '/','\' :
  839. TargetFixFileName[i]:=target_info.dirsep;
  840. 'A'..'Z' :
  841. TargetFixFileName[i]:=char(byte(s[i])+32);
  842. else
  843. TargetFixFileName[i]:=s[i];
  844. end;
  845. end;
  846. end;
  847. end;
  848. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  849. var
  850. i : longint;
  851. begin
  852. i:=pos(' ',s);
  853. if i>0 then
  854. begin
  855. bstr:=Copy(s,1,i-1);
  856. cstr:=Copy(s,i+1,length(s)-i);
  857. end
  858. else
  859. begin
  860. bstr:=s;
  861. cstr:='';
  862. end;
  863. end;
  864. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  865. begin
  866. AddPath('',s,AddFirst);
  867. end;
  868. procedure TSearchPathList.AddPath(SrcPath,s:TCmdStr;addfirst:boolean);
  869. var
  870. staridx,
  871. i,j : longint;
  872. prefix,
  873. suffix,
  874. CurrentDir,
  875. currPath : TCmdStr;
  876. subdirfound : boolean;
  877. {$ifdef usedircache}
  878. dir : TCachedSearchRec;
  879. {$else usedircache}
  880. dir : TSearchRec;
  881. {$endif usedircache}
  882. hp : TCmdStrListItem;
  883. procedure WarnNonExistingPath(const path : TCmdStr);
  884. begin
  885. if do_checkverbosity(V_Tried) then
  886. do_comment(V_Tried,'Path "'+path+'" not found');
  887. end;
  888. procedure AddCurrPath;
  889. begin
  890. if addfirst then
  891. begin
  892. Remove(currPath);
  893. Insert(currPath);
  894. end
  895. else
  896. begin
  897. { Check if already in path, then we don't add it }
  898. hp:=Find(currPath);
  899. if not assigned(hp) then
  900. Concat(currPath);
  901. end;
  902. end;
  903. begin
  904. if s='' then
  905. exit;
  906. { Support default macro's }
  907. DefaultReplacements(s);
  908. {$warnings off}
  909. if PathSeparator <> ';' then
  910. for i:=1 to length(s) do
  911. if s[i]=PathSeparator then
  912. s[i]:=';';
  913. {$warnings on}
  914. { get current dir }
  915. CurrentDir:=GetCurrentDir;
  916. repeat
  917. { get currpath }
  918. if addfirst then
  919. begin
  920. j:=length(s);
  921. while (j>0) and (s[j]<>';') do
  922. dec(j);
  923. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  924. if j=0 then
  925. s:=''
  926. else
  927. System.Delete(s,j,length(s)-j+1);
  928. end
  929. else
  930. begin
  931. j:=Pos(';',s);
  932. if j=0 then
  933. j:=length(s)+1;
  934. currPath:= TrimSpace(Copy(s,1,j-1));
  935. System.Delete(s,1,j);
  936. end;
  937. { fix pathname }
  938. DePascalQuote(currPath);
  939. currPath:=SrcPath+FixPath(currPath,false);
  940. if currPath='' then
  941. currPath:= CurDirRelPath(source_info)
  942. else
  943. begin
  944. currPath:=FixPath(ExpandFileName(currpath),false);
  945. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  946. begin
  947. {$if defined(amiga) and defined(morphos)}
  948. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  949. {$else}
  950. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  951. {$endif}
  952. end;
  953. end;
  954. { wildcard adding ? }
  955. staridx:=pos('*',currpath);
  956. if staridx>0 then
  957. begin
  958. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  959. suffix:=Copy(currpath,staridx+1,length(currpath));
  960. subdirfound:=false;
  961. {$ifdef usedircache}
  962. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  963. begin
  964. repeat
  965. if (dir.attr and faDirectory)<>0 then
  966. begin
  967. subdirfound:=true;
  968. currpath:=prefix+dir.name+suffix;
  969. if (suffix='') or PathExists(currpath,true) then
  970. begin
  971. hp:=Find(currPath);
  972. if not assigned(hp) then
  973. AddCurrPath;
  974. end;
  975. end;
  976. until not DirCache.FindNext(dir);
  977. end;
  978. DirCache.FindClose(dir);
  979. {$else usedircache}
  980. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  981. begin
  982. repeat
  983. if (dir.name<>'.') and
  984. (dir.name<>'..') and
  985. ((dir.attr and faDirectory)<>0) then
  986. begin
  987. subdirfound:=true;
  988. currpath:=prefix+dir.name+suffix;
  989. if (suffix='') or PathExists(currpath,false) then
  990. begin
  991. hp:=Find(currPath);
  992. if not assigned(hp) then
  993. AddCurrPath;
  994. end;
  995. end;
  996. until findnext(dir) <> 0;
  997. end;
  998. FindClose(dir);
  999. {$endif usedircache}
  1000. if not subdirfound then
  1001. WarnNonExistingPath(currpath);
  1002. end
  1003. else
  1004. begin
  1005. if PathExists(currpath,true) then
  1006. AddCurrPath
  1007. else
  1008. WarnNonExistingPath(currpath);
  1009. end;
  1010. until (s='');
  1011. end;
  1012. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1013. var
  1014. s : TCmdStr;
  1015. hl : TSearchPathList;
  1016. hp,hp2 : TCmdStrListItem;
  1017. begin
  1018. if list.empty then
  1019. exit;
  1020. { create temp and reverse the list }
  1021. if addfirst then
  1022. begin
  1023. hl:=TSearchPathList.Create;
  1024. hp:=TCmdStrListItem(list.first);
  1025. while assigned(hp) do
  1026. begin
  1027. hl.insert(hp.Str);
  1028. hp:=TCmdStrListItem(hp.next);
  1029. end;
  1030. while not hl.empty do
  1031. begin
  1032. s:=hl.GetFirst;
  1033. Remove(s);
  1034. Insert(s);
  1035. end;
  1036. hl.Free;
  1037. end
  1038. else
  1039. begin
  1040. hp:=TCmdStrListItem(list.first);
  1041. while assigned(hp) do
  1042. begin
  1043. hp2:=Find(hp.Str);
  1044. { Check if already in path, then we don't add it }
  1045. if not assigned(hp2) then
  1046. Concat(hp.Str);
  1047. hp:=TCmdStrListItem(hp.next);
  1048. end;
  1049. end;
  1050. end;
  1051. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1052. Var
  1053. p : TCmdStrListItem;
  1054. begin
  1055. FindFile:=false;
  1056. p:=TCmdStrListItem(first);
  1057. while assigned(p) do
  1058. begin
  1059. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1060. if result then
  1061. exit;
  1062. p:=TCmdStrListItem(p.next);
  1063. end;
  1064. { Return original filename if not found }
  1065. FoundFile:=f;
  1066. end;
  1067. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1068. Var
  1069. StartPos, EndPos, L: LongInt;
  1070. begin
  1071. Result:=False;
  1072. StartPos := 1;
  1073. L := Length(Path);
  1074. repeat
  1075. EndPos := StartPos;
  1076. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1077. Inc(EndPos);
  1078. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1079. if Result then
  1080. Exit;
  1081. StartPos := EndPos + 1;
  1082. until StartPos > L;
  1083. FoundFile:=f;
  1084. end;
  1085. {
  1086. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1087. Var
  1088. singlepathstring : TCmdStr;
  1089. startpc,pc : pchar;
  1090. begin
  1091. FindFilePchar:=false;
  1092. if Assigned (Path) then
  1093. begin
  1094. pc:=path;
  1095. repeat
  1096. startpc:=pc;
  1097. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1098. inc(pc);
  1099. SetLength(singlepathstring, pc-startpc);
  1100. move(startpc^,singlepathstring[1],pc-startpc);
  1101. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1102. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1103. if result then
  1104. exit;
  1105. if (pc^=#0) then
  1106. break;
  1107. inc(pc);
  1108. until false;
  1109. end;
  1110. foundfile:=f;
  1111. end;
  1112. }
  1113. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1114. var
  1115. Path : TCmdStr;
  1116. found : boolean;
  1117. begin
  1118. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),exepath,allowcache,foundfile);
  1119. if not found then
  1120. begin
  1121. {$ifdef macos}
  1122. Path:=GetEnvironmentVariable('Commands');
  1123. {$else}
  1124. Path:=GetEnvironmentVariable('PATH');
  1125. {$endif}
  1126. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),Path,allowcache,foundfile);
  1127. end;
  1128. FindExe:=found;
  1129. end;
  1130. function GetShortName(const n:TCmdStr):TCmdStr;
  1131. {$ifdef win32}
  1132. var
  1133. hs,hs2 : TCmdStr;
  1134. i : longint;
  1135. {$endif}
  1136. {$if defined(go32v2) or defined(watcom)}
  1137. var
  1138. hs : shortstring;
  1139. {$endif}
  1140. begin
  1141. GetShortName:=n;
  1142. {$ifdef win32}
  1143. hs:=n+#0;
  1144. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1145. setlength(hs2,length(hs)*2);
  1146. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1147. if (i>0) and (i<=length(hs)*2) then
  1148. begin
  1149. setlength(hs2,strlen(@hs2[1]));
  1150. GetShortName:=hs2;
  1151. end;
  1152. {$endif}
  1153. {$if defined(go32v2) or defined(watcom)}
  1154. hs:=n;
  1155. if Dos.GetShortName(hs) then
  1156. GetShortName:=hs;
  1157. {$endif}
  1158. end;
  1159. {****************************************************************************
  1160. Init / Done
  1161. ****************************************************************************}
  1162. procedure InitFileUtils;
  1163. begin
  1164. DirCache:=TDirectoryCache.Create;
  1165. end;
  1166. procedure DoneFileUtils;
  1167. begin
  1168. DirCache.Free;
  1169. end;
  1170. end.