cfileutl.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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): longint;
  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): longint;
  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. findclose(dir);
  246. end;
  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. { make result unique since we're going to change it via a pchar }
  612. uniquestring(result);
  613. L := Length(Result);
  614. if L=0 then
  615. exit;
  616. { Fix separator }
  617. P := @Result[1];
  618. for i:=0 to L-1 do
  619. begin
  620. if p^ in ['/','\'] then
  621. p^:=source_info.DirSep;
  622. inc(p);
  623. end;
  624. { Fix ending / }
  625. if (L>0) and (Result[L]<>source_info.DirSep) and
  626. (Result[L]<>DriveSeparator) then
  627. Result:=Result+source_info.DirSep; { !still results in temp AnsiString }
  628. { Remove ./ }
  629. if (not allowdot) and ((Length(Result)=2) and (Result[1]='.') and (Result[2] = source_info.DirSep)) then
  630. begin
  631. Result:='';
  632. Exit;
  633. end;
  634. { return }
  635. if not ((tf_files_case_aware in source_info.flags) or
  636. (tf_files_case_sensitive in source_info.flags)) then
  637. Result := lower(Result);
  638. end;
  639. {Actually the version in macutils.pp could be used,
  640. but that would not work for crosscompiling, so this is a slightly modified
  641. version of it.}
  642. function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
  643. function GetVolumeIdentifier: TCmdStr;
  644. begin
  645. GetVolumeIdentifier := '{Boot}'
  646. (*
  647. if mpw then
  648. GetVolumeIdentifier := '{Boot}'
  649. else
  650. GetVolumeIdentifier := macosBootVolumeName;
  651. *)
  652. end;
  653. var
  654. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  655. begin
  656. oldpos := 1;
  657. slashPos := Pos('/', path);
  658. if (slashPos <> 0) then {its a unix path}
  659. begin
  660. if slashPos = 1 then
  661. begin {its a full path}
  662. oldpos := 2;
  663. TranslatePathToMac := GetVolumeIdentifier;
  664. end
  665. else {its a partial path}
  666. TranslatePathToMac := ':';
  667. end
  668. else
  669. begin
  670. slashPos := Pos('\', path);
  671. if (slashPos <> 0) then {its a dos path}
  672. begin
  673. if slashPos = 1 then
  674. begin {its a full path, without drive letter}
  675. oldpos := 2;
  676. TranslatePathToMac := GetVolumeIdentifier;
  677. end
  678. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  679. begin
  680. oldpos := 4;
  681. TranslatePathToMac := GetVolumeIdentifier;
  682. end
  683. else {its a partial path}
  684. TranslatePathToMac := ':';
  685. end;
  686. end;
  687. if (slashPos <> 0) then {its a unix or dos path}
  688. begin
  689. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  690. newpos := Length(TranslatePathToMac);
  691. oldlen := Length(path);
  692. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  693. {prepended plus length of path.}
  694. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  695. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  696. {drive and slash was removed, or it was a relative path without a preceding slash.}
  697. while oldpos <= oldlen do
  698. begin
  699. {Check if special dirs, ./ or ../ }
  700. if path[oldPos] = '.' then
  701. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  702. begin
  703. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  704. begin
  705. {It is "../" or ".." translates to ":" }
  706. if newPos = maxPos then
  707. begin {Shouldn't actually happen, but..}
  708. Exit('');
  709. end;
  710. newPos := newPos + 1;
  711. TranslatePathToMac[newPos] := ':';
  712. oldPos := oldPos + 3;
  713. continue; {Start over again}
  714. end;
  715. end
  716. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  717. begin
  718. {It is "./" or "." ignor it }
  719. oldPos := oldPos + 2;
  720. continue; {Start over again}
  721. end;
  722. {Collect file or dir name}
  723. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  724. begin
  725. if newPos = maxPos then
  726. begin {Shouldn't actually happen, but..}
  727. Exit('');
  728. end;
  729. newPos := newPos + 1;
  730. TranslatePathToMac[newPos] := path[oldPos];
  731. oldPos := oldPos + 1;
  732. end;
  733. {When we come here there is either a slash or we are at the end.}
  734. if (oldpos <= oldlen) then
  735. begin
  736. if newPos = maxPos then
  737. begin {Shouldn't actually happen, but..}
  738. Exit('');
  739. end;
  740. newPos := newPos + 1;
  741. TranslatePathToMac[newPos] := ':';
  742. oldPos := oldPos + 1;
  743. end;
  744. end;
  745. SetLength(TranslatePathToMac, newpos);
  746. end
  747. else if (path = '.') then
  748. TranslatePathToMac := ':'
  749. else if (path = '..') then
  750. TranslatePathToMac := '::'
  751. else
  752. TranslatePathToMac := path; {its a mac path}
  753. end;
  754. function FixFileName(const s:TCmdStr):TCmdStr;
  755. var
  756. i : longint;
  757. begin
  758. if source_info.system = system_powerpc_MACOS then
  759. FixFileName:= TranslatePathToMac(s, true)
  760. else
  761. if (tf_files_case_aware in source_info.flags) or
  762. (tf_files_case_sensitive in source_info.flags) then
  763. begin
  764. setlength(FixFileName,length(s));
  765. for i:=1 to length(s) do
  766. begin
  767. case s[i] of
  768. '/','\' :
  769. FixFileName[i]:=source_info.dirsep;
  770. else
  771. FixFileName[i]:=s[i];
  772. end;
  773. end;
  774. end
  775. else
  776. begin
  777. setlength(FixFileName,length(s));
  778. for i:=1 to length(s) do
  779. begin
  780. case s[i] of
  781. '/','\' :
  782. FixFileName[i]:=source_info.dirsep;
  783. 'A'..'Z' :
  784. FixFileName[i]:=char(byte(s[i])+32);
  785. else
  786. FixFileName[i]:=s[i];
  787. end;
  788. end;
  789. end;
  790. end;
  791. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  792. var
  793. i : longint;
  794. begin
  795. { Fix separator }
  796. for i:=1 to length(s) do
  797. if s[i] in ['/','\'] then
  798. s[i]:=target_info.DirSep;
  799. { Fix ending / }
  800. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  801. (s[length(s)]<>':') then
  802. s:=s+target_info.DirSep;
  803. { Remove ./ }
  804. if (not allowdot) and (s='.'+target_info.DirSep) then
  805. s:='';
  806. { return }
  807. if (tf_files_case_aware in target_info.flags) or
  808. (tf_files_case_sensitive in target_info.flags) then
  809. TargetFixPath:=s
  810. else
  811. TargetFixPath:=Lower(s);
  812. end;
  813. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  814. var
  815. i : longint;
  816. begin
  817. if target_info.system = system_powerpc_MACOS then
  818. TargetFixFileName:= TranslatePathToMac(s, true)
  819. else
  820. if (tf_files_case_aware in target_info.flags) or
  821. (tf_files_case_sensitive in target_info.flags) then
  822. begin
  823. setlength(TargetFixFileName,length(s));
  824. for i:=1 to length(s) do
  825. begin
  826. case s[i] of
  827. '/','\' :
  828. TargetFixFileName[i]:=target_info.dirsep;
  829. else
  830. TargetFixFileName[i]:=s[i];
  831. end;
  832. end;
  833. end
  834. else
  835. begin
  836. setlength(TargetFixFileName,length(s));
  837. for i:=1 to length(s) do
  838. begin
  839. case s[i] of
  840. '/','\' :
  841. TargetFixFileName[i]:=target_info.dirsep;
  842. 'A'..'Z' :
  843. TargetFixFileName[i]:=char(byte(s[i])+32);
  844. else
  845. TargetFixFileName[i]:=s[i];
  846. end;
  847. end;
  848. end;
  849. end;
  850. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  851. var
  852. i : longint;
  853. begin
  854. i:=pos(' ',s);
  855. if i>0 then
  856. begin
  857. bstr:=Copy(s,1,i-1);
  858. cstr:=Copy(s,i+1,length(s)-i);
  859. end
  860. else
  861. begin
  862. bstr:=s;
  863. cstr:='';
  864. end;
  865. end;
  866. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  867. begin
  868. AddPath('',s,AddFirst);
  869. end;
  870. procedure TSearchPathList.AddPath(SrcPath,s:TCmdStr;addfirst:boolean);
  871. var
  872. staridx,
  873. i,j : longint;
  874. prefix,
  875. suffix,
  876. CurrentDir,
  877. currPath : TCmdStr;
  878. subdirfound : boolean;
  879. {$ifdef usedircache}
  880. dir : TCachedSearchRec;
  881. {$else usedircache}
  882. dir : TSearchRec;
  883. {$endif usedircache}
  884. hp : TCmdStrListItem;
  885. procedure WarnNonExistingPath(const path : TCmdStr);
  886. begin
  887. if do_checkverbosity(V_Tried) then
  888. do_comment(V_Tried,'Path "'+path+'" not found');
  889. end;
  890. procedure AddCurrPath;
  891. begin
  892. if addfirst then
  893. begin
  894. Remove(currPath);
  895. Insert(currPath);
  896. end
  897. else
  898. begin
  899. { Check if already in path, then we don't add it }
  900. hp:=Find(currPath);
  901. if not assigned(hp) then
  902. Concat(currPath);
  903. end;
  904. end;
  905. begin
  906. if s='' then
  907. exit;
  908. { Support default macro's }
  909. DefaultReplacements(s);
  910. {$warnings off}
  911. if PathSeparator <> ';' then
  912. for i:=1 to length(s) do
  913. if s[i]=PathSeparator then
  914. s[i]:=';';
  915. {$warnings on}
  916. { get current dir }
  917. CurrentDir:=GetCurrentDir;
  918. repeat
  919. { get currpath }
  920. if addfirst then
  921. begin
  922. j:=length(s);
  923. while (j>0) and (s[j]<>';') do
  924. dec(j);
  925. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  926. if j=0 then
  927. s:=''
  928. else
  929. System.Delete(s,j,length(s)-j+1);
  930. end
  931. else
  932. begin
  933. j:=Pos(';',s);
  934. if j=0 then
  935. j:=length(s)+1;
  936. currPath:= TrimSpace(Copy(s,1,j-1));
  937. System.Delete(s,1,j);
  938. end;
  939. { fix pathname }
  940. DePascalQuote(currPath);
  941. currPath:=SrcPath+FixPath(currPath,false);
  942. if currPath='' then
  943. currPath:= CurDirRelPath(source_info)
  944. else
  945. begin
  946. currPath:=FixPath(ExpandFileName(currpath),false);
  947. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  948. begin
  949. {$if defined(amiga) and defined(morphos)}
  950. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,length(currPath));
  951. {$else}
  952. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,length(currPath));
  953. {$endif}
  954. end;
  955. end;
  956. { wildcard adding ? }
  957. staridx:=pos('*',currpath);
  958. if staridx>0 then
  959. begin
  960. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  961. suffix:=Copy(currpath,staridx+1,length(currpath));
  962. subdirfound:=false;
  963. {$ifdef usedircache}
  964. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  965. begin
  966. repeat
  967. if (dir.attr and faDirectory)<>0 then
  968. begin
  969. subdirfound:=true;
  970. currpath:=prefix+dir.name+suffix;
  971. if (suffix='') or PathExists(currpath,true) then
  972. begin
  973. hp:=Find(currPath);
  974. if not assigned(hp) then
  975. AddCurrPath;
  976. end;
  977. end;
  978. until not DirCache.FindNext(dir);
  979. end;
  980. DirCache.FindClose(dir);
  981. {$else usedircache}
  982. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  983. begin
  984. repeat
  985. if (dir.name<>'.') and
  986. (dir.name<>'..') and
  987. ((dir.attr and faDirectory)<>0) then
  988. begin
  989. subdirfound:=true;
  990. currpath:=prefix+dir.name+suffix;
  991. if (suffix='') or PathExists(currpath,false) then
  992. begin
  993. hp:=Find(currPath);
  994. if not assigned(hp) then
  995. AddCurrPath;
  996. end;
  997. end;
  998. until findnext(dir) <> 0;
  999. FindClose(dir);
  1000. end;
  1001. {$endif usedircache}
  1002. if not subdirfound then
  1003. WarnNonExistingPath(currpath);
  1004. end
  1005. else
  1006. begin
  1007. if PathExists(currpath,true) then
  1008. AddCurrPath
  1009. else
  1010. WarnNonExistingPath(currpath);
  1011. end;
  1012. until (s='');
  1013. end;
  1014. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  1015. var
  1016. s : TCmdStr;
  1017. hl : TSearchPathList;
  1018. hp,hp2 : TCmdStrListItem;
  1019. begin
  1020. if list.empty then
  1021. exit;
  1022. { create temp and reverse the list }
  1023. if addfirst then
  1024. begin
  1025. hl:=TSearchPathList.Create;
  1026. hp:=TCmdStrListItem(list.first);
  1027. while assigned(hp) do
  1028. begin
  1029. hl.insert(hp.Str);
  1030. hp:=TCmdStrListItem(hp.next);
  1031. end;
  1032. while not hl.empty do
  1033. begin
  1034. s:=hl.GetFirst;
  1035. Remove(s);
  1036. Insert(s);
  1037. end;
  1038. hl.Free;
  1039. end
  1040. else
  1041. begin
  1042. hp:=TCmdStrListItem(list.first);
  1043. while assigned(hp) do
  1044. begin
  1045. hp2:=Find(hp.Str);
  1046. { Check if already in path, then we don't add it }
  1047. if not assigned(hp2) then
  1048. Concat(hp.Str);
  1049. hp:=TCmdStrListItem(hp.next);
  1050. end;
  1051. end;
  1052. end;
  1053. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1054. Var
  1055. p : TCmdStrListItem;
  1056. begin
  1057. FindFile:=false;
  1058. p:=TCmdStrListItem(first);
  1059. while assigned(p) do
  1060. begin
  1061. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1062. if result then
  1063. exit;
  1064. p:=TCmdStrListItem(p.next);
  1065. end;
  1066. { Return original filename if not found }
  1067. FoundFile:=f;
  1068. end;
  1069. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1070. Var
  1071. StartPos, EndPos, L: LongInt;
  1072. begin
  1073. Result:=False;
  1074. StartPos := 1;
  1075. L := Length(Path);
  1076. repeat
  1077. EndPos := StartPos;
  1078. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1079. Inc(EndPos);
  1080. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1081. if Result then
  1082. Exit;
  1083. StartPos := EndPos + 1;
  1084. until StartPos > L;
  1085. FoundFile:=f;
  1086. end;
  1087. {
  1088. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1089. Var
  1090. singlepathstring : TCmdStr;
  1091. startpc,pc : pchar;
  1092. begin
  1093. FindFilePchar:=false;
  1094. if Assigned (Path) then
  1095. begin
  1096. pc:=path;
  1097. repeat
  1098. startpc:=pc;
  1099. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1100. inc(pc);
  1101. SetLength(singlepathstring, pc-startpc);
  1102. move(startpc^,singlepathstring[1],pc-startpc);
  1103. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1104. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1105. if result then
  1106. exit;
  1107. if (pc^=#0) then
  1108. break;
  1109. inc(pc);
  1110. until false;
  1111. end;
  1112. foundfile:=f;
  1113. end;
  1114. }
  1115. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1116. var
  1117. Path : TCmdStr;
  1118. found : boolean;
  1119. begin
  1120. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),exepath,allowcache,foundfile);
  1121. if not found then
  1122. begin
  1123. {$ifdef macos}
  1124. Path:=GetEnvironmentVariable('Commands');
  1125. {$else}
  1126. Path:=GetEnvironmentVariable('PATH');
  1127. {$endif}
  1128. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),Path,allowcache,foundfile);
  1129. end;
  1130. FindExe:=found;
  1131. end;
  1132. function GetShortName(const n:TCmdStr):TCmdStr;
  1133. {$ifdef win32}
  1134. var
  1135. hs,hs2 : TCmdStr;
  1136. i : longint;
  1137. {$endif}
  1138. {$if defined(go32v2) or defined(watcom)}
  1139. var
  1140. hs : shortstring;
  1141. {$endif}
  1142. begin
  1143. GetShortName:=n;
  1144. {$ifdef win32}
  1145. hs:=n+#0;
  1146. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1147. setlength(hs2,length(hs)*2);
  1148. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1149. if (i>0) and (i<=length(hs)*2) then
  1150. begin
  1151. setlength(hs2,strlen(@hs2[1]));
  1152. GetShortName:=hs2;
  1153. end;
  1154. {$endif}
  1155. {$if defined(go32v2) or defined(watcom)}
  1156. hs:=n;
  1157. if Dos.GetShortName(hs) then
  1158. GetShortName:=hs;
  1159. {$endif}
  1160. end;
  1161. {****************************************************************************
  1162. Init / Done
  1163. ****************************************************************************}
  1164. procedure InitFileUtils;
  1165. begin
  1166. DirCache:=TDirectoryCache.Create;
  1167. end;
  1168. procedure DoneFileUtils;
  1169. begin
  1170. DirCache.Free;
  1171. end;
  1172. end.