fina.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. function ChangeFileExt(const FileName, Extension: string): string;
  20. var i: longint;
  21. begin
  22. I := Length(FileName);
  23. while (I > 0) and not(FileName[I] in ['/', '.', '\', ':']) do
  24. Dec(I);
  25. if (I = 0) or (FileName[I] <> '.') then
  26. I := Length(FileName)+1;
  27. Result := Copy(FileName, 1, I - 1) + Extension;
  28. end;
  29. function ExtractFilePath(const FileName: string): string;
  30. var i: longint;
  31. begin
  32. i := Length(FileName);
  33. while (i > 0) and not (FileName[i] in ['/', '\', ':']) do Dec(i);
  34. If I>0 then
  35. Result := Copy(FileName, 1, i)
  36. else
  37. Result:='';
  38. end;
  39. function ExtractFileDir(const FileName: string): string;
  40. var i: longint;
  41. begin
  42. I := Length(FileName);
  43. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  44. if (I > 1) and (FileName[I] in ['\', '/']) and
  45. not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);
  46. Result := Copy(FileName, 1, I);
  47. end;
  48. function ExtractFileDrive(const FileName: string): string;
  49. var i: longint;
  50. begin
  51. if (Length(FileName) >= 3) and (FileName[2] = ':') then
  52. result := Copy(FileName, 1, 2)
  53. else if (Length(FileName) >= 2) and (FileName[1] in ['/', '\']) and
  54. (FileName[2] in ['/', '\']) then begin
  55. i := 2;
  56. While (i < Length(Filename)) do begin
  57. if Filename[i + 1] in ['/', '\'] then break;
  58. inc(i);
  59. end ;
  60. Result := Copy(FileName, 1, i);
  61. end
  62. else Result := '';
  63. end;
  64. function ExtractFileName(const FileName: string): string;
  65. var i: longint;
  66. begin
  67. I := Length(FileName);
  68. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  69. Result := Copy(FileName, I + 1, 255);
  70. end;
  71. function ExtractFileExt(const FileName: string): string;
  72. var i: longint;
  73. begin
  74. I := Length(FileName);
  75. while (I > 0) and not (FileName[I] in ['.', '/', '\', ':']) do Dec(I);
  76. if (I > 0) and (FileName[I] = '.') then
  77. Result := Copy(FileName, I, 255)
  78. else Result := '';
  79. end;
  80. function ExpandFileName (Const FileName : string): String;
  81. Var S : String;
  82. Begin
  83. S:=FileName;
  84. DoDirSeparators(S);
  85. {$ifdef HasUnix}
  86. Result:=Unix.fexpand(S);
  87. {$else}
  88. Result:=Dos.Fexpand(S);
  89. {$endif}
  90. end;
  91. function ExpandUNCFileName (Const FileName : string): String;
  92. begin
  93. Result:=ExpandFileName (FileName);
  94. //!! Here should follow code to replace the drive: part with UNC...
  95. end;
  96. Const MaxDirs = 129;
  97. function ExtractRelativepath (Const BaseName,DestName : String): String;
  98. Var Source, Dest : String;
  99. Sc,Dc,I,J : Longint;
  100. SD,DD : Array[1..MaxDirs] of PChar;
  101. Const OneLevelBack = '..'+PathDelim;
  102. begin
  103. If Upcase(ExtractFileDrive(BaseName))<>Upcase(ExtractFileDrive(DestName)) Then
  104. begin
  105. Result:=DestName;
  106. exit;
  107. end;
  108. Source:=ExtractFilePath(BaseName);
  109. Dest:=ExtractFilePath(DestName);
  110. SC:=GetDirs (Source,SD);
  111. DC:=GetDirs (Dest,DD);
  112. I:=1;
  113. While (I<DC) and (I<SC) do
  114. begin
  115. If StrIcomp(DD[i],SD[i])=0 then
  116. Inc(i)
  117. else
  118. Break;
  119. end;
  120. Result:='';
  121. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  122. For J:=I to DC-1 do Result:=Result+DD[J]+PathDelim;
  123. Result:=Result+ExtractFileName(DestNAme);
  124. end;
  125. Procedure DoDirSeparators (Var FileName : String);
  126. VAr I : longint;
  127. begin
  128. For I:=1 to Length(FileName) do
  129. If FileName[I] in DirSeparators then
  130. FileName[i]:=PathDelim;
  131. end;
  132. Function SetDirSeparators (Const FileName : string) : String;
  133. begin
  134. Result:=FileName;
  135. DoDirSeparators (Result);
  136. end;
  137. {
  138. DirName is split in a #0 separated list of directory names,
  139. Dirs is an array of pchars, pointing to these directory names.
  140. The function returns the number of directories found, or -1
  141. if none were found.
  142. DirName must contain only PathDelim as Directory separator chars.
  143. }
  144. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  145. Var I : Longint;
  146. begin
  147. I:=1;
  148. Result:=-1;
  149. While I<=Length(DirName) do
  150. begin
  151. If DirName[i]=PathDelim then
  152. begin
  153. DirName[i]:=#0;
  154. Inc(Result);
  155. Dirs[Result]:=@DirName[I+1];
  156. end;
  157. Inc(I);
  158. end;
  159. If Result>-1 then inc(Result);
  160. end;
  161. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  162. Var
  163. l : Integer;
  164. begin
  165. Result:=Path;
  166. l:=Length(Result);
  167. If (L=0) or (Result[l]<>PathDelim) then
  168. Result:=Result+PathDelim;
  169. end;
  170. function IncludeTrailingBackslash(Const Path : String) : String;
  171. begin
  172. Result:=IncludeTrailingPathDelimiter(Path);
  173. end;
  174. function ExcludeTrailingBackslash(Const Path: string): string;
  175. begin
  176. Result:=ExcludeTrailingPathDelimiter(Path);
  177. end;
  178. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  179. Var
  180. L : Integer;
  181. begin
  182. L:=Length(Path);
  183. If (L>0) and (Path[L]=PathDelim) then
  184. Dec(L);
  185. Result:=Copy(Path,1,L);
  186. end;
  187. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  188. begin
  189. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index]=PathDelim);
  190. end;
  191. {
  192. $Log$
  193. Revision 1.9 2003-01-10 21:02:13 marco
  194. * hasunix fix for beos
  195. Revision 1.8 2002/10/22 21:57:54 michael
  196. + Added some missing path functions
  197. Revision 1.7 2002/10/12 15:34:09 michael
  198. + Fixed changefileexit for long (>255) filenames
  199. Revision 1.6 2002/09/07 16:01:22 peter
  200. * old logs removed and tabs fixed
  201. }