cfileutils.pas 32 KB

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