cfileutils.pas 31 KB

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