-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBetterFileStream.pas
More file actions
210 lines (164 loc) · 5.54 KB
/
BetterFileStream.pas
File metadata and controls
210 lines (164 loc) · 5.54 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
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
ModeAtOpen: cardinal;
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;
TScalableFileStream = class(TFileStreamwithVirtualConstructors)
private
FStripeSize: int64;
FGroupOf: byte;
FGroupIndex: byte;
function GetGroupMask: int64;
public
procedure NotifySizeChangingExternally(const nuSize: int64);
procedure NotifySizeChangedExternally(const nuSize: int64);
property StripeSize: int64 read FStripeSize write FStripeSize;
property GroupIndex: byte read FGroupIndex write FGroupIndex;
property GroupOf: byte read FGroupOf write FGroupOf;
property GroupMask: int64 read GetGroupMask;
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
ModeatOpen := Mode;
Create(afilename, mode, rights,0);
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode: Word);
begin
ModeatOpen := Mode;
Create(afilename, cardinal(mode), cardinal(0),cardinal(0));
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode: Word; Rights, Flags: cardinal);
begin
ModeatOpen := Mode;
Create(afilename, cardinal(mode), rights, flags);
end;
constructor TFileStreamWithVirtualConstructors.Create(const AFileName: string;
Mode, Rights, Flags: cardinal);
begin
ModeatOpen := Mode;
inherited CReate(afilename, mode, rights, flags);
end;
{ TScalableFileStream }
function TScalableFileStream.GetGroupMask: int64;
begin
result := int64(FGroupOf)-1;
end;
procedure TScalableFileStream.NotifySizeChangedExternally(const nuSize: int64);
begin
//
end;
procedure TScalableFileStream.NotifySizeChangingExternally(const nuSize: int64);
begin
//
end;
end.