fina.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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
  49. i,l: longint;
  50. begin
  51. Result := '';
  52. l:=Length(FileName);
  53. if (L>=2) then
  54. begin
  55. If (FileName[2]=':') then
  56. result:=Copy(FileName,1,2)
  57. else if (FileName[1] in ['/','\']) and
  58. (FileName[2] in ['/','\']) then
  59. begin
  60. i := 2;
  61. While (i<L) and Not (Filename[i+1] in ['/', '\']) do
  62. inc(i);
  63. Result:=Copy(FileName,1,i);
  64. end;
  65. end;
  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, MaxInt);
  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, MaxInt)
  81. else Result := '';
  82. end;
  83. type
  84. PathStr=string;
  85. {$DEFINE FPC_FEXPAND_SYSUTILS}
  86. {$I fexpand.inc}
  87. function ExpandFileName (Const FileName : string): String;
  88. Var S : String;
  89. Begin
  90. S:=FileName;
  91. DoDirSeparators(S);
  92. Result:=Fexpand(S);
  93. end;
  94. {$ifndef HASEXPANDUNCFILENAME}
  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. {$endif HASEXPANDUNCFILENAME}
  101. Const
  102. MaxDirs = 129;
  103. function ExtractRelativepath (Const BaseName,DestName : String): String;
  104. Var Source, Dest : String;
  105. Sc,Dc,I,J : Longint;
  106. SD,DD : Array[1..MaxDirs] of PChar;
  107. Const OneLevelBack = '..'+PathDelim;
  108. begin
  109. If Uppercase(ExtractFileDrive(BaseName))<>Uppercase(ExtractFileDrive(DestName)) Then
  110. begin
  111. Result:=DestName;
  112. exit;
  113. end;
  114. Source:=ExtractFilePath(BaseName);
  115. Dest:=ExtractFilePath(DestName);
  116. SC:=GetDirs (Source,SD);
  117. DC:=GetDirs (Dest,DD);
  118. I:=1;
  119. While (I<DC) and (I<SC) do
  120. begin
  121. If StrIcomp(DD[i],SD[i])=0 then
  122. Inc(i)
  123. else
  124. Break;
  125. end;
  126. Result:='';
  127. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  128. For J:=I to DC-1 do Result:=Result+DD[J]+PathDelim;
  129. Result:=Result+ExtractFileName(DestNAme);
  130. end;
  131. Procedure DoDirSeparators (Var FileName : String);
  132. VAr I : longint;
  133. begin
  134. For I:=1 to Length(FileName) do
  135. If FileName[I] in DirSeparators then
  136. FileName[i]:=PathDelim;
  137. end;
  138. Function SetDirSeparators (Const FileName : string) : String;
  139. begin
  140. Result:=FileName;
  141. DoDirSeparators (Result);
  142. end;
  143. {
  144. DirName is split in a #0 separated list of directory names,
  145. Dirs is an array of pchars, pointing to these directory names.
  146. The function returns the number of directories found, or -1
  147. if none were found.
  148. DirName must contain only PathDelim as Directory separator chars.
  149. }
  150. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  151. Var I : Longint;
  152. begin
  153. I:=1;
  154. Result:=-1;
  155. While I<=Length(DirName) do
  156. begin
  157. If DirName[i]=PathDelim then
  158. begin
  159. DirName[i]:=#0;
  160. Inc(Result);
  161. Dirs[Result]:=@DirName[I+1];
  162. end;
  163. Inc(I);
  164. end;
  165. If Result>-1 then inc(Result);
  166. end;
  167. function IncludeTrailingPathDelimiter(Const Path : String) : String;
  168. Var
  169. l : Integer;
  170. begin
  171. Result:=Path;
  172. l:=Length(Result);
  173. If (L=0) or (Result[l]<>PathDelim) then
  174. Result:=Result+PathDelim;
  175. end;
  176. function IncludeTrailingBackslash(Const Path : String) : String;
  177. begin
  178. Result:=IncludeTrailingPathDelimiter(Path);
  179. end;
  180. function ExcludeTrailingBackslash(Const Path: string): string;
  181. begin
  182. Result:=ExcludeTrailingPathDelimiter(Path);
  183. end;
  184. function ExcludeTrailingPathDelimiter(Const Path: string): string;
  185. Var
  186. L : Integer;
  187. begin
  188. L:=Length(Path);
  189. If (L>0) and (Path[L]=PathDelim) then
  190. Dec(L);
  191. Result:=Copy(Path,1,L);
  192. end;
  193. function IsPathDelimiter(Const Path: string; Index: Integer): Boolean;
  194. begin
  195. Result:=(Index>0) and (Index<=Length(Path)) and (Path[Index]=PathDelim);
  196. end;
  197. Function GetFileHandle(var f : File):Longint;
  198. begin
  199. result:=filerec(f).handle;
  200. end;
  201. Function GetFileHandle(var f : Text):Longint;
  202. begin
  203. result:=textrec(f).handle;
  204. end;