cfileutl.pas 35 KB

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