cfileutils.pas 36 KB

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