cfileutils.pas 30 KB

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