filutil.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2012 by the Free Pascal development team
  4. File utility calls
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifndef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  12. Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
  13. begin
  14. Result:=FileOpen(ToSingleByteFileSystemEncodedFileName(FileName),Mode);
  15. end;
  16. Function FileCreate (Const FileName : UnicodeString) : THandle;
  17. begin
  18. Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName));
  19. end;
  20. Function FileCreate (Const FileName : UnicodeString; Rights : Integer) : THandle;
  21. begin
  22. Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),Rights);
  23. end;
  24. Function FileCreate (Const FileName : UnicodeString; ShareMode : Integer; Rights : Integer) : THandle;
  25. begin
  26. Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),ShareMode,Rights);
  27. end;
  28. Function FileAge (Const FileName : UnicodeString): Int64;
  29. begin
  30. Result:=FileAge(ToSingleByteFileSystemEncodedFileName(FileName));
  31. end;
  32. Function FileExists (Const FileName : UnicodeString; FollowLink : Boolean) : Boolean;
  33. begin
  34. Result:=FileExists(ToSingleByteFileSystemEncodedFileName(FileName), FollowLink);
  35. end;
  36. Function DirectoryExists (Const Directory : UnicodeString; FollowLink : Boolean) : Boolean;
  37. begin
  38. Result:=DirectoryExists(ToSingleByteFileSystemEncodedFileName(Directory), FollowLink);
  39. end;
  40. Function FileGetAttr (Const FileName : UnicodeString) : Longint;
  41. begin
  42. Result:=FileGetAttr(ToSingleByteFileSystemEncodedFileName(FileName));
  43. end;
  44. Function FileSetAttr (Const Filename : UnicodeString; Attr: longint) : Longint;
  45. begin
  46. Result:=FileSetAttr(ToSingleByteFileSystemEncodedFileName(FileName),Attr);
  47. end;
  48. Function DeleteFile (Const FileName : UnicodeString) : Boolean;
  49. begin
  50. Result:=DeleteFile(ToSingleByteFileSystemEncodedFileName(FileName));
  51. end;
  52. Function RenameFile (Const OldName, NewName : UnicodeString) : Boolean;
  53. begin
  54. Result:=RenameFile(ToSingleByteFileSystemEncodedFileName(OldName),
  55. ToSingleByteFileSystemEncodedFileName(NewName));
  56. end;
  57. {$ifdef OS_FILEISREADONLY}
  58. Function FileIsReadOnly(const FileName: UnicodeString): Boolean;
  59. begin
  60. Result:=FileIsReadOnly(ToSingleByteFileSystemEncodedFileName(FileName));
  61. end;
  62. {$endif}
  63. {$ifdef OS_FILESETDATEBYNAME}
  64. Function FileSetDate (Const FileName : UnicodeString;Age : Int64) : Longint;
  65. begin
  66. Result:=FileSetDate(ToSingleByteFileSystemEncodedFileName(FileName),Age);
  67. end;
  68. {$endif}
  69. function FileAge(const FileName: RawByteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
  70. Var
  71. Info : TRawByteSearchRec;
  72. A : Integer;
  73. begin
  74. for A:=1 to Length(FileName) do
  75. if CharInSet(FileName[A],['?','*']) then
  76. Exit(False);
  77. A:=0;
  78. if not FollowLink then
  79. A:=A or faSymLink;
  80. Result:=FindFirst(FileName,A,Info)=0;
  81. if Result then
  82. begin
  83. FileDateTime:=FileDatetoDateTime(Info.Time);
  84. FindClose(Info);
  85. end;
  86. end;
  87. function FileAgeUTC(const FileName: RawByteString; out FileDateTimeUTC: TDateTime; FollowLink: Boolean = True): Boolean;
  88. Var
  89. Info : TRawByteSearchRec;
  90. A : Integer;
  91. begin
  92. for A:=1 to Length(FileName) do
  93. if CharInSet(FileName[A],['?','*']) then
  94. Exit(False);
  95. A:=0;
  96. if not FollowLink then
  97. A:=A or faSymLink;
  98. Result:=FindFirst(FileName,A,Info)=0;
  99. if Result then
  100. begin
  101. FileDateTimeUTC:=FileDateToUniversal(Info.Time);
  102. FindClose(Info);
  103. end;
  104. end;
  105. Function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
  106. begin
  107. Result:=FileAge(ToSingleByteFileSystemEncodedFileName(FileName),FileDateTime,FollowLink);
  108. end;
  109. Function FileAgeUTC(const FileName: UnicodeString; out FileDateTimeUTC: TDateTime; FollowLink: Boolean = True): Boolean;
  110. begin
  111. Result:=FileAgeUTC(ToSingleByteFileSystemEncodedFileName(FileName),FileDateTimeUTC,FollowLink);
  112. end;
  113. function FileGetSymLinkTarget(const FileName: UnicodeString; out SymLinkRec: TUnicodeSymLinkRec): Boolean;
  114. var
  115. sr: TRawbyteSymLinkRec;
  116. begin
  117. Result := FileGetSymLinkTarget(ToSingleByteFileSystemEncodedFileName(FileName), sr);
  118. if Result then
  119. begin
  120. SymLinkRec.TargetName := UnicodeString(sr.TargetName);
  121. SymLinkRec.Size := sr.Size;
  122. SymLinkRec.Attr := sr.Attr;
  123. {$ifdef SYMLINKREC_USEFINDDATA}
  124. SymLinkRec.FindData := sr.FindData;
  125. {$endif}
  126. {$ifdef unix}
  127. SymLinkRec.Mode := sr.Mode;
  128. {$endif}
  129. end;
  130. end;
  131. Function FileSearch (Const Name, DirList : UnicodeString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : UnicodeString;
  132. begin
  133. Result:=UnicodeString(FileSearch(ToSingleByteFileSystemEncodedFileName(Name),
  134. ToSingleByteFileSystemEncodedFileName(Dirlist),Options));
  135. end;
  136. Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : Boolean) : UnicodeString;
  137. begin
  138. Result:=UnicodeString(FileSearch(ToSingleByteFileSystemEncodedFileName(Name),
  139. ToSingleByteFileSystemEncodedFileName(DirList),ImplicitCurrentDir));
  140. end;
  141. Function ExeSearch (Const Name : UnicodeString; Const DirList : UnicodeString ='' ) : UnicodeString;
  142. begin
  143. Result:=UnicodeString(ExeSearch(ToSingleByteFileSystemEncodedFileName(Name),
  144. ToSingleByteFileSystemEncodedFileName(Dirlist)));
  145. end;
  146. Function FileSearch (Const Name, DirList : RawByteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
  147. Var
  148. I : longint;
  149. Temp : RawByteString;
  150. begin
  151. Result:=Name;
  152. temp:=SetDirSeparators(DirList);
  153. // Start with checking the file in the current directory
  154. If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
  155. exit;
  156. while True do begin
  157. If Temp = '' then
  158. Break; // No more directories to search - fail
  159. I:=pos(PathSeparator,Temp);
  160. If I<>0 then
  161. begin
  162. Result:=Copy (Temp,1,i-1);
  163. system.Delete(Temp,1,I);
  164. end
  165. else
  166. begin
  167. Result:=Temp;
  168. Temp:='';
  169. end;
  170. If Result<>'' then
  171. begin
  172. If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
  173. Result:=Copy(Result,2,Length(Result)-2);
  174. if (Result<>'') then
  175. Result:=IncludeTrailingPathDelimiter(Result)+name;
  176. end;
  177. If (Result <> '') and FileExists(Result) Then
  178. exit;
  179. end;
  180. Result:='';
  181. end;
  182. Function FileSearch (Const Name, DirList : RawByteString; ImplicitCurrentDir : Boolean) : RawByteString;
  183. begin
  184. if ImplicitCurrentDir then
  185. Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
  186. else
  187. Result:=FileSearch(Name,DirList,[]);
  188. end;
  189. Function ExeSearch (Const Name : RawByteString; Const DirList : RawByteString ='' ) : RawByteString;
  190. Var
  191. D : RawByteString;
  192. O : TFileSearchOptions;
  193. begin
  194. D:=DirList;
  195. if (D='') then
  196. D:=GetEnvironmentVariable('PATH');
  197. {$ifdef unix}
  198. O:=[];
  199. {$else unix}
  200. O:=[sfoImplicitCurrentDir,sfoStripQuotes];
  201. {$endif unix}
  202. Result := FileSearch(Name, D, O);
  203. end;
  204. {$endif}
  205. {$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  206. Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
  207. begin
  208. Result:=FileOpen(UnicodeString(FileName),Mode);
  209. end;
  210. Function FileCreate (Const FileName : RawByteString) : THandle;
  211. begin
  212. Result:=FileCreate(UnicodeString(FileName));
  213. end;
  214. Function FileCreate (Const FileName : RawByteString; Rights : Integer) : THandle;
  215. begin
  216. Result:=FileCreate(UnicodeString(FileName),Rights);
  217. end;
  218. Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
  219. begin
  220. Result:=FileCreate(UnicodeString(FileName),ShareMode,Rights);
  221. end;
  222. Function FileAge (Const FileName : RawByteString): Int64;
  223. begin
  224. Result:=FileAge(UnicodeString(FileName));
  225. end;
  226. Function FileExists (Const FileName : RawByteString; FollowLink : Boolean) : Boolean;
  227. begin
  228. Result:=FileExists(UnicodeString(FileName), FollowLink);
  229. end;
  230. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  231. begin
  232. Result:=DirectoryExists(UnicodeString(Directory), FollowLink);
  233. end;
  234. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  235. begin
  236. Result:=FileGetAttr(unicodestring(FileName));
  237. end;
  238. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  239. begin
  240. Result:=FileSetAttr(unicodestring(FileName),Attr);
  241. end;
  242. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  243. begin
  244. Result:=DeleteFile(UnicodeString(FileName));
  245. end;
  246. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  247. begin
  248. Result:=RenameFile(UnicodeString(OldName),UnicodeString(NewName));
  249. end;
  250. {$ifdef OS_FILEISREADONLY}
  251. Function FileIsReadOnly(const FileName: RawByteString): Boolean;
  252. begin
  253. Result:=FileIsReadOnly(UnicodeString(FileName));
  254. end;
  255. {$endif}
  256. {$ifdef OS_FILESETDATEBYNAME}
  257. Function FileSetDate (Const FileName : RawByteString;Age : Int64) : Longint;
  258. begin
  259. Result:=FileSetDate(UnicodeString(FileName),Age);
  260. end;
  261. {$endif}
  262. Function FileSetDate (Const FileName : RawByteString;const FileDateTime : TDateTime) : Longint;
  263. begin
  264. Result:=FileSetDate(UnicodeString(FileName),FileDateTime);
  265. end;
  266. Function FileSetDateUTC (Const FileName : RawByteString;const FileDateTimeUTC : TDateTime) : Longint;
  267. begin
  268. Result:=FileSetDateUTC(UnicodeString(FileName),FileDateTimeUTC);
  269. end;
  270. function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
  271. Var
  272. Info : TUnicodeSearchRec;
  273. A : Integer;
  274. begin
  275. for A:=1 to Length(FileName) do
  276. if CharInSet(FileName[A],['?','*']) then
  277. Exit(False);
  278. A:=0;
  279. if not FollowLink then
  280. A:=A or faSymLink;
  281. Result:=FindFirst(FileName,A,Info)=0;
  282. if Result then
  283. begin
  284. FileDateTime:=FileDatetoDateTime(Info.Time);
  285. FindClose(Info);
  286. end;
  287. end;
  288. function FileAgeUTC(const FileName: UnicodeString; out FileDateTimeUTC: TDateTime; FollowLink: Boolean = True): Boolean;
  289. Var
  290. Info : TUnicodeSearchRec;
  291. A : Integer;
  292. begin
  293. for A:=1 to Length(FileName) do
  294. if CharInSet(FileName[A],['?','*']) then
  295. Exit(False);
  296. A:=0;
  297. if not FollowLink then
  298. A:=A or faSymLink;
  299. Result:=FindFirst(FileName,A,Info)=0;
  300. if Result then
  301. begin
  302. FileDateTimeUTC:=Info.TimeStampUTC;
  303. FindClose(Info);
  304. end;
  305. end;
  306. Function FileAge(const FileName: RawbyteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
  307. begin
  308. Result:=FileAge(UnicodeString(FileName),FileDateTime,FollowLink);
  309. end;
  310. function FileAgeUTC(const FileName: RawByteString; out FileDateTimeUTC: TDateTime; FollowLink: Boolean = True): Boolean;
  311. begin
  312. Result:=FileAgeUTC(UnicodeString(FileName),FileDateTimeUTC,FollowLink);
  313. end;
  314. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  315. var
  316. sr: TUnicodeSymLinkRec;
  317. begin
  318. Result := FileGetSymLinkTarget(UnicodeString(FileName), sr);
  319. if Result then
  320. begin
  321. SymLinkRec.TargetName := ToSingleByteFileSystemEncodedFileName(sr.TargetName);
  322. SymLinkRec.Size := sr.Size;
  323. SymLinkRec.Attr := sr.Attr;
  324. {$ifdef SYMLINKREC_USEFINDDATA}
  325. SymLinkRec.FindData := sr.FindData;
  326. {$endif}
  327. {$ifdef unix}
  328. SymLinkRec.Mode := sr.Mode;
  329. {$endif}
  330. end;
  331. end;
  332. Function FileSearch (Const Name, DirList : UnicodeString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : UnicodeString;
  333. Var
  334. I : longint;
  335. Temp : UnicodeString;
  336. begin
  337. Result:=Name;
  338. temp:=SetDirSeparators(DirList);
  339. // Start with checking the file in the current directory
  340. If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
  341. exit;
  342. while True do begin
  343. If Temp = '' then
  344. Break; // No more directories to search - fail
  345. I:=pos(PathSeparator,Temp);
  346. If I<>0 then
  347. begin
  348. Result:=Copy (Temp,1,i-1);
  349. system.Delete(Temp,1,I);
  350. end
  351. else
  352. begin
  353. Result:=Temp;
  354. Temp:='';
  355. end;
  356. If Result<>'' then
  357. begin
  358. If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
  359. Result:=Copy(Result,2,Length(Result)-2);
  360. if (Result<>'') then
  361. Result:=IncludeTrailingPathDelimiter(Result)+name;
  362. end;
  363. If (Result <> '') and FileExists(Result) Then
  364. exit;
  365. end;
  366. Result:='';
  367. end;
  368. Function FileSearch (Const Name, DirList : RawbyteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
  369. begin
  370. Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),options));
  371. end;
  372. Function FileSearch (Const Name, DirList : RawbyteString; ImplicitCurrentDir : Boolean) : RawByteString;
  373. begin
  374. Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),ImplicitCurrentDir));
  375. end;
  376. Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : Boolean) : UnicodeString;
  377. begin
  378. if ImplicitCurrentDir then
  379. Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
  380. else
  381. Result:=FileSearch(Name,DirList,[]);
  382. end;
  383. Function ExeSearch (Const Name : UnicodeString; Const DirList : UnicodeString ='' ) : UnicodeString;
  384. Var
  385. D : UnicodeString;
  386. O : TFileSearchOptions;
  387. begin
  388. D:=DirList;
  389. if (D='') then
  390. D:=UnicodeString(GetEnvironmentVariable('PATH'));
  391. {$ifdef unix}
  392. O:=[];
  393. {$else unix}
  394. O:=[sfoImplicitCurrentDir,sfoStripQuotes];
  395. {$endif unix}
  396. Result := FileSearch(Name, D, O);
  397. end;
  398. Function ExeSearch (Const Name : RawbyteString; Const DirList : RawbyteString ='' ) : RawByteString;
  399. begin
  400. Result:=ToSingleByteFileSystemEncodedFileName(ExeSearch(unicodestring(name),unicodestring(dirlist)));
  401. end;
  402. {$endif}
  403. function FileGetSymLinkTarget(const FileName: UnicodeString; out TargetName: UnicodeString): Boolean;
  404. var
  405. sr: TUnicodeSymLinkRec;
  406. begin
  407. Result := FileGetSymLinkTarget(FileName, sr);
  408. if Result then
  409. TargetName := sr.TargetName;
  410. end;
  411. function FileGetSymLinkTarget(const FileName: RawByteString; out TargetName: RawByteString): Boolean;
  412. var
  413. sr: TRawbyteSymLinkRec;
  414. begin
  415. Result := FileGetSymLinkTarget(FileName, sr);
  416. if Result then
  417. TargetName := sr.TargetName;
  418. end;
  419. Function GetFileHandle(var f : File):THandle;
  420. begin
  421. Result:=filerec(f).handle;
  422. end;
  423. Function GetFileHandle(var f : Text):THandle;
  424. begin
  425. Result:=textrec(f).handle;
  426. end;
  427. { FindFirst/FindNext. In order to avoid having to duplicate most code in th
  428. OS-specific implementations, we let those implementations fill in all
  429. fields of TRawbyte/UnicodeSearchRec, except for the name. That field is
  430. filled in by the OS-indepedent wrappers, which also takes care of setting
  431. the appropriate code page if applicable.
  432. }
  433. type
  434. TAbstractSearchRec = Record
  435. Time : Int64;
  436. Size : Int64;
  437. Attr : Longint;
  438. { this will be assigned by the generic code; it is actually either a
  439. rawbytestring or unicodestring; keep it a reference-counted type
  440. so that -gt doesn't overwrite it, the field name should be
  441. indication enough that you should not touch it }
  442. Name_do_not_touch : RawByteString;
  443. ExcludeAttr : Longint;
  444. FindHandle : {$ifdef FINDHANDLE_IS_POINTER}Pointer{$else}THandle{$endif};
  445. {$ifdef unix}
  446. Mode : TMode;
  447. {$endif unix}
  448. {$ifdef SEARCHREC_USEFINDDATA}
  449. FindData : TFindData;
  450. {$endif}
  451. end;
  452. {$ifdef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  453. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint; forward;
  454. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint; forward;
  455. {$endif SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  456. {$ifdef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  457. Function InternalFindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: UnicodeString) : Longint; forward;
  458. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : UnicodeString) : Longint; forward;
  459. {$endif SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  460. procedure InternalFindClose(var Handle: {$ifdef FINDHANDLE_IS_POINTER}Pointer{$else}THandle{$endif}{$ifdef SEARCHREC_USEFINDDATA};var FindData: TFindData{$endif}); forward;
  461. {$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  462. Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
  463. var
  464. Name: UnicodeString;
  465. begin
  466. Result:=InternalFindFirst(UnicodeString(Path),Attr,TAbstractSearchRec(Rslt),Name);
  467. if Result=0 then
  468. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Name),Rslt.Name,DefaultRTLFileSystemCodePage,length(Name));
  469. end;
  470. Function FindNext (Var Rslt : TRawByteSearchRec) : Longint;
  471. var
  472. Name: UnicodeString;
  473. begin
  474. Result:=InternalFindNext(TAbstractSearchRec(Rslt),Name);
  475. if Result=0 then
  476. widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Name),Rslt.Name,DefaultRTLFileSystemCodePage,length(Name));
  477. end;
  478. {$else not SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  479. Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
  480. begin
  481. Result:=InternalFindFirst(Path,Attr,TAbstractSearchRec(Rslt),Rslt.Name);
  482. if Result=0 then
  483. SetCodePage(Rslt.Name,DefaultRTLFileSystemCodePage);
  484. end;
  485. Function FindNext (Var Rslt : TRawByteSearchRec) : Longint;
  486. begin
  487. Result:=InternalFindNext(TAbstractSearchRec(Rslt),Rslt.Name);
  488. if Result=0 then
  489. SetCodePage(Rslt.Name,DefaultRTLFileSystemCodePage);
  490. end;
  491. {$endif not SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  492. {$ifndef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  493. Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
  494. var
  495. Name: RawByteString;
  496. begin
  497. Result:=InternalFindFirst(ToSingleByteFileSystemEncodedFileName(Path),Attr,TAbstractSearchRec(Rslt),Name);
  498. if Result=0 then
  499. Rslt.Name:=UnicodeString(Name);
  500. end;
  501. Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
  502. var
  503. Name: RawByteString;
  504. begin
  505. Result:=InternalFindNext(TAbstractSearchRec(Rslt),Name);
  506. if Result=0 then
  507. Rslt.Name:=UnicodeString(Name);
  508. end;
  509. {$else not SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  510. Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
  511. begin
  512. Result:=InternalFindFirst(Path,Attr,TAbstractSearchRec(Rslt),Rslt.Name);
  513. end;
  514. Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
  515. begin
  516. Result:=InternalFindNext(TAbstractSearchRec(Rslt),Rslt.Name);
  517. end;
  518. {$endif not SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
  519. Procedure FindClose(Var f: TRawByteSearchRec);
  520. begin
  521. InternalFindClose(f.FindHandle{$ifdef SEARCHREC_USEFINDDATA},f.FindData{$endif});
  522. end;
  523. Procedure FindClose(Var f: TUnicodeSearchRec);
  524. begin
  525. InternalFindClose(f.FindHandle{$ifdef SEARCHREC_USEFINDDATA},f.FindData{$endif});
  526. end;
  527. {$if defined(windows) and not defined(win16)}
  528. function FindDataTimeToDateTime(constref aFileTime: FILETIME; out aResult: TDateTime): Boolean;
  529. var
  530. st, lt: TSystemTime;
  531. begin
  532. Result := FileTimeToSystemTime(aFileTime, st);
  533. if Result then begin
  534. {$ifndef wince}
  535. if SystemTimeToTzSpecificLocalTime(Nil, st, lt) then
  536. aResult := SystemTimeToDateTime(lt)
  537. else
  538. {$endif}
  539. aResult := SystemTimeToDateTime(st);
  540. end;
  541. end;
  542. function FindDataTimeToUTC(constref aFileTime: FILETIME; out aResult: TDateTime): Boolean;
  543. var
  544. st: TSystemTime;
  545. begin
  546. Result := FileTimeToSystemTime(aFileTime, st);
  547. if Result then
  548. aResult := SystemTimeToDateTime(st);
  549. end;
  550. {$endif}
  551. { TUnicodeSearchRec }
  552. function TUnicodeSearchRec.GetTimeStamp: TDateTime;
  553. begin
  554. {$if declared(FindDataTimeToDateTime)}
  555. if not FindDataTimeToDateTime(FindData.ftLastWriteTime, Result) then
  556. {$endif}
  557. Result := FileDateToDateTime(Time);
  558. end;
  559. function TUnicodeSearchRec.IsDirectory: Boolean;
  560. begin
  561. Result:=(Attr and faDirectory)<>0;
  562. end;
  563. function TUnicodeSearchRec.IsCurrentOrParentDir: Boolean;
  564. begin
  565. Result:=IsDirectory and ((Name='.') or (Name='..'));
  566. end;
  567. function TUnicodeSearchRec.GetTimeStampUTC: TDateTime;
  568. begin
  569. {$if declared(FindDataTimeToUTC)}
  570. if not FindDataTimeToUTC(FindData.ftLastWriteTime, Result) then
  571. {$endif}
  572. Result := FileDateToUniversal(Time);
  573. end;
  574. { TRawbyteSearchRec }
  575. function TRawbyteSearchRec.GetTimeStamp: TDateTime;
  576. begin
  577. {$if declared(FindDataTimeToDateTime)}
  578. if not FindDataTimeToDateTime(FindData.ftLastWriteTime, Result) then
  579. {$endif}
  580. Result := FileDateToDateTime(Time);
  581. end;
  582. function TRawbyteSearchRec.IsDirectory: Boolean;
  583. begin
  584. Result:=(Attr and faDirectory)<>0;
  585. end;
  586. function TRawbyteSearchRec.IsCurrentOrParentDir: Boolean;
  587. begin
  588. Result:=IsDirectory and ((Name='.') or (Name='..'));
  589. end;
  590. function TRawbyteSearchRec.GetTimeStampUTC: TDateTime;
  591. begin
  592. {$if declared(FindDataTimeToDateTime)}
  593. if not FindDataTimeToUTC(FindData.ftLastWriteTime, Result) then
  594. {$endif}
  595. Result := FileDateToUniversal(Time);
  596. end;
  597. { TUnicodeSymLinkRec }
  598. function TUnicodeSymLinkRec.GetTimeStamp: TDateTime;
  599. begin
  600. {$if declared(FindDataTimeToDateTime)}
  601. if not FindDataTimeToDateTime(FindData.ftLastWriteTime, Result) then
  602. {$endif}
  603. Result := 0;
  604. end;
  605. { TRawbyteSymLinkRec }
  606. function TRawbyteSymLinkRec.GetTimeStamp: TDateTime;
  607. begin
  608. {$if declared(FindDataTimeToDateTime)}
  609. if not FindDataTimeToDateTime(FindData.ftLastWriteTime, Result) then
  610. {$endif}
  611. Result := 0;
  612. end;
  613. {$ifndef SYSUTILS_HAS_FILEFLUSH_IMPL}
  614. function FileFlush(Handle: THandle): Boolean;
  615. begin
  616. Result:= False;
  617. end;
  618. {$endif}
  619. Function GetFileContents(Const aFileName : RawByteString) : TBytes;
  620. Var
  621. H : Thandle;
  622. begin
  623. H:=FileOpen(aFileName,fmOpenRead or fmShareDenyWrite);
  624. if H<0 then
  625. Raise EFileNotFoundException.Create(SFileNotFound);
  626. try
  627. Result:=GetFileContents(H);
  628. finally
  629. FileClose(H);
  630. end;
  631. end;
  632. Function GetFileContents(Const aFileName : UnicodeString) : TBytes;
  633. Var
  634. H : Thandle;
  635. begin
  636. H:=FileOpen(aFileName,fmOpenRead or fmShareDenyWrite);
  637. if H<0 then
  638. Raise EFileNotFoundException.Create(SFileNotFound);
  639. try
  640. Result:=GetFileContents(H);
  641. finally
  642. FileClose(H);
  643. end;
  644. end;
  645. Function GetFileContents(Const aHandle : THandle) : TBytes;
  646. Var
  647. aLen,aOffset,aRead : Int64;
  648. aBuf : PByte;
  649. begin
  650. aLen:=FileSeek(aHandle,0,fsFromEnd);
  651. FileSeek(aHandle,0,fsFromBeginning);
  652. SetLength(Result,aLen);
  653. aOffset:=0;
  654. Repeat
  655. aBuf:=@Result[aOffset];
  656. aRead:=FileRead(aHandle,aBuf^,aLen-aOffset);
  657. aOffset:=aOffset+aRead;
  658. Until (aOffset>=aLen) or (aRead<=0);
  659. if aRead<0 then
  660. RaiseLastOSError;
  661. end;
  662. Function GetFileAsString(Const aFileName : RawByteString; aEncoding : TEncoding) : RawByteString;
  663. begin
  664. Result:=aEncoding.GetAnsiString(GetFileContents(aFileName));
  665. end;
  666. Function GetFileAsString(Const aFileName : RawByteString) : RawByteString;
  667. begin
  668. Result:=GetFileAsString(aFileName,TEncoding.SystemEncoding);
  669. end;
  670. Function GetFileAsString(Const aFileName : UnicodeString) : UnicodeString;
  671. begin
  672. Result:=GetFileAsString(aFileName, TEncoding.Unicode);
  673. end;
  674. Function GetFileAsString(Const aFileName : UnicodeString; aEncoding : TEncoding) : UnicodeString;
  675. begin
  676. Result:=aEncoding.GetString(GetFileContents(aFileName))
  677. end;
  678. {$IFNDEF HAS_FILEDATETIME}
  679. Function FileGetDate (Handle : THandle; out FileDateTime: TDateTime) : Boolean;
  680. Var
  681. aDate : Int64;
  682. begin
  683. aDate:=FileGetDate(Handle);
  684. Result:=aDate>=0;
  685. if Result then
  686. FileDateTime:=FileDateToDateTime(aDate);
  687. end;
  688. Function FileGetDateUTC (Handle : THandle; out FileDateTimeUTC: TDateTime) : Boolean;
  689. Var
  690. aDate : Int64;
  691. begin
  692. aDate:=FileGetDate(Handle);
  693. Result:=aDate>=0;
  694. if Result then
  695. FileDateTimeUTC:=FileDateToUniversal(aDate);
  696. end;
  697. Function FileSetDate (Handle : THandle; const FileDateTime: TDateTime) : Longint;
  698. begin
  699. Result := FileSetDate(Handle,DateTimeToFileDate(FileDateTime));
  700. end;
  701. Function FileSetDateUTC (Handle : THandle; const FileDateTimeUTC: TDateTime) : Longint;
  702. begin
  703. Result := FileSetDate(Handle,UniversalToFileDate(FileDateTimeUTC));
  704. end;
  705. Function FileSetDate (Const FileName : UnicodeString;const FileDateTime : TDateTime) : Longint;
  706. begin
  707. Result:=FileSetDate(FileName,DateTimeToFileDate(FileDateTime));
  708. end;
  709. Function FileSetDateUTC (Const FileName : UnicodeString;const FileDateTimeUTC : TDateTime) : Longint;
  710. begin
  711. Result:=FileSetDate(FileName,UniversalToFileDate(FileDateTimeUTC));
  712. end;
  713. {$ENDIF}