filutil.inc 10 KB

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