cfileutils.pas 37 KB

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