cfileutl.pas 38 KB

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