cfileutl.pas 38 KB

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