cfileutils.pas 31 KB

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