fina.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. Const
  135. MaxDirs = 129;
  136. function ExtractRelativepath (Const BaseName,DestName : String): String;
  137. Var Source, Dest : String;
  138. Sc,Dc,I,J : Longint;
  139. SD,DD : Array[1..MaxDirs] of PChar;
  140. Const OneLevelBack = '..'+DirectorySeparator;
  141. begin
  142. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  143. begin
  144. Result:=DestName;
  145. exit;
  146. end;
  147. Source:=ExcludeTrailingPathDelimiter(ExtractFilePath(BaseName));
  148. Dest:=ExcludeTrailingPathDelimiter(ExtractFilePath(DestName));
  149. SC:=GetDirs (Source,SD);
  150. DC:=GetDirs (Dest,DD);
  151. I:=1;
  152. While (I<=DC) and (I<=SC) do
  153. begin
  154. If StrIcomp(DD[i],SD[i])=0 then
  155. Inc(i)
  156. else
  157. Break;
  158. end;
  159. Result:='';
  160. For J:=I to SC do Result:=Result+OneLevelBack;
  161. For J:=I to DC do Result:=Result+DD[J]+DirectorySeparator;
  162. Result:=Result+ExtractFileName(DestNAme);
  163. end;
  164. Procedure DoDirSeparators (Var FileName : String);
  165. VAr I : longint;
  166. begin
  167. For I:=1 to Length(FileName) do
  168. If FileName[I] in AllowDirectorySeparators then
  169. FileName[i]:=DirectorySeparator;
  170. end;
  171. Function SetDirSeparators (Const FileName : string) : String;
  172. begin
  173. Result:=FileName;
  174. DoDirSeparators (Result);
  175. end;
  176. {
  177. DirName is split in a #0 separated list of directory names,
  178. Dirs is an array of pchars, pointing to these directory names.
  179. The function returns the number of directories found, or -1
  180. if none were found.
  181. }
  182. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  183. Var I : Longint;
  184. begin
  185. I:=1;
  186. Result:=-1;
  187. While I<=Length(DirName) do
  188. begin
  189. If (DirName[i] in AllowDirectorySeparators) and
  190. { avoid error in case last char=pathdelim }
  191. (length(dirname)>i) then
  192. begin
  193. DirName[i]:=#0;
  194. Inc(Result);
  195. Dirs[Result]:=@DirName[I+1];
  196. end;
  197. Inc(I);
  198. end;
  199. If Result>-1 then inc(Result);
  200. end;
  201. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  202. Var
  203. l : Integer;
  204. begin
  205. Result:=Path;
  206. l:=Length(Result);
  207. If (L=0) or not(Result[l] in AllowDirectorySeparators) then
  208. Result:=Result+DirectorySeparator;
  209. end;
  210. function IncludeTrailingBackslash(Const Path : String) : String;
  211. begin
  212. Result:=IncludeTrailingPathDelimiter(Path);
  213. end;
  214. function ExcludeTrailingBackslash(Const Path: string): string;
  215. begin
  216. Result:=ExcludeTrailingPathDelimiter(Path);
  217. end;
  218. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  219. Var
  220. L : Integer;
  221. begin
  222. L:=Length(Path);
  223. If (L>0) and (Path[L] in AllowDirectorySeparators) then
  224. Dec(L);
  225. Result:=Copy(Path,1,L);
  226. end;
  227. function IncludeLeadingPathDelimiter(Const Path : String) : String;
  228. Var
  229. l : Integer;
  230. begin
  231. Result:=Path;
  232. l:=Length(Result);
  233. If (L=0) or not(Result[1] in AllowDirectorySeparators) then
  234. Result:=DirectorySeparator+Result;
  235. end;
  236. function ExcludeLeadingPathDelimiter(Const Path: string): string;
  237. Var
  238. L : Integer;
  239. begin
  240. L:=Length(Path);
  241. If (L>0) and (Path[1] in AllowDirectorySeparators) then
  242. Dec(L);
  243. Result:=Copy(Path,2,L);
  244. end;
  245. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  246. begin
  247. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index] in AllowDirectorySeparators);
  248. end;
  249. function ConcatPaths(const Paths: array of String): String;
  250. var
  251. I: Integer;
  252. begin
  253. if Length(Paths) > 0 then
  254. begin
  255. Result := Paths[0];
  256. for I := 1 to Length(Paths) - 1 do
  257. Result := IncludeTrailingPathDelimiter(Result) + ExcludeLeadingPathDelimiter(Paths[I]);
  258. end else
  259. Result := '';
  260. end;
  261. Function GetFileHandle(var f : File):THandle;
  262. begin
  263. result:=filerec(f).handle;
  264. end;
  265. Function GetFileHandle(var f : Text):THandle;
  266. begin
  267. result:=textrec(f).handle;
  268. end;