2
0

filutil.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by the Free Pascal development team
  5. File utility calls
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  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.
  11. **********************************************************************}
  12. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  13. const
  14. AccessMode: array[0..2] of Integer = (
  15. GENERIC_READ,
  16. GENERIC_WRITE,
  17. GENERIC_READ or GENERIC_WRITE);
  18. ShareMode: array[0..4] of Integer = (
  19. 0,
  20. 0,
  21. FILE_SHARE_READ,
  22. FILE_SHARE_WRITE,
  23. FILE_SHARE_READ or FILE_SHARE_WRITE);
  24. Var FN : string;
  25. begin
  26. FN:=FileName+#0;
  27. Result := CreateFile(@FN[1], AccessMode[Mode and 3],
  28. ShareMode[(Mode and $F0) shr 4], nil, OPEN_EXISTING,
  29. FILE_ATTRIBUTE_NORMAL, 0);
  30. end;
  31. Function FileCreate (Const FileName : String) : Longint;
  32. Var FN : string;
  33. begin
  34. FN:=FileName+#0;
  35. Result := CreateFile(@FN[1], GENERIC_READ or GENERIC_WRITE,
  36. 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  37. end;
  38. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  39. Var res : Longint;
  40. begin
  41. if not ReadFile(Handle, @Buffer, Count, @res, nil) then res := -1;
  42. FileRead:=Res;
  43. end;
  44. Function FileWrite (Handle : Longint; Var Buffer; Count : Longint) : Longint;
  45. Var Res : longint;
  46. begin
  47. if not WriteFile(Handle, @Buffer, Count, @Res, nil) then Res:= -1;
  48. FileWrite:=Res;
  49. end;
  50. Function FileSeek (Handle,Offset,Origin : Longint) : Longint;
  51. begin
  52. Result := SetFilePointer(Handle, Offset, nil, Origin);
  53. end;
  54. Procedure FileClose (Handle : Longint);
  55. begin
  56. CloseHandle(Handle);
  57. end;
  58. Function FileTruncate (Handle,Size: Longint) : boolean;
  59. begin
  60. Result:=SetFilePointer(handle,Size,nil,FILE_BEGIN)<>-1;
  61. If Result then
  62. Result:=SetEndOfFile(handle);
  63. end;
  64. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  65. var
  66. lft : TFileTime;
  67. begin
  68. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,@lft) and
  69. LocalFileTimeToFileTime(lft,@Wtime);
  70. end;
  71. Function WinToDosTime (Var Wtime : TFileTime;var DTime:longint):longbool;
  72. var
  73. lft : FileTime;
  74. begin
  75. WinToDosTime:=FileTimeToLocalFileTime(WTime,@lft) and
  76. FileTimeToDosDateTime(lft,@Longrec(Dtime).Hi,@LongRec(DTIME).lo);
  77. end;
  78. Function FileAge (Const FileName : String): Longint;
  79. var
  80. Handle: THandle;
  81. FindData: TWin32FindData;
  82. LocalFileTime: TFileTime;
  83. Fn : String;
  84. begin
  85. Handle := FindFirstFile(Pchar(FileName), @FindData);
  86. if Handle <> INVALID_HANDLE_VALUE then
  87. begin
  88. Windows.FindClose(Handle);
  89. if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  90. If WinToDosTime(FindData.ftLastWriteTime,Result) then exit;
  91. end;
  92. Result := -1;
  93. end;
  94. Function FileExists (Const FileName : String) : Boolean;
  95. var
  96. Handle: THandle;
  97. FindData: TWin32FindData;
  98. P : Pchar;
  99. begin
  100. P:=Pchar(Filename);
  101. Handle := FindFirstFile(Pchar(FileName), @FindData);
  102. Result:=Handle <> INVALID_HANDLE_VALUE;
  103. If Result then
  104. Windows.FindClose(Handle);
  105. end;
  106. Function FindMatch(var f: TSearchRec) : Longint;
  107. begin
  108. { Find file with correct attribute }
  109. While (F.FindData.dwFileAttributes and F.ExcludeAttr)<>0 do
  110. begin
  111. if not FindNextFile (F.FindHandle,@F.FindData) then
  112. begin
  113. Result:=GetLastError;
  114. exit;
  115. end;
  116. end;
  117. { Convert some attributes back }
  118. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  119. f.size:=F.FindData.NFileSizeLow;
  120. f.attr:=F.FindData.dwFileAttributes;
  121. f.Name:=StrPas(@F.FindData.cFileName);
  122. end;
  123. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  124. begin
  125. Rslt.Name:=Path;
  126. Rslt.Attr:=attr;
  127. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  128. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  129. { FindFirstFile is a Win32 Call }
  130. Rslt.FindHandle:=FindFirstFile (PChar(Path),@Rslt.FindData);
  131. If Rslt.FindHandle=Invalid_Handle_value then
  132. begin
  133. Result:=GetLastError;
  134. exit;
  135. end;
  136. { Find file with correct attribute }
  137. Result:=FindMatch(Rslt);
  138. end;
  139. Function FindNext (Var Rslt : TSearchRec) : Longint;
  140. begin
  141. if FindNextFile(Rslt.FindHandle, @Rslt.FindData) then
  142. Result := FindMatch(Rslt)
  143. else
  144. Result := GetLastError;
  145. end;
  146. Procedure FindClose (Var F : TSearchrec);
  147. begin
  148. if F.FindHandle <> INVALID_HANDLE_VALUE then
  149. Windows.FindClose(F.FindHandle);
  150. end;
  151. Function FileGetDate (Handle : Longint) : Longint;
  152. Var FT : TFileTime;
  153. begin
  154. If GetFileTime(Handle,nil,nil,@ft) and
  155. WinToDosTime(FT,Result) then exit;
  156. Result:=-1;
  157. end;
  158. Function FileSetDate (Handle,Age : Longint) : Longint;
  159. Var FT: TFileTime;
  160. begin
  161. Result := 0;
  162. if DosToWinTime(Age,FT) and
  163. SetFileTime(Handle, ft, ft, FT) then Exit;
  164. Result := GetLastError;
  165. end;
  166. Function FileGetAttr (Const FileName : String) : Longint;
  167. begin
  168. Result:=GetFileAttributes(PChar(FileName));
  169. end;
  170. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  171. begin
  172. if not SetFileAttributes(PChar(FileName), Attr) then
  173. Result := GetLastError
  174. else
  175. Result:=0;
  176. end;
  177. Function DeleteFile (Const FileName : String) : Boolean;
  178. begin
  179. DeleteFile:=Windows.DeleteFile(Pchar(FileName));
  180. end;
  181. Function RenameFile (Const OldName, NewName : String) : Boolean;
  182. begin
  183. Result := MoveFile(PChar(OldName), PChar(NewName));
  184. end;
  185. Function FileSearch (Const Name, DirList : String) : String;
  186. Var I : longint;
  187. Temp : String;
  188. begin
  189. Result:='';
  190. temp:=Dirlist;
  191. repeat
  192. I:=pos(';',Temp);
  193. If I<>0 then
  194. begin
  195. Result:=Copy (Temp,1,i-1);
  196. system.Delete(Temp,1,I);
  197. end
  198. else
  199. begin
  200. Result:=Temp;
  201. Temp:='';
  202. end;
  203. If result[length(result)]<>'\' then
  204. Result:=Result+'\';
  205. Result:=Result+name;
  206. If not FileExists(Result) Then
  207. Result:='';
  208. until (length(temp)=0) or (length(result)<>0);
  209. end;
  210. Procedure GetLocalTime(var ST: TSystemTime);
  211. Var Syst:Systemtime;
  212. begin
  213. windows.Getlocaltime(@syst);
  214. st.year:=syst.wYear;
  215. st.month:=syst.wMonth;
  216. st.day:=syst.wDay;
  217. st.hour:=syst.wHour;
  218. st.minute:=syst.wMinute;
  219. st.second:=syst.wSecond;
  220. st.millisecond:=syst.wMilliSeconds;
  221. end;
  222. Procedure InitInternational;
  223. {
  224. called by sysutils initialization routines to set up
  225. internationalization support.
  226. }
  227. begin
  228. end;
  229. {
  230. $Log$
  231. Revision 1.5 1999-02-28 13:18:11 michael
  232. + Added internationalization support
  233. Revision 1.4 1999/02/24 15:57:30 michael
  234. + Moved getlocaltime to system-dependent files
  235. Revision 1.3 1999/02/09 12:01:03 michael
  236. + Implemented filetruncate
  237. Revision 1.2 1999/02/03 11:41:30 michael
  238. + Added filetruncate
  239. Revision 1.1 1998/10/11 12:21:01 michael
  240. Added file calls. Implemented for linux only
  241. }