[go: up one dir, main page]

Menu

[r22]: / rptypeval.pas  Maximize  Restore  History

Download this file

674 lines (601 with data), 15.8 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
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
{*******************************************************}
{ }
{ Rptypeval Datatypes and functions for }
{ the Rpevaluator }
{ Report Manager }
{ }
{ Copyright (c) 1994-2002 Toni Martir }
{ toni@pala.com }
{ }
{ This file is under the MPL license }
{ If you enhace this file you must provide }
{ source code }
{ }
{ }
{*******************************************************}
unit rptypeval;
{$I rpconf.inc}
interface
uses
SysUtils,Classes,rpmdconsts,
{$IFDEF USEVARIANTS}
Variants,
{$ENDIF}
{$IFDEF USEBCD}
FMTBcd,
{$ENDIF}
DB,
rptypes;
const
// Parser datatypes for constants
toEOF = Char(0);
toSymbol = Char(1);
tkString = Char(2);
toInteger = Char(3);
toFloat = Char(4);
toOperator = Char(5);
toWString = Char(6);
// Max number of parameters in a function
Maxparams = 50;
type
// Token datatypes
TRpToken=(totEOF,totSymbol,totString,totInteger,TotFloat,totOperator,
totBoolean,totDate,totTime,totDateTime);
// New exception to raise avaluating operations
TRpNamedException=Class(Exception)
public
ErrorMessage:string;
// The element implied on the exception
ElementError:string;
constructor Create(Msg,Element:String);
end;
// New exception to raise avaluating operations
TRpEvalException=Class(TRpNamedException)
public
// Error position
ErrorLine:Integer;
ErrorPosition:Integer;
// Constructor for the Eval Exception
constructor Create(Msg,Element:String;Line,Position:integer);
end;
// Data type of a value returned by the expresion evaluator
TRpValue = Variant;
// Identifier }
RTypeIdentificator = (RTypeidenFunction,RTypeIdenVariable,RTypeIdenConstant);
// Identifier, abstract object
TRpIdentifier=class(TComponent)
protected
FParamCount:integer;
procedure SetRpValue(Value:TRpValue);virtual;abstract;
function GetRpValue:TRpValue;virtual;abstract;
public
evaluator:TComponent;
// Identifier type
RType:RTypeIdentificator;
// Internal identifier
Idenname:string;
// Help
help:string;
model:string;
aparams:string;
// Param function structure
params:array[0..MAXparams-1] of TRpValue;
constructor Create(AOwner:TComponent);override;
// Parameter count
property ParamCount:integer read FParamCount;
// Function Value (read) or variable value(read/write)
property Value:TRpValue read GeTRpValue write SeTRpValue;
end;
// Generic function, all function can inherit from this object
TIdenFunction=class(TRpIdentifier)
protected
procedure SetRpValue(Value:TRpValue);override;
public
constructor Create(AOwner:TComponent);override;
end;
// Variable
TIdenVariable=class(TRpIdentifier)
protected
FValue:TRpValue;
procedure SetRpValue(Value:TRpValue);override;
function GetRpValue:TRpValue;override;
public
constructor Create(AOwner:TComponent);override;
end;
// Constant
TIdenConstant=class(TRpIdentifier)
protected
FValue:TRpValue;
procedure SetRpValue(Value:TRpValue);override;
function GetRpValue:TRpValue;override;
public
constructor Create(AOwner:TComponent);override;
end;
// Field
TIdenField=class(TRpIdentifier)
private
FField:TField;
protected
function GetRpValue:TRpValue;override;
procedure SetRpValue(Value:TRpValue);override;
public
constructor Create(AOwner:TComponent);override;
constructor CreateField(AOWner:TComponent;Nom:string);
property Field:TField read FField write FField;
end;
// True Constant
TIdenTrue=class(TIdenConstant)
public
constructor Create(AOwner:TComponent);override;
end;
// False Constant
TIdenFalse=class(TIdenConstant)
public
constructor Create(AOwner:TComponent);override;
end;
// Math operations
function SumTRpValue(Value1,Value2:TRpValue):TRpValue;
function DifTRpValue(Value1,Value2:TRpValue):TRpValue;
function MultTRpValue(Value1,Value2:TRpValue):TRpValue;
function DivTRpValue(Value1,Value2:TRpValue):TRpValue;
function SignTRpValue(Value:TRpValue):TRpValue;
// Comp operations
function EqualTRpValue(Value1,Value2:TRpValue):TRpValue;
function EqualEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
function MorethanEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
function LessthanEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
function MorethanTRpValue(Value1,Value2:TRpValue):TRpValue;
function LessthanTRpValue(Value1,Value2:TRpValue):TRpValue;
function DiferentTRpValue(Value1,Value2:TRpValue):TRpValue;
function IsNullTRpValue(Value:TRpValue):Boolean;
// Logical operations
function LogicalANDTRpValue(Value1,Value2:TRpValue):TRpValue;
function LogicalORTRpValue(Value1,Value2:TRpValue):TRpValue;
function LogicalNOTTRpValue(Value1:TRpValue):TRpValue;
// Formatting operations
function TRpValueToString(Value:TRpValue):string;
function FormatTRpValue(sFormat:string;Value:TRpValue;Userdef:Boolean):string;
// Date operation validation
procedure DatetimeValidation(var Value1,Value2:TRpValue);
var
DefaultDecimals:Integer;
implementation
// TRpNamedexception
constructor TRpNamedException.Create(Msg,Element:string);
begin
ErrorMessage:=Msg;
ElementError:=Element;
inherited Create(ErrorMessage);
end;
// TRpEvalException
constructor TRpEvalException.Create(Msg,Element:string;Line,Position:integer);
begin
Inherited Create(Msg,Element);
ErrorLine:=Line;
ErrorPosition:=Position;
ErrorMessage:=Msg;
ElementError:=Element;
end;
// TRpIdentifier
constructor TRpIdentifier.Create(AOwner:TComponent);
begin
inherited create(AOwner);
help:=SRpNohelp;
aparams:=SRpNoaparams;
model:=SRpNomodel;
end;
// TIdenfunction
constructor TIdenFunction.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
RType:=RTypeIdenfunction;
end;
procedure TIdenFunction.SeTRpValue(Value:TRpValue);
begin
Raise TRpNamedException.Create(SRpAssignFunc,
Idenname);
end;
// TIdenTrue
constructor TIdenTrue.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FValue:=True;
RType:=RTypeIdenConstant;
help:=SRpTrueHelp;
Idenname:='True';
end;
// TIdenFalse
constructor TIdenFalse.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FValue:=False;
help:=SRpFalseHelp;
Idenname:='False';
end;
// TIdenVariable
constructor TIdenVariable.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
RType:=RTypeIdenvariable;
end;
function TIdenVariable.GeTRpValue:TRpValue;
begin
Result:=FValue;
end;
procedure TIdenVariable.SeTRpValue(Value:TRpValue);
begin
FValue:=Value;
end;
// TIdenConstant
constructor TIdenConstant.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
RType:=RTypeidenconstant;
end;
function TIdenConstant.GeTRpValue:TRpValue;
begin
Result:=FValue;
end;
procedure TIdenConstant.SeTRpValue(Value:TRpValue);
begin
Raise TRpNamedException.Create(SRpAssignConst,
Idenname);
end;
// TIdenField
constructor TIdenField.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
end;
constructor TIdenField.CreateField(AOwner:TComponent;Nom:string);
begin
inherited Create(AOwner);
Idenname:=Nom;
help:=SRpFieldHelp;
end;
procedure TIdenField.SeTRpValue(Value:TRpValue);
begin
Raise TRpNamedException.Create(SRpAssignfield,
Idenname);
end;
function TIdenField.GeTRpValue:TRpValue;
var
atype:TVarType;
begin
if Field=nil then
begin
Result:=Null;
Exit;
end;
Result:=Field.AsVariant;
{$IFNDEF USEBCD}
atype:=VarType(Result);
if atype=14 then
Result:=Field.AsInteger;
{$ENDIF}
{$IFDEF USEBCD}
atype:=VarType(Result);
if atype=varFmtBCD then
begin
Result:=BCDToDouble(VarToBCD(Result));
end;
{$ENDIF}
end;
// Math Functions
function SumTRpValue(Value1,Value2:TRpValue):TRpValue;
var
atype1,atype2:TVarType;
begin
if VarIsNull(Value1) then
begin
Result:=Value2;
end
else
if VarIsNull(Value2) then
begin
Result:=Value1;
end
else
begin
// Bugfix suming widestring+string
// The Result should be by default a widestring
// but it's a String
atype1:=VarType(Value1);
atype2:=VarType(Value2);
if atype1=atype2 then
Result:=Value1+Value2
else
begin
if atype1=varOleStr then
Result:=Value1+WideString(Value2)
else
if atype2=varOleStr then
Result:=WideString(Value1)+Value2
else
Result:=Value1+Value2
end;
end;
end;
function DifTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
if Value1=NULL then
begin
Result:=-Value2;
end
else
if Value2=NULL then
begin
Result:=Value1;
end
else
Result:=Value1-Value2;
end;
function MultTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
Result:=Value1*Value2;
end;
function DivTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
Result:=Value1/Value2;
end;
function SignTRpValue(Value:TRpValue):TRpValue;
begin
Result:=-Value;
end;
// Formating functions
function TRpValueToString(Value:TRpValue):string;
begin
if Value=NULL then
begin
result:='';
exit;
end;
case VarType(Value) of
vardate:
Result:=FormatDateTime(FormatSettings.ShortDateFormat,TDateTime(Value));
else
Result:=Value;
end;
end;
function FormatTRpValue(sFormat:string;Value:TRpValue;Userdef:Boolean):string;
var
index:integer;
General:Boolean;
begin
if VarIsNull(Value) then
begin
result:='';
exit;
end;
case VarType(Value) of
varSmallInt..varInteger:
begin
if Not Userdef then
begin
General:=False;
index:=Pos('n',sformat);
if index<>0 then
General:=True
else
begin
index:=Pos('f',sformat);
if index<>0 then
General:=True
else
begin
index:=Pos('m',sformat);
if index<>0 then
General:=True
end;
end;
if General then
begin
if Pos('.',sformat)=0 then
begin
Insert('.'+IntToStr(DefaultDecimals),sformat,index);
end;
end;
end;
if Userdef then
Result:=sysutils.formatFloat(sformat,Double(Value))
else
begin
index:=Pos('d',sformat);
if index<>0 then
Result:=format(sformat,[Integer(Value)])
else
Result:=format(sformat,[Double(Value)]);
end;
end;
varSingle,varDouble,VarCurrency:
begin
if Not Userdef then
begin
General:=False;
index:=Pos('n',sformat);
if index<>0 then
General:=True
else
begin
index:=Pos('f',sformat);
if index<>0 then
General:=True
else
begin
index:=Pos('m',sformat);
if index<>0 then
begin
General:=True;
end;
end;
end;
if (General) then
begin
if Pos('.',sformat)=0 then
begin
Insert('.'+IntToStr(DefaultDecimals),sformat,index);
end;
end;
end;
try
if Userdef then
Result:=sysutils.formatFloat(sformat,Double(Value))
else
Result:=format(sformat,[Double(Value)]);
except
if Double(Value)<>Integer(Value) then
Raise;
if Userdef then
Result:=sysutils.formatFloat(sformat,Double(Value))
else
Result:=format(sformat,[Integer(Value)]);
end;
end;
{$IFNDEF DOTNETD}
varOleStr,
{$ENDIF}
varString:
if Value='' then Result:='' else
Result:=format(sformat,[string(Value)]);
varBoolean:
begin
if Userdef then
Result:=sformat
else
if Boolean(Value) then
Result:='True'
else
Result:='False';
end;
varDate:
begin
if Userdef then
begin
Result:=FormatDateTime(sformat,TDateTime(Value));
end
else
begin
if (sformat='%xl') then
begin
if TDateTime(Value)=0 then
begin
Result:='';
Exit;
end;
if TDateTime(Value)<1 then
Result:=FormatDateTime(FormatSettings.LongTimeFormat,TDateTime(Value))
else
if TDateTime(Value)-Trunc(double(Value))=0 then
Result:=FormatDateTime(FormatSettings.LongDateFormat,TDateTime(Value))
else
Result:=FormatDateTime(FormatSettings.LongDateFormat+' '+FormatSettings.LongTimeFormat,TDateTime(Value))
end
else
begin
if TDateTime(Value)=0 then
begin
Result:='';
Exit;
end;
if TDateTime(Value)<1 then
Result:=FormatDateTime(FormatSettings.ShortTimeFormat,TDateTime(Value))
else
if TDateTime(Value)-Trunc(double(Value))=0 then
Result:=FormatDateTime(FormatSettings.ShortDateFormat,TDateTime(Value))
else
Result:=FormatDateTime(FormatSettings.ShortDateFormat+' '+FormatSettings.ShortTimeFormat,TDateTime(Value))
end;
end;
end;
else
Result:='';
end;
end;
// Logical functions
function IsNullTRpValue(Value:TRpValue):Boolean;
begin
case VarType(Value) of
varSmallInt..varDouble:
Result:=(Double(Value)=0);
{$IFNDEF DOTNETD}
varOleStr,
{$ENDIF}
varString:
Result:=(String(Value)='');
varBoolean:
Result:=Not Boolean(Value);
varDate:
Result:=(TDateTime(Value)=0);
else
Result:=VarIsNull(Value);
end;
end;
function LogicalANDTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
Result:=Value1 And Value2;
end;
function LogicalORTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
Result:=Value1 or Value2;
end;
function LogicalNOTTRpValue(Value1:TRpValue):TRpValue;
begin
Result:=NOT Value1;
end;
function EqualTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1=Value2);
end;
function MorethanEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=Value1>=Value2;
end;
function LessthanEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1<=Value2);
end;
function MorethanTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1>Value2);
end;
function LessthanTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1<Value2);
end;
function DiferentTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1<>Value2);
end;
function EqualEqualTRpValue(Value1,Value2:TRpValue):TRpValue;
begin
DatetimeValidation(Value1,Value2);
Result:=(Value1=Value2);
end;
// Date time validation
procedure DatetimeValidation(var Value1,Value2:TRpValue);
begin
if VarType(Value1)=varDate then
begin
if VarType(Value2)<>varDate then
if Value2<>null then
begin
Value2:=varastype(Value2,varDate);
end;
end;
if VarType(Value2)=varDate then
begin
if VarType(Value1)<>varDate then
if Value1<>Null then
begin
Value1:=varastype(Value1,varDate);
end;
end;
end;
initialization
DefaultDecimals:=2;
end.