cfileutl.pas 37 KB

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