cfileutl.pas 35 KB

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