cfileutils.pas 36 KB

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