filutil.inc 10 KB

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