fina.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  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. System Utilities For Free Pascal
  17. }
  18. function ChangeFileExt(const FileName, Extension: string): string;
  19. var
  20. i : longint;
  21. EndSep : Set of Char;
  22. begin
  23. i := Length(FileName);
  24. EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];
  25. while (I > 0) and not(FileName[I] in EndSep) do
  26. Dec(I);
  27. if (I = 0) or (FileName[I] <> ExtensionSeparator) then
  28. I := Length(FileName)+1;
  29. Result := Copy(FileName, 1, I - 1) + Extension;
  30. end;
  31. function ExtractFilePath(const FileName: string): string;
  32. var
  33. i : longint;
  34. EndSep : Set of Char;
  35. begin
  36. i := Length(FileName);
  37. EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
  38. while (i > 0) and not (FileName[i] in EndSep) do
  39. Dec(i);
  40. If I>0 then
  41. Result := Copy(FileName, 1, i)
  42. else
  43. Result:='';
  44. end;
  45. function ExtractFileDir(const FileName: string): string;
  46. var
  47. i : longint;
  48. EndSep : Set of Char;
  49. begin
  50. I := Length(FileName);
  51. EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
  52. while (I > 0) and not (FileName[I] in EndSep) do
  53. Dec(I);
  54. if (I > 1) and (FileName[I] in AllowDirectorySeparators) and
  55. not (FileName[I - 1] in EndSep) then
  56. Dec(I);
  57. Result := Copy(FileName, 1, I);
  58. end;
  59. function ExtractFileDrive(const FileName: string): string;
  60. var
  61. i,l: longint;
  62. begin
  63. Result := '';
  64. l:=Length(FileName);
  65. if (l<2) then
  66. exit;
  67. If (FileName[2] in AllowDriveSeparators) then
  68. result:=Copy(FileName,1,2)
  69. else if (FileName[1] in AllowDirectorySeparators) and
  70. (FileName[2] in AllowDirectorySeparators) then
  71. begin
  72. i := 2;
  73. { skip share }
  74. While (i<l) and Not (Filename[i+1] in AllowDirectorySeparators) do
  75. inc(i);
  76. inc(i);
  77. While (i<l) and Not (Filename[i+1] in AllowDirectorySeparators) do
  78. inc(i);
  79. Result:=Copy(FileName,1,i);
  80. end;
  81. end;
  82. function ExtractFileName(const FileName: string): string;
  83. var
  84. i : longint;
  85. EndSep : Set of Char;
  86. begin
  87. I := Length(FileName);
  88. EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
  89. while (I > 0) and not (FileName[I] in EndSep) do
  90. Dec(I);
  91. Result := Copy(FileName, I + 1, MaxInt);
  92. end;
  93. function ExtractFileExt(const FileName: string): string;
  94. var
  95. i : longint;
  96. EndSep : Set of Char;
  97. begin
  98. I := Length(FileName);
  99. EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];
  100. while (I > 0) and not (FileName[I] in EndSep) do
  101. Dec(I);
  102. if (I > 0) and (FileName[I] = ExtensionSeparator) then
  103. Result := Copy(FileName, I, MaxInt)
  104. else
  105. Result := '';
  106. end;
  107. function ExtractShortPathName(Const FileName : String) : String;
  108. begin
  109. {$ifdef MSWINDOWS}
  110. SetLength(Result,Max_Path);
  111. SetLength(Result,GetShortPathName(PChar(FileName), Pchar(Result),Length(Result)));
  112. {$else}
  113. Result:=FileName;
  114. {$endif}
  115. end;
  116. type
  117. PathStr=string;
  118. {$DEFINE FPC_FEXPAND_SYSUTILS}
  119. {$I fexpand.inc}
  120. function ExpandFileName (Const FileName : string): String;
  121. Var S : String;
  122. Begin
  123. S:=FileName;
  124. DoDirSeparators(S);
  125. Result:=Fexpand(S);
  126. end;
  127. {$ifndef HASEXPANDUNCFILENAME}
  128. function ExpandUNCFileName (Const FileName : string): String;
  129. begin
  130. Result:=ExpandFileName (FileName);
  131. //!! Here should follow code to replace the drive: part with UNC...
  132. end;
  133. {$endif HASEXPANDUNCFILENAME}
  134. function ExpandFileNameCase (const FileName: string; out MatchFound: TFilenameCaseMatch): string;
  135. var
  136. SR: TSearchRec;
  137. ItemsFound: byte;
  138. FoundPath: string;
  139. RestPos: SizeUInt;
  140. Root: string;
  141. procedure TryCase (const Base, Rest: string);
  142. var
  143. SR: TSearchRec;
  144. RC: longint;
  145. NextDirPos: SizeUInt;
  146. NextPart: string;
  147. NextRest: string;
  148. SearchBase: string;
  149. begin
  150. NextDirPos := 1;
  151. while (NextDirPos <= Length (Rest)) and
  152. not (Rest [NextDirPos] in (AllowDirectorySeparators)) do
  153. Inc (NextDirPos);
  154. NextPart := Copy (Rest, 1, Pred (NextDirPos));
  155. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  156. if (Length (Rest) >= NextDirPos) and
  157. (Rest [NextDirPos] in AllowDirectorySeparators) then
  158. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  159. while (Length (Rest) >= NextDirPos) and
  160. (Rest [NextDirPos] in AllowDirectorySeparators) do
  161. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  162. Inc (NextDirPos);
  163. NextRest := Copy (Rest, NextDirPos, Length (Rest) - Pred (NextDirPos));
  164. if (Base = '') or (Base [Length (Base)] in AllowDirectorySeparators) then
  165. SearchBase := Base
  166. else
  167. SearchBase := Base + DirectorySeparator;
  168. RC := FindFirst (SearchBase + AllFilesMask, faAnyFile, SR);
  169. while (RC = 0) and (ItemsFound < 2) do
  170. begin
  171. if UpCase (NextPart) = UpCase (SR.Name) then
  172. begin
  173. if Length (NextPart) = Length (Rest) then
  174. begin
  175. Inc (ItemsFound);
  176. if ItemsFound = 1 then
  177. FoundPath := SearchBase + SR.Name;
  178. end
  179. else if SR.Attr and faDirectory = faDirectory then
  180. TryCase (SearchBase + SR.Name + DirectorySeparator, NextRest);
  181. end;
  182. if ItemsFound < 2 then
  183. RC := FindNext (SR);
  184. end;
  185. FindClose (SR);
  186. end;
  187. begin
  188. Result := ExpandFileName (FileName);
  189. if FileName = '' then
  190. MatchFound := mkExactMatch
  191. else
  192. if (FindFirst (FileName, faAnyFile, SR) = 0) or
  193. (* Special check for a root directory or a directory with a trailing slash *)
  194. (* which are not found using FindFirst. *)
  195. DirectoryExists (FileName) then
  196. begin
  197. MatchFound := mkExactMatch;
  198. Result := ExtractFilePath (Result) + SR.Name;
  199. FindClose (SR);
  200. end
  201. else
  202. begin
  203. (* Better close the search handle here before starting the recursive search *)
  204. FindClose (SR);
  205. MatchFound := mkNone;
  206. if FileNameCaseSensitive then
  207. begin
  208. ItemsFound := 0;
  209. FoundPath := '';
  210. RestPos := Length (ExtractFileDrive (FileName)) + 1;
  211. if (Length (FileName) > RestPos) then
  212. begin
  213. {$IFDEF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  214. if (Length (FileName) >= RestPos) and
  215. (FileName [RestPos] in AllowDirectorySeparators) then
  216. {$ELSE FPC_FEXPAND_DIRSEP_IS_UPDIR}
  217. while (Length (FileName) >= RestPos) and
  218. (FileName [RestPos] in AllowDirectorySeparators) do
  219. {$ENDIF FPC_FEXPAND_DIRSEP_IS_UPDIR}
  220. Inc (RestPos);
  221. Root := Copy (FileName, 1, Pred (RestPos));
  222. TryCase (Root, Copy (FileName, RestPos, Length (FileName) - Length (Root)));
  223. if ItemsFound > 0 then
  224. begin
  225. Result := ExpandFileName (FoundPath);
  226. if ItemsFound = 1 then
  227. MatchFound := mkSingleMatch
  228. else
  229. MatchFound := mkAmbiguous;
  230. end;
  231. end;
  232. end;
  233. end;
  234. end;
  235. Const
  236. MaxDirs = 129;
  237. function ExtractRelativepath (Const BaseName,DestName : String): String;
  238. Var Source, Dest : String;
  239. Sc,Dc,I,J : Longint;
  240. SD,DD : Array[1..MaxDirs] of PChar;
  241. Const OneLevelBack = '..'+DirectorySeparator;
  242. begin
  243. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  244. begin
  245. Result:=DestName;
  246. exit;
  247. end;
  248. Source:=ExcludeTrailingPathDelimiter(ExtractFilePath(BaseName));
  249. Dest:=ExcludeTrailingPathDelimiter(ExtractFilePath(DestName));
  250. SC:=GetDirs (Source,SD);
  251. DC:=GetDirs (Dest,DD);
  252. I:=1;
  253. While (I<=DC) and (I<=SC) do
  254. begin
  255. If StrIcomp(DD[i],SD[i])=0 then
  256. Inc(i)
  257. else
  258. Break;
  259. end;
  260. Result:='';
  261. For J:=I to SC do Result:=Result+OneLevelBack;
  262. For J:=I to DC do Result:=Result+DD[J]+DirectorySeparator;
  263. Result:=Result+ExtractFileName(DestNAme);
  264. end;
  265. Procedure DoDirSeparators (Var FileName : String);
  266. VAr I : longint;
  267. begin
  268. For I:=1 to Length(FileName) do
  269. If FileName[I] in AllowDirectorySeparators then
  270. FileName[i]:=DirectorySeparator;
  271. end;
  272. Function SetDirSeparators (Const FileName : string) : String;
  273. begin
  274. Result:=FileName;
  275. DoDirSeparators (Result);
  276. end;
  277. {
  278. DirName is split in a #0 separated list of directory names,
  279. Dirs is an array of pchars, pointing to these directory names.
  280. The function returns the number of directories found, or -1
  281. if none were found.
  282. }
  283. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  284. Var I : Longint;
  285. begin
  286. I:=1;
  287. Result:=-1;
  288. While I<=Length(DirName) do
  289. begin
  290. If (DirName[i] in AllowDirectorySeparators) and
  291. { avoid error in case last char=pathdelim }
  292. (length(dirname)>i) then
  293. begin
  294. DirName[i]:=#0;
  295. Inc(Result);
  296. Dirs[Result]:=@DirName[I+1];
  297. end;
  298. Inc(I);
  299. end;
  300. If Result>-1 then inc(Result);
  301. end;
  302. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  303. Var
  304. l : Integer;
  305. begin
  306. Result:=Path;
  307. l:=Length(Result);
  308. If (L=0) or not(Result[l] in AllowDirectorySeparators) then
  309. Result:=Result+DirectorySeparator;
  310. end;
  311. function IncludeTrailingBackslash(Const Path : String) : String;
  312. begin
  313. Result:=IncludeTrailingPathDelimiter(Path);
  314. end;
  315. function ExcludeTrailingBackslash(Const Path: string): string;
  316. begin
  317. Result:=ExcludeTrailingPathDelimiter(Path);
  318. end;
  319. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  320. Var
  321. L : Integer;
  322. begin
  323. L:=Length(Path);
  324. If (L>0) and (Path[L] in AllowDirectorySeparators) then
  325. Dec(L);
  326. Result:=Copy(Path,1,L);
  327. end;
  328. function IncludeLeadingPathDelimiter(Const Path : String) : String;
  329. Var
  330. l : Integer;
  331. begin
  332. Result:=Path;
  333. l:=Length(Result);
  334. If (L=0) or not(Result[1] in AllowDirectorySeparators) then
  335. Result:=DirectorySeparator+Result;
  336. end;
  337. function ExcludeLeadingPathDelimiter(Const Path: string): string;
  338. Var
  339. L : Integer;
  340. begin
  341. Result:=Path;
  342. L:=Length(Result);
  343. If (L>0) and (Result[1] in AllowDirectorySeparators) then
  344. Delete(Result,1,1);
  345. end;
  346. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  347. begin
  348. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index] in AllowDirectorySeparators);
  349. end;
  350. function ConcatPaths(const Paths: array of String): String;
  351. var
  352. I: Integer;
  353. begin
  354. if Length(Paths) > 0 then
  355. begin
  356. Result := Paths[0];
  357. for I := 1 to Length(Paths) - 1 do
  358. Result := IncludeTrailingPathDelimiter(Result) + ExcludeLeadingPathDelimiter(Paths[I]);
  359. end else
  360. Result := '';
  361. end;
  362. Function GetFileHandle(var f : File):THandle;
  363. begin
  364. result:=filerec(f).handle;
  365. end;
  366. Function GetFileHandle(var f : Text):THandle;
  367. begin
  368. result:=textrec(f).handle;
  369. end;