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