fina.inc 5.8 KB

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