cfileutl.pas 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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 : byte;
  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(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;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. if not(tf_files_case_sensitive in source_info.flags) then
  218. if (tf_files_case_aware in source_info.flags) then
  219. begin
  220. new(entry);
  221. entry^.RealName:=Dir.Name;
  222. entry^.Attr:=Dir.Attr;
  223. DirectoryEntries.Add(Lower(Dir.Name),entry)
  224. end
  225. else
  226. DirectoryEntries.Add(Lower(Dir.Name),Pointer(Ptrint(Dir.Attr)))
  227. else
  228. DirectoryEntries.Add(Dir.Name,Pointer(Ptrint(Dir.Attr)));
  229. end;
  230. until findnext(dir) <> 0;
  231. end;
  232. findclose(dir);
  233. end;
  234. function TCachedDirectory.FileExists(const AName:TCmdStr):boolean;
  235. var
  236. Attr : Longint;
  237. begin
  238. if not TryUseCache then
  239. begin
  240. { prepend directory name again }
  241. result:=cfileutl.FileExists(Name+AName,false);
  242. exit;
  243. end;
  244. Attr:=GetItemAttr(AName);
  245. if Attr<>0 then
  246. Result:=((Attr and faDirectory)=0)
  247. else
  248. Result:=false;
  249. end;
  250. function TCachedDirectory.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  251. var
  252. entry : PCachedDirectoryEntry;
  253. Attr : Longint;
  254. begin
  255. if (tf_files_case_aware in source_info.flags) then
  256. begin
  257. if not TryUseCache then
  258. begin
  259. Result:=FileExistsNonCase(path,fn,false,FoundName);
  260. exit;
  261. end;
  262. entry:=PCachedDirectoryEntry(DirectoryEntries.Find(Lower(ExtractFileName(fn))));
  263. if assigned(entry) and
  264. (entry^.Attr<>0) and
  265. ((entry^.Attr and faDirectory) = 0) then
  266. begin
  267. FoundName:=ExtractFilePath(path+fn)+entry^.RealName;
  268. Result:=true
  269. end
  270. else
  271. Result:=false;
  272. end
  273. else
  274. { should not be called in this case, use plain FileExists }
  275. Result:=False;
  276. end;
  277. function TCachedDirectory.DirectoryExists(const AName:TCmdStr):boolean;
  278. var
  279. Attr : Longint;
  280. begin
  281. if not TryUseCache then
  282. begin
  283. Result:=PathExists(Name+AName,false);
  284. exit;
  285. end;
  286. Attr:=GetItemAttr(AName);
  287. if Attr<>0 then
  288. Result:=((Attr and faDirectory)=faDirectory)
  289. else
  290. Result:=false;
  291. end;
  292. {****************************************************************************
  293. TDirectoryCache
  294. ****************************************************************************}
  295. constructor TDirectoryCache.create;
  296. begin
  297. inherited create;
  298. FDirectories:=TFPHashObjectList.Create(true);
  299. end;
  300. destructor TDirectoryCache.destroy;
  301. begin
  302. FDirectories.Free;
  303. inherited destroy;
  304. end;
  305. function TDirectoryCache.GetDirectory(const ADir:TCmdStr):TCachedDirectory;
  306. var
  307. CachedDir : TCachedDirectory;
  308. DirName : TCmdStr;
  309. begin
  310. if ADir='' then
  311. DirName:='.'+source_info.DirSep
  312. else
  313. DirName:=ADir;
  314. CachedDir:=TCachedDirectory(FDirectories.Find(DirName));
  315. if not assigned(CachedDir) then
  316. CachedDir:=TCachedDirectory.Create(FDirectories,DirName);
  317. Result:=CachedDir;
  318. end;
  319. function TDirectoryCache.FileExists(const AName:TCmdStr):boolean;
  320. var
  321. CachedDir : TCachedDirectory;
  322. begin
  323. Result:=false;
  324. CachedDir:=GetDirectory(ExtractFilePath(AName));
  325. if assigned(CachedDir) then
  326. Result:=CachedDir.FileExists(ExtractFileName(AName));
  327. end;
  328. function TDirectoryCache.FileExistsCaseAware(const path, fn: TCmdStr; out FoundName: TCmdStr):boolean;
  329. var
  330. CachedDir : TCachedDirectory;
  331. begin
  332. Result:=false;
  333. CachedDir:=GetDirectory(ExtractFilePath(path+fn));
  334. if assigned(CachedDir) then
  335. Result:=CachedDir.FileExistsCaseAware(path,fn,FoundName);
  336. end;
  337. function TDirectoryCache.DirectoryExists(const AName:TCmdStr):boolean;
  338. var
  339. CachedDir : TCachedDirectory;
  340. begin
  341. Result:=false;
  342. CachedDir:=GetDirectory(ExtractFilePath(AName));
  343. if assigned(CachedDir) then
  344. Result:=CachedDir.DirectoryExists(ExtractFileName(AName));
  345. end;
  346. function TDirectoryCache.FindFirst(const APattern:TCmdStr;var Res:TCachedSearchRec):boolean;
  347. begin
  348. Res.Pattern:=ExtractFileName(APattern);
  349. Res.CachedDir:=GetDirectory(ExtractFilePath(APattern));
  350. Res.CachedDir.ForceUseCache;
  351. Res.EntryIndex:=0;
  352. if assigned(Res.CachedDir) then
  353. Result:=FindNext(Res)
  354. else
  355. Result:=false;
  356. end;
  357. function TDirectoryCache.FindNext(var Res:TCachedSearchRec):boolean;
  358. var
  359. entry: PCachedDirectoryEntry;
  360. begin
  361. if Res.EntryIndex<Res.CachedDir.DirectoryEntries.Count then
  362. begin
  363. if (tf_files_case_aware in source_info.flags) then
  364. begin
  365. entry:=Res.CachedDir.DirectoryEntries[Res.EntryIndex];
  366. Res.Name:=entry^.RealName;
  367. Res.Attr:=entry^.Attr;
  368. end
  369. else
  370. begin
  371. Res.Name:=Res.CachedDir.DirectoryEntries.NameOfIndex(Res.EntryIndex);
  372. Res.Attr:=PtrUInt(Res.CachedDir.DirectoryEntries[Res.EntryIndex]);
  373. end;
  374. inc(Res.EntryIndex);
  375. Result:=true;
  376. end
  377. else
  378. Result:=false;
  379. end;
  380. function TDirectoryCache.FindClose(var Res:TCachedSearchRec):boolean;
  381. begin
  382. { nothing todo }
  383. result:=true;
  384. end;
  385. {****************************************************************************
  386. Utils
  387. ****************************************************************************}
  388. function bstoslash(const s : TCmdStr) : TCmdStr;
  389. {
  390. return TCmdStr s with all \ changed into /
  391. }
  392. var
  393. i : longint;
  394. begin
  395. setlength(bstoslash,length(s));
  396. for i:=1to length(s) do
  397. if s[i]='\' then
  398. bstoslash[i]:='/'
  399. else
  400. bstoslash[i]:=s[i];
  401. end;
  402. {Gives the absolute path to the current directory}
  403. var
  404. CachedCurrentDir : TCmdStr;
  405. function GetCurrentDir:TCmdStr;
  406. begin
  407. if CachedCurrentDir='' then
  408. begin
  409. GetDir(0,CachedCurrentDir);
  410. CachedCurrentDir:=FixPath(CachedCurrentDir,false);
  411. end;
  412. result:=CachedCurrentDir;
  413. end;
  414. {Gives the relative path to the current directory,
  415. with a trailing dir separator. E. g. on unix ./ }
  416. function CurDirRelPath(systeminfo: tsysteminfo): TCmdStr;
  417. begin
  418. if systeminfo.system <> system_powerpc_macos then
  419. CurDirRelPath:= '.'+systeminfo.DirSep
  420. else
  421. CurDirRelPath:= ':'
  422. end;
  423. function path_absolute(const s : TCmdStr) : boolean;
  424. {
  425. is path s an absolute path?
  426. }
  427. begin
  428. result:=false;
  429. {$if defined(unix)}
  430. if (length(s)>0) and (s[1]='/') then
  431. result:=true;
  432. {$elseif defined(amiga) or defined(morphos)}
  433. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  434. result:=true;
  435. {$elseif defined(macos)}
  436. if IsMacFullPath(s) then
  437. result:=true;
  438. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  439. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  440. result:=true;
  441. {$endif unix}
  442. end;
  443. Function FileExists ( Const F : TCmdStr;allowcache:boolean) : Boolean;
  444. begin
  445. {$ifdef usedircache}
  446. if allowcache then
  447. Result:=DirCache.FileExists(F)
  448. else
  449. {$endif usedircache}
  450. Result:=SysUtils.FileExists(F);
  451. if assigned(do_comment) then
  452. begin
  453. if Result then
  454. do_comment(V_Tried,'Searching file '+F+'... found')
  455. else
  456. do_comment(V_Tried,'Searching file '+F+'... not found');
  457. end;
  458. end;
  459. function FileExistsNonCase(const path,fn:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  460. var
  461. fn2 : TCmdStr;
  462. begin
  463. result:=false;
  464. if tf_files_case_sensitive in source_info.flags then
  465. begin
  466. {
  467. Search order for case sensitive systems:
  468. 1. NormalCase
  469. 2. lowercase
  470. 3. UPPERCASE
  471. }
  472. FoundFile:=path+fn;
  473. If FileExists(FoundFile,allowcache) then
  474. begin
  475. result:=true;
  476. exit;
  477. end;
  478. fn2:=Lower(fn);
  479. if fn2<>fn then
  480. begin
  481. FoundFile:=path+fn2;
  482. If FileExists(FoundFile,allowcache) then
  483. begin
  484. result:=true;
  485. exit;
  486. end;
  487. end;
  488. fn2:=Upper(fn);
  489. if fn2<>fn then
  490. begin
  491. FoundFile:=path+fn2;
  492. If FileExists(FoundFile,allowcache) then
  493. begin
  494. result:=true;
  495. exit;
  496. end;
  497. end;
  498. end
  499. else
  500. if tf_files_case_aware in source_info.flags then
  501. begin
  502. {
  503. Search order for case aware systems:
  504. 1. NormalCase
  505. }
  506. {$ifdef usedircache}
  507. if allowcache then
  508. begin
  509. result:=DirCache.FileExistsCaseAware(path,fn,fn2);
  510. if result then
  511. begin
  512. FoundFile:=fn2;
  513. exit;
  514. end;
  515. end
  516. else
  517. {$endif usedircache}
  518. begin
  519. FoundFile:=path+fn;
  520. If FileExists(FoundFile,allowcache) then
  521. begin
  522. { don't know the real name in this case }
  523. result:=true;
  524. exit;
  525. end;
  526. end;
  527. end
  528. else
  529. begin
  530. { None case sensitive only lowercase }
  531. FoundFile:=path+Lower(fn);
  532. If FileExists(FoundFile,allowcache) then
  533. begin
  534. result:=true;
  535. exit;
  536. end;
  537. end;
  538. { Set foundfile to something usefull }
  539. FoundFile:=fn;
  540. end;
  541. Function PathExists (const F : TCmdStr;allowcache:boolean) : Boolean;
  542. Var
  543. i: longint;
  544. hs : TCmdStr;
  545. begin
  546. if F = '' then
  547. begin
  548. result := true;
  549. exit;
  550. end;
  551. hs := ExpandFileName(F);
  552. I := Pos (DriveSeparator, hs);
  553. if (hs [Length (hs)] = DirectorySeparator) and
  554. (((I = 0) and (Length (hs) > 1)) or (I <> Length (hs) - 1)) then
  555. Delete (hs, Length (hs), 1);
  556. {$ifdef usedircache}
  557. if allowcache then
  558. Result:=DirCache.DirectoryExists(hs)
  559. else
  560. {$endif usedircache}
  561. Result:=SysUtils.DirectoryExists(hs);
  562. end;
  563. Function RemoveDir(d:TCmdStr):boolean;
  564. begin
  565. if d[length(d)]=source_info.DirSep then
  566. Delete(d,length(d),1);
  567. {$I-}
  568. rmdir(d);
  569. {$I+}
  570. RemoveDir:=(ioresult=0);
  571. end;
  572. Function FixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  573. var
  574. i : longint;
  575. begin
  576. { Fix separator }
  577. for i:=1 to length(s) do
  578. if s[i] in ['/','\'] then
  579. s[i]:=source_info.DirSep;
  580. { Fix ending / }
  581. if (length(s)>0) and (s[length(s)]<>source_info.DirSep) and
  582. (s[length(s)]<>DriveSeparator) then
  583. s:=s+source_info.DirSep;
  584. { Remove ./ }
  585. if (not allowdot) and (s='.'+source_info.DirSep) then
  586. s:='';
  587. { return }
  588. if (tf_files_case_aware in source_info.flags) or
  589. (tf_files_case_sensitive in source_info.flags) then
  590. FixPath:=s
  591. else
  592. FixPath:=Lower(s);
  593. end;
  594. {Actually the version in macutils.pp could be used,
  595. but that would not work for crosscompiling, so this is a slightly modified
  596. version of it.}
  597. function TranslatePathToMac (const path: TCmdStr; mpw: Boolean): TCmdStr;
  598. function GetVolumeIdentifier: TCmdStr;
  599. begin
  600. GetVolumeIdentifier := '{Boot}'
  601. (*
  602. if mpw then
  603. GetVolumeIdentifier := '{Boot}'
  604. else
  605. GetVolumeIdentifier := macosBootVolumeName;
  606. *)
  607. end;
  608. var
  609. slashPos, oldpos, newpos, oldlen, maxpos: Longint;
  610. begin
  611. oldpos := 1;
  612. slashPos := Pos('/', path);
  613. if (slashPos <> 0) then {its a unix path}
  614. begin
  615. if slashPos = 1 then
  616. begin {its a full path}
  617. oldpos := 2;
  618. TranslatePathToMac := GetVolumeIdentifier;
  619. end
  620. else {its a partial path}
  621. TranslatePathToMac := ':';
  622. end
  623. else
  624. begin
  625. slashPos := Pos('\', path);
  626. if (slashPos <> 0) then {its a dos path}
  627. begin
  628. if slashPos = 1 then
  629. begin {its a full path, without drive letter}
  630. oldpos := 2;
  631. TranslatePathToMac := GetVolumeIdentifier;
  632. end
  633. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  634. begin
  635. oldpos := 4;
  636. TranslatePathToMac := GetVolumeIdentifier;
  637. end
  638. else {its a partial path}
  639. TranslatePathToMac := ':';
  640. end;
  641. end;
  642. if (slashPos <> 0) then {its a unix or dos path}
  643. begin
  644. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" }
  645. newpos := Length(TranslatePathToMac);
  646. oldlen := Length(path);
  647. SetLength(TranslatePathToMac, newpos + oldlen); {It will be no longer than what is already}
  648. {prepended plus length of path.}
  649. maxpos := Length(TranslatePathToMac); {Get real maxpos, can be short if String is ShortString}
  650. {There is never a slash in the beginning, because either it was an absolute path, and then the}
  651. {drive and slash was removed, or it was a relative path without a preceding slash.}
  652. while oldpos <= oldlen do
  653. begin
  654. {Check if special dirs, ./ or ../ }
  655. if path[oldPos] = '.' then
  656. if (oldpos + 1 <= oldlen) and (path[oldPos + 1] = '.') then
  657. begin
  658. if (oldpos + 2 > oldlen) or (path[oldPos + 2] in ['/', '\']) then
  659. begin
  660. {It is "../" or ".." translates to ":" }
  661. if newPos = maxPos then
  662. begin {Shouldn't actually happen, but..}
  663. Exit('');
  664. end;
  665. newPos := newPos + 1;
  666. TranslatePathToMac[newPos] := ':';
  667. oldPos := oldPos + 3;
  668. continue; {Start over again}
  669. end;
  670. end
  671. else if (oldpos + 1 > oldlen) or (path[oldPos + 1] in ['/', '\']) then
  672. begin
  673. {It is "./" or "." ignor it }
  674. oldPos := oldPos + 2;
  675. continue; {Start over again}
  676. end;
  677. {Collect file or dir name}
  678. while (oldpos <= oldlen) and not (path[oldPos] in ['/', '\']) do
  679. begin
  680. if newPos = maxPos then
  681. begin {Shouldn't actually happen, but..}
  682. Exit('');
  683. end;
  684. newPos := newPos + 1;
  685. TranslatePathToMac[newPos] := path[oldPos];
  686. oldPos := oldPos + 1;
  687. end;
  688. {When we come here there is either a slash or we are at the end.}
  689. if (oldpos <= oldlen) then
  690. begin
  691. if newPos = maxPos then
  692. begin {Shouldn't actually happen, but..}
  693. Exit('');
  694. end;
  695. newPos := newPos + 1;
  696. TranslatePathToMac[newPos] := ':';
  697. oldPos := oldPos + 1;
  698. end;
  699. end;
  700. SetLength(TranslatePathToMac, newpos);
  701. end
  702. else if (path = '.') then
  703. TranslatePathToMac := ':'
  704. else if (path = '..') then
  705. TranslatePathToMac := '::'
  706. else
  707. TranslatePathToMac := path; {its a mac path}
  708. end;
  709. function FixFileName(const s:TCmdStr):TCmdStr;
  710. var
  711. i : longint;
  712. begin
  713. if source_info.system = system_powerpc_MACOS then
  714. FixFileName:= TranslatePathToMac(s, true)
  715. else
  716. if (tf_files_case_aware in source_info.flags) or
  717. (tf_files_case_sensitive in source_info.flags) then
  718. begin
  719. setlength(FixFileName,length(s));
  720. for i:=1 to length(s) do
  721. begin
  722. case s[i] of
  723. '/','\' :
  724. FixFileName[i]:=source_info.dirsep;
  725. else
  726. FixFileName[i]:=s[i];
  727. end;
  728. end;
  729. end
  730. else
  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. 'A'..'Z' :
  739. FixFileName[i]:=char(byte(s[i])+32);
  740. else
  741. FixFileName[i]:=s[i];
  742. end;
  743. end;
  744. end;
  745. end;
  746. Function TargetFixPath(s:TCmdStr;allowdot:boolean):TCmdStr;
  747. var
  748. i : longint;
  749. begin
  750. { Fix separator }
  751. for i:=1 to length(s) do
  752. if s[i] in ['/','\'] then
  753. s[i]:=target_info.DirSep;
  754. { Fix ending / }
  755. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  756. (s[length(s)]<>':') then
  757. s:=s+target_info.DirSep;
  758. { Remove ./ }
  759. if (not allowdot) and (s='.'+target_info.DirSep) then
  760. s:='';
  761. { return }
  762. if (tf_files_case_aware in target_info.flags) or
  763. (tf_files_case_sensitive in target_info.flags) then
  764. TargetFixPath:=s
  765. else
  766. TargetFixPath:=Lower(s);
  767. end;
  768. function TargetFixFileName(const s:TCmdStr):TCmdStr;
  769. var
  770. i : longint;
  771. begin
  772. if target_info.system = system_powerpc_MACOS then
  773. TargetFixFileName:= TranslatePathToMac(s, true)
  774. else
  775. if (tf_files_case_aware in target_info.flags) or
  776. (tf_files_case_sensitive in target_info.flags) then
  777. begin
  778. setlength(TargetFixFileName,length(s));
  779. for i:=1 to length(s) do
  780. begin
  781. case s[i] of
  782. '/','\' :
  783. TargetFixFileName[i]:=target_info.dirsep;
  784. else
  785. TargetFixFileName[i]:=s[i];
  786. end;
  787. end;
  788. end
  789. else
  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. 'A'..'Z' :
  798. TargetFixFileName[i]:=char(byte(s[i])+32);
  799. else
  800. TargetFixFileName[i]:=s[i];
  801. end;
  802. end;
  803. end;
  804. end;
  805. procedure SplitBinCmd(const s:TCmdStr;var bstr:TCmdStr;var cstr:TCmdStr);
  806. var
  807. i : longint;
  808. begin
  809. i:=pos(' ',s);
  810. if i>0 then
  811. begin
  812. bstr:=Copy(s,1,i-1);
  813. cstr:=Copy(s,i+1,length(s)-i);
  814. end
  815. else
  816. begin
  817. bstr:=s;
  818. cstr:='';
  819. end;
  820. end;
  821. procedure TSearchPathList.AddPath(s:TCmdStr;addfirst:boolean);
  822. begin
  823. AddPath('',s,AddFirst);
  824. end;
  825. procedure TSearchPathList.AddPath(SrcPath,s:TCmdStr;addfirst:boolean);
  826. var
  827. staridx,
  828. j : longint;
  829. prefix,
  830. suffix,
  831. CurrentDir,
  832. currPath : TCmdStr;
  833. subdirfound : boolean;
  834. {$ifdef usedircache}
  835. dir : TCachedSearchRec;
  836. {$else usedircache}
  837. dir : TSearchRec;
  838. {$endif usedircache}
  839. hp : TCmdStrListItem;
  840. procedure WarnNonExistingPath(const path : TCmdStr);
  841. begin
  842. if assigned(do_comment) then
  843. do_comment(V_Tried,'Path "'+path+'" not found');
  844. end;
  845. procedure AddCurrPath;
  846. begin
  847. if addfirst then
  848. begin
  849. Remove(currPath);
  850. Insert(currPath);
  851. end
  852. else
  853. begin
  854. { Check if already in path, then we don't add it }
  855. hp:=Find(currPath);
  856. if not assigned(hp) then
  857. Concat(currPath);
  858. end;
  859. end;
  860. begin
  861. if s='' then
  862. exit;
  863. { Support default macro's }
  864. DefaultReplacements(s);
  865. { get current dir }
  866. CurrentDir:=GetCurrentDir;
  867. repeat
  868. { get currpath }
  869. if addfirst then
  870. begin
  871. j:=length(s);
  872. while (j>0) and (s[j]<>';') do
  873. dec(j);
  874. currPath:= TrimSpace(Copy(s,j+1,length(s)-j));
  875. if j=0 then
  876. s:=''
  877. else
  878. System.Delete(s,j,length(s)-j+1);
  879. end
  880. else
  881. begin
  882. j:=Pos(';',s);
  883. if j=0 then
  884. j:=255;
  885. currPath:= TrimSpace(Copy(s,1,j-1));
  886. System.Delete(s,1,j);
  887. end;
  888. { fix pathname }
  889. DePascalQuote(currPath);
  890. currPath:=SrcPath+FixPath(currPath,false);
  891. if currPath='' then
  892. currPath:= CurDirRelPath(source_info)
  893. else
  894. begin
  895. currPath:=FixPath(ExpandFileName(currpath),false);
  896. if (CurrentDir<>'') and (Copy(currPath,1,length(CurrentDir))=CurrentDir) then
  897. begin
  898. {$if defined(amiga) and defined(morphos)}
  899. currPath:= CurrentDir+Copy(currPath,length(CurrentDir)+1,255);
  900. {$else}
  901. currPath:= CurDirRelPath(source_info)+Copy(currPath,length(CurrentDir)+1,255);
  902. {$endif}
  903. end;
  904. end;
  905. { wildcard adding ? }
  906. staridx:=pos('*',currpath);
  907. if staridx>0 then
  908. begin
  909. prefix:=ExtractFilePath(Copy(currpath,1,staridx));
  910. suffix:=Copy(currpath,staridx+1,length(currpath));
  911. subdirfound:=false;
  912. {$ifdef usedircache}
  913. if DirCache.FindFirst(Prefix+AllFilesMask,dir) then
  914. begin
  915. repeat
  916. if (dir.attr and faDirectory)<>0 then
  917. begin
  918. subdirfound:=true;
  919. currpath:=prefix+dir.name+suffix;
  920. if (suffix='') or PathExists(currpath,true) then
  921. begin
  922. hp:=Find(currPath);
  923. if not assigned(hp) then
  924. AddCurrPath;
  925. end;
  926. end;
  927. until not DirCache.FindNext(dir);
  928. end;
  929. DirCache.FindClose(dir);
  930. {$else usedircache}
  931. if findfirst(prefix+AllFilesMask,faDirectory,dir) = 0 then
  932. begin
  933. repeat
  934. if (dir.name<>'.') and
  935. (dir.name<>'..') and
  936. ((dir.attr and faDirectory)<>0) then
  937. begin
  938. subdirfound:=true;
  939. currpath:=prefix+dir.name+suffix;
  940. if (suffix='') or PathExists(currpath,false) then
  941. begin
  942. hp:=Find(currPath);
  943. if not assigned(hp) then
  944. AddCurrPath;
  945. end;
  946. end;
  947. until findnext(dir) <> 0;
  948. end;
  949. FindClose(dir);
  950. {$endif usedircache}
  951. if not subdirfound then
  952. WarnNonExistingPath(currpath);
  953. end
  954. else
  955. begin
  956. if PathExists(currpath,true) then
  957. AddCurrPath
  958. else
  959. WarnNonExistingPath(currpath);
  960. end;
  961. until (s='');
  962. end;
  963. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  964. var
  965. s : TCmdStr;
  966. hl : TSearchPathList;
  967. hp,hp2 : TCmdStrListItem;
  968. begin
  969. if list.empty then
  970. exit;
  971. { create temp and reverse the list }
  972. if addfirst then
  973. begin
  974. hl:=TSearchPathList.Create;
  975. hp:=TCmdStrListItem(list.first);
  976. while assigned(hp) do
  977. begin
  978. hl.insert(hp.Str);
  979. hp:=TCmdStrListItem(hp.next);
  980. end;
  981. while not hl.empty do
  982. begin
  983. s:=hl.GetFirst;
  984. Remove(s);
  985. Insert(s);
  986. end;
  987. hl.Free;
  988. end
  989. else
  990. begin
  991. hp:=TCmdStrListItem(list.first);
  992. while assigned(hp) do
  993. begin
  994. hp2:=Find(hp.Str);
  995. { Check if already in path, then we don't add it }
  996. if not assigned(hp2) then
  997. Concat(hp.Str);
  998. hp:=TCmdStrListItem(hp.next);
  999. end;
  1000. end;
  1001. end;
  1002. function TSearchPathList.FindFile(const f :TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1003. Var
  1004. p : TCmdStrListItem;
  1005. begin
  1006. FindFile:=false;
  1007. p:=TCmdStrListItem(first);
  1008. while assigned(p) do
  1009. begin
  1010. result:=FileExistsNonCase(p.Str,f,allowcache,FoundFile);
  1011. if result then
  1012. exit;
  1013. p:=TCmdStrListItem(p.next);
  1014. end;
  1015. { Return original filename if not found }
  1016. FoundFile:=f;
  1017. end;
  1018. function FindFile(const f : TCmdStr;path : TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1019. Var
  1020. singlepathstring : TCmdStr;
  1021. i : longint;
  1022. begin
  1023. if PathSeparator <> ';' then
  1024. for i:=1 to length(path) do
  1025. if path[i]=PathSeparator then
  1026. path[i]:=';';
  1027. FindFile:=false;
  1028. repeat
  1029. i:=pos(';',path);
  1030. if i=0 then
  1031. i:=Succ (Length (Path));
  1032. singlepathstring:=FixPath(copy(path,1,i-1),false);
  1033. delete(path,1,i);
  1034. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1035. if result then
  1036. exit;
  1037. until path='';
  1038. FoundFile:=f;
  1039. end;
  1040. {
  1041. function FindFilePchar(const f : TCmdStr;path : pchar;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1042. Var
  1043. singlepathstring : TCmdStr;
  1044. startpc,pc : pchar;
  1045. begin
  1046. FindFilePchar:=false;
  1047. if Assigned (Path) then
  1048. begin
  1049. pc:=path;
  1050. repeat
  1051. startpc:=pc;
  1052. while (pc^<>PathSeparator) and (pc^<>';') and (pc^<>#0) do
  1053. inc(pc);
  1054. SetLength(singlepathstring, pc-startpc);
  1055. move(startpc^,singlepathstring[1],pc-startpc);
  1056. singlepathstring:=FixPath(ExpandFileName(singlepathstring),false);
  1057. result:=FileExistsNonCase(singlepathstring,f,allowcache,FoundFile);
  1058. if result then
  1059. exit;
  1060. if (pc^=#0) then
  1061. break;
  1062. inc(pc);
  1063. until false;
  1064. end;
  1065. foundfile:=f;
  1066. end;
  1067. }
  1068. function FindExe(const bin:TCmdStr;allowcache:boolean;var foundfile:TCmdStr):boolean;
  1069. var
  1070. Path : TCmdStr;
  1071. found : boolean;
  1072. begin
  1073. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),'.;'+exepath,allowcache,foundfile);
  1074. if not found then
  1075. begin
  1076. {$ifdef macos}
  1077. Path:=GetEnvironmentVariable('Commands');
  1078. {$else}
  1079. Path:=GetEnvironmentVariable('PATH');
  1080. {$endif}
  1081. found:=FindFile(FixFileName(ChangeFileExt(bin,source_info.exeext)),Path,allowcache,foundfile);
  1082. end;
  1083. FindExe:=found;
  1084. end;
  1085. function GetShortName(const n:TCmdStr):TCmdStr;
  1086. {$ifdef win32}
  1087. var
  1088. hs,hs2 : TCmdStr;
  1089. i : longint;
  1090. {$endif}
  1091. {$if defined(go32v2) or defined(watcom)}
  1092. var
  1093. hs : shortstring;
  1094. {$endif}
  1095. begin
  1096. GetShortName:=n;
  1097. {$ifdef win32}
  1098. hs:=n+#0;
  1099. { may become longer in case of e.g. ".a" -> "a~1" or so }
  1100. setlength(hs2,length(hs)*2);
  1101. i:=Windows.GetShortPathName(@hs[1],@hs2[1],length(hs)*2);
  1102. if (i>0) and (i<=length(hs)*2) then
  1103. begin
  1104. setlength(hs2,strlen(@hs2[1]));
  1105. GetShortName:=hs2;
  1106. end;
  1107. {$endif}
  1108. {$if defined(go32v2) or defined(watcom)}
  1109. hs:=n;
  1110. if Dos.GetShortName(hs) then
  1111. GetShortName:=hs;
  1112. {$endif}
  1113. end;
  1114. {****************************************************************************
  1115. Init / Done
  1116. ****************************************************************************}
  1117. procedure InitFileUtils;
  1118. begin
  1119. DirCache:=TDirectoryCache.Create;
  1120. end;
  1121. procedure DoneFileUtils;
  1122. begin
  1123. DirCache.Free;
  1124. end;
  1125. end.