cfileutils.pas 32 KB

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