forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VariantRtn.pas
532 lines (454 loc) · 13.3 KB
/
VariantRtn.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2011 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit VariantRtn;
interface
{$I FIBPlus.inc}
uses
SysUtils,FIBPlatforms {$IFDEF D6+}, Variants{$ENDIF};
{$IFDEF SUPPORT_ARRAY_FIELD}
type
// CallBack procedures definitions
TProcReadElementValue=procedure (Value:Variant; IndexValue:array of integer;
const HighBoundInd:integer;
Var Continue:boolean
);
TProcWriteElementValue=
procedure (OldValue:Variant; IndexValue:array of integer;
Var NewValue:Variant; Var Continue:boolean
);
// End CallBack procedures definitions
function CycleReadArray(vArray:Variant;CallBackProc:TProcReadElementValue):boolean;
function CycleWriteArray(var vArray:Variant;CallBackProc:TProcWriteElementValue):boolean;
{$ENDIF}
function CompareVarArray1(vArray1,vArray2:Variant):boolean;
function EasyCompareVarArray1(vArray1,vArray2:Variant;HighBound:integer):boolean;
function NeedCastForCompare(const v,v1:Variant):boolean;
{$IFNDEF D6+}
function VarTypeIsNumeric(const AVarType: word): Boolean;
function VarIsNumeric(const V: Variant): Boolean;
{$ENDIF}
function CompareVariantsEQ(const v,v1:Variant):boolean;
function CompareVariants(const v,v1:Variant):Integer;
function ComparePDoubleAndVariant(P:PDouble;const v:Variant):Integer;
function ComparePIntegerAndVariant(P:PInteger;const v:Variant):Integer;
function ComparePInt64AndVariant(P:PInt64;const v:Variant):Integer;
function ComparePByteAndVariant(P:PByte;const v:Variant):Integer;
function ComparePDateAndVariant(P:PDateTime;const v:Variant):Integer;
function ComparePSmallAndVariant(P:PSmallInt;const v:Variant):Integer;
function CompareWideStringAndVariant(P:Pointer;const v:Variant):Integer;
function ComparePCurrencyAndVariant(P:PCurrency;const v:Variant):Integer;
function ComparePShortAndVariant(P:PShortInt;const v:Variant):Integer;
implementation
uses FIBConsts;
{$IFNDEF D6+}
{$I SysVariantD5.inc}
{$ENDIF}
// CompareRnts
function GetVariantData(const v: Variant):Pointer;
begin
case TVarData(v).VType of
varSmallInt:
result:=@TVarData(v).VSmallInt;
varInteger:
result:=@TVarData(v).VInteger;
varSingle :
result:=@TVarData(v).VSingle;
{$IFDEF D6+}
varInt64:
result:=@TVarData(v).VInt64;
varShortInt:
result:=@TVarData(v).VShortInt;
varWord:
result:=@TVarData(v).VWord;
varLongWord:
result:=@TVarData(v).VLongWord;
{$ENDIF}
varByte:
result:=@TVarData(v).VByte;
varDouble : result:=@TVarData(v).VDouble;
varCurrency: result:=@TVarData(v).VCurrency;
varDate : result:=@TVarData(v).VDate;
varOleStr : result:=TVarData(v).VOleStr;
varBoolean : result:=@TVarData(v).VBoolean;
varVariant,varByRef: result:=GetVariantData(PVariant(@TVarData(v).VPointer)^);
varString : result:=TVarData(v).VString;
{$IFDEF D2009+}
varUString : result:=TVarData(v).VUString;
{$ENDIF}
else
result := nil;
end;
end;
{$DEFINE BODY_NUMERIC_COMPARE_EQ}
{$WARNINGS OFF}
function ComparePIntegerAndVariant(P:PInteger;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePSmallAndVariant(P:PSmallInt;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePSingleAndVariant(P:PSingle;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePDoubleAndVariant(P:PDouble;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePCurrencyAndVariant(P:PCurrency;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePDateAndVariant(P:PDateTime;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePInt64AndVariant(P:PInt64;const v:Variant):Integer;
{$DEFINE C_INT64}
{$I VarFnc.inc}
{$UNDEF C_INT64}
function ComparePByteAndVariant(P:PByte;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePShortAndVariant(P:PShortInt;const v:Variant):Integer;
{$I VarFnc.inc}
{$IFDEF D6+}
function ComparePWordAndVariant(P:PWord;const v:Variant):Integer;
{$I VarFnc.inc}
function ComparePLongWordAndVariant(P:PLongWord;const v:Variant):Integer;
{$I VarFnc.inc}
{$ENDIF}
{$WARNINGS ON}
{$UNDEF BODY_NUMERIC_COMPARE_EQ}
function CompareWideStringAndVariantEQ(P:Pointer;const v:Variant):boolean;
begin
case VarType(v) of
varString: result:=WideString(P)=string(TVarData(v).VString);
varOleStr: result:=WideString(P)=WideString(TVarData(v).VOleStr);
{$IFDEF D2009+}
varUString : result:=WideString(P)=string(TVarData(v).VUString);
{$ENDIF}
else
Result := WideString(P)=VarToStr(V);
end;
end;
function CompareStringAndVariantEQ(P:Pointer;const v:Variant):boolean;
begin
case VarType(v) of
varString: result:=AnsiString(P)=String(TVarData(v).VString);
varOleStr: result:=AnsiString(P)=WideString(TVarData(v).VOleStr);
{$IFDEF D2009+}
varUString : result:=AnsiString(P)=string(TVarData(v).VUString);
{$ENDIF}
else
result := AnsiString(P)=VarToStr(V);
end;
end;
function CompareWideStringAndVariant(P:Pointer;const v:Variant):Integer;
begin
case VarType(v) of
varString:
begin
if WideString(P)=string(TVarData(v).VString) then
Result := 0
else
if WideString(P)>string(TVarData(v).VString) then
Result:=1
else
Result := -1;
end;
varOleStr:
begin
if WideString(P)=WideString(TVarData(v).VOleStr) then
Result := 0
else
if WideString(P)>WideString(TVarData(v).VOleStr) then
Result:=1
else
Result := -1;
end;
{$IFDEF D2009+}
varUString :
begin
if WideString(P)=string(TVarData(v).VUString) then
Result := 0
else
if WideString(P)>string(TVarData(v).VUString) then
Result:=1
else
Result := -1;
end;
{$ENDIF}
else
if WideString(P)=VarToStr(V) then
Result :=0
else
if WideString(P)>VarToStr(V) then
Result:=1
else
Result := -1;
end;
end;
{$WARNINGS OFF}
function CompareStringAndVariant(P:Pointer;const v:Variant):Integer;
{$IFDEF D2009+}
var
s:UnicodeString;
{$ENDIF}
begin
case VarType(v) of
varString:
begin
if AnsiString(P)=AnsiString(TVarData(v).VString) then
Result :=0
else
if AnsiString(P)>AnsiString(TVarData(v).VString) then
Result :=1
else
Result :=-1
end;
varOleStr:
begin
if AnsiString(P)=WideString(TVarData(v).VOleStr) then
Result :=0
else
if AnsiString(P)>WideString(TVarData(v).VOleStr) then
Result :=1
else
Result :=-1
end;
{$IFDEF D2009+}
varUString:
begin
s:=AnsiString(P);
if s=UnicodeString(TVarData(v).vUString) then
Result :=0
else
begin
if s>UnicodeString(TVarData(v).vUString) then
Result:= 1
else
Result := -1;
end
end
{$ENDIF}
else
if AnsiString(P)=VarToStr(V) then
Result :=0
else
if AnsiString(P)>VarToStr(V) then
Result:=1
else
Result := -1;
end;
end;
{$WARNINGS ON}
function CompareVariants(const v,v1:Variant):Integer;
begin
case TVarData(v).VType of
varSmallInt:
result:=ComparePSmallAndVariant(GetVariantData(v),v1);
varInteger :
result:=ComparePIntegerAndVariant(GetVariantData(v),v1);
varSingle :
result:=ComparePSingleAndVariant(GetVariantData(v),v1);
varDouble :
result:=ComparePDoubleAndVariant(GetVariantData(v),v1);
varCurrency:
result:=ComparePCurrencyAndVariant(GetVariantData(v),v1);
varDate :
result:=ComparePDateAndVariant(GetVariantData(v),v1);
varOleStr:
result:=CompareWideStringAndVariant(GetVariantData(v),v1);
{$IFDEF D6+}
varShortInt:
result:=ComparePShortAndVariant(GetVariantData(v),v1);
varByte :
result:=ComparePByteAndVariant(GetVariantData(v),v1);
varWord :
result:=ComparePWordAndVariant(GetVariantData(v),v1);
varLongWord:
result:=ComparePLongWordAndVariant(GetVariantData(v),v1);
varInt64 :
result:=ComparePInt64AndVariant(GetVariantData(v),v1);
{$ENDIF}
//{$IFNDEF D_XE}
varString :
result:=CompareStringAndVariant(GetVariantData(v),v1);
//{$ENDIF}
else
if v=v1 then
Result := 0
else
if v>v1 then
Result:=1
else
Result:=-1
end;
end;
function CompareVariantsEQ(const v,v1:Variant):boolean;
begin
case TVarData(v).VType of
varSmallInt:
result:=ComparePSmallAndVariant(GetVariantData(v),v1)=0;
varInteger :
result:=ComparePIntegerAndVariant(GetVariantData(v),v1)=0;
varSingle :
result:=ComparePSingleAndVariant(GetVariantData(v),v1)=0;
varDouble :
result:=ComparePDoubleAndVariant(GetVariantData(v),v1)=0;
varCurrency:
result:=ComparePCurrencyAndVariant(GetVariantData(v),v1)=0;
varDate :
result:=ComparePDateAndVariant(GetVariantData(v),v1)=0;
varOleStr:
result:=CompareWideStringAndVariantEQ(GetVariantData(v),v1);
{$IFDEF D6+}
varShortInt:
result:=ComparePShortAndVariant(GetVariantData(v),v1)=0;
varByte :
result:=ComparePByteAndVariant(GetVariantData(v),v1)=0;
varWord :
result:=ComparePWordAndVariant(GetVariantData(v),v1)=0;
varLongWord:
result:=ComparePLongWordAndVariant(GetVariantData(v),v1)=0;
varInt64 :
result:=ComparePInt64AndVariant(GetVariantData(v),v1)=0;
{$ENDIF}
varString :
result:=CompareStringAndVariantEQ(GetVariantData(v),v1);
else
result:=v=v1;
end;
end;
//////
function NeedCastForCompare(const v,v1:Variant):boolean;
begin
case VarType(v) of
varDate: Result:= not (VarIsNumeric(v1) or (VarType(v1)=varDate));
varBoolean:Result:= VarType(v1)<>varBoolean;
else
case VarType(v1) of
varDate : Result:= not (VarIsNumeric(v) {or (VarType(v)=varDate)});
varBoolean: Result:= True;
else
Result:= (VarIsNumeric(v1) xor VarIsNumeric(v))
end
end;
end;
function EasyCompareVarArray1( vArray1,vArray2:Variant;HighBound:integer)
:boolean;
var
j:integer;
v,v1:Variant;
begin
Result:=False;
try
for j:= HighBound downto 0 do
begin
v1:=vArray2[j];
v :=vArray1[j];
if VarIsEmpty(v) then v := Null;
if VarIsEmpty(v1) then v1 := Null;
if (VarIsNull(v) xor VarIsNull(v1)) or (v<>v1) then
Exit;
end;
Result:=True
except
end
end;
function CompareVarArray1(vArray1,vArray2:Variant):boolean;
var j,l,h:integer;
begin
Result:=False;
try
if VarIsArray(vArray1) and VarIsArray(vArray2) then
begin
h:=VarArrayHighBound(vArray1,1);
l:=VarArrayLowBound(vArray1,1);
for j:=l to h do
begin
if vArray1[j]<>vArray2[j] then
Exit;
end;
Result:=True
end
except
end
end;
{$IFDEF SUPPORT_ARRAY_FIELD}
function NextElements(v: Variant;var CurIndex:array of Integer): boolean;
var
Dimensions: integer;
i: integer;
begin
Result := False;
Dimensions := VarArrayDimCount(v);
for i := Dimensions-1 downto 0 do
begin
if CurIndex[i] = VarArrayHighBound(v,i+1) then
begin
CurIndex[i] := VarArrayLowBound(v,i+1);
end
else
begin
CurIndex[i] := CurIndex[i]+1;
Result := True;
Exit;
end;
end;
end;
function CycleReadArray(vArray:Variant;CallBackProc:TProcReadElementValue):boolean;
var
Value:Variant;
CurIndex:array of Integer;
DimCount,I:Integer;
begin
Result:=false;
if not Assigned(CallBackProc) then Exit;
if not VarIsArray(vArray) then Exit;
DimCount := VarArrayDimCount(vArray);
SetLength(CurIndex,DimCount);
for i:=0 to DimCount-1 do
CurIndex[i]:= VarArrayLowBound(vArray,i+1);
// InitializationCurIndexArray(vArray,CurIndex);
repeat
Value:= VarArrayGet(vArray, CurIndex);
Result:=true;
CallBackProc(Value,CurIndex,DimCount-1,Result);
if not Result then Exit;
if not NextElements(vArray,CurIndex) then Break;
until False;
end;
function CycleWriteArray
(var vArray:Variant;CallBackProc:TProcWriteElementValue):boolean;
var
OldValue,NewValue: Variant;
CurIndex:array of Integer;
DimCount,I:Integer;
begin
// vArray - Variant array of Variant
Result:=false;
if not Assigned(CallBackProc) then Exit;
DimCount := VarArrayDimCount(vArray);
SetLength(CurIndex,DimCount);
for i:=0 to DimCount-1 do
CurIndex[i]:= VarArrayLowBound(vArray,i+1);
// InitializationCurIndexArray(vArray,CurIndex);
repeat
OldValue:=VarArrayGet(vArray, CurIndex);
Result:=true;
CallBackProc(OldValue,CurIndex,NewValue,Result);
if not Result then Exit;
VarArrayPut(vArray, NewValue, CurIndex );
if not NextElements(vArray,CurIndex) then Break;
until False;
end;
{$ENDIF}
end.