cfileutl.pas 35 KB

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