cfileutl.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 : longint;
  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. {$elseif defined(win32) or defined(win64) or defined(go32v2)}
  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. {$warnings off}
  880. if PathSeparator <> ';' then
  881. for i:=1 to length(s) do
  882. if s[i]=PathSeparator then
  883. s[i]:=';';
  884. {$warnings on}
  885. { get current dir }
  886. CurrentDir:=GetCurrentDir;
  887. repeat
  888. { get currpath }
  889. if addfirst then
  890. begin
  891. j:=length(s);
  892. while (j>0) and (s[j]<>';') do
  893. dec(j);
  894. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  895. if j=0 then
  896. s:=''
  897. else
  898. System.Delete(s,j,length(s)-j+1);
  899. end
  900. else
  901. begin
  902. j:=Pos(';',s);
  903. if j=0 then
  904. j:=255;
  905. currPath:= TrimSpace(Copy(s,1,j-1));
  906. System.Delete(s,1,j);
  907. end;
  908. { fix pathname }
  909. DePascalQuote(currPath);
  910. currPath:=SrcPath+FixPath(currPath,false);
  911. if currPath='' then
  912. currPath:= CurDirRelPath(source_info)
  913. else
  914. begin
  915. currPath:=FixPath(ExpandFileName(currpath),false);
  916. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  917. begin
  918. {$if defined(amiga) and defined(morphos)}
  919. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,255);
  920. {$else}
  921. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,255);
  922. {$endif}
  923. end;
  924. end;
  925. { wildcard adding ? }
  926. staridx:=pos('*',currpath);
  927. if staridx>0 then
  928. begin
  929. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  930. suffix:=Copy(currpath,staridx+1,length(currpath));
  931. subdirfound:=false;
  932. {$ifdef usedircache}
  933. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  934. begin
  935. repeat
  936. if (dir.attr and faDirectory)<>0 then
  937. begin
  938. subdirfound:=true;
  939. currpath:=prefix+dir.name+suffix;
  940. if (suffix='') or PathExists(currpath,true) then
  941. begin
  942. hp:=Find(currPath);
  943. if not assigned(hp) then
  944. AddCurrPath;
  945. end;
  946. end;
  947. until not DirCache.FindNext(dir);
  948. end;
  949. DirCache.FindClose(dir);
  950. {$else usedircache}
  951. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  952. begin
  953. repeat
  954. if (dir.name<>'.') and
  955. (dir.name<>'..') and
  956. ((dir.attr and faDirectory)<>0) then
  957. begin
  958. subdirfound:=true;
  959. currpath:=prefix+dir.name+suffix;
  960. if (suffix='') or PathExists(currpath,false) then
  961. begin
  962. hp:=Find(currPath);
  963. if not assigned(hp) then
  964. AddCurrPath;
  965. end;
  966. end;
  967. until findnext(dir) <> 0;
  968. end;
  969. FindClose(dir);
  970. {$endif usedircache}
  971. if not subdirfound then
  972. WarnNonExistingPath(currpath);
  973. end
  974. else
  975. begin
  976. if PathExists(currpath,true) then
  977. AddCurrPath
  978. else
  979. WarnNonExistingPath(currpath);
  980. end;
  981. until (s='');
  982. end;
  983. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  984. var
  985. s : TCmdStr;
  986. hl : TSearchPathList;
  987. hp,hp2 : TCmdStrListItem;
  988. begin
  989. if list.empty then
  990. exit;
  991. { create temp and reverse the list }
  992. if addfirst then
  993. begin
  994. hl:=TSearchPathList.Create;
  995. hp:=TCmdStrListItem(list.first);
  996. while assigned(hp) do
  997. begin
  998. hl.insert(hp.Str);
  999. hp:=TCmdStrListItem(hp.next);
  1000. end;
  1001. while not hl.empty do
  1002. begin
  1003. s:=hl.GetFirst;
  1004. Remove(s);
  1005. Insert(s);
  1006. end;
  1007. hl.Free;
  1008. end
  1009. else
  1010. begin
  1011. hp:=TCmdStrListItem(list.first);
  1012. while assigned(hp) do
  1013. begin
  1014. hp2:=Find(hp.Str);
  1015. { Check if already in path, then we don't add it }
  1016. if not assigned(hp2) then
  1017. Concat(hp.Str);
  1018. hp:=TCmdStrListItem(hp.next);
  1019. end;
  1020. end;
  1021. end;
  1022. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1023. Var
  1024. p : TCmdStrListItem;
  1025. begin
  1026. FindFile:=false;
  1027. p:=TCmdStrListItem(first);
  1028. while assigned(p) do
  1029. begin
  1030. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1031. if result then
  1032. exit;
  1033. p:=TCmdStrListItem(p.next);
  1034. end;
  1035. { Return original filename if not found }
  1036. FoundFile:=f;
  1037. end;
  1038. function FindFile(const f : TCmdStr; const path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1039. Var
  1040. StartPos, EndPos, L: LongInt;
  1041. begin
  1042. Result:=False;
  1043. StartPos := 1;
  1044. L := Length(Path);
  1045. repeat
  1046. EndPos := StartPos;
  1047. while (EndPos <= L) and ((Path[EndPos] <> PathSeparator) and (Path[EndPos] <> ';')) do
  1048. Inc(EndPos);
  1049. Result := FileExistsNonCase(FixPath(Copy(Path, StartPos, EndPos-StartPos), False), f, allowcache, FoundFile);
  1050. if Result then
  1051. Exit;
  1052. StartPos := EndPos + 1;
  1053. until StartPos > L;
  1054. FoundFile:=f;
  1055. end;
  1056. {
  1057. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1058. Var
  1059. singlepathstring : TCmdStr;
  1060. startpc,pc : pchar;
  1061. begin
  1062. FindFilePchar:=false;
  1063. if Assigned (Path) then
  1064. begin
  1065. pc:=path;
  1066. repeat
  1067. startpc:=pc;
  1068. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1069. inc(pc);
  1070. SetLength(singlepathstring, pc-startpc);
  1071. move(startpc^,singlepathstring[1],pc-startpc);
  1072. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1073. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1074. if result then
  1075. exit;
  1076. if (pc^=#0) then
  1077. break;
  1078. inc(pc);
  1079. until false;
  1080. end;
  1081. foundfile:=f;
  1082. end;
  1083. }
  1084. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1085. var
  1086. Path : TCmdStr;
  1087. found : boolean;
  1088. begin
  1089. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),'.;'+exepath,allowcache,foundfile);
  1090. if not found then
  1091. begin
  1092. {$ifdef macos}
  1093. Path:=GetEnvironmentVariable('Commands');
  1094. {$else}
  1095. Path:=GetEnvironmentVariable('PATH');
  1096. {$endif}
  1097. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),Path,allowcache,foundfile);
  1098. end;
  1099. FindExe:=found;
  1100. end;
  1101. function GetShortName(const n:TCmdStr):TCmdStr;
  1102. {$ifdef win32}
  1103. var
  1104. hs,hs2 : TCmdStr;
  1105. i : longint;
  1106. {$endif}
  1107. {$if defined(go32v2) or defined(watcom)}
  1108. var
  1109. hs : shortstring;
  1110. {$endif}
  1111. begin
  1112. GetShortName:=n;
  1113. {$ifdef win32}
  1114. hs:=n+#0;
  1115. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1116. setlength(hs2,length(hs)*2);
  1117. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1118. if (i>0) and (i<=length(hs)*2) then
  1119. begin
  1120. setlength(hs2,strlen(@hs2[1]));
  1121. GetShortName:=hs2;
  1122. end;
  1123. {$endif}
  1124. {$if defined(go32v2) or defined(watcom)}
  1125. hs:=n;
  1126. if Dos.GetShortName(hs) then
  1127. GetShortName:=hs;
  1128. {$endif}
  1129. end;
  1130. {****************************************************************************
  1131. Init / Done
  1132. ****************************************************************************}
  1133. procedure InitFileUtils;
  1134. begin
  1135. DirCache:=TDirectoryCache.Create;
  1136. end;
  1137. procedure DoneFileUtils;
  1138. begin
  1139. DirCache.Free;
  1140. end;
  1141. end.