filutil.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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; const 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,FOffset,Origin : Longint) : Longint;
  51. begin
  52. Result := SetFilePointer(Handle, FOffset, nil, Origin);
  53. end;
  54. Procedure FileClose (Handle : Longint);
  55. begin
  56. if Handle<=4 then
  57. exit;
  58. CloseHandle(Handle);
  59. end;
  60. Function FileTruncate (Handle,Size: Longint) : boolean;
  61. begin
  62. Result:=SetFilePointer(handle,Size,nil,FILE_BEGIN)<>-1;
  63. If Result then
  64. Result:=SetEndOfFile(handle);
  65. end;
  66. Function DosToWinTime (DTime:longint;Var Wtime : TFileTime):longbool;
  67. var
  68. lft : TFileTime;
  69. begin
  70. DosToWinTime:=DosDateTimeToFileTime(longrec(dtime).hi,longrec(dtime).lo,@lft) and
  71. LocalFileTimeToFileTime(lft,@Wtime);
  72. end;
  73. Function WinToDosTime (Var Wtime : TFileTime;var DTime:longint):longbool;
  74. var
  75. lft : FileTime;
  76. begin
  77. WinToDosTime:=FileTimeToLocalFileTime(WTime,@lft) and
  78. FileTimeToDosDateTime(lft,@Longrec(Dtime).Hi,@LongRec(DTIME).lo);
  79. end;
  80. Function FileAge (Const FileName : String): Longint;
  81. var
  82. Handle: THandle;
  83. FindData: TWin32FindData;
  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. begin
  99. Handle := FindFirstFile(Pchar(FileName), @FindData);
  100. Result:=Handle <> INVALID_HANDLE_VALUE;
  101. If Result then
  102. Windows.FindClose(Handle);
  103. end;
  104. Function FindMatch(var f: TSearchRec) : Longint;
  105. begin
  106. { Find file with correct attribute }
  107. While (F.FindData.dwFileAttributes and F.ExcludeAttr)<>0 do
  108. begin
  109. if not FindNextFile (F.FindHandle,@F.FindData) then
  110. begin
  111. Result:=GetLastError;
  112. exit;
  113. end;
  114. end;
  115. { Convert some attributes back }
  116. WinToDosTime(F.FindData.ftLastWriteTime,F.Time);
  117. f.size:=F.FindData.NFileSizeLow;
  118. f.attr:=F.FindData.dwFileAttributes;
  119. f.Name:=StrPas(@F.FindData.cFileName);
  120. Result:=0;
  121. end;
  122. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  123. begin
  124. Rslt.Name:=Path;
  125. Rslt.Attr:=attr;
  126. Rslt.ExcludeAttr:=(not Attr) and ($1e);
  127. { $1e = faHidden or faSysFile or faVolumeID or faDirectory }
  128. { FindFirstFile is a Win32 Call }
  129. Rslt.FindHandle:=FindFirstFile (PChar(Path),@Rslt.FindData);
  130. If Rslt.FindHandle=Invalid_Handle_value then
  131. begin
  132. Result:=GetLastError;
  133. exit;
  134. end;
  135. { Find file with correct attribute }
  136. Result:=FindMatch(Rslt);
  137. end;
  138. Function FindNext (Var Rslt : TSearchRec) : Longint;
  139. begin
  140. if FindNextFile(Rslt.FindHandle, @Rslt.FindData) then
  141. Result := FindMatch(Rslt)
  142. else
  143. Result := GetLastError;
  144. end;
  145. Procedure FindClose (Var F : TSearchrec);
  146. begin
  147. if F.FindHandle <> INVALID_HANDLE_VALUE then
  148. Windows.FindClose(F.FindHandle);
  149. end;
  150. Function FileGetDate (Handle : Longint) : Longint;
  151. Var FT : TFileTime;
  152. begin
  153. If GetFileTime(Handle,nil,nil,@ft) and
  154. WinToDosTime(FT,Result) then exit;
  155. Result:=-1;
  156. end;
  157. Function FileSetDate (Handle,Age : Longint) : Longint;
  158. Var FT: TFileTime;
  159. begin
  160. Result := 0;
  161. if DosToWinTime(Age,FT) and
  162. SetFileTime(Handle, ft, ft, FT) then Exit;
  163. Result := GetLastError;
  164. end;
  165. Function FileGetAttr (Const FileName : String) : Longint;
  166. begin
  167. Result:=GetFileAttributes(PChar(FileName));
  168. end;
  169. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  170. begin
  171. if not SetFileAttributes(PChar(FileName), Attr) then
  172. Result := GetLastError
  173. else
  174. Result:=0;
  175. end;
  176. Function DeleteFile (Const FileName : String) : Boolean;
  177. begin
  178. DeleteFile:=Windows.DeleteFile(Pchar(FileName));
  179. end;
  180. Function RenameFile (Const OldName, NewName : String) : Boolean;
  181. begin
  182. Result := MoveFile(PChar(OldName), PChar(NewName));
  183. end;
  184. Function FileSearch (Const Name, DirList : String) : String;
  185. Var I : longint;
  186. Temp : String;
  187. begin
  188. Result:='';
  189. temp:=Dirlist;
  190. repeat
  191. I:=pos(';',Temp);
  192. If I<>0 then
  193. begin
  194. Result:=Copy (Temp,1,i-1);
  195. system.Delete(Temp,1,I);
  196. end
  197. else
  198. begin
  199. Result:=Temp;
  200. Temp:='';
  201. end;
  202. If result[length(result)]<>'\' then
  203. Result:=Result+'\';
  204. Result:=Result+name;
  205. If not FileExists(Result) Then
  206. Result:='';
  207. until (length(temp)=0) or (length(result)<>0);
  208. end;
  209. Procedure GetLocalTime(var ST: TSystemTime);
  210. Var Syst:Systemtime;
  211. begin
  212. windows.Getlocaltime(@syst);
  213. st.year:=syst.wYear;
  214. st.month:=syst.wMonth;
  215. st.day:=syst.wDay;
  216. st.hour:=syst.wHour;
  217. st.minute:=syst.wMinute;
  218. st.second:=syst.wSecond;
  219. st.millisecond:=syst.wMilliSeconds;
  220. end;
  221. Procedure InitAnsi;
  222. Var i : longint;
  223. begin
  224. { Fill table entries 0 to 127 }
  225. for i := 0 to 96 do
  226. UpperCaseTable[i] := chr(i);
  227. for i := 97 to 122 do
  228. UpperCaseTable[i] := chr(i - 32);
  229. for i := 123 to 191 do
  230. UpperCaseTable[i] := chr(i);
  231. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  232. for i := 0 to 64 do
  233. LowerCaseTable[i] := chr(i);
  234. for i := 65 to 90 do
  235. LowerCaseTable[i] := chr(i + 32);
  236. for i := 91 to 191 do
  237. LowerCaseTable[i] := chr(i);
  238. Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  239. end;
  240. function GetLocaleStr(LID, LT: Longint; const Def: string): ShortString;
  241. var
  242. L: Integer;
  243. Buf: array[0..255] of Char;
  244. begin
  245. L := GetLocaleInfo(LID, LT, Buf, SizeOf(Buf));
  246. if L > 0 then
  247. SetString(Result, @Buf[0], L - 1)
  248. else
  249. Result := Def;
  250. end;
  251. function GetLocaleChar(LID, LT: Longint; Def: Char): Char;
  252. var
  253. Buf: array[0..1] of Char;
  254. begin
  255. if GetLocaleInfo(LID, LT, Buf, 2) > 0 then
  256. Result := Buf[0]
  257. else
  258. Result := Def;
  259. end;
  260. Function GetLocaleInt(LID,TP,Def: LongInt): LongInt;
  261. Var
  262. S: String;
  263. C: Integer;
  264. Begin
  265. S:=GetLocaleStr(LID,TP,'0');
  266. Val(S,Result,C);
  267. If C<>0 Then
  268. Result:=Def;
  269. End;
  270. procedure GetFormatSettings;
  271. var
  272. HF : Shortstring;
  273. LID : LCID;
  274. I,Day,DateOrder : longint;
  275. begin
  276. LID := GetThreadLocale;
  277. { Date stuff }
  278. for I := 1 to 12 do
  279. begin
  280. ShortMonthNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVMONTHNAME1+I-1,ShortMonthNames[i]);
  281. LongMonthNames[I]:=GetLocaleStr(LID,LOCALE_SMONTHNAME1+I-1,LongMonthNames[i]);
  282. end;
  283. for I := 1 to 7 do
  284. begin
  285. Day := (I + 5) mod 7;
  286. ShortDayNames[I]:=GetLocaleStr(LID,LOCALE_SABBREVDAYNAME1+Day,ShortDayNames[i]);
  287. LongDayNames[I]:=GetLocaleStr(LID,LOCALE_SDAYNAME1+Day,LongDayNames[i]);
  288. end;
  289. DateSeparator := GetLocaleChar(LID, LOCALE_SDATE, '/');
  290. DateOrder := GetLocaleInt(LID, LOCALE_IDate, 0);
  291. Case DateOrder Of
  292. 1: Begin
  293. ShortDateFormat := 'dd/mm/yyyy';
  294. LongDateFormat := 'dddd, d. mmmm yyyy';
  295. End;
  296. 2: Begin
  297. ShortDateFormat := 'yyyy/mm/dd';
  298. LongDateFormat := 'dddd, yyyy mmmm d.';
  299. End;
  300. else
  301. // Default american settings...
  302. ShortDateFormat := 'mm/dd/yyyy';
  303. LongDateFormat := 'dddd, mmmm d. yyyy';
  304. End;
  305. { Time stuff }
  306. TimeSeparator := GetLocaleChar(LID, LOCALE_STIME, ':');
  307. TimeAMString := GetLocaleStr(LID, LOCALE_S1159, 'AM');
  308. TimePMString := GetLocaleStr(LID, LOCALE_S2359, 'PM');
  309. if StrToIntDef(GetLocaleStr(LID, LOCALE_ITLZERO, '0'), 0) = 0 then
  310. HF:='h'
  311. else
  312. HF:='hh';
  313. // No support for 12 hour stuff at the moment...
  314. ShortTimeFormat := HF+':mm';
  315. LongTimeFormat := HF + ':mm:ss';
  316. { Currency stuff }
  317. CurrencyString:=GetLocaleStr(LID, LOCALE_SCURRENCY, '');
  318. CurrencyFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRENCY, '0'), 0);
  319. NegCurrFormat:=StrToIntDef(GetLocaleStr(LID, LOCALE_INEGCURR, '0'), 0);
  320. { Number stuff }
  321. ThousandSeparator:=GetLocaleChar(LID, LOCALE_STHOUSAND, ',');
  322. DecimalSeparator:=GetLocaleChar(LID, LOCALE_SDECIMAL, '.');
  323. CurrencyDecimals:=StrToIntDef(GetLocaleStr(LID, LOCALE_ICURRDIGITS, '0'), 0);
  324. end;
  325. Procedure InitInternational;
  326. {
  327. called by sysutils initialization routines to set up
  328. internationalization support.
  329. }
  330. begin
  331. InitAnsi;
  332. GetFormatSettings;
  333. end;
  334. {
  335. $Log$
  336. Revision 1.1 2000-07-13 06:31:20 michael
  337. + Initial import
  338. Revision 1.17 2000/06/04 14:16:54 hajny
  339. * parameter name change in FileSeek
  340. Revision 1.16 2000/05/15 19:28:41 peter
  341. * int64 support for diskfree,disksize
  342. Revision 1.15 2000/02/17 22:16:05 sg
  343. * Changed the second argument of FileWrite from "var buffer" to
  344. "const buffer", like in Delphi.
  345. Revision 1.14 2000/02/09 16:59:34 peter
  346. * truncated log
  347. Revision 1.13 2000/01/16 22:25:38 peter
  348. * check handle for file closing
  349. Revision 1.12 2000/01/07 16:41:52 daniel
  350. * copyright 2000
  351. Revision 1.11 1999/08/18 08:38:42 michael
  352. * Fixed bug 533, in findmatch
  353. }