-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdbf_common.pas
executable file
·453 lines (368 loc) · 9.69 KB
/
dbf_common.pas
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
unit dbf_common;
interface
{$I dbf_common.inc}
uses
SysUtils, Classes, DB
{$ifndef WINDOWS}
, Types, dbf_wtil
{$ifdef KYLIX}
, Libc
{$endif}
{$endif}
;
const
TDBF_MAJOR_VERSION = 6;
TDBF_MINOR_VERSION = 9;
TDBF_SUB_MINOR_VERSION = 2;
TDBF_TABLELEVEL_FOXPRO = 25;
JulianDateDelta = 1721425; { number of days between 1.1.4714 BC and "0" }
type
EDbfError = class (EDatabaseError);
EDbfWriteError = class (EDbfError);
TDbfFieldType = char;
TXBaseVersion = (xUnknown, xClipper, xBaseIII, xBaseIV, xBaseV, xFoxPro, xBaseVII);
TSearchKeyType = (stEqual, stGreaterEqual, stGreater);
TDateTimeHandling = (dtDateTime, dtBDETimeStamp);
//-------------------------------------
PDateTime = ^TDateTime;
{$ifndef FPC_VERSION}
PtrInt = Longint;
{$endif}
PSmallInt = ^SmallInt;
PCardinal = ^Cardinal;
PDouble = ^Double;
PString = ^String;
{$ifdef DELPHI_3}
dword = cardinal;
{$endif}
//-------------------------------------
{$ifndef SUPPORT_FREEANDNIL}
// some procedures for the less lucky who don't have newer versions yet :-)
procedure FreeAndNil(var v);
{$endif}
procedure FreeMemAndNil(var P: Pointer);
//-------------------------------------
{$ifndef SUPPORT_PATHDELIM}
const
{$ifdef WINDOWS}
PathDelim = '\';
{$else}
PathDelim = '/';
{$endif}
{$endif}
{$ifndef SUPPORT_INCLTRAILPATHDELIM}
function IncludeTrailingPathDelimiter(const Path: string): string;
{$endif}
//-------------------------------------
function GetCompletePath(const Base, Path: string): string;
function GetCompleteFileName(const Base, FileName: string): string;
function IsFullFilePath(const Path: string): Boolean; // full means not relative
function DateTimeToBDETimeStamp(aDT: TDateTime): double;
function BDETimeStampToDateTime(aBT: double): TDateTime;
procedure FindNextName(BaseName: string; var OutName: string; var Modifier: Integer);
{$ifdef USE_CACHE}
function GetFreeMemory: Integer;
{$endif}
function SwapWordBE(const Value: word): word;
function SwapWordLE(const Value: word): word;
function SwapIntBE(const Value: dword): dword;
function SwapIntLE(const Value: dword): dword;
{$ifdef SUPPORT_INT64}
procedure SwapInt64BE(Value, Result: Pointer); register;
procedure SwapInt64LE(Value, Result: Pointer); register;
{$endif}
function TranslateString(FromCP, ToCP: Cardinal; Src, Dest: PChar; Length: Integer): Integer;
// Returns a pointer to the first occurence of Chr in Str within the first Length characters
// Does not stop at null (#0) terminator!
function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
// Delphi 3 does not have a Min function
{$ifdef DELPHI_3}
{$ifndef DELPHI_4}
function Min(x, y: integer): integer;
function Max(x, y: integer): integer;
{$endif}
{$endif}
implementation
{$ifdef WINDOWS}
uses
Windows;
{$endif}
//====================================================================
function GetCompletePath(const Base, Path: string): string;
begin
if IsFullFilePath(Path)
then begin
Result := Path;
end else begin
if Length(Base) > 0 then
Result := ExpandFileName(IncludeTrailingPathDelimiter(Base) + Path)
else
Result := ExpandFileName(Path);
end;
// add last backslash if not present
if Length(Result) > 0 then
Result := IncludeTrailingPathDelimiter(Result);
end;
function IsFullFilePath(const Path: string): Boolean; // full means not relative
begin
{$ifdef WINDOWS}
Result := Length(Path) > 1;
if Result then
// check for 'x:' or '\\' at start of path
Result := ((Path[2]=':') and (upcase(Path[1]) in ['A'..'Z']))
or ((Path[1]='\') and (Path[2]='\'));
{$else} // Linux
Result := Length(Path) > 0;
if Result then
Result := Path[1]='/';
{$endif}
end;
//====================================================================
function GetCompleteFileName(const Base, FileName: string): string;
var
lpath: string;
lfile: string;
begin
lpath := GetCompletePath(Base, ExtractFilePath(FileName));
lfile := ExtractFileName(FileName);
lpath := lpath + lfile;
result := lpath;
end;
function DateTimeToBDETimeStamp(aDT: TDateTime): double;
var
aTS: TTimeStamp;
begin
aTS := DateTimeToTimeStamp(aDT);
Result := TimeStampToMSecs(aTS);
end;
function BDETimeStampToDateTime(aBT: double): TDateTime;
var
aTS: TTimeStamp;
begin
aTS := MSecsToTimeStamp(Round(aBT));
Result := TimeStampToDateTime(aTS);
end;
//====================================================================
{$ifndef SUPPORT_FREEANDNIL}
procedure FreeAndNil(var v);
var
Temp: TObject;
begin
Temp := TObject(v);
TObject(v) := nil;
Temp.Free;
end;
{$endif}
procedure FreeMemAndNil(var P: Pointer);
var
Temp: Pointer;
begin
Temp := P;
P := nil;
FreeMem(Temp);
end;
//====================================================================
{$ifndef SUPPORT_INCLTRAILPATHDELIM}
{$ifndef SUPPORT_INCLTRAILBACKSLASH}
function IncludeTrailingPathDelimiter(const Path: string): string;
var
len: Integer;
begin
Result := Path;
len := Length(Result);
if len = 0 then
Result := PathDelim
else
if Result[len] <> PathDelim then
Result := Result + PathDelim;
end;
{$else}
function IncludeTrailingPathDelimiter(const Path: string): string;
begin
{$ifdef WINDOWS}
Result := IncludeTrailingBackslash(Path);
{$else}
Result := IncludeTrailingSlash(Path);
{$endif}
end;
{$endif}
{$endif}
{$ifdef USE_CACHE}
function GetFreeMemory: Integer;
var
MemStatus: TMemoryStatus;
begin
GlobalMemoryStatus(MemStatus);
Result := MemStatus.dwAvailPhys;
end;
{$endif}
//====================================================================
// Utility routines
//====================================================================
{$ifdef ENDIAN_LITTLE}
function SwapWordBE(const Value: word): word;
{$else}
function SwapWordLE(const Value: word): word;
{$endif}
begin
Result := ((Value and $FF) shl 8) or ((Value shr 8) and $FF);
end;
{$ifdef ENDIAN_LITTLE}
function SwapWordLE(const Value: word): word;
{$else}
function SwapWordBE(const Value: word): word;
{$endif}
begin
Result := Value;
end;
{$ifdef FPC}
function SwapIntBE(const Value: dword): dword;
begin
Result := BEtoN(Value);
end;
function SwapIntLE(const Value: dword): dword;
begin
Result := LEtoN(Value);
end;
procedure SwapInt64BE(Value, Result: Pointer);
begin
PInt64(Result)^ := BEtoN(PInt64(Value)^);
end;
procedure SwapInt64LE(Value, Result: Pointer);
begin
PInt64(Result)^ := LEtoN(PInt64(Value)^);
end;
{$else}
{$ifdef USE_ASSEMBLER_486_UP}
function SwapIntBE(const Value: dword): dword; register; assembler;
asm
BSWAP EAX;
end;
procedure SwapInt64BE(Value {EAX}, Result {EDX}: Pointer); register; assembler;
asm
MOV ECX, dword ptr [EAX]
MOV EAX, dword ptr [EAX + 4]
BSWAP ECX
BSWAP EAX
MOV dword ptr [EDX+4], ECX
MOV dword ptr [EDX], EAX
end;
{$else}
function SwapIntBE(const Value: Cardinal): Cardinal;
begin
PByteArray(@Result)[0] := PByteArray(@Value)[3];
PByteArray(@Result)[1] := PByteArray(@Value)[2];
PByteArray(@Result)[2] := PByteArray(@Value)[1];
PByteArray(@Result)[3] := PByteArray(@Value)[0];
end;
procedure SwapInt64BE(Value, Result: Pointer); register;
var
PtrResult: PByteArray;
PtrSource: PByteArray;
begin
// temporary storage is actually not needed, but otherwise compiler crashes (?)
PtrResult := PByteArray(Result);
PtrSource := PByteArray(Value);
PtrResult[0] := PtrSource[7];
PtrResult[1] := PtrSource[6];
PtrResult[2] := PtrSource[5];
PtrResult[3] := PtrSource[4];
PtrResult[4] := PtrSource[3];
PtrResult[5] := PtrSource[2];
PtrResult[6] := PtrSource[1];
PtrResult[7] := PtrSource[0];
end;
{$endif}
function SwapIntLE(const Value: dword): dword;
begin
Result := Value;
end;
{$ifdef SUPPORT_INT64}
procedure SwapInt64LE(Value, Result: Pointer);
begin
PInt64(Result)^ := PInt64(Value)^;
end;
{$endif}
{$endif}
function TranslateString(FromCP, ToCP: Cardinal; Src, Dest: PChar; Length: Integer): Integer;
var
WideCharStr: array[0..1023] of WideChar;
wideBytes: Cardinal;
begin
if Length = -1 then
Length := StrLen(Src);
Result := Length;
{$ifndef WINCE}
if (FromCP = GetOEMCP) and (ToCP = GetACP) then
OemToCharBuff(Src, Dest, Length)
else
if (FromCP = GetACP) and (ToCP = GetOEMCP) then
CharToOemBuff(Src, Dest, Length)
else
{$endif}
if FromCP = ToCP then
begin
if Src <> Dest then
Move(Src^, Dest^, Length);
end else begin
// does this work on Win95/98/ME?
wideBytes := MultiByteToWideChar(FromCP, MB_PRECOMPOSED, Src, Length, LPWSTR(@WideCharStr[0]), 1024);
Result := WideCharToMultiByte(ToCP, 0, LPWSTR(@WideCharStr[0]), wideBytes, Dest, Length, nil, nil);
end;
end;
procedure FindNextName(BaseName: string; var OutName: string; var Modifier: Integer);
var
Extension: string;
begin
Extension := ExtractFileExt(BaseName);
BaseName := Copy(BaseName, 1, Length(BaseName)-Length(Extension));
repeat
Inc(Modifier);
OutName := ChangeFileExt(BaseName+'_'+IntToStr(Modifier), Extension);
until not FileExists(OutName);
end;
{$ifdef FPC}
function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
var
I: Integer;
begin
I := System.IndexByte(Buffer, Length, Chr);
if I = -1 then
Result := nil
else
Result := Buffer+I;
end;
{$else}
function MemScan(const Buffer: Pointer; Chr: Byte; Length: Integer): Pointer;
asm
PUSH EDI
MOV EDI,Buffer
MOV AL, Chr
MOV ECX,Length
REPNE SCASB
MOV EAX,0
JNE @@1
MOV EAX,EDI
DEC EAX
@@1: POP EDI
end;
{$endif}
{$ifdef DELPHI_3}
{$ifndef DELPHI_4}
function Min(x, y: integer): integer;
begin
if x < y then
result := x
else
result := y;
end;
function Max(x, y: integer): integer;
begin
if x < y then
result := y
else
result := x;
end;
{$endif}
{$endif}
end.