fina.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. If I>0 then
  33. Result := Copy(FileName, 1, i)
  34. else
  35. Result:='';
  36. end;
  37. function ExtractFileDir(const FileName: string): string;
  38. var i: longint;
  39. begin
  40. I := Length(FileName);
  41. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  42. if (I > 1) and (FileName[I] in ['\', '/']) and
  43. not (FileName[I - 1] in ['/', '\', ':']) then Dec(I);
  44. Result := Copy(FileName, 1, I);
  45. end;
  46. function ExtractFileDrive(const FileName: string): string;
  47. var i: longint;
  48. begin
  49. if (Length(FileName) >= 3) and (FileName[2] = ':') then
  50. result := Copy(FileName, 1, 2)
  51. else if (Length(FileName) >= 2) and (FileName[1] in ['/', '\']) and
  52. (FileName[2] in ['/', '\']) then begin
  53. i := 2;
  54. While (i < Length(Filename)) do begin
  55. if Filename[i + 1] in ['/', '\'] then break;
  56. inc(i);
  57. end ;
  58. Result := Copy(FileName, 1, i);
  59. end
  60. else Result := '';
  61. end;
  62. function ExtractFileName(const FileName: string): string;
  63. var i: longint;
  64. begin
  65. I := Length(FileName);
  66. while (I > 0) and not (FileName[I] in ['/', '\', ':']) do Dec(I);
  67. Result := Copy(FileName, I + 1, 255);
  68. end;
  69. function ExtractFileExt(const FileName: string): string;
  70. var i: longint;
  71. begin
  72. I := Length(FileName);
  73. while (I > 0) and not (FileName[I] in ['.', '/', '\', ':']) do Dec(I);
  74. if (I > 0) and (FileName[I] = '.') then
  75. Result := Copy(FileName, I, 255)
  76. else Result := '';
  77. end;
  78. function ExpandFileName (Const FileName : string): String;
  79. Var S : String;
  80. Begin
  81. S:=FileName;
  82. DoDirSeparators(S);
  83. {$ifdef linux}
  84. Result:=Linux.fexpand(S);
  85. {$else}
  86. Result:=Dos.Fexpand(S);
  87. {$endif}
  88. end;
  89. function ExpandUNCFileName (Const FileName : string): String;
  90. begin
  91. Result:=ExpandFileName (FileName);
  92. //!! Here should follow code to replace the drive: part with UNC...
  93. end;
  94. Const MaxDirs = 129;
  95. function ExtractRelativepath (Const BaseName,DestName : String): String;
  96. Var Source, Dest : String;
  97. Sc,Dc,I,J : Longint;
  98. SD,DD : Array[1..MaxDirs] of PChar;
  99. Const OneLevelBack = '..'+OSDirSeparator;
  100. begin
  101. If Upcase(ExtractFileDrive(BaseName))<>Upcase(ExtractFileDrive(DestName)) Then
  102. begin
  103. Result:=DestName;
  104. exit;
  105. end;
  106. Source:=ExtractFilePath(BaseName);
  107. Dest:=ExtractFilePath(DestName);
  108. SC:=GetDirs (Source,SD);
  109. DC:=GetDirs (Dest,DD);
  110. I:=1;
  111. While (I<DC) and (I<SC) do
  112. begin
  113. If StrIcomp(DD[i],SD[i])=0 then
  114. Inc(i)
  115. else
  116. Break;
  117. end;
  118. Result:='';
  119. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  120. For J:=I to DC-1 do Result:=Result+DD[J]+OsDirSeparator;
  121. Result:=Result+ExtractFileName(DestNAme);
  122. end;
  123. Procedure DoDirSeparators (Var FileName : String);
  124. VAr I : longint;
  125. begin
  126. For I:=1 to Length(FileName) do
  127. If FileName[I] in DirSeparators then
  128. FileName[i]:=OSDirSeparator;
  129. end;
  130. Function SetDirSeparators (Const FileName : string) : String;
  131. begin
  132. Result:=FileName;
  133. DoDirSeparators (Result);
  134. end;
  135. {
  136. DirName is split in a #0 separated list of directory names,
  137. Dirs is an array of pchars, pointing to these directory names.
  138. The function returns the number of directories found, or -1
  139. if none were found.
  140. DirName must contain only OSDirSeparator as Directory separator chars.
  141. }
  142. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  143. Var I : Longint;
  144. begin
  145. I:=1;
  146. Result:=-1;
  147. While I<=Length(DirName) do
  148. begin
  149. If DirName[i]=OsDirSeparator then
  150. begin
  151. DirName[i]:=#0;
  152. Inc(Result);
  153. Dirs[Result]:=@DirName[I+1];
  154. end;
  155. Inc(I);
  156. end;
  157. If Result>-1 then inc(Result);
  158. end;
  159. {
  160. $Log$
  161. Revision 1.7 2000-02-09 16:59:32 peter
  162. * truncated log
  163. }