-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBetterFileStream.pas
More file actions
171 lines (132 loc) · 4.58 KB
/
Copy pathBetterFileStream.pas
File metadata and controls
171 lines (132 loc) · 4.58 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
unit BetterFileStream;
interface
uses
classes, sysutils, system.RTLConsts, systemx, betteradapters;
type
TBetterFileStream = class(THandleStream)
strict private
FFileName: string;
function GetEof: boolean;
public
property eof: boolean read GetEof;
constructor Create(const AFileName: string; Mode: cardinal); overload; virtual;
constructor Create(const AFileName: string; Mode: cardinal; Rights: Cardinal); overload; virtual;
constructor Create(const AFileName: string; Mode: cardinal; Rights: Cardinal; Flags: cardinal); overload; virtual;
destructor Destroy; override;
property FileName: string read FFileName;
procedure Reopen;
end;
TFileStreamWithVirtualConstructors = class(TBetterFileStream)
public
constructor Create(const AFileName: string; Mode: cardinal; Rights: Cardinal; Flags: cardinal);override;
constructor Create(const AFileName: string; Mode: Word; Rights:cardinal; Flags:cardinal);reintroduce;overload;
constructor Create(const AFileName: string; Mode: Word; Rights:cardinal);reintroduce;overload;
constructor Create(const AFileName: string; Mode: Word);reintroduce;overload;
end;
TMoreBetterFileStream = class(TFileStreamWithVirtualConstructors)
public
end;
const
FILE_FLAG_WRITE_THROUGH = $80000000;
FILE_FLAG_NO_BUFFERING = $20000000;
implementation
{ TBetterFileStream }
constructor TBetterFileStream.Create(const AFileName: string; Mode: cardinal);
{$IF Defined(MSWINDOWS)}
begin
Create(AFilename, Mode, 0);
end;
{$ELSEIF Defined(POSIX)}
begin
Create(AFilename, Mode, FileAccessRights);
end;
{$ENDIF POSIX}
constructor TBetterFileStream.Create(const AFileName: string; Mode: cardinal; Rights: Cardinal; Flags: cardinal);
var
LShareMode: Word;
begin
if (Mode and fmCreate = fmCreate) then
begin
LShareMode := Mode and $FF;
if LShareMode = $FF then
LShareMode := fmShareExclusive; // For compat in case $FFFF passed as Mode
inherited Create(BetterFileCreate(AFileName, LShareMode, Rights, Flags));
if FHandle = INVALID_HANDLE_VALUE then
raise EFCreateError.CreateResFmt(@SFCreateErrorEx, [ExpandFileName(AFileName), SysErrorMessage(GetLastError)]);
end
else
begin
{$IFDEF MSWINDOWS}
inherited Create(BetterFileOpen(AFileName, Mode, rights, flags));
{$ELSE !MSWINDOWS}
inherited Create(BetterFileOpen(AFileName, Mode, rights, flags));
{$ENDIF MSWINDOWS}
if FHandle = INVALID_HANDLE_VALUE then
raise EFOpenError.CreateResFmt(@SFOpenErrorEx, [ExpandFileName(AFileName), SysErrorMessage(GetLastError)]);
end;
FFileName := AFileName;
end;
constructor TBetterFileStream.Create(const AFileName: string; Mode,
Rights: Cardinal);
var
LShareMode: Word;
begin
if (Mode and fmCreate = fmCreate) then
begin
LShareMode := Mode and $FF;
if LShareMode = $FF then
LShareMode := fmShareExclusive; // For compat in case $FFFF passed as Mode
inherited Create(FileCreate(AFileName, LShareMode, Rights));
if FHandle = INVALID_HANDLE_VALUE then
raise EFCreateError.CreateResFmt(PResStringRec(@SFCreateErrorEx), [ExpandFileName(AFileName), SysErrorMessage(GetLastError)]);
end
else
begin
{$IFDEF MSWINDOWS}
inherited Create(FileOpen(AFileName, Mode or Rights));
{$ELSE !MSWINDOWS}
inherited Create(FileOpen(AFileName, Mode));
{$ENDIF MSWINDOWS}
if FHandle = INVALID_HANDLE_VALUE then
raise EFOpenError.CreateResFmt(PResStringRec(@SFOpenErrorEx), [ExpandFileName(AFileName), SysErrorMessage(GetLastError)]);
end;
FFileName := AFileName;
end;
destructor TBetterFileStream.Destroy;
begin
if FHandle <> INVALID_HANDLE_VALUE then
FileClose(FHandle);
FHandle := INVALID_HANDLE_VALUE;
inherited Destroy;
end;
function TBetterFileStream.GetEof: boolean;
begin
result := position >= size;
end;
procedure TBetterFileStream.Reopen;
begin
if FHandle <> INVALID_HANDLE_VALUE then
FileClose(FHandle);
end;
{ TFileStreamWithVirtualConstructors }
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode: Word; Rights: cardinal);
begin
Create(afilename, mode, rights,0);
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode: Word);
begin
Create(afilename, cardinal(mode), cardinal(0),cardinal(0));
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode: Word; Rights, Flags: cardinal);
begin
Create(afilename, cardinal(mode), rights, flags);
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode, Rights, Flags: cardinal);
begin
inherited CReate(afilename, mode, rights, flags);
end;
end.