fina.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 Dec(I);
  24. if (I = 0) or (FileName[I] <> '.') then I := 255;
  25. Result := Copy(FileName, 1, I - 1) + Extension;
  26. end;
  27. function ExtractFilePath(const FileName: string): string;
  28. var i: longint;
  29. begin
  30. i := Length(FileName);
  31. while (i > 0) and not (FileName[i] in ['/', '\', ':']) do Dec(i);
  32. Result := Copy(FileName, 1, i);
  33. end;
  34. function ExtractFileDir(const FileName: string): string;
  35. var i: longint;
  36. begin
  37. I := Length(FileName);
  38. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  39. if (I > 1) and (FileName[I] in ['\', '/']) and
  40. not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);
  41. Result := Copy(FileName, 1, I);
  42. end;
  43. function ExtractFileDrive(const FileName: string): string;
  44. var i: longint;
  45. begin
  46. if (Length(FileName) >= 3) and (FileName[2] = ':') then
  47. result := Copy(FileName, 1, 2)
  48. else if (Length(FileName) >= 2) and (FileName[1] in ['/', '\']) and
  49. (FileName[2] in ['/', '\']) then begin
  50. i := 2;
  51. While (i < Length(Filename)) do begin
  52. if Filename[i + 1] in ['/', '\'] then break;
  53. inc(i);
  54. end ;
  55. Result := Copy(FileName, 1, i);
  56. end
  57. else Result := '';
  58. end;
  59. function ExtractFileName(const FileName: string): string;
  60. var i: longint;
  61. begin
  62. I := Length(FileName);
  63. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  64. Result := Copy(FileName, I + 1, 255);
  65. end;
  66. function ExtractFileExt(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. if (I > 0) and (FileName[I] = '.') then
  72. Result := Copy(FileName, I, 255)
  73. else Result := '';
  74. end;
  75. function ExpandFileName (Const FileName : string): String;
  76. Begin
  77. {$ifdef linux}
  78. Result:=Linux.fexpand(FileName);
  79. {$else}
  80. Result:=Dos.Fexpand(FileName);
  81. {$endif}
  82. end;
  83. function ExpandUNCFileName (Const FileName : string): String;
  84. begin
  85. Result:=ExpandFileName (FileName);
  86. //!! Here should follow code to replace the drive: part with UNC...
  87. end;
  88. Const MaxDirs = 129;
  89. function ExtractRelativepath (Const BaseName,DestName : String): String;
  90. Var Source, Dest : String;
  91. Sc,Dc,I,J : Longint;
  92. SD,DD : Array[1..MaxDirs] of PChar;
  93. Const OneLevelBack = '..'+OSDirSeparator;
  94. begin
  95. {$ifdef autoobjpas}
  96. If Upcase(ExtractFileDrive(BaseName))<>Upcase(ExtractFileDrive(DestName)) Then
  97. begin
  98. Result:=DestName;
  99. exit;
  100. end;
  101. Source:=ExtractFilePath(BaseName);
  102. Dest:=ExtractFilePath(DestName);
  103. SC:=GetDirs (Source,SD);
  104. DC:=GetDirs (Dest,DD);
  105. I:=1;
  106. While (I<DC) and (I<SC) do
  107. begin
  108. If StrIcomp(DD[i],SD[i])=0 then
  109. Inc(i)
  110. else
  111. Break;
  112. end;
  113. Result:='';
  114. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  115. For J:=I to DC-1 do Result:=Result+DD[J]+OsDirSeparator;
  116. Result:=Result+ExtractFileName(DestNAme);
  117. {$endif}
  118. end;
  119. Procedure DoDirSeparators (Var FileName : String);
  120. VAr I : longint;
  121. begin
  122. For I:=1 to Length(FileName) do
  123. If FileName[I] in DirSeparators then
  124. FileName[i]:=OSDirSeparator;
  125. end;
  126. Function SetDirSeparators (Const FileName : string) : String;
  127. begin
  128. Result:=FileName;
  129. DoDirSeparators (Result);
  130. end;
  131. {
  132. DirName is split in a #0 separated list of directory names,
  133. Dirs is an array of pchars, pointing to these directory names.
  134. The function returns the number of directories found, or -1
  135. if none were found.
  136. DirName must contain only OSDirSeparator as Directory separator chars.
  137. }
  138. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  139. Var I : Longint;
  140. begin
  141. I:=1;
  142. Result:=-1;
  143. While I<=Length(DirName) do
  144. begin
  145. If DirName[i]=OsDirSeparator then
  146. begin
  147. DirName[i]:=#0;
  148. Inc(Result);
  149. Dirs[Result]:=@DirName[I+1];
  150. end;
  151. Inc(I);
  152. end;
  153. If Result>-1 then inc(Result);
  154. end;
  155. {
  156. $Log$
  157. Revision 1.4 1998-10-05 21:35:41 peter
  158. * fixed for 0.99.8
  159. Revision 1.3 1998/10/04 20:19:56 michael
  160. + Added missing functions and some extra
  161. Revision 1.2 1998/09/16 08:28:38 michael
  162. Update from gertjan Schouten, plus small fix for linux
  163. Revision 1.1 1998/04/10 15:17:46 michael
  164. + Initial implementation; Donated by Gertjan Schouten
  165. His file was split into several files, to keep it a little bit structured.
  166. }