-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuCommon.pas
More file actions
276 lines (243 loc) · 7.06 KB
/
uCommon.pas
File metadata and controls
276 lines (243 loc) · 7.06 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
unit uCommon;
interface
uses
Classes, Windows, SysUtils, uStructs;
const
MAX_PAGE_SIZE = 32768;
MIN_PAGE_SIZE = 1024;
type
trhd1 = record
rhd_transaction: SLong;
rhd_b_page: SLong;
rhd_b_line: UShort;
rhd_flags: UShort;
rhd_format: UChar; // Èñïîëüçóåì UChar èç uStructs
end;
// Âîçìîæíî, íåñòàíäàðòíàÿ ñòðóêòóðà, èñïîëüçóåì TPag èç uStructs, UChar èç uStructs
Trhd_page = record
fix_data: trhd1;
rhd_data: array [0 .. (MAX_PAGE_SIZE - sizeof(trhd1))] of UChar; // Îøèáêà ðàçìåðà, êàê è â ñòàðîì êîäå
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
tip = packed record
tip_header: TPag; // Èñïîëüçóåì TPag èç uStructs
tip_next: SLong;
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
// Ìàññèâ tip_transactions äîëæåí áûòü äèíàìè÷åñêèì ïðè ÷òåíèè ñòðàíèöû
// Ýòà ñòðóêòóðà îïðåäåëÿåò *ôèêñèðîâàííóþ* ÷àñòü
tippage_fixed_part = packed record
fix_data: tip;
// tip_transactions: array [0 .. N-1] of UCHAR; // Äîáàâëÿåòñÿ äèíàìè÷åñêè
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
Tgenerator_page = record
gpg_header: TPag; // Èñïîëüçóåì TPag èç uStructs
gpg_sequence: ULong; // Sequence number
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
// Ìàññèâ gpg_values äîëæåí áûòü äèíàìè÷åñêèì ïðè ÷òåíèè ñòðàíèöû
Tgnrtr_page_fixed_part = record
fix_data1: Tgenerator_page; // Sequence number
// gpg_values: array [0 .. N-1] of Int64; // Äîáàâëÿåòñÿ äèíàìè÷åñêè
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
Tpnr_page = record
pp_header: TPag; // Èñïîëüçóåì TPag èç uStructs
ppg_sequence: SLong;
ppg_next: SLong;
ppg_count: UShort;
ppg_relation: UShort;
ppg_min_space: UShort;
ppg_max_space: UShort;
end;
// --- ÈÑÏÐÀÂËÅÍÎ: Èñïîëüçóåì TPag èç uStructs ---
// Ìàññèâ ppg_page äîëæåí áûòü äèíàìè÷åñêèì ïðè ÷òåíèè ñòðàíèöû
TPointer_page_fixed_part = record
fix_data: Tpnr_page;
// ppg_page: array [0 .. N-1] of SLong; // Äîáàâëÿåòñÿ äèíàìè÷åñêè
end;
function getCurrPageSize(const fileName: string): Integer;
function ByteToHex(InByte: byte): shortstring;
function GetFileSizeBase(const AFileName: String): Int64;
function IntToBinLowByte(const Value: LongWord): string;
function IntToBin1(Value: Longint; Digits: Integer): string;
function IntToBin2(d: Longint): string;
function Get_a_Bit(const aValue: Cardinal; const Bit: byte): Boolean;
function Set_a_Bit(const aValue: Cardinal; const Bit: byte): Cardinal;
function Clear_a_Bit(const aValue: Cardinal; const Bit: byte): Cardinal;
function BinToInt(BinStr: string): Int64;
function Hex2Byte(S: String): byte;
function Enable_a_Bit(const aValue: Cardinal; const Bit: byte;
const Flag: Boolean): Cardinal;
procedure CopyDatabaseFile(ADatabaseOriginalPath: string;
ADatabaseCopyPath: String);
implementation
// bits function for flags
// get if a particular bit is 1
function Get_a_Bit(const aValue: Cardinal; const Bit: byte): Boolean;
begin
Result := (aValue and (1 shl Bit)) <> 0;
end;
// set a particular bit as 1
function Set_a_Bit(const aValue: Cardinal; const Bit: byte): Cardinal;
begin
Result := aValue or (1 shl Bit);
end;
// set a particular bit as 0
function Clear_a_Bit(const aValue: Cardinal; const Bit: byte): Cardinal;
begin
Result := aValue and not(1 shl Bit);
end;
// Enable o disable a bit
function Enable_a_Bit(const aValue: Cardinal; const Bit: byte;
const Flag: Boolean): Cardinal;
begin
Result := (aValue or (1 shl Bit)) xor (Integer(not Flag) shl Bit);
end;
function BinToInt(BinStr: string): Int64;
var
i: byte;
RetVar: Int64;
begin
BinStr := UpperCase(BinStr);
if BinStr[length(BinStr)] = 'B' then
Delete(BinStr, length(BinStr), 1);
RetVar := 0;
for i := 1 to length(BinStr) do
begin
if not(BinStr[i] in ['0', '1']) then
begin
RetVar := 0;
Break;
end;
RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1);
end;
Result := RetVar;
end;
// Integer to Binary
function IntToBin1(Value: Longint; Digits: Integer): string;
var
i: Integer;
begin
Result := '';
for i := Digits downto 0 do
if Value and (1 shl i) <> 0 then
Result := Result + '1'
else
Result := Result + '0';
end;
function Hex2Byte(S: String): byte;
const
C: string[16] = '0123456789ABCDEF';
begin
if length(S) < 2 then
Result := Pos(S, C) - 1
else
Result := (Pos(S[1], C) - 1) * 16 + Pos(S[2], C) - 1
end;
function IntToBin2(d: Longint): string;
var
x, p: Integer;
bin: string;
begin
bin := '';
for x := 1 to 8 * sizeof(d) do
begin
if Odd(d) then
bin := '1' + bin
else
bin := '0' + bin;
d := d shr 1;
end;
Delete(bin, 1, 8 * ((Pos('1', bin) - 1) div 8));
Result := bin;
end;
function IntToBinLowByte(const Value: LongWord): string;
var
i: Integer;
begin
SetLength(Result, 8);
for i := 1 to 8 do
begin
if ((Value shl (24 + i - 1)) shr 31) = 0 then
begin
Result[i] := '0'
end
else
begin
Result[i] := '1';
end;
end;
end;
function getCurrPageSize(const fileName: string): Integer;
var
fs: TFileStream;
HeaderPage: THdrPage;
begin
try
fs := TFileStream.Create(fileName, fmOpenRead or fmShareDenyNone);
fs.Read(HeaderPage, MIN_PAGE_SIZE);
finally
fs.Free;
end;
Result := HeaderPage.fix_data.hdr_page_size;
end;
function ByteToHex(InByte: byte): shortstring;
const
Digits: array [0 .. 15] of char = '0123456789ABCDEF';
begin
Result := Digits[InByte shr 4] + Digits[InByte and $0F];
end;
function GetFileSizeBase(const AFileName: String): Int64;
var
SR: TSearchRec;
begin
Result := -1;
if FindFirst(AFileName, faAnyFile, SR) = 0 then
try
Result := (SR.FindData.nFileSizeHigh * Int64(MAXDWORD)) +
SR.FindData.nFileSizeLow;
finally
FindClose(SR);
end;
end;
procedure CopyDatabaseFile(ADatabaseOriginalPath: string;
ADatabaseCopyPath: String);
const
BUFFER_SIZE = 10240;
var
FromFile, ToFile: TFileStream;
Buffer: array [0 .. BUFFER_SIZE - 1] of byte;
NumRead: Integer;
FileSize, CopiedSize: Int64;
begin
FileSize := GetFileSizeBase(ADatabaseOriginalPath);
FromFile := TFileStream.Create(ADatabaseOriginalPath, fmOpenRead or
fmShareDenyNone);
try
if FileExists(ADatabaseCopyPath) then
ToFile := TFileStream.Create(ADatabaseCopyPath, fmOpenReadWrite or
fmShareDenyWrite)
else
ToFile := TFileStream.Create(ADatabaseCopyPath, fmCreate);
try
CopiedSize := 0;
ToFile.Size := FileSize;
ToFile.Position := 0;
FromFile.Position := 0;
NumRead := FromFile.Read(Buffer[0], BUFFER_SIZE);
while NumRead > 0 do
begin
ToFile.Write(Buffer[0], NumRead);
CopiedSize := CopiedSize + NumRead;
NumRead := FromFile.Read(Buffer[0], BUFFER_SIZE);
end;
finally
FreeAndNil(ToFile);
end;
finally
FreeAndNil(FromFile);
end;
end;
end.