Repository navigation
Expand file tree
/
Copy pathFiles.pas
More file actions
990 lines (804 loc) · 28.1 KB
/
Copy pathFiles.pas
File metadata and controls
990 lines (804 loc) · 28.1 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
unit Files;
{
DESCRIPTION: Implementations of virtual device with sequential access
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
}
(***) interface (***)
uses
Math,
SysUtils,
Windows,
CFiles,
Log,
Utils,
WinWrappers;
const
(* Import *)
MODE_OFF = CFiles.MODE_OFF;
MODE_READ = CFiles.MODE_READ;
MODE_WRITE = CFiles.MODE_WRITE;
MODE_READWRITE = CFiles.MODE_READWRITE;
(* Scan function settings *)
faNotDirectory = SysUtils.faAnyFile and not SysUtils.faDirectory;
ANY_EXT = '';
PATH_SEPARATORS = ['/', '\'];
DISK_LETTER_SEPARATOR = ':';
type
(* Import *)
TDeviceMode = CFiles.TDeviceMode;
TItemInfo = CFiles.TItemInfo;
TFixedBuf = class (CFiles.TAbstractFile)
(***) protected (***)
{OUn} fBuf: pointer;
fOwnsMem: boolean;
(***) public (***)
destructor Destroy; override;
procedure Open ({n} Buf: pointer; BufSize: integer; DeviceMode: TDeviceMode);
procedure Close;
procedure CreateNew (BufSize: integer);
function ReadUpTo (Count: integer; {n} Buf: pointer; out BytesRead: integer): boolean; override;
function WriteUpTo (Count: integer; {n} Buf: pointer; out ByteWritten: integer): boolean; override;
function Seek (NewPos: integer): boolean; override;
property Buf: pointer read fBuf;
property OwnsMem: boolean read fOwnsMem;
end; // .class TFixedBuf
TFile = class (CFiles.TAbstractFile)
(***) protected (***)
fhFile: integer;
fFilePath: string;
(***) public (***)
destructor Destroy; override;
function Open (const FilePath: string; DeviceMode: TDeviceMode): boolean;
function AttachToHandle (FileHandle: integer): boolean;
procedure DetachHandle;
procedure Close;
function CreateNew (const FilePath: string): boolean;
function ReadUpTo (Count: integer; {n} Buf: pointer; out BytesRead: integer): boolean; override;
function WriteUpTo (Count: integer; {n} Buf: pointer; out ByteWritten: integer): boolean; override;
function Seek (NewPos: integer): boolean; override;
property hFile: integer read fhFile;
property FilePath: string read fFilePath;
end; // .class TFile
TFileItemInfo = class (CFiles.TItemInfo)
Data: Windows.TWin32FindData;
end;
TFileLocator = class (CFiles.TAbstractLocator)
(***) protected (***)
fOpened: boolean;
fSearchHandle: integer;
fFindData: Windows.TWin32FindData;
fDirPath: string;
(***) public (***)
destructor Destroy; override;
procedure FinitSearch; override;
procedure InitSearch (const Mask: string); override;
function GetNextItem (out ItemInfo: TItemInfo): string; override;
function GetItemInfo (const ItemName: string; out ItemInfo: TItemInfo): boolean; override;
property DirPath: string read fDirPath write fDirPath;
end;
TScanCallback = function (var SearchRes: SysUtils.TSearchRec): boolean;
(* High level directory scanning
Files are strictly matched against template with wildcards *)
PSearchRec = ^TSearchRec;
TSearchRec = record
Rec: SysUtils.TSearchRec;
function IsFile: boolean;
function IsDir: boolean;
end;
TSearchSubj = (ONLY_FILES, ONLY_DIRS, FILES_AND_DIRS);
ILocator = interface
procedure Locate (const MaskedPath: string; SearchSubj: TSearchSubj);
function FindNext: boolean;
procedure FindClose;
function GetFoundName: string;
function GetFoundPath: string;
function GetFoundRec: {U} PSearchRec;
property FoundName: string read GetFoundName;
property FoundPath: string read GetFoundPath;
property FoundRec: PSearchRec read GetFoundRec;
end; // .interface ILocator
TClearDirFilter = function (const FileName, RelPath, FilePath: string; IsDirectory: boolean): boolean;
PClearDirContext = ^TClearDirContext;
TClearDirContext = record
BasePathLen: integer;
end;
function HasPathSeparators (const FileName: string): boolean;
function IsBaseName (const FileName: string): boolean;
function IsAbsPath (const FilePath: string): boolean;
(* Replaces '/' with '\' and after that replaces all repeating '\' with single '\', unless it's leading double backslash like '\\?\' *)
function NormalizePathSeparators (const Path: string): string;
(* Normalizes paths and converts main path to relative path. Returns empty string on failure *)
function ToRelativePath (FilePath, BasePath: string): string;
(* Converts path to relative if possible. Returns original path unmodified on failure *)
function ToRelativePathIfPossible (FilePath, BasePath: string): string;
function ReadFileContents (const FilePath: string; out FileContents: string): boolean; overload;
function ReadFileContents (FileHandle: integer; out FileContents: string): boolean; overload;
function WriteFileContents (const FileContents, FilePath: string): boolean;
function AppendFileContents (const FileContents, FilePath: string): boolean; overload;
function AppendFileContents ({n} FileContentsBuf: pointer; FileContentsBufSize: integer; const FilePath: string): boolean; overload;
function DeleteDir (const DirPath: string): boolean;
function ClearDir (const DirPath: string; {n} Filter: TClearDirFilter = nil; {n} _Context: PClearDirContext = nil): boolean;
function GetFileSize (const FilePath: string; out Res: integer): boolean;
function Scan
(
const FileMask: string;
AdditionalAttrs: integer;
const FileLowCaseExt: string;
Callback: TScanCallback
): boolean;
function FileExists (const FilePath: string): boolean;
function DirExists (const FilePath: string): boolean;
(* Safe replacement for SysUtils.ForceDirectories, not raising exceptions *)
function ForcePath (const DirPath: string): boolean;
(* Converts file name into name, used internally by file system in order to apply search mask afterwards *)
function FileNameToFsInternalFileName (const Name: string): string;
(* Converts file name mask into mask, suited for internal file system names *)
function MaskToFsInternalMask (const Mask: string): string;
function Locate (const MaskedPath: string; SearchSubj: TSearchSubj): ILocator;
(***) implementation (***)
uses StrLib;
const
FILES_EXTRA_DEBUG = false;
type
TLocator = class (TInterfacedObject, ILocator)
protected
fLastOperRes: boolean;
fSearchStarted: boolean;
fDir: string;
fFileMask: string;
fSearchSubj: TSearchSubj;
fFoundRec: SysUtils.TSearchRec;
function MatchResult: boolean;
public
constructor Create;
destructor Destroy; override;
procedure Locate (const MaskedPath: string; SearchSubj: TSearchSubj);
function FindNext: boolean;
procedure FindClose;
function GetFoundName: string;
function GetFoundPath: string;
function GetFoundRec: {U} PSearchRec;
end; // .class TLocator
function HasPathSeparators (const FileName: string): boolean;
var
i: integer;
begin
result := false;
for i := 1 to Length(FileName) do begin
if FileName[i] in PATH_SEPARATORS then begin
result := true;
exit;
end;
end;
end;
function IsBaseName (const FileName: string): boolean;
begin
result := not HasPathSeparators(FileName);
end;
function IsAbsPath (const FilePath: string): boolean;
begin
result := (FilePath <> '') and ((FilePath[1] in PATH_SEPARATORS) or ((Length(FilePath) >= 2) and (FilePath[2] = DISK_LETTER_SEPARATOR)));
end;
function NormalizePathSeparators (const Path: string): string;
var
PrevWasPathDelim: boolean;
StartPos: integer;
i, j: integer;
begin
result := Path;
StartPos := 1;
PrevWasPathDelim := false;
if (Length(result) >= 2) and (result[1] in PATH_SEPARATORS) and (result[2] in PATH_SEPARATORS) then begin
result[1] := '\';
result[2] := '\';
PrevWasPathDelim := true;
StartPos := 3;
end;
j := StartPos;
for i := StartPos to Length(result) do begin
if not (result[i] in PATH_SEPARATORS) then begin
result[j] := result[i];
PrevWasPathDelim := false;
Inc(j);
end else if not PrevWasPathDelim then begin
result[j] := '\';
PrevWasPathDelim := true;
Inc(j);
end;
end;
if (j - 1) <> Length(result) then begin
SetLength(result, j - 1);
end;
end; // .function NormalizePathSeparators
function ToRelativePath (FilePath, BasePath: string): string;
var
FilePathLen: integer;
BasePathLen: integer;
begin
FilePath := SysUtils.ExpandFileName(FilePath);
BasePath := SysUtils.ExpandFileName(BasePath);
FilePathLen := Length(FilePath);
BasePathLen := Length(BasePath);
result := '';
if (FilePath = '') or (BasePath = '') then begin
exit;
end;
if FilePath[FilePathLen] in PATH_SEPARATORS then begin
Dec(FilePathLen);
end;
if BasePath[BasePathLen] in PATH_SEPARATORS then begin
Dec(BasePathLen);
end;
if FilePath < BasePath then begin
exit;
end;
if
SysUtils.CompareMem(pchar(SysUtils.AnsiLowerCase(FilePath)), pchar(SysUtils.AnsiLowerCase(BasePath)), BasePathLen) and
((FilePathLen = BasePathLen) or (FilePath[BasePathLen + 1] in PATH_SEPARATORS))
then begin
result := System.Copy(FilePath, BasePathLen + 1 + ord(FilePath[BasePathLen + 1] in PATH_SEPARATORS));
if result = '' then begin
result := '.';
end;
end;
end; // .function ToRelativePath
function ToRelativePathIfPossible (FilePath, BasePath: string): string;
begin
result := ToRelativePath(FilePath, BasePath);
if result = '' then begin
result := FilePath;
end;
end;
destructor TFixedBuf.Destroy;
begin
Self.Close;
inherited;
end;
procedure TFixedBuf.Open ({n} Buf: pointer; BufSize: integer; DeviceMode: TDeviceMode);
begin
{!} Assert(Utils.IsValidBuf(Buf, BufSize));
{!} Assert(DeviceMode <> MODE_OFF);
Self.Close;
Self.fMode := DeviceMode;
Self.fHasKnownSize := true;
Self.fSizeIsConst := true;
Self.fSize := BufSize;
Self.fPos := 0;
Self.fEOF := BufSize = 0;
Self.fBuf := Buf;
Self.fOwnsMem := false;
end; // .procedure TFixedBuf.Open
procedure TFixedBuf.Close;
begin
if (Self.fMode <> MODE_OFF) and Self.OwnsMem then begin
FreeMem(Self.fBuf); Self.fBuf := nil;
end;
Self.fMode := MODE_OFF;
end;
procedure TFixedBuf.CreateNew (BufSize: integer);
var
(* on *) NewBuf: pointer;
begin
{!} Assert(BufSize >= 0);
NewBuf := nil;
// * * * * * //
if BufSize > 0 then begin
GetMem(NewBuf, BufSize);
end;
Self.Open(NewBuf, BufSize, MODE_READWRITE); NewBuf := nil;
Self.fOwnsMem := true;
end; // .procedure TFixedBuf.CreateNew
function TFixedBuf.ReadUpTo (Count: integer; {n} Buf: pointer; out BytesRead: integer): boolean;
begin
{!} Assert(Utils.IsValidBuf(Buf, Count));
result := ((Self.Mode = MODE_READ) or (Self.Mode = MODE_READWRITE)) and (not Self.EOF) and (Count > 0);
if result then begin
BytesRead := Math.Min(Count, Self.Size - Self.Pos);
Utils.CopyMem(BytesRead, Utils.PtrOfs(Self.Buf, Self.Pos), Buf);
Self.fPos := Self.Pos + BytesRead;
Self.fEOF := Self.Pos = Self.Size;
end;
end; // .function TFixedBuf.ReadUpTo
function TFixedBuf.WriteUpTo (Count: integer; {n} Buf: pointer; out ByteWritten: integer): boolean;
begin
{!} Assert(Utils.IsValidBuf(Buf, Count));
result := ((Self.Mode = MODE_WRITE) or (Self.Mode = MODE_READWRITE)) and (not Self.EOF);
if result then begin
ByteWritten := Math.Min(Count, Self.Size - Self.Pos);
Utils.CopyMem(ByteWritten, Buf, Utils.PtrOfs(Self.Buf, Self.Pos));
Self.fPos := Self.Pos + ByteWritten;
Self.fEOF := Self.Pos = Self.Size;
end;
end; // .function TFixedBuf.WriteUpTo
function TFixedBuf.Seek (NewPos: integer): boolean;
begin
{!} Assert(NewPos >= 0);
result := (Self.Mode <> MODE_OFF) and (NewPos <= Self.Size);
if result then begin
Self.fPos := NewPos;
Self.fEOF := Self.Pos = Self.Size;
end;
end;
destructor TFile.Destroy;
begin
Self.Close;
inherited Destroy;
end;
function TFile.Open (const FilePath: string; DeviceMode: TDeviceMode): boolean;
var
OpeningMode: integer;
FileSizeL: integer;
FileSizeH: integer;
begin
{!} Assert(DeviceMode <> MODE_OFF);
Self.Close;
Self.fhFile := WinWrappers.INVALID_HANDLE;
case DeviceMode of
MODE_READ: OpeningMode := SysUtils.fmOpenRead or SysUtils.fmShareDenyWrite;
MODE_WRITE: OpeningMode := SysUtils.fmOpenWrite or SysUtils.fmShareExclusive;
MODE_READWRITE: OpeningMode := SysUtils.fmOpenReadWrite or SysUtils.fmShareExclusive;
else
OpeningMode := 0;
end;
result := WinWrappers.FileOpen(FilePath, OpeningMode, Self.fhFile) and WinWrappers.GetFileSize(Self.hFile, FileSizeL, FileSizeH) and (FileSizeH = 0);
if result then begin
Self.fMode := DeviceMode;
Self.fSize := FileSizeL;
Self.fPos := 0;
Self.fEOF := Self.Pos = Self.Size;
Self.fFilePath := FilePath;
Self.fHasKnownSize := true;
Self.fSizeIsConst := false;
end;
// * * * * * //
if (not result) and (Self.hFile <> WinWrappers.INVALID_HANDLE) then begin
Windows.CloseHandle(Self.hFile);
end;
end; // .function TFile.Open
function TFile.AttachToHandle (FileHandle: integer): boolean;
begin
Self.Close;
Self.fhFile := FileHandle;
result := FileHandle <> WinWrappers.INVALID_HANDLE;
if result then begin
Self.fMode := MODE_READWRITE;
Self.fSize := 0;
Self.fPos := 0;
Self.fEOF := false;
Self.fFilePath := '';
Self.fHasKnownSize := false;
Self.fSizeIsConst := false;
end;
end; // .function TFile.AttachToHandle
procedure TFile.DetachHandle;
begin
Self.fhFile := WinWrappers.INVALID_HANDLE;
Self.Close;
end;
procedure TFile.Close;
begin
if (Self.fMode <> MODE_OFF) and (Self.fhFile <> WinWrappers.INVALID_HANDLE) then begin
Windows.CloseHandle(Self.fhFile);
Self.fhFile := WinWrappers.INVALID_HANDLE;
end;
Self.fMode := MODE_OFF;
Self.fFilePath := '';
end;
function TFile.CreateNew (const FilePath: string): boolean;
begin
Self.Close;
result := WinWrappers.FileCreate(FilePath, Self.fhFile);
if result then begin
Self.fMode := MODE_READWRITE;
Self.fSize := 0;
Self.fPos := 0;
Self.fEOF := true;
Self.fFilePath := FilePath;
Self.fHasKnownSize := true;
Self.fSizeIsConst := false;
end;
end;
function TFile.ReadUpTo (Count: integer; {n} Buf: pointer; out BytesRead: integer): boolean;
begin
{!} Assert(Utils.IsValidBuf(Buf, Count));
result := ((Self.Mode = MODE_READ) or (Self.Mode = MODE_READWRITE)) and (not Self.EOF);
if result then begin
BytesRead := SysUtils.FileRead(Self.hFile, Buf^, Count);
result := BytesRead > 0;
Self.fPos := Self.Pos + BytesRead;
Self.fEOF := (Self.fHasKnownSize and (Self.Pos = Self.Size)) or (BytesRead <= 0);
end;
end;
function TFile.WriteUpTo (Count: integer; {n} Buf: pointer; out ByteWritten: integer): boolean;
begin
{!} Assert(Utils.IsValidBuf(Buf, Count));
result := (Self.Mode = MODE_WRITE) or (Self.Mode = MODE_READWRITE);
if result then begin
ByteWritten := SysUtils.FileWrite(Self.hFile, Buf^, Count);
result := ByteWritten > 0;
Self.fPos := Self.Pos + ByteWritten;
Self.fSize := Self.Size + ByteWritten;
Self.fEOF := Self.Pos = Self.Size;
end;
end;
function TFile.Seek (NewPos: integer): boolean;
var
SeekRes: integer;
begin
{!} Assert(NewPos >= 0);
result := Self.Mode <> MODE_OFF;
if result then begin
SeekRes := SysUtils.FileSeek(Self.hFile, NewPos, 0);
result := SeekRes <> -1;
if result then begin
Self.fPos := SeekRes;
result := SeekRes = NewPos;
end;
Self.fEOF := Self.Pos = Self.Size;
end;
end;
procedure TFileLocator.FinitSearch;
begin
if Self.fOpened then begin
Windows.FindClose(Self.fSearchHandle);
Self.fOpened := false;
end;
end;
procedure TFileLocator.InitSearch (const Mask: string);
begin
Self.FinitSearch;
Self.fSearchMask := Mask;
Self.fOpened := WinWrappers.FindFirstFile(Self.DirPath + '\' + Mask, Self.fSearchHandle, Self.fFindData);
Self.fNotEnd := Self.fOpened;
end;
function TFileLocator.GetNextItem (out ItemInfo: TItemInfo): string;
var
(* O *) FileInfo: TFileItemInfo;
begin
{!} Assert(Self.NotEnd);
{!} Assert(ItemInfo = nil);
FileInfo := TFileItemInfo.Create;
// * * * * * //
FileInfo.IsDir := (Self.fFindData.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY) <> 0;
if not FileInfo.IsDir and (Self.fFindData.nFileSizeHigh = 0) and (Self.fFindData.nFileSizeLow < $7FFFFFFF) then begin
FileInfo.HasKnownSize := true;
FileInfo.FileSize := Self.fFindData.nFileSizeLow;
end;
FileInfo.Data := Self.fFindData;
ItemInfo := FileInfo; FileInfo := nil;
result := Self.fFindData.cFileName;
Self.fNotEnd := WinWrappers.FindNextFile(Self.fSearchHandle, Self.fFindData);
end;
destructor TFileLocator.Destroy;
begin
Self.FinitSearch;
inherited;
end;
function TFileLocator.GetItemInfo (const ItemName: string; out ItemInfo: TItemInfo): boolean;
var
(* O *) Locator: TFileLocator;
ItemPath: string;
begin
{!} Assert(ItemInfo = nil);
Locator := TFileLocator.Create;
// * * * * * //
ItemPath := Self.DirPath + '\' + ItemName;
result := SysUtils.FileExists(ItemPath);
if result then begin
Locator.InitSearch(ItemPath);
if Locator.NotEnd then begin
Locator.GetNextItem(ItemInfo);
end;
end;
SysUtils.FreeAndNil(Locator);
end;
function ReadFileContents (const FilePath: string; out FileContents: string): boolean;
var
{O} TheFile: TFile;
begin
TheFile := TFile.Create;
// * * * * * //
result := TheFile.Open(FilePath, MODE_READ) and TheFile.ReadAllToStr(FileContents);
if not result then begin
FileContents := '';
end;
// * * * * * //
SysUtils.FreeAndNil(TheFile);
end;
function ReadFileContents (FileHandle: integer; out FileContents: string): boolean; overload;
var
{O} TheFile: TFile;
begin
TheFile := TFile.Create;
// * * * * * //
result := TheFile.AttachToHandle(FileHandle) and TheFile.ReadAllToStr(FileContents);
TheFile.DetachHandle;
if not result then begin
FileContents := '';
end;
// * * * * * //
SysUtils.FreeAndNil(TheFile);
end;
function WriteFileContents (const FileContents, FilePath: string): boolean;
var
{O} MyFile: TFile;
begin
MyFile := TFile.Create;
// * * * * * //
result :=
MyFile.CreateNew(FilePath) and
MyFile.WriteStr(FileContents);
// * * * * * //
SysUtils.FreeAndNil(MyFile);
end;
function AppendFileContents (const FileContents, FilePath: string): boolean; overload;
var
{O} MyFile: TFile;
begin
MyFile := TFile.Create;
// * * * * * //
if SysUtils.FileExists(FilePath) then begin
result := MyFile.Open(FilePath, MODE_WRITE) and MyFile.Seek(MyFile.Size);
end else begin
result := MyFile.CreateNew(FilePath);
end;
result := result and MyFile.WriteStr(FileContents);
// * * * * * //
SysUtils.FreeAndNil(MyFile);
end;
function AppendFileContents ({n} FileContentsBuf: pointer; FileContentsBufSize: integer; const FilePath: string): boolean; overload;
var
{O} MyFile: TFile;
begin
MyFile := TFile.Create;
// * * * * * //
if SysUtils.FileExists(FilePath) then begin
result := MyFile.Open(FilePath, MODE_WRITE) and MyFile.Seek(MyFile.Size);
end else begin
result := MyFile.CreateNew(FilePath);
end;
result := result and MyFile.Write(FileContentsBufSize, FileContentsBuf);
// * * * * * //
SysUtils.FreeAndNil(MyFile);
end;
function ClearDir (const DirPath: string; {n} Filter: TClearDirFilter = nil; {n} _Context: PClearDirContext = nil): boolean;
var
{O} Locator: TFileLocator;
{O} FileInfo: TFileItemInfo;
FileName: string;
FilePath: string;
IsDirectory: longbool;
Context: TClearDirContext;
begin
Locator := TFileLocator.Create;
FileInfo := nil;
// * * * * * //
result := true;
if _Context = nil then begin
_Context := @Context;
Context.BasePathLen := Length(DirPath) + Length('\');
end;
Locator.DirPath := DirPath;
Locator.InitSearch('*');
while result and Locator.NotEnd do begin
FileName := Locator.GetNextItem(CFiles.TItemInfo(FileInfo));
if (FileName <> '.') and (FileName <> '..') then begin
FilePath := DirPath + '\' + FileName;
IsDirectory := (FileInfo.Data.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY) <> 0;
if (@Filter = nil) or (Filter(FileName, System.Copy(FilePath, _Context.BasePathLen + 1), FilePath, IsDirectory)) then begin
if IsDirectory then begin
result := ClearDir(FilePath, Filter, _Context);
SysUtils.RemoveDir(FilePath);
end else begin
result := SysUtils.DeleteFile(FilePath);
end;
end;
end;
SysUtils.FreeAndNil(FileInfo);
end;
Locator.FinitSearch;
// * * * * * //
SysUtils.FreeAndNil(Locator);
end; // .function ClearDir
function DeleteDir (const DirPath: string): boolean;
begin
ClearDir(DirPath);
result := SysUtils.RemoveDir(DirPath);
end;
function GetFileSize (const FilePath: string; out Res: integer): boolean;
var
{O} MyFile: TFile;
begin
MyFile := TFile.Create;
// * * * * * //
result := MyFile.Open(FilePath, MODE_READ) and MyFile.HasKnownSize;
if result then begin
Res := MyFile.Size;
end;
// * * * * * //
SysUtils.FreeAndNil(MyFile);
end; // .function GetFileSize
function Scan
(
const FileMask: string;
AdditionalAttrs: integer;
const FileLowCaseExt: string;
Callback: TScanCallback
): boolean;
var
SearchRec: SysUtils.TSearchRec;
begin
result := true;
if SysUtils.FindFirst(FileMask, AdditionalAttrs, SearchRec) = 0 then begin
repeat
if
(FileLowCaseExt = ANY_EXT) or
(SysUtils.ExtractFileExt(SysUtils.AnsiLowerCase(SearchRec.Name)) = FileLowCaseExt)
then begin
result := Callback(SearchRec);
end;
until SysUtils.FindNext(SearchRec) <> 0;
SysUtils.FindClose(SearchRec);
end; // .if
end; // .function Scan
function FileExists (const FilePath: string): boolean;
var
Attrs: integer;
begin
Attrs := Windows.GetFileAttributes(pchar(FilePath));
result := (Attrs <> - 1) and ((Attrs and Windows.FILE_ATTRIBUTE_DIRECTORY) = 0);
end;
function DirExists (const FilePath: string): boolean;
var
Attrs: integer;
begin
Attrs := Windows.GetFileAttributes(pchar(FilePath));
result := (Attrs <> - 1) and ((Attrs and Windows.FILE_ATTRIBUTE_DIRECTORY) <> 0);
end;
function ForcePath (const DirPath: string): boolean;
var
PathParts: StrLib.TArrayOfStr;
TestPath: string;
i: integer;
begin
result := true;
if (DirPath = '') or (DirPath = '\') or (DirPath = '\\') or (DirPath = '/') then begin
// Root or current directory always exist, do nothing
end else begin
PathParts := StrLib.Explode(StrLib.TrimEx(StringReplace(DirPath, '/', '\', [rfReplaceAll]), ['\'], [StrLib.RIGHT_SIDE]), '\');
if length(PathParts) > 0 then begin
TestPath := '';
i := 0;
while result and (i < length(PathParts)) do begin
if PathParts[i] <> '' then begin
if TestPath <> '' then begin
TestPath := TestPath + '\';
end;
TestPath := TestPath + PathParts[i];
if not DirExists(TestPath) then begin
result := SysUtils.CreateDir(TestPath);
end;
end else if (TestPath = '') then begin
TestPath := '\';
end; // .elseif
inc(i);
end; // .while
end; // .if
end; // .else
end; // .function ForcePath
function MaskToFsInternalMask (const Mask: string): string;
var
i: integer;
begin
result := SysUtils.AnsiLowerCase(Mask);
i := Length(result);
while ((i > 0) and (result[i] = '*')) do begin
Dec(i);
end;
// Almost each file name ends with "." internally in file system,
// thus "test" mask should be actually "test."
if (i > 0) and (result[i] <> '.') then begin
result := result + '.';
end;
end;
function FileNameToFsInternalFileName (const Name: string): string;
begin
result := SysUtils.AnsiLowerCase(Name);
// Almost each file name ends with "." internally in file system,
// thus "test" is actually "test."
if (result <> '') and (result[Length(result)] <> '.') then begin
result := result + '.';
end;
end;
function TSearchRec.IsFile: boolean;
begin
result := (Self.Rec.FindData.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY) = 0;
end;
function TSearchRec.IsDir: boolean;
begin
result := (Self.Rec.FindData.dwFileAttributes and Windows.FILE_ATTRIBUTE_DIRECTORY) <> 0;
end;
constructor TLocator.Create;
begin
inherited;
Self.fLastOperRes := true;
end;
destructor TLocator.Destroy;
begin
Self.FindClose;
inherited;
end;
procedure TLocator.FindClose;
begin
if Self.fSearchStarted then begin
SysUtils.FindClose(Self.fFoundRec);
Self.fSearchStarted := false;
end;
end;
function TLocator.MatchResult: boolean;
begin
{!} Assert(Self.fSearchStarted and Self.fLastOperRes);
result := false;
case Self.fSearchSubj of
ONLY_FILES: result := (Self.fFoundRec.Attr and SysUtils.faDirectory) = 0;
ONLY_DIRS: result := (Self.fFoundRec.Attr and SysUtils.faDirectory) <> 0;
FILES_AND_DIRS: result := true;
else
{!} Assert(false);
end;
result := result and StrLib.Match(FileNameToFsInternalFileName(Self.fFoundRec.Name), MaskToFsInternalMask(Self.fFileMask));
if FILES_EXTRA_DEBUG then begin
Log.Write('Files', 'TLocator.MatchResult', 'Match "' + Self.fFoundRec.Name + '" to "' +
Self.fFileMask + '" is ' + IntToStr(ord(result)));
end;
end; // .function TLocator.MatchResult
function TLocator.FindNext: boolean;
begin
{!} Assert(Self.fLastOperRes);
result := false;
if not Self.fSearchStarted then begin
Self.fLastOperRes := SysUtils.FindFirst(Self.fDir + '\*', SysUtils.faAnyFile, Self.fFoundRec) = 0;
Self.fSearchStarted := Self.fLastOperRes;
result := Self.fSearchStarted and Self.MatchResult;
end;
if not result and Self.fSearchStarted then begin
while not result and (SysUtils.FindNext(Self.fFoundRec) = 0) do begin
result := Self.MatchResult;
end;
Self.fLastOperRes := result;
end;
end; // .function TLocator.FindNext
procedure TLocator.Locate (const MaskedPath: string; SearchSubj: TSearchSubj);
begin
Self.fDir := SysUtils.ExtractFileDir(MaskedPath);
if Self.fDir = '' then begin
Self.fDir := '.';
end;
Self.fFileMask := SysUtils.ExtractFileName(MaskedPath);
Self.fSearchSubj := SearchSubj;
end;
function TLocator.GetFoundName: string;
begin
{!} Assert(Self.fSearchStarted and Self.fLastOperRes);
result := Self.fFoundRec.Name;
end;
function TLocator.GetFoundPath: string;
begin
{!} Assert(Self.fSearchStarted and Self.fLastOperRes);
result := Self.fDir + '\' + Self.fFoundRec.Name;
end;
function TLocator.GetFoundRec: {U} PSearchRec;
begin
{!} Assert(Self.fSearchStarted and Self.fLastOperRes);
result := @Self.fFoundRec;
end;
function Locate (const MaskedPath: string; SearchSubj: TSearchSubj): ILocator;
var
{O} Locator: TLocator;
begin
Locator := TLocator.Create;
// * * * * * //
Locator.Locate(MaskedPath, SearchSubj);
result := Locator; Locator := nil;
end;
end.