-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbfpublics.pas
681 lines (629 loc) · 15.6 KB
/
dbfpublics.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
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
unit dbfpublics;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StdCtrls, db, dbf, dbf_fields, dbf_common, dialogs, strutils;
type
_TSize= record
Min, Max, Def: Integer;
end;
TNativeType = record
Name: string;
NativeType: Char;
Size: _TSize;
Digit: _TSize;
sFixed: Boolean;
dFixed: Boolean;
end;
TLevel= (L3,L4,L7);
TXItem= record
Exp: string;
Name: string;
NewIndex: Boolean;
Opts: TIndexOptions;
end;
TFldItem= record
Name: string;
VCLType: string;
NativeType: Char;
end;
TXItems= array of TXItem;
TFldItems= array of TFldItem;
TXObject= object
Values: TXItems;
function IndexOf(const S: string): Integer;
end;
TFldObject= object
Fields: TFldItems;
function IndexOf(const S: string): Integer;
end;
var
Level: TLevel;
FieldTypeCount: array[TLevel] of Byte= (5, 6, 11);
MaxFieldLen: array[TLevel] of Byte=(10,10,31);
NativeField: TNativeType;
FldItems: TFldObject;
XItems: TXObject;
XBase: array[TLevel,1..11] of TNativeType;
NativeChars: array[TLevel] of set of Char=
(['C','N','D','L','M'],
['C','F','N','D','L','M'],
['+','C','I','F','N','L','D','@','B','M','G']);
TmpFieldDefs: TDbfFieldDefs;
DbfVer: array[TXBaseVersion] of string=
('Unknown','Clipper','dBaseIII','dBaseIV',
'dBaseV','FoxPro','dBaseVII','VisualFoxPro');
FldIndexable: set of Char=['+','@','C','D','F','I','N','O','V'];
{'+','I','O','@','C','L','F','N','D','M','B','G','Y','0','P','V','W','Q'}
{dbf_fields.pas: VCLToNative ve NativeToVCL}
{+ = AutoInc F = Float Y = BCD
I = Integer N = Numeric 0 = Bytes
O = Float D = Date P = Blob
@ = DateTime M = Memo V = String
C = String B = Blob W = Blob
L = Boolean G = DbaseOle Q = VarBytes }
TmpDbf: TDbf;
IndexedFields: TStringList;
procedure TrimBrackets(var S: string);
function XOptsToByte(Opts: TIndexOptions): Byte;
function ByteToXOpts(B: Byte): TIndexOptions;
procedure DeleteX(var XItems: TXItems; const Index: Cardinal);
procedure DeleteFld(var FldItems: TFldItems; const Index: Cardinal);
function GetIndexFields(Exp: string; IndexedFields: TStringList): Integer;
procedure InitFieldProperties;
function ExpressionIsOk(var Fld: String; Fn: string): Boolean;
function RightPos(C: Char; const S: string): Integer;
function VCLType(NativeType: Char; Level: TLevel): string;
function DBaseVersion(Dbf: TDbf): string;
implementation
procedure TrimBrackets(var S: string);
var
Yes: Boolean;
begin
if Length(S) > 1 then
begin
Yes:=(S[1]='(') and (S[Length(S)]=')');
while Yes do
begin
Delete(S,1,1);
Delete(S,Length(S),1);
Yes:=(S[1]='(') and (S[Length(S)]=')');
end;
end;
end;
function RightPos(C: Char; const S: string): Integer;
var
I: Integer;
begin
Result := 0;
for I := Length(S) downto 1 do
if S[I] = C then
begin
Result := I;
Break;
end;
end;
function TXObject.IndexOf(const S: string): Integer;
var
I: Integer;
begin
Result:=-1;
for I:=Low(Values) to High(Values) do
if S=Values[I].Name then
begin
Result:=I;
Break;
end;
end;
function TFldObject.IndexOf(const S: string): Integer;
var
I: Integer;
begin
Result:=-1;
for I:=Low(Fields) to High(Fields) do
if S=Fields[I].Name then
begin
Result:=I;
Break;
end;
end;
procedure DeleteX(var XItems: TXItems; const Index: Cardinal);
var
I,ALength: Cardinal;
begin
ALength := Length(XItems);
Assert(ALength > 0);
Assert(Index < ALength);
for I := Index + 1 to ALength - 1 do
XItems[I - 1] := XItems[I];
SetLength(XItems, ALength - 1);
end;
procedure DeleteFld(var FldItems: TFldItems; const Index: Cardinal);
var
I,ALength: Cardinal;
begin
ALength := Length(FldItems);
Assert(ALength > 0);
Assert(Index < ALength);
for I := Index + 1 to ALength - 1 do
FldItems[I - 1] := FldItems[I];
SetLength(FldItems, ALength - 1);
end;
function XOptsToByte(Opts: TIndexOptions): Byte;
var
XOpt: TIndexOption;
begin
Result:=0;
for XOpt:= Low(XOpt) to High(XOpt) do
if XOpt in Opts then Result:=Result or (1 shl Ord(XOpt));
end;
function ByteToXOpts(B: Byte): TIndexOptions;
var
I: Byte;
XOpt: TIndexOption;
begin
Result := [];
for XOpt:= Low(XOpt) to High(XOpt) do
begin
I := B and (1 shl Ord(XOpt));
if I > 0 then Result := Result + [XOpt];
end;
end;
function GetIndexFields(Exp: string; IndexedFields: TStringList): Integer;
var
I,J: Integer;
Fld: string;
List: TStringList;
begin
Result := 0;
Exp := DelSpace(Exp);
List:=TStringList.Create;
try
I:=Pos('+',Exp);
while I > 0 do
begin
List.Add(Copy(Exp,1,I-1));
Delete(Exp,1,I);
I:=Pos('+',Exp);
end;
List.Add(Exp);
for I:=0 to List.Count-1 do
begin
if ExpressionIsOk(Fld, List[I]) then
begin
J := IndexedFields.IndexOf(Fld);
if J < 0 then IndexedFields.Add(Fld);
end
else
Inc(Result); //No ExpressionIsOk
end;
finally
List.Free;
end;
end;
function ExpressionIsOk(var Fld: String; Fn: string): Boolean;
function RightPos(C: Char; const S: string): Integer;
var
I: Integer;
begin
Result := 0;
for I := Length(S) downto 1 do
if S[I] = C then
begin
Result := I;
Break;
end;
end;
function STRParamsIsOk(var Fn: string): Boolean;
var
S: string;
P: Integer;
function StrToNum(const S: string): Boolean;
var
Err,Num: Integer;
begin
if S > '' then
begin
Val(S, Num, Err);
Result := (Err = 0) and (Num >= 0);
end
else
Result := True;
end;
begin
Result := False;
Delete(Fn, 1, 4);
P := RightPos(')',Fn);
if P < Length(Fn) then Exit;
Delete(Fn,P,1);
P := RightPos(',',Fn);
if P < 1 then //no comma
begin
Result := True;
TrimBrackets(Fn);
Exit;
end;
S := Copy(Fn,P+1,Length(Fn)-P);
Delete(Fn,P,(Length(Fn)-P)+1);
if not StrToNum(S) then Exit;
P := RightPos(',',Fn);
if P < 1 then
begin
Result := True;
TrimBrackets(Fn);
Exit;
end;
Delete(Fn,P,(Length(Fn)-P)+1);
if not StrToNum(S) then Exit;
Result := True;
TrimBrackets(Fn);
end;
function SUBSTRParamsIsOk(var Fn: string): Boolean;
var
S: string;
P: Integer;
function StrToNum(const S: string): Boolean;
var
Err,Num: Integer;
begin
Val(S, Num, Err);
Result := (Err = 0) and (Num > 0);
end;
begin
Result := False;
Delete(Fn, 1, 7); //NAME,1,10)
P := RightPos(')',Fn); //P = 10
if P < Length(Fn) then Exit;
Delete(Fn,P,1); //NAME,1,10
P := RightPos(',',Fn); //P=7
if P < 1 then Exit;
S := Copy(Fn,P+1,Length(Fn)-P); //NAME,1,10
Delete(Fn,P,(Length(Fn)-P)+1); //NAME,1
if not StrToNum(S) then Exit;
P := RightPos(',',Fn); //NAME,1
if P < 1 then Exit;
S := Copy(Fn,P+1,Length(Fn)-P); //S=1
Delete(Fn,P,(Length(Fn)-P)+1); //NAME
if not StrToNum(S) then Exit;
TrimBrackets(Fn);
Result := True;
end;
function DTOSParamsIsOk(var Fn: string): Boolean;
var
P: Integer;
begin
Delete(Fn, 1, 5);
P := RightPos(')',Fn);
Result := P = Length(Fn);
if Result then
begin
Delete(Fn,P,1);
TrimBrackets(Fn);
end;
end;
function LOWERParamsIsOk(var Fn: string): Boolean;
var
P: Integer;
begin
Delete(Fn, 1, 6);
P := RightPos(')',Fn);
Result := P = Length(Fn);
if Result then
begin
Delete(Fn,P,1);
TrimBrackets(Fn);
end;
end;
function UPPERParamsIsOk(var Fn: string): Boolean;
var
P: Integer;
begin
Delete(Fn, 1, 6);
P := RightPos(')',Fn);
Result := P = Length(Fn);
if Result then
begin
Delete(Fn,P,1);
TrimBrackets(Fn);
end;
end;
var
I,J,P: Integer;
FnList: TStringList;
Funcs: array[0..4] of string= ('DTOS(','LOWER(','UPPER(','SUBSTR(','STR(');
function ValidFunction(Fn: string): Boolean;
var
S: string;
A,B,I,P: Integer;
List: TStringList;
begin
Result := False;
List := TStringList.Create;
try
A := 0; B := 0;
for I := 1 to Length(Fn) do
begin
if Fn[I]='(' then Inc(A) else
if Fn[I]=')' then Inc(B);
end;
if A <> B then Exit;
for I := 0 to 4 do List.Add(Funcs[I]);
P := Pos('(',Fn);
while P > 0 do
begin
S := Copy(Fn,1,P);
I := List.IndexOf(S);
if I >= 0 then FnList.Add(S);
Delete(Fn,1,P);
P := Pos('(',Fn);
end;
Result := True;
finally
List.Free;
end;
end;
begin
Result := False;
FnList:=TStringList.Create;
try
Fn := DelSpace(Fn);
if not ValidFunction(Fn) then Exit;
J := FnList.Count;
if J = 0 then
begin
Fld := Fn;
TrimBrackets(Fld);
Result := True;
Exit;
end;
for I := 1 to J do
begin
TrimBrackets(Fn);
if Pos('STR(',Fn) = 1 then
begin
if STRParamsIsOk(Fn) then
begin
P := FnList.IndexOf('STR(');
FnList.Delete(P);
Fld := Fn;
Result := True;
end else Exit;
end
else
if Pos('DTOS(',Fn) = 1 then
begin
if DTOSParamsIsOk(Fn) then
begin
P := FnList.IndexOf('DTOS(');
FnList.Delete(P);
Fld := Fn;
Result := True;
end else Exit;
end
else
if Pos('LOWER(',Fn) = 1 then
begin
if LOWERParamsIsOk(Fn) then
begin
P := FnList.IndexOf('LOWER(');
FnList.Delete(P);
Fld := Fn;
Result := True;
end else Exit;
end
else
if Pos('UPPER(',Fn) = 1 then
begin
if UPPERParamsIsOk(Fn) then
begin
P := FnList.IndexOf('UPPER(');
FnList.Delete(P);
Fld := Fn;
Result := True;
end else Exit;
end
else
if Pos('SUBSTR(',Fn) = 1 then
begin
if SUBSTRParamsIsOk(Fn) then
begin
P := FnList.IndexOf('SUBSTR(');
FnList.Delete(P);
Fld := Fn;
Result := True;
end else Exit;
end;
end; {for}
finally
FnList.Free;
end;
end;
procedure InitFieldProperties;
begin
(****** dBASE VII ***********)
with XBase[L7,1] do
begin
Name:='AutoInc'; NativeType:= '+';
Size.Max:=4; Size.Min:=4; Size.Def:=4;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,2] do
begin
Name:='String'; NativeType:= 'C';
Size.Max:=255; Size.Min:=1; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=False; dFixed:=True;
end;
with XBase[L7,3] do
begin
Name:='Integer'; NativeType:= 'I';
Size.Max:=4; Size.Min:=4; Size.Def:=4;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,4] do
begin
Name:='Float'; NativeType:= 'F';
Size.Max:=20; Size.Min:=1; Size.Def:=10;
Digit.Max:=18; Digit.Min:=0; Digit.Def:=2;
sFixed:=False; dFixed:=False;
end;
with XBase[L7,5] do
begin
Name:='Largeint'; NativeType:= 'N';
Size.Max:=20; Size.Min:=1; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=False; dFixed:=True;
end;
with XBase[L7,6] do
begin
Name:='Boolean'; NativeType:= 'L';
Size.Max:=1; Size.Min:=1; Size.Def:=1;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,7] do
begin
Name:='Date'; NativeType:= 'D';
Size.Max:=8; Size.Min:=8; Size.Def:=8;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,8] do
begin
Name:='DateTime'; NativeType:= '@';
Size.Max:=8; Size.Min:=8; Size.Def:=8;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,9] do
begin
Name:='Blob'; NativeType:= 'B';
Size.Max:=10; Size.Min:=10; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,10] do
begin
Name:='Memo'; NativeType:= 'M';
Size.Max:=10; Size.Min:=10; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L7,11] do
begin
Name:='DBaseOle'; NativeType:= 'G';
Size.Max:=10; Size.Min:=10; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
(****** dBASE IV ************)
with XBase[L4,1] do
begin
Name:='String'; NativeType:= 'C';
Size.Max:=255; Size.Min:=1; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=False; dFixed:=True;
end;
with XBase[L4,2] do
begin
Name:='Float'; NativeType:= 'F';
Size.Max:=20; Size.Min:=1; Size.Def:=10;
Digit.Max:=18; Digit.Min:=0; Digit.Def:=2;
sFixed:=False; dFixed:=False;
end;
with XBase[L4,3] do
begin
Name:='Integer'; NativeType:= 'N';
Size.Max:=20; Size.Min:=1; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=False; dFixed:=True;
end;
with XBase[L4,4] do
begin
Name:='Date'; NativeType:= 'D';
Size.Max:=8; Size.Min:=8; Size.Def:=8;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L4,5] do
begin
Name:='Boolean'; NativeType:= 'L';
Size.Max:=1; Size.Min:=1; Size.Def:=1;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L4,6] do
begin
Name:='Memo'; NativeType:= 'M';
Size.Max:=10; Size.Min:=10; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
(****** dBASE III ************)
with XBase[L3,1] do
begin
Name:='String'; NativeType:= 'C';
Size.Max:=255; Size.Min:=1; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=False; dFixed:=True;
end;
with XBase[L3,2] do
begin
Name:='Numeric'; NativeType:= 'N';
Size.Max:=20; Size.Min:=1; Size.Def:=10;
Digit.Max:=18; Digit.Min:=0; Digit.Def:=2;
sFixed:=False; dFixed:=False;
end;
with XBase[L3,3] do
begin
Name:='Date'; NativeType:= 'D';
Size.Max:=8; Size.Min:=8; Size.Def:=8;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L3,4] do
begin
Name:='Boolean'; NativeType:= 'L';
Size.Max:=1; Size.Min:=1; Size.Def:=1;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
with XBase[L3,5] do
begin
Name:='Memo'; NativeType:= 'M';
Size.Max:=10; Size.Min:=10; Size.Def:=10;
Digit.Max:=0; Digit.Min:=0; Digit.Def:=0;
sFixed:=True; dFixed:=True;
end;
end;
function VCLType(NativeType: Char; Level: TLevel): string;
var
I: Integer;
begin
case Level of
L3,L4,L7:
for I := 1 to FieldTypeCount[Level] do
if NativeType = XBase[Level,I].NativeType then
begin
Result := XBase[Level,I].Name;
Exit;
end;
end;
Result := 'Unknown';
end;
function DBaseVersion(Dbf: TDbf): string;
begin
Result := DbfVer[Dbf.DbfFieldDefs.DbfVersion];
end;
initialization
DefaultFormatSettings.ShortDateFormat :=
'dd'+DateSeparator+'mm'+DateSeparator+'yyyy';
DefaultFormatSettings.LongDateFormat :=
'dd'+DateSeparator+'mm'+DateSeparator+'yyyy';
DefaultFormatSettings.DateSeparator := DateSeparator;
DefaultFormatSettings.DecimalSeparator := DecimalSeparator;
(********)
InitFieldProperties;
end.