fina.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. {$IFDEF VIRTUALPASCAL}
  20. {$J+}
  21. {$ENDIF}
  22. function ChangeFileExt(const FileName, Extension: string): string;
  23. var i: longint;
  24. begin
  25. I := Length(FileName);
  26. while (I > 0) and not(FileName[I] in ['/', '.', '\', ':']) do
  27. Dec(I);
  28. if (I = 0) or (FileName[I] <> '.') then
  29. I := Length(FileName)+1;
  30. Result := Copy(FileName, 1, I - 1) + Extension;
  31. end;
  32. function ExtractFilePath(const FileName: string): string;
  33. var i: longint;
  34. begin
  35. i := Length(FileName);
  36. while (i > 0) and not (FileName[i] in ['/', '\', ':']) do Dec(i);
  37. If I>0 then
  38. Result := Copy(FileName, 1, i)
  39. else
  40. Result:='';
  41. end;
  42. function ExtractFileDir(const FileName: string): string;
  43. var i: longint;
  44. begin
  45. I := Length(FileName);
  46. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  47. if (I > 1) and (FileName[I] in ['\', '/']) and
  48. not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);
  49. Result := Copy(FileName, 1, I);
  50. end;
  51. function ExtractFileDrive(const FileName: string): string;
  52. var i: longint;
  53. begin
  54. if (Length(FileName) >= 3) and (FileName[2] = ':') then
  55. result := Copy(FileName, 1, 2)
  56. else if (Length(FileName) >= 2) and (FileName[1] in ['/', '\']) and
  57. (FileName[2] in ['/', '\']) then begin
  58. i := 2;
  59. While (i < Length(Filename)) do begin
  60. if Filename[i + 1] in ['/', '\'] then break;
  61. inc(i);
  62. end ;
  63. Result := Copy(FileName, 1, i);
  64. end
  65. else Result := '';
  66. end;
  67. function ExtractFileName(const FileName: string): string;
  68. var i: longint;
  69. begin
  70. I := Length(FileName);
  71. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  72. Result := Copy(FileName, I + 1, 255);
  73. end;
  74. function ExtractFileExt(const FileName: string): string;
  75. var i: longint;
  76. begin
  77. I := Length(FileName);
  78. while (I > 0) and not (FileName[I] in ['.', '/', '\', ':']) do Dec(I);
  79. if (I > 0) and (FileName[I] = '.') then
  80. Result := Copy(FileName, I, 255)
  81. else Result := '';
  82. end;
  83. function ExpandFileName (Const FileName : string): String;
  84. Var S : String;
  85. Begin
  86. S:=FileName;
  87. {$IFNDEF VIRTUALPASCAL}
  88. DoDirSeparators(S);
  89. {$ENDIF}
  90. {$ifdef HasUnix}
  91. Result:=Unix.fexpand(S);
  92. {$else}
  93. Result:=Dos.Fexpand(S);
  94. {$endif}
  95. end;
  96. function ExpandUNCFileName (Const FileName : string): String;
  97. begin
  98. Result:=ExpandFileName (FileName);
  99. //!! Here should follow code to replace the drive: part with UNC...
  100. end;
  101. Const MaxDirs = 129;
  102. function ExtractRelativepath (Const BaseName,DestName : String): String;
  103. Var Source, Dest : String;
  104. Sc,Dc,I,J : Longint;
  105. SD,DD : Array[1..MaxDirs] of PChar;
  106. Const OneLevelBack = '..'+PathDelim;
  107. begin
  108. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  109. begin
  110. Result:=DestName;
  111. exit;
  112. end;
  113. Source:=ExtractFilePath(BaseName);
  114. Dest:=ExtractFilePath(DestName);
  115. SC:=GetDirs (Source,SD);
  116. DC:=GetDirs (Dest,DD);
  117. I:=1;
  118. While (I<DC) and (I<SC) do
  119. begin
  120. If StrIcomp(DD[i],SD[i])=0 then
  121. Inc(i)
  122. else
  123. Break;
  124. end;
  125. Result:='';
  126. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  127. For J:=I to DC-1 do Result:=Result+DD[J]+PathDelim;
  128. Result:=Result+ExtractFileName(DestNAme);
  129. end;
  130. Procedure DoDirSeparators (Var FileName : String);
  131. VAr I : longint;
  132. begin
  133. For I:=1 to Length(FileName) do
  134. If FileName[I] in DirSeparators then
  135. FileName[i]:=PathDelim;
  136. end;
  137. Function SetDirSeparators (Const FileName : string) : String;
  138. begin
  139. Result:=FileName;
  140. DoDirSeparators (Result);
  141. end;
  142. {
  143. DirName is split in a #0 separated list of directory names,
  144. Dirs is an array of pchars, pointing to these directory names.
  145. The function returns the number of directories found, or -1
  146. if none were found.
  147. DirName must contain only PathDelim as Directory separator chars.
  148. }
  149. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  150. Var I : Longint;
  151. begin
  152. I:=1;
  153. Result:=-1;
  154. While I<=Length(DirName) do
  155. begin
  156. If DirName[i]=PathDelim then
  157. begin
  158. DirName[i]:=#0;
  159. Inc(Result);
  160. Dirs[Result]:=@DirName[I+1];
  161. end;
  162. Inc(I);
  163. end;
  164. If Result>-1 then inc(Result);
  165. end;
  166. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  167. Var
  168. l : Integer;
  169. begin
  170. Result:=Path;
  171. l:=Length(Result);
  172. If (L=0) or (Result[l]<>PathDelim) then
  173. Result:=Result+PathDelim;
  174. end;
  175. function IncludeTrailingBackslash(Const Path : String) : String;
  176. begin
  177. Result:=IncludeTrailingPathDelimiter(Path);
  178. end;
  179. function ExcludeTrailingBackslash(Const Path: string): string;
  180. begin
  181. Result:=ExcludeTrailingPathDelimiter(Path);
  182. end;
  183. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  184. Var
  185. L : Integer;
  186. begin
  187. L:=Length(Path);
  188. If (L>0) and (Path[L]=PathDelim) then
  189. Dec(L);
  190. Result:=Copy(Path,1,L);
  191. end;
  192. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  193. begin
  194. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index]=PathDelim);
  195. end;
  196. {
  197. $Log$
  198. Revision 1.1 2003-10-06 21:01:06 peter
  199. * moved classes unit to rtl
  200. Revision 1.10 2003/09/06 21:52:24 marco
  201. * commited.
  202. Revision 1.9 2003/01/10 21:02:13 marco
  203. * hasunix fix for beos
  204. Revision 1.8 2002/10/22 21:57:54 michael
  205. + Added some missing path functions
  206. Revision 1.7 2002/10/12 15:34:09 michael
  207. + Fixed changefileexit for long (>255) filenames
  208. Revision 1.6 2002/09/07 16:01:22 peter
  209. * old logs removed and tabs fixed
  210. }