fina.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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] <> '.') 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. While (i<L) and Not (Filename[i+1] in AllowDirectorySeparators) do
  74. inc(i);
  75. Result:=Copy(FileName,1,i);
  76. end;
  77. end;
  78. function ExtractFileName(const FileName: string): string;
  79. var
  80. i : longint;
  81. EndSep : Set of Char;
  82. begin
  83. I := Length(FileName);
  84. EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];
  85. while (I > 0) and not (FileName[I] in EndSep) do
  86. Dec(I);
  87. Result := Copy(FileName, I + 1, MaxInt);
  88. end;
  89. function ExtractFileExt(const FileName: string): string;
  90. var
  91. i : longint;
  92. EndSep : Set of Char;
  93. begin
  94. I := Length(FileName);
  95. EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
  96. while (I > 0) and not (FileName[I] in EndSep) do
  97. Dec(I);
  98. if (I > 0) and (FileName[I] = '.') then
  99. Result := Copy(FileName, I, MaxInt)
  100. else
  101. Result := '';
  102. end;
  103. function ExtractShortPathName(Const FileName : String) : String;
  104. begin
  105. {$ifdef MSWINDOWS}
  106. SetLength(Result,Max_Path);
  107. SetLength(Result,GetShortPathName(PChar(FileName), Pchar(Result),Length(Result)));
  108. {$else}
  109. Result:=FileName;
  110. {$endif}
  111. end;
  112. type
  113. PathStr=string;
  114. {$DEFINE FPC_FEXPAND_SYSUTILS}
  115. {$I fexpand.inc}
  116. function ExpandFileName (Const FileName : string): String;
  117. Var S : String;
  118. Begin
  119. S:=FileName;
  120. DoDirSeparators(S);
  121. Result:=Fexpand(S);
  122. end;
  123. {$ifndef HASEXPANDUNCFILENAME}
  124. function ExpandUNCFileName (Const FileName : string): String;
  125. begin
  126. Result:=ExpandFileName (FileName);
  127. //!! Here should follow code to replace the drive: part with UNC...
  128. end;
  129. {$endif HASEXPANDUNCFILENAME}
  130. Const
  131. MaxDirs = 129;
  132. function ExtractRelativepath (Const BaseName,DestName : String): String;
  133. Var Source, Dest : String;
  134. Sc,Dc,I,J : Longint;
  135. SD,DD : Array[1..MaxDirs] of PChar;
  136. Const OneLevelBack = '..'+DirectorySeparator;
  137. begin
  138. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  139. begin
  140. Result:=DestName;
  141. exit;
  142. end;
  143. Source:=ExcludeTrailingPathDelimiter(ExtractFilePath(BaseName));
  144. Dest:=ExcludeTrailingPathDelimiter(ExtractFilePath(DestName));
  145. SC:=GetDirs (Source,SD);
  146. DC:=GetDirs (Dest,DD);
  147. I:=1;
  148. While (I<=DC) and (I<=SC) do
  149. begin
  150. If StrIcomp(DD[i],SD[i])=0 then
  151. Inc(i)
  152. else
  153. Break;
  154. end;
  155. Result:='';
  156. For J:=I to SC do Result:=Result+OneLevelBack;
  157. For J:=I to DC do Result:=Result+DD[J]+DirectorySeparator;
  158. Result:=Result+ExtractFileName(DestNAme);
  159. end;
  160. Procedure DoDirSeparators (Var FileName : String);
  161. VAr I : longint;
  162. begin
  163. For I:=1 to Length(FileName) do
  164. If FileName[I] in AllowDirectorySeparators then
  165. FileName[i]:=DirectorySeparator;
  166. end;
  167. Function SetDirSeparators (Const FileName : string) : String;
  168. begin
  169. Result:=FileName;
  170. DoDirSeparators (Result);
  171. end;
  172. {
  173. DirName is split in a #0 separated list of directory names,
  174. Dirs is an array of pchars, pointing to these directory names.
  175. The function returns the number of directories found, or -1
  176. if none were found.
  177. }
  178. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  179. Var I : Longint;
  180. begin
  181. I:=1;
  182. Result:=-1;
  183. While I<=Length(DirName) do
  184. begin
  185. If (DirName[i] in AllowDirectorySeparators) and
  186. { avoid error in case last char=pathdelim }
  187. (length(dirname)>i) then
  188. begin
  189. DirName[i]:=#0;
  190. Inc(Result);
  191. Dirs[Result]:=@DirName[I+1];
  192. end;
  193. Inc(I);
  194. end;
  195. If Result>-1 then inc(Result);
  196. end;
  197. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  198. Var
  199. l : Integer;
  200. begin
  201. Result:=Path;
  202. l:=Length(Result);
  203. If (L=0) or not(Result[l] in AllowDirectorySeparators) then
  204. Result:=Result+DirectorySeparator;
  205. end;
  206. function IncludeTrailingBackslash(Const Path : String) : String;
  207. begin
  208. Result:=IncludeTrailingPathDelimiter(Path);
  209. end;
  210. function ExcludeTrailingBackslash(Const Path: string): string;
  211. begin
  212. Result:=ExcludeTrailingPathDelimiter(Path);
  213. end;
  214. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  215. Var
  216. L : Integer;
  217. begin
  218. L:=Length(Path);
  219. If (L>0) and (Path[L] in AllowDirectorySeparators) then
  220. Dec(L);
  221. Result:=Copy(Path,1,L);
  222. end;
  223. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  224. begin
  225. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index] in AllowDirectorySeparators);
  226. end;
  227. Function GetFileHandle(var f : File):Longint;
  228. begin
  229. result:=filerec(f).handle;
  230. end;
  231. Function GetFileHandle(var f : Text):Longint;
  232. begin
  233. result:=textrec(f).handle;
  234. end;