cfileutils.pas 32 KB

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