cfileutils.pas 36 KB

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