1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2012 by the Free Pascal development team
File utility calls
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$ifndef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
Function FileOpen (Const FileName : unicodestring; Mode : Integer) : THandle;
begin
Result:=FileOpen(ToSingleByteFileSystemEncodedFileName(FileName),Mode);
end;
Function FileCreate (Const FileName : UnicodeString) : THandle;
begin
Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName));
end;
Function FileCreate (Const FileName : UnicodeString; Rights : Integer) : THandle;
begin
Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),Rights);
end;
Function FileCreate (Const FileName : UnicodeString; ShareMode : Integer; Rights : Integer) : THandle;
begin
Result:=FileCreate(ToSingleByteFileSystemEncodedFileName(FileName),ShareMode,Rights);
end;
Function FileAge (Const FileName : UnicodeString): Longint;
begin
Result:=FileAge(ToSingleByteFileSystemEncodedFileName(FileName));
end;
Function FileExists (Const FileName : UnicodeString) : Boolean;
begin
Result:=FileExists(ToSingleByteFileSystemEncodedFileName(FileName));
end;
Function DirectoryExists (Const Directory : UnicodeString) : Boolean;
begin
Result:=DirectoryExists(ToSingleByteFileSystemEncodedFileName(Directory));
end;
Function FileGetAttr (Const FileName : UnicodeString) : Longint;
begin
Result:=FileGetAttr(ToSingleByteFileSystemEncodedFileName(FileName));
end;
Function FileSetAttr (Const Filename : UnicodeString; Attr: longint) : Longint;
begin
Result:=FileSetAttr(ToSingleByteFileSystemEncodedFileName(FileName),Attr);
end;
Function DeleteFile (Const FileName : UnicodeString) : Boolean;
begin
Result:=DeleteFile(ToSingleByteFileSystemEncodedFileName(FileName));
end;
Function RenameFile (Const OldName, NewName : UnicodeString) : Boolean;
begin
Result:=RenameFile(ToSingleByteFileSystemEncodedFileName(OldName),
ToSingleByteFileSystemEncodedFileName(NewName));
end;
{$ifdef OS_FILEISREADONLY}
Function FileIsReadOnly(const FileName: UnicodeString): Boolean;
begin
Result:=FileIsReadOnly(ToSingleByteFileSystemEncodedFileName(FileName));
end;
{$endif}
{$ifdef OS_FILESETDATEBYNAME}
Function FileSetDate (Const FileName : UnicodeString;Age : Longint) : Longint;
begin
Result:=FileSetDate(ToSingleByteFileSystemEncodedFileName(FileName),Age);
end;
{$endif}
function FileAge(const FileName: RawByteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
Var
Info : TRawByteSearchRec;
A : Integer;
begin
for A:=1 to Length(FileName) do
if CharInSet(FileName[A],['?','*']) then
Exit(False);
A:=0;
if not FollowLink then
A:=A or faSymLink;
Result:=FindFirst(FileName,A,Info)=0;
if Result then
begin
FileDateTime:=FileDatetoDateTime(Info.Time);
FindClose(Info);
end;
end;
Function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
begin
Result:=FileAge(ToSingleByteFileSystemEncodedFileName(FileName),FileDateTime,FollowLink);
end;
Function FileSearch (Const Name, DirList : UnicodeString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : UnicodeString;
begin
Result:=UnicodeString(FileSearch(ToSingleByteFileSystemEncodedFileName(Name),
ToSingleByteFileSystemEncodedFileName(Dirlist),Options));
end;
Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : Boolean) : UnicodeString;
begin
Result:=UnicodeString(FileSearch(ToSingleByteFileSystemEncodedFileName(Name),
ToSingleByteFileSystemEncodedFileName(DirList),ImplicitCurrentDir));
end;
Function ExeSearch (Const Name : UnicodeString; Const DirList : UnicodeString ='' ) : UnicodeString;
begin
Result:=UnicodeString(ExeSearch(ToSingleByteFileSystemEncodedFileName(Name),
ToSingleByteFileSystemEncodedFileName(Dirlist)));
end;
Function FileSearch (Const Name, DirList : RawByteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
Var
I : longint;
Temp : RawByteString;
begin
Result:=Name;
temp:=SetDirSeparators(DirList);
// Start with checking the file in the current directory
If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
exit;
while True do begin
If Temp = '' then
Break; // No more directories to search - fail
I:=pos(PathSeparator,Temp);
If I<>0 then
begin
Result:=Copy (Temp,1,i-1);
system.Delete(Temp,1,I);
end
else
begin
Result:=Temp;
Temp:='';
end;
If Result<>'' then
begin
If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
Result:=Copy(Result,2,Length(Result)-2);
if (Result<>'') then
Result:=IncludeTrailingPathDelimiter(Result)+name;
end;
If (Result <> '') and FileExists(Result) Then
exit;
end;
Result:='';
end;
Function FileSearch (Const Name, DirList : RawByteString; ImplicitCurrentDir : Boolean) : RawByteString;
begin
if ImplicitCurrentDir then
Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
else
Result:=FileSearch(Name,DirList,[]);
end;
Function ExeSearch (Const Name : RawByteString; Const DirList : RawByteString ='' ) : RawByteString;
Var
D : RawByteString;
O : TFileSearchOptions;
begin
D:=DirList;
if (D='') then
D:=GetEnvironmentVariable('PATH');
{$ifdef unix}
O:=[];
{$else unix}
O:=[sfoImplicitCurrentDir,sfoStripQuotes];
{$endif unix}
Result := FileSearch(Name, D, O);
end;
{$endif}
{$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
Function FileOpen (Const FileName : rawbytestring; Mode : Integer) : THandle;
begin
Result:=FileOpen(UnicodeString(FileName),Mode);
end;
Function FileCreate (Const FileName : RawByteString) : THandle;
begin
Result:=FileCreate(UnicodeString(FileName));
end;
Function FileCreate (Const FileName : RawByteString; Rights : Integer) : THandle;
begin
Result:=FileCreate(UnicodeString(FileName),Rights);
end;
Function FileCreate (Const FileName : RawByteString; ShareMode : Integer; Rights : Integer) : THandle;
begin
Result:=FileCreate(UnicodeString(FileName),ShareMode,Rights);
end;
Function FileAge (Const FileName : RawByteString): Longint;
begin
Result:=FileAge(UnicodeString(FileName));
end;
Function FileExists (Const FileName : RawByteString) : Boolean;
begin
Result:=FileExists(UnicodeString(FileName));
end;
Function DirectoryExists (Const Directory : RawByteString) : Boolean;
begin
Result:=DirectoryExists(UnicodeString(Directory));
end;
Function FileGetAttr (Const FileName : RawByteString) : Longint;
begin
Result:=FileGetAttr(unicodestring(FileName));
end;
Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
begin
Result:=FileSetAttr(unicodestring(FileName),Attr);
end;
Function DeleteFile (Const FileName : RawByteString) : Boolean;
begin
Result:=DeleteFile(UnicodeString(FileName));
end;
Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
begin
Result:=RenameFile(UnicodeString(OldName),UnicodeString(NewName));
end;
{$ifdef OS_FILEISREADONLY}
Function FileIsReadOnly(const FileName: RawByteString): Boolean;
begin
Result:=FileIsReadOnly(UnicodeString(FileName));
end;
{$endif}
{$ifdef OS_FILESETDATEBYNAME}
Function FileSetDate (Const FileName : RawByteString;Age : Longint) : Longint;
begin
Result:=FileSetDate(UnicodeString(FileName),Age);
end;
{$endif}
function FileAge(const FileName: UnicodeString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
Var
Info : TUnicodeSearchRec;
A : Integer;
begin
for A:=1 to Length(FileName) do
if CharInSet(FileName[A],['?','*']) then
Exit(False);
A:=0;
if not FollowLink then
A:=A or faSymLink;
Result:=FindFirst(FileName,A,Info)=0;
if Result then
begin
FileDateTime:=FileDatetoDateTime(Info.Time);
FindClose(Info);
end;
end;
Function FileAge(const FileName: RawbyteString; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
begin
Result:=FileAge(UnicodeString(FileName),FileDateTime,FollowLink);
end;
Function FileSearch (Const Name, DirList : UnicodeString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : UnicodeString;
Var
I : longint;
Temp : UnicodeString;
begin
Result:=Name;
temp:=SetDirSeparators(DirList);
// Start with checking the file in the current directory
If (sfoImplicitCurrentDir in Options) and (Result <> '') and FileExists(Result) Then
exit;
while True do begin
If Temp = '' then
Break; // No more directories to search - fail
I:=pos(PathSeparator,Temp);
If I<>0 then
begin
Result:=Copy (Temp,1,i-1);
system.Delete(Temp,1,I);
end
else
begin
Result:=Temp;
Temp:='';
end;
If Result<>'' then
begin
If (sfoStripQuotes in Options) and (Result[1]='"') and (Result[Length(Result)]='"') then
Result:=Copy(Result,2,Length(Result)-2);
if (Result<>'') then
Result:=IncludeTrailingPathDelimiter(Result)+name;
end;
If (Result <> '') and FileExists(Result) Then
exit;
end;
Result:='';
end;
Function FileSearch (Const Name, DirList : RawbyteString; Options : TFileSearchoptions = [sfoImplicitCurrentDir]) : RawByteString;
begin
Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),options));
end;
Function FileSearch (Const Name, DirList : RawbyteString; ImplicitCurrentDir : Boolean) : RawByteString;
begin
Result:=ToSingleByteFileSystemEncodedFileName(FileSearch(unicodestring(name),unicodestring(dirlist),ImplicitCurrentDir));
end;
Function FileSearch (Const Name, DirList : UnicodeString; ImplicitCurrentDir : Boolean) : UnicodeString;
begin
if ImplicitCurrentDir then
Result:=FileSearch(Name,DirList,[sfoImplicitCurrentDir])
else
Result:=FileSearch(Name,DirList,[]);
end;
Function ExeSearch (Const Name : UnicodeString; Const DirList : UnicodeString ='' ) : UnicodeString;
Var
D : UnicodeString;
O : TFileSearchOptions;
begin
D:=DirList;
if (D='') then
D:=UnicodeString(GetEnvironmentVariable('PATH'));
{$ifdef unix}
O:=[];
{$else unix}
O:=[sfoImplicitCurrentDir,sfoStripQuotes];
{$endif unix}
Result := FileSearch(Name, D, O);
end;
Function ExeSearch (Const Name : RawbyteString; Const DirList : RawbyteString ='' ) : RawByteString;
begin
Result:=ToSingleByteFileSystemEncodedFileName(ExeSearch(unicodestring(name),unicodestring(dirlist)));
end;
{$endif}
Function GetFileHandle(var f : File):THandle;
begin
Result:=filerec(f).handle;
end;
Function GetFileHandle(var f : Text):THandle;
begin
Result:=textrec(f).handle;
end;
{ FindFirst/FindNext. In order to avoid having to duplicate most code in th
OS-specific implementations, we let those implementations fill in all
fields of TRawbyte/UnicodeSearchRec, except for the name. That field is
filled in by the OS-indepedent wrappers, which also takes care of setting
the appropriate code page if applicable.
}
type
TAbstractSearchRec = Record
Time : Longint;
Size : Int64;
Attr : Longint;
{ this will be assigned by the generic code; it is actually either a
rawbytestring or unicodestring; keep it a reference-counted type
so that -gt doesn't overwrite it, the field name should be
indication enough that you should not touch it }
Name_do_not_touch : RawByteString;
ExcludeAttr : Longint;
FindHandle : {$ifdef FINDHANDLE_IS_POINTER}Pointer{$else}THandle{$endif};
{$ifdef unix}
Mode : TMode;
{$endif unix}
{$ifdef USEFINDDATA}
FindData : TFindData;
{$endif}
end;
{$ifdef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint; forward;
Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint; forward;
{$endif SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
{$ifdef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
Function InternalFindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: UnicodeString) : Longint; forward;
Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : UnicodeString) : Longint; forward;
{$endif SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
procedure InternalFindClose(var Handle: {$ifdef FINDHANDLE_IS_POINTER}Pointer{$else}THandle{$endif}{$ifdef USEFINDDATA};var FindData: TFindData{$endif}); forward;
{$ifndef SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
var
Name: UnicodeString;
begin
Result:=InternalFindFirst(UnicodeString(Path),Attr,TAbstractSearchRec(Rslt),Name);
if Result=0 then
widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Name),Rslt.Name,DefaultRTLFileSystemCodePage,length(Name));
end;
Function FindNext (Var Rslt : TRawByteSearchRec) : Longint;
var
Name: UnicodeString;
begin
Result:=InternalFindNext(TAbstractSearchRec(Rslt),Name);
if Result=0 then
widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Name),Rslt.Name,DefaultRTLFileSystemCodePage,length(Name));
end;
{$else not SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
Function FindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TRawByteSearchRec) : Longint;
begin
Result:=InternalFindFirst(Path,Attr,TAbstractSearchRec(Rslt),Rslt.Name);
if Result=0 then
SetCodePage(Rslt.Name,DefaultRTLFileSystemCodePage);
end;
Function FindNext (Var Rslt : TRawByteSearchRec) : Longint;
begin
Result:=InternalFindNext(TAbstractSearchRec(Rslt),Rslt.Name);
if Result=0 then
SetCodePage(Rslt.Name,DefaultRTLFileSystemCodePage);
end;
{$endif not SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
{$ifndef SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
var
Name: RawByteString;
begin
Result:=InternalFindFirst(ToSingleByteFileSystemEncodedFileName(Path),Attr,TAbstractSearchRec(Rslt),Name);
if Result=0 then
Rslt.Name:=UnicodeString(Name);
end;
Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
var
Name: RawByteString;
begin
Result:=InternalFindNext(TAbstractSearchRec(Rslt),Name);
if Result=0 then
Rslt.Name:=UnicodeString(Name);
end;
{$else not SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
Function FindFirst (Const Path : UnicodeString; Attr : Longint; out Rslt : TUnicodeSearchRec) : Longint;
begin
Result:=InternalFindFirst(Path,Attr,TAbstractSearchRec(Rslt),Rslt.Name);
end;
Function FindNext (Var Rslt : TUnicodeSearchRec) : Longint;
begin
Result:=InternalFindNext(TAbstractSearchRec(Rslt),Rslt.Name);
end;
{$endif not SYSUTILS_HAS_UNICODESTR_FILEUTIL_IMPL}
Procedure FindClose(Var f: TRawByteSearchRec);
begin
InternalFindClose(f.FindHandle{$ifdef USEFINDDATA},f.FindData{$endif});
end;
Procedure FindClose(Var f: TUnicodeSearchRec);
begin
InternalFindClose(f.FindHandle{$ifdef USEFINDDATA},f.FindData{$endif});
end;
{ TUnicodeSearchRec }
function TUnicodeSearchRec.GetTimeStamp: TDateTime;
begin
Result := FileDateToDateTime(Time);
end;
{ TRawbyteSearchRec }
function TRawbyteSearchRec.GetTimeStamp: TDateTime;
begin
Result := FileDateToDateTime(Time);
end;
{$ifndef SYSUTILS_HAS_FILEFLUSH_IMPL}
function FileFlush(Handle: THandle): Boolean;
begin
Result:= False;
end;
{$endif}
|