fina.inc 7.0 KB

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