fina.inc 5.8 KB

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