fina.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. If Upcase(ExtractFileDrive(BaseName))<>Upcase(ExtractFileDrive(DestName)) Then
  96. begin
  97. Result:=DestName;
  98. exit;
  99. end;
  100. Source:=ExtractFilePath(BaseName);
  101. Dest:=ExtractFilePath(DestName);
  102. SC:=GetDirs (Source,SD);
  103. DC:=GetDirs (Dest,DD);
  104. I:=1;
  105. While (I<DC) and (I<SC) do
  106. begin
  107. If StrIcomp(DD[i],SD[i])=0 then
  108. Inc(i)
  109. else
  110. Break;
  111. end;
  112. Result:='';
  113. For J:=I to SC-1 do Result:=Result+OneLevelBack;
  114. For J:=I to DC-1 do Result:=Result+DD[J]+OsDirSeparator;
  115. Result:=Result+ExtractFileName(DestNAme);
  116. end;
  117. Procedure DoDirSeparators (Var FileName : String);
  118. VAr I : longint;
  119. begin
  120. For I:=1 to Length(FileName) do
  121. If FileName[I] in DirSeparators then
  122. FileName[i]:=OSDirSeparator;
  123. end;
  124. Function SetDirSeparators (Const FileName : string) : String;
  125. begin
  126. Result:=FileName;
  127. DoDirSeparators (Result);
  128. end;
  129. {
  130. DirName is split in a #0 separated list of directory names,
  131. Dirs is an array of pchars, pointing to these directory names.
  132. The function returns the number of directories found, or -1
  133. if none were found.
  134. DirName must contain only OSDirSeparator as Directory separator chars.
  135. }
  136. Function GetDirs (Var DirName : String; Var Dirs : Array of pchar) : Longint;
  137. Var I : Longint;
  138. begin
  139. I:=1;
  140. Result:=-1;
  141. While I<=Length(DirName) do
  142. begin
  143. If DirName[i]=OsDirSeparator then
  144. begin
  145. DirName[i]:=#0;
  146. Inc(Result);
  147. Dirs[Result]:=@DirName[I+1];
  148. end;
  149. Inc(I);
  150. end;
  151. If Result>-1 then inc(Result);
  152. end;
  153. {
  154. $Log$
  155. Revision 1.5 1998-12-19 14:52:28 peter
  156. * removed temp define
  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. }