fina.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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:=DirSeparators+[':','.'];
  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:=DirSeparators+[':'];
  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:=DirSeparators+[':'];
  52. while (I > 0) and not (FileName[I] in EndSep) do
  53. Dec(I);
  54. if (I > 1) and (FileName[I] in DirSeparators) 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]=':') then
  68. result:=Copy(FileName,1,2)
  69. else if (FileName[1] in DirSeparators) and
  70. (FileName[2] in DirSeparators) then
  71. begin
  72. i := 2;
  73. While (i<L) and Not (Filename[i+1] in DirSeparators) 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:=DirSeparators+[':'];
  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:=DirSeparators+['.', ':'];
  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. type
  104. PathStr=string;
  105. {$DEFINE FPC_FEXPAND_SYSUTILS}
  106. {$I fexpand.inc}
  107. function ExpandFileName (Const FileName : string): String;
  108. Var S : String;
  109. Begin
  110. S:=FileName;
  111. DoDirSeparators(S);
  112. Result:=Fexpand(S);
  113. end;
  114. {$ifndef HASEXPANDUNCFILENAME}
  115. function ExpandUNCFileName (Const FileName : string): String;
  116. begin
  117. Result:=ExpandFileName (FileName);
  118. //!! Here should follow code to replace the drive: part with UNC...
  119. end;
  120. {$endif HASEXPANDUNCFILENAME}
  121. Const
  122. MaxDirs = 129;
  123. function ExtractRelativepath (Const BaseName,DestName : String): String;
  124. Var Source, Dest : String;
  125. Sc,Dc,I,J : Longint;
  126. SD,DD : Array[1..MaxDirs] of PChar;
  127. Const OneLevelBack = '..'+PathDelim;
  128. begin
  129. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  130. begin
  131. Result:=DestName;
  132. exit;
  133. end;
  134. Source:=ExcludeTrailingPathDelimiter(ExtractFilePath(BaseName));
  135. Dest:=ExcludeTrailingPathDelimiter(ExtractFilePath(DestName));
  136. SC:=GetDirs (Source,SD);
  137. DC:=GetDirs (Dest,DD);
  138. I:=1;
  139. While (I<=DC) and (I<=SC) do
  140. begin
  141. If StrIcomp(DD[i],SD[i])=0 then
  142. Inc(i)
  143. else
  144. Break;
  145. end;
  146. Result:='';
  147. For J:=I to SC do Result:=Result+OneLevelBack;
  148. For J:=I to DC do Result:=Result+DD[J]+PathDelim;
  149. Result:=Result+ExtractFileName(DestNAme);
  150. end;
  151. Procedure DoDirSeparators (Var FileName : String);
  152. VAr I : longint;
  153. begin
  154. For I:=1 to Length(FileName) do
  155. If FileName[I] in DirSeparators then
  156. FileName[i]:=PathDelim;
  157. end;
  158. Function SetDirSeparators (Const FileName : string) : String;
  159. begin
  160. Result:=FileName;
  161. DoDirSeparators (Result);
  162. end;
  163. {
  164. DirName is split in a #0 separated list of directory names,
  165. Dirs is an array of pchars, pointing to these directory names.
  166. The function returns the number of directories found, or -1
  167. if none were found.
  168. DirName must contain only PathDelim as Directory separator chars.
  169. }
  170. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  171. Var I : Longint;
  172. begin
  173. I:=1;
  174. Result:=-1;
  175. While I<=Length(DirName) do
  176. begin
  177. If (DirName[i]=PathDelim) and
  178. { avoid error in case last char=pathdelim }
  179. (length(dirname)>i) then
  180. begin
  181. DirName[i]:=#0;
  182. Inc(Result);
  183. Dirs[Result]:=@DirName[I+1];
  184. end;
  185. Inc(I);
  186. end;
  187. If Result>-1 then inc(Result);
  188. end;
  189. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  190. Var
  191. l : Integer;
  192. begin
  193. Result:=Path;
  194. l:=Length(Result);
  195. If (L=0) or not(Result[l] in DirSeparators) then
  196. Result:=Result+PathDelim;
  197. end;
  198. function IncludeTrailingBackslash(Const Path : String) : String;
  199. begin
  200. Result:=IncludeTrailingPathDelimiter(Path);
  201. end;
  202. function ExcludeTrailingBackslash(Const Path: string): string;
  203. begin
  204. Result:=ExcludeTrailingPathDelimiter(Path);
  205. end;
  206. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  207. Var
  208. L : Integer;
  209. begin
  210. L:=Length(Path);
  211. If (L>0) and (Path[L] in DirSeparators) then
  212. Dec(L);
  213. Result:=Copy(Path,1,L);
  214. end;
  215. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  216. begin
  217. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index] in DirSeparators);
  218. end;
  219. Function GetFileHandle(var f : File):Longint;
  220. begin
  221. result:=filerec(f).handle;
  222. end;
  223. Function GetFileHandle(var f : Text):Longint;
  224. begin
  225. result:=textrec(f).handle;
  226. end;