cfileutl.pas 38 KB

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