-
Notifications
You must be signed in to change notification settings - Fork 5
/
TSMask.pas
2252 lines (1975 loc) · 69.4 KB
/
TSMask.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*******************************************************}
{ }
{ Top Support Visual Components }
{ Classes for masked edit }
{ }
{ Copyright (c) 1998 - 1999, Top Support }
{ }
{*******************************************************}
unit TSMask;
{$INCLUDE TSCmpVer}
interface
uses
Windows, Classes, SysUtils {$IFDEF TSVER_V6}, Variants {$ENDIF};
type
TtsShowThousands = (sthParent, sthOn, sthOff);
TtsShowTrailZeros = (stzParent, stzOn, stzOff);
TtsSymbolPosition = (spoParent, spoWindows, spoBefore, spoAfter);
TtsCaseType = (cstNone, cstUpper, cstLower, cstSentence, cstTitle);
TtsPictureType = (pctText, pctDate, pctTime);
TtsPictureNodeType = (pntNone, pntGroup, pntLiteralChars, pntMaskChars,
pntCase, pntRange);
TtsMaskOption = (moNone, moUpper, moOptUpper, moLower, moOptLower,
moFullCompare, moConvertInput, moInsertLiteral, moCheckUnique);
TtsMaskOptions = set of TtsMaskOption;
TtsMaskCheck = (mcOnEdit, mcOnExit);
TtsMaskChecks = set of TtsMaskCheck;
EtsMaskError = class(Exception);
TtsParsePictureEvent = procedure(Sender: TObject; Picture: string; PictureType: TtsPictureType;
var Cancel: Boolean) of object;
TtsMaskErrorEvent = procedure(Sender: TObject; Msg: string; var RaiseError: Boolean) of object;
type
TtsMask = class;
TtsMaskDefs = class;
TtsMaskCollection = class;
TtsPictureNode = class;
TtsMaskLink = class;
TtsCharSet = set of Char;
TtsPictureNode = class(TObject)
protected
FNodeType: TtsPictureNodeType;
FCount: Integer;
FOptional: Boolean;
FComplement: Boolean;
FStartPos: Integer;
FEndPos: Integer;
FCharCount: Integer;
FSubItems: TtsPictureNode;
FNextItem: TtsPictureNode;
FNextAlternative: TtsPictureNode;
public
constructor Create(NodeType: TtsPictureNodeType); virtual;
destructor Destroy; override;
property Complement: Boolean read FComplement write FComplement;
property Count: Integer read FCount write FCount;
property SubItems: TtsPictureNode read FSubItems write FSubItems;
property NextItem: TtsPictureNode read FNextItem write FNextItem;
property NextAlternative: TtsPictureNode read FNextAlternative write FNextAlternative;
property NodeType: TtsPictureNodeType read FNodeType write FNodeType;
property Optional: Boolean read FOptional write FOptional;
property StartPos: Integer read FStartPos write FStartPos;
property EndPos: Integer read FEndPos write FEndPos;
property CharCount: Integer read FCharCount write FCharCount;
end;
TtsPictureList = class(TObject)
protected
FPictureType: TtsPictureType;
FStartNode: TtsPictureNode;
public
constructor Create(PictureType: TtsPictureType); virtual;
destructor Destroy; override;
property PictureType: TtsPictureType read FPictureType write FPictureType;
property StartNode: TtsPictureNode read FStartNode write FStartNode;
end;
TtsParseStackElement = record
Parent: Integer;
Node: TtsPictureNode;
Count: Integer;
TextPos: Integer;
end;
PtsParseStackArray = ^TtsParseStackArray;
TtsParseStackArray = array[1..(MaxListSize div ((SizeOf(TtsParseStackElement) div SizeOf(Longint)) + 1))] of TtsParseStackElement;
TtsParseStack = class(TObject)
protected
FBuffer: PtsParseStackArray;
FBufSize: Integer;
FCount: Integer;
function Push(Parent: Integer; Node: TtsPictureNode; TextPos, Count: Integer): Integer;
procedure Pop;
procedure Reset;
property Count: Integer read FCount;
property Items: PtsParseStackArray read FBuffer;
public
constructor Create;
destructor Destroy; override;
end;
TtsTextCase = class(TPersistent)
protected
FMask: TtsMask;
FCaseType: TtsCaseType;
FOptional: Boolean;
procedure SetCaseType(Value: TtsCaseType);
procedure SetOptional(Value: Boolean);
public
constructor Create(Mask: TtsMask); virtual;
procedure Assign(Source: TPersistent); override;
published
property CaseType: TtsCaseType read FCaseType write SetCaseType default cstNone;
property Optional: Boolean read FOptional write SetOptional default False;
end;
TtsMaskInput = record
Text: string;
TextPos: Integer;
InsertPos: Integer;
InsertLen: Integer;
Literals: string;
MatchedLiterals: string;
MinMatchedLiterals: string;
InsertChars: string;
end;
TtsTextMask = class(TPersistent)
protected
FMask: TtsMask;
FPictureList: TtsPictureList;
FPictureLength: Integer;
FValidPicture: Boolean;
FPictureParsed: Boolean;
FCreateTree: Boolean;
{property fields}
FPicture: string;
{Error detection procedures}
procedure ErrChar(Chars: string; TextPos: Integer);
{Create procedures}
function CreatePictureTree(PictureType: TtsPictureType): TtsPictureList;
function CreatePictureNode(NodeType: TtsPictureNodeType): TtsPictureNode;
{Parse procedures}
function CharCount(const Text: string; TextPos: Integer): Integer;
function PictureCharCount(TextPos: Integer): Integer;
function IsLiteral(TextPos: Integer): Boolean;
function IsMaskChar(TextPos: Integer): Boolean;
function IsCaseChar(TextPos: Integer): Boolean;
function IsRangeChar(TextPos: Integer): Boolean;
function IsCountChar(TextPos: Integer): Boolean;
function IsGroupStart(TextPos: Integer): Boolean;
function IsGroupEnd(TextPos: Integer): Boolean;
procedure CheckGroupEnd(var TextPos: Integer; EndChar: Char);
function IsRange(TextPos: Integer; var Chars: Integer): Boolean;
function GetItemCount(var TextPos: Integer): Integer;
function GetGroupType(var TextPos: Integer; var EndChar: Char): Boolean;
function GetItem(var TextPos: Integer; Chars: Integer; NodeType: TtsPictureNodeType): TtsPictureNode;
function GetNextItem(var TextPos: Integer): TtsPictureNode;
function GetRange(var TextPos: Integer; var Node: TtsPictureNode): Boolean;
function GetNextAlternative(var TextPos: Integer; EndChar: Char): TtsPictureNode;
function GetGroup(var TextPos: Integer): TtsPictureNode;
procedure ParsePicture(SyntaxOnly: Boolean);
function CheckInput(Stack: TtsParseStack; MaskInput: TtsMaskInput;
Options: TtsMaskOptions; var InsertChars: string): Boolean;
{Value check procedures}
function CheckCase(Text: string; TextPos: Integer; Chars: Integer; Option: TtsMaskOption): Boolean;
function IsWordStart(MaskInput: TtsMaskInput): Boolean;
function IsSentenceStart(MaskInput: TtsMaskInput): Boolean;
function GetCaseType(const MaskInput: TtsMaskInput; Options: TtsMaskOptions;
DefaultCase: TtsMaskOption): TtsMaskOption;
function IsInputChar(const MaskInput: TtsMaskInput): Boolean;
function CanConvertCase(const MaskInput: TtsMaskInput; Options: TtsMaskOptions; CaseType: TtsMaskOption): Boolean;
procedure ConvertCase(var MaskInput: TtsMaskInput; Chars: Integer; ConvertType: TtsMaskOption);
function CanInsertLiteral(MaskInput: TtsMaskInput; Options: TtsMaskOptions): Boolean;
function CheckLiteralChar(var MaskInput: TtsMaskInput; var MaskPos: Integer;
var Matched: Boolean; Node: TtsPictureNode; Options: TtsMaskOptions): Boolean;
function CheckLiterals(Node: TtsPictureNode; var MaskInput: TtsMaskInput;
Options: TtsMaskOptions; var Matched: Boolean): Boolean;
function CheckMaskChar(var MaskInput: TtsMaskInput; var MaskPos: Integer; Options: TtsMaskOptions): Boolean;
function CheckMaskChars(Node: TtsPictureNode; var MaskInput: TtsMaskInput; Options: TtsMaskOptions): Boolean;
function CheckRange(Node: TtsPictureNode; var MaskInput: TtsMaskInput; Options: TtsMaskOptions): Boolean;
function CheckItem(Node: TtsPictureNode; var MaskInput: TtsMaskInput; Options: TtsMaskOptions): Boolean;
procedure CheckCaseOptions(Node: TtsPictureNode; var Options: TtsMaskOptions);
function CheckNextItems(Stack: TtsParseStack; StackPos: Integer; Options: TtsMaskOptions;
var MaskInput: TtsMaskInput; var NrOfMatches: Integer): Boolean;
function CheckSubItems(Stack: TtsParseStack; Parent: Integer; Node: TtsPictureNode;
Count: Integer; Options: TtsMaskOptions;
var MaskInput: TtsMaskInput; var NrOfMatches: Integer): Boolean;
{Property procedures}
procedure SetPicture(Value: string);
function GetTextCase: TtsTextCase;
function GetPictureList: TtsPictureList;
function ValidText(const Text: string; FullCompare: Boolean): Boolean;
function ValidInput(const Text: string; var InsertChars: string; InsertPos: Integer;
FullCompare, AutoFill: Boolean): Boolean;
property PictureList: TtsPictureList read GetPictureList write FPictureList;
property Picture: string read FPicture write SetPicture;
property TextCase: TtsTextCase read GetTextCase;
public
constructor Create(Mask: TtsMask); virtual;
destructor Destroy; override;
end;
TtsMaskItem = class(TCollectionItem)
protected
function GetAutoFill: TtsMaskChecks; virtual; abstract;
procedure SetAutoFill(Value: TtsMaskChecks); virtual; abstract;
function GetEvaluate: TtsMaskChecks; virtual; abstract;
procedure SetEvaluate(Value: TtsMaskChecks); virtual; abstract;
function GetName: string; virtual; abstract;
procedure SetName(Value: string); virtual; abstract;
function GetPicture: string; virtual; abstract;
procedure SetPicture(Value: string); virtual; abstract;
function GetTextCase: TtsTextCase; virtual; abstract;
procedure SetTextCase(Value: TtsTextCase); virtual; abstract;
function GetOnChange: TNotifyEvent; virtual; abstract;
procedure SetOnChange(Value: TNotifyEvent); virtual; abstract;
function GetOnParseError: TtsMaskErrorEvent; virtual; abstract;
procedure SetOnParseError(Value: TtsMaskErrorEvent); virtual; abstract;
public
function ValidText(const Text: string; FullCompare: Boolean): Boolean; virtual; abstract;
function ValidInput(const Text: string; var InsertChars: string; InsertPos: Integer;
FullCompare, AutoFill: Boolean): Boolean; virtual; abstract;
property OnChange: TNotifyEvent read GetOnChange write SetOnChange;
property OnParseError: TtsMaskErrorEvent read GetOnParseError write SetOnParseError;
published
property AutoFill: TtsMaskChecks read GetAutoFill write SetAutoFill default [Low(TtsMaskCheck)..High(TtsMaskCheck)];
property Evaluate: TtsMaskChecks read GetEvaluate write SetEvaluate default [Low(TtsMaskCheck)..High(TtsMaskCheck)];
property Name: string read GetName write SetName;
property Picture: string read GetPicture write SetPicture;
property TextCase: TtsTextCase read GetTextCase write SetTextCase;
end;
TtsMask = class(TtsMaskItem)
protected
FName: string;
FUpdateCount: Integer;
{Property fields}
FTextMask: TtsTextMask;
FAutoFill: TtsMaskChecks;
FEvaluate: TtsMaskChecks;
FPicture: string;
FTextCase: TtsTextCase;
{Event fields}
FOnChange: TNotifyEvent;
FOnParseError: TtsMaskErrorEvent;
{Error detection procedures}
procedure InvalidOp(Msg: string);
function CheckRaise: Boolean;
procedure DoParseError(Msg: string; var RaiseError: Boolean);
function ParentComponent: TComponent; virtual;
procedure CheckName(Value: string);
procedure BeginUpdate;
procedure EndUpdate;
procedure Changed; virtual;
function GetDisplayName: string; {$IFDEF TSVER_V3} override; {$ENDIF}
procedure DoChange; virtual;
{Property procedures}
procedure SetAutoFill(Value: TtsMaskChecks); override;
procedure SetEvaluate(Value: TtsMaskChecks); override;
procedure SetName(Value: string); override;
procedure SetPicture(Value: string); override;
procedure SetTextCase(Value: TtsTextCase); override;
function GetAutoFill: TtsMaskChecks; override;
function GetEvaluate: TtsMaskChecks; override;
function GetName: string; override;
function GetPicture: string; override;
function GetTextCase: TtsTextCase; override;
function GetOnChange: TNotifyEvent; override;
procedure SetOnChange(Value: TNotifyEvent); override;
function GetOnParseError: TtsMaskErrorEvent; override;
procedure SetOnParseError(Value: TtsMaskErrorEvent); override;
property TextMask: TtsTextMask read FTextMask;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function ValidText(const Text: string; FullCompare: Boolean): Boolean; override;
function ValidInput(const Text: string; var InsertChars: string; InsertPos: Integer;
FullCompare, AutoFill: Boolean): Boolean; override;
end;
TtsMaskDefsComponent = class(TComponent)
protected
procedure RemoveLink(MaskLink: TtsMaskLink); virtual; abstract;
procedure AddLink(MaskLink: TtsMaskLink); virtual; abstract;
function GetMask(Index: Variant): TtsMaskItem; virtual; abstract;
procedure SetMask(Index: Variant; Value: TtsMaskItem); virtual; abstract;
public
property Mask[Index: Variant]: TtsMaskItem read GetMask write SetMask;
end;
TtsMaskDefs = class(TtsMaskDefsComponent)
protected
FMasks: TtsMaskCollection;
FLinks: TList;
procedure RemoveLink(MaskLink: TtsMaskLink); override;
procedure AddLink(MaskLink: TtsMaskLink); override;
function GetMask(Index: Variant): TtsMaskItem; override;
procedure SetMask(Index: Variant; Value: TtsMaskItem); override;
procedure RemoveLinks;
procedure SetMasks(Value: TtsMaskCollection);
procedure CheckErrorEvent(Mask: TtsMask);
procedure Changed(Mask: TtsMask); virtual;
procedure DoParseError(Sender: TObject; Msg: string; var RaiseError: Boolean);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Masks: TtsMaskCollection read FMasks write SetMasks;
end;
TtsMaskClass = class of TtsMask;
TtsMaskCollection = class(TCollection)
protected
FMaskDefs: TtsMaskDefs;
function NameIndex(Name: string): Integer;
function GetMask(Index: Variant): TtsMask;
procedure SetMask(Index: Variant; Value: TtsMask);
procedure Update(Item: TCollectionItem); override;
function FindUniqueName(Item: TCollectionItem): string;
function GetOwner: TPersistent; {$IFDEF TSVER_V3} override; {$ENDIF}
procedure SetItemName(Item: TCollectionItem); {$IFDEF TSVER_V3} override; {$ENDIF}
public
constructor Create(MaskDefs: TtsMaskDefs; MaskClass: TtsMaskClass);
function Add: TtsMask;
property MaskDefs: TtsMaskDefs read FMaskDefs;
property Items[Index: Variant]: TtsMask read GetMask write SetMask; default;
end;
TtsMaskLink = class(TPersistent)
protected
FMaskDefs: TtsMaskDefsComponent;
procedure SetMaskDefs(Value: TtsMaskDefsComponent);
procedure MaskChanged(Mask: TtsMaskItem); virtual;
procedure MaskDefsDeleted; virtual;
function GetMask(Name: string): TtsMaskItem;
public
constructor Create;
destructor Destroy; override;
property MaskDefs: TtsMaskDefsComponent read FMaskDefs write SetMaskDefs;
property Mask[Name: string]: TtsMaskItem read GetMask;
end;
const
StsPctExpChar = 'Error in mask %s, expected ''%s'' at position %d';
StsPctErrEnd = 'Error in mask %s, unexpected end of picture';
StsPctInvalidChar = 'Error in mask %s, invalid character ''%s'' at position %d';
StsMaskNotUnique = 'Mask ''%s'' already exists. Mask names must be unique';
implementation
{$R *.dcr}
uses
TSMbcs, TSGLib, Dialogs;
var
FNodeCount: Integer = 0;
const
VersionNumber = '2.0';
{Picture mask characters}
tsPicDigit = '#';
tsPicLetter = '?';
tsPicUpperLetter = '&';
tsPicAny = '@';
tsPicUpperAny = '!';
tsPicOptStartChar = '[';
tsPicOptEndChar = ']';
tsPicNonOptStartChar = '{';
tsPicNonOptEndChar = '}';
tsPicAltChar = ',';
tsPicNotChar = '^';
tsPicRangeChar = '-';
tsPicEscapeChar = ';';
tsPicCountChar = '*';
tsPicLower = '<';
tsPicUpper = '>';
tsPicAllChars = tsPicOptStartChar + tsPicCountChar + tsPicAny + tsPicOptEndChar;
tsPicMaskChars = [tsPicDigit, tsPicLetter, tsPicUpperLetter, tsPicAny,
tsPicUpperAny];
tsPicNonMaskChars = [tsPicAltChar, tsPicOptStartChar, tsPicOptEndChar,
tsPicNonOptStartChar, tsPicNonOptEndChar, tsPicNotChar,
tsPicEscapeChar, tsPicCountChar, tsPicLower, tsPicUpper];
tsPicCharSet = tsPicMaskChars + tsPicNonMaskChars + [tsPicRangeChar];
tsPicNonLiteral = tsPicMaskChars + tsPicNonMaskChars;
function SetMaskInput(Text: string; TextPos, InsertPos, InsertLen: Integer): TtsMaskInput;
begin
Result.Text := Text;
Result.TextPos := 1;
Result.InsertPos := InsertPos;
Result.InsertLen := InsertLen;
Result.Literals := '';
Result.MatchedLiterals := '';
Result.MinMatchedLiterals := '';
Result.InsertChars := '';
end;
procedure CopyMaskInput(const FromInput: TtsMaskInput; var ToInput: TtsMaskInput);
var
Literals: string;
MinLiterals: string;
InsertChars: string;
begin
Literals := ToInput.MatchedLiterals;
MinLiterals := ToInput.MinMatchedLiterals;
InsertChars := ToInput.InsertChars;
ToInput := FromInput;
ToInput.MatchedLiterals := Literals;
ToInput.MinMatchedLiterals := MinLiterals;
ToInput.InsertChars := InsertChars;
end;
{TtsPictureNode}
constructor TtsPictureNode.Create(NodeType: TtsPictureNodeType);
begin
inherited Create;
FNodeType := NodeType;
FCount := 0;
FOptional := False;
FComplement := False;
FStartPos := 0;
FEndPos := 0;
FCharCount := 0;
FSubItems := nil;
FNextItem := nil;
FNextAlternative := nil;
Inc(FNodeCount);
end;
destructor TtsPictureNode.Destroy;
begin
FSubItems.Free;
FNextItem.Free;
FNextAlternative.Free;
inherited;
end;
{TtsPictureList}
constructor TtsPictureList.Create(PictureType: TtsPictureType);
begin
inherited Create;
FPictureType := PictureType;
FStartNode := nil;
end;
destructor TtsPictureList.Destroy;
begin
FStartNode.Free;
inherited;
end;
{TtsParseStack}
constructor TtsParseStack.Create;
begin
inherited;
FBuffer := nil;
FBufSize := 0;
FCount := 0;
end;
destructor TtsParseStack.Destroy;
begin
ReallocMem(FBuffer, 0);
inherited;
end;
function TtsParseStack.Push(Parent: Integer; Node: TtsPictureNode; TextPos, Count: Integer): Integer;
begin
if FCount >= FBufSize then
begin
FBufSize := FCount + 10;
ReallocMem(FBuffer, FBufSize * SizeOf(TtsParseStackElement));
end;
Inc(FCount);
FBuffer[FCount].Parent := Parent;
FBuffer[FCount].Node := Node;
FBuffer[FCount].Count := Count;
FBuffer[FCount].TextPos := TextPos;
Result := FCount;
end;
procedure TtsParseStack.Pop;
begin
if Count > 0 then Dec(FCount);
end;
procedure TtsParseStack.Reset;
begin
FCount := 0;
end;
{TtsTextCase}
constructor TtsTextCase.Create(Mask: TtsMask);
begin
inherited Create;
FMask := Mask;
FCaseType := cstNone;
FOptional := False;
end;
procedure TtsTextCase.Assign(Source: TPersistent);
begin
if Source is TtsTextCase then
begin
FCaseType := TtsTextCase(Source).CaseType;
FOptional := TtsTextCase(Source).Optional;
end
else
inherited;
end;
procedure TtsTextCase.SetCaseType(Value: TtsCaseType);
begin
if FCaseType <> Value then
begin
FCaseType := Value;
FMask.Changed;
end;
end;
procedure TtsTextCase.SetOptional(Value: Boolean);
begin
if FOptional <> Value then
begin
FOptional := Value;
FMask.Changed;
end;
end;
{TtsTextMask}
constructor TtsTextMask.Create(Mask: TtsMask);
begin
inherited Create;
FMask := Mask;
FPicture := '';
FPictureLength := Length(Picture);
FPictureList := nil;
FCreateTree := False;
FValidPicture := True;
FPictureParsed := True;
end;
destructor TtsTextMask.Destroy;
begin
FPictureList.Free;
inherited;
end;
procedure TtsTextMask.ErrChar(Chars: string; TextPos: Integer);
var
Msg: string;
begin
Msg := Format(StsPctExpChar, [FMask.Name, Chars, TextPos]);
FMask.InvalidOp(Msg);
end;
function TtsTextMask.CreatePictureTree(PictureType: TtsPictureType): TtsPictureList;
begin
Result := nil;
if FCreateTree then Result := TtsPictureList.Create(PictureType);
end;
function TtsTextMask.CreatePictureNode(NodeType: TtsPictureNodeType): TtsPictureNode;
begin
Result := nil;
if FCreateTree then Result := TtsPictureNode.Create(NodeType);
end;
function TtsTextMask.CharCount(const Text: string; TextPos: Integer): Integer;
begin
Result := 1;
if tsIsFarEast then
begin
if StrByteType(PChar(Text) + TextPos - 1, 0) <> mbSingleByte then
Result := 2;
end;
end;
function TtsTextMask.PictureCharCount(TextPos: Integer): Integer;
begin
if Picture[TextPos] = tsPicEscapeChar then
begin
if TextPos + 1 > FPictureLength then FMask.InvalidOp(Format(StsPctErrEnd, [FMask.Name]));
if tsIsfarEast then Result := CharCount(Picture, TextPos + 1) + 1
else Result := 2;
end
else if Picture[TextPos] = tsPicLower then
begin
Result := 1;
if (TextPos < FPictureLength) and
(Picture[TextPos+1] in [tsPicUpper, tsPicLower]) then Inc(Result);
end
else if Picture[TextPos] = tsPicUpper then
begin
Result := 1;
if (TextPos < FPictureLength) and
(Picture[TextPos+1] = tsPicUpper) then Inc(Result);
end
else
begin
if tsIsFarEast then Result := CharCount(Picture, TextPos)
else Result := 1;
end
end;
function TtsTextMask.IsLiteral(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := (Picture[TextPos] = tsPicEscapeChar) or
not (Picture[TextPos] in tsPicNonLiteral);
end;
function TtsTextMask.IsMaskChar(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := Picture[TextPos] in tsPicMaskChars;
end;
function TtsTextMask.IsCaseChar(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := (Picture[TextPos] = tsPicUpper) or
(Picture[TextPos] = tsPicLower);
end;
function TtsTextMask.IsRangeChar(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := Picture[TextPos] = tsPicRangeChar;
end;
function TtsTextMask.IsCountChar(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := Picture[TextPos] = tsPicCountChar;
end;
function TtsTextMask.IsGroupStart(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := (Picture[TextPos] = tsPicOptStartChar) or
(Picture[TextPos] = tsPicNonOptStartChar);
end;
function TtsTextMask.IsGroupEnd(TextPos: Integer): Boolean;
begin
Result := False;
if TextPos > FPictureLength then Exit;
Result := (Picture[TextPos] = tsPicOptEndChar) or
(Picture[TextPos] = tsPicNonOptEndChar);
end;
procedure TtsTextMask.CheckGroupEnd(var TextPos: Integer; EndChar: Char);
begin
if EndChar <> #0 then
begin
if (TextPos > FPictureLength) or (Picture[TextPos] <> EndChar) then
ErrChar(EndChar, TextPos);
Inc(TextPos);
end;
end;
function TtsTextMask.IsRange(TextPos: Integer; var Chars: Integer): Boolean;
var
Pos: Integer;
RangeOk: Boolean;
begin
Chars := 0;
Pos := TextPos;
Result := False;
if Pos > FPictureLength then Exit;
if Picture[Pos] = tsPicNotChar then Inc(Pos);
RangeOk := IsLiteral(Pos);
if RangeOk then
begin
Pos := Pos + PictureCharCount(Pos);
RangeOk := IsRangeChar(Pos);
end;
if RangeOk then
begin
Inc(Pos);
RangeOk := IsLiteral(Pos);
end;
if RangeOk then
begin
Pos := Pos + PictureCharCount(Pos);
RangeOk := IsGroupEnd(Pos);
end;
Result := RangeOk;
if Result then Chars := Pos - TextPos;
end;
function TtsTextMask.GetItemCount(var TextPos: Integer): Integer;
var
Pos: Integer;
CountStr: string;
begin
Result := 1;
if Picture[TextPos] <> tsPicCountChar then Exit;
CountStr := '';
Pos := TextPos + 1;
while Pos <= FPictureLength do
begin
if not (Picture[Pos] in ['0'..'9']) then Break;
Inc(Pos);
end;
if Pos > TextPos + 1 then CountStr := Copy(Picture, TextPos + 1, Pos - TextPos - 1);
TextPos := Pos;
if CountStr = '' then Result := -1
else Result := StrToInt(CountStr);
end;
function TtsTextMask.GetGroupType(var TextPos: Integer; var EndChar: Char): Boolean;
begin
Result := False;
EndChar := #0;
if TextPos > FPictureLength then Exit;
if Picture[TextPos] = tsPicOptStartChar then
begin
Result := True;
TextPos := TextPos + 1;
EndChar := tsPicOptEndChar;
end
else if Picture[TextPos] = tsPicNonOptStartChar then
begin
TextPos := TextPos + 1;
EndChar := tsPicNonOptEndChar;
end;
end;
function TtsTextMask.GetItem(var TextPos: Integer; Chars: Integer; NodeType: TtsPictureNodeType): TtsPictureNode;
var
ItemNode: TtsPictureNode;
begin
Result := nil;
if TextPos > FPictureLength then Exit;
ItemNode := CreatePictureNode(NodeType);
if Assigned(ItemNode) then
begin
try
ItemNode.Count := 1;
ItemNode.Optional := False;
ItemNode.StartPos := TextPos;
ItemNode.CharCount := Chars;
except
ItemNode.Free;
raise;
end;
end;
Result := ItemNode;
end;
function TtsTextMask.GetNextItem(var TextPos: Integer): TtsPictureNode;
var
Count: Integer;
CountFound: Boolean;
ItemNode: TtsPictureNode;
Pos: Integer;
begin
Result := nil;
if TextPos > FPictureLength then Exit;
ItemNode := nil;
Count := 1;
CountFound := False;
if Picture[TextPos] = tsPicCountChar then
begin
CountFound := True;
Count := GetItemCount(TextPos);
end;
if TextPos > FPictureLength then
FMask.InvalidOp(Format(StsPctErrEnd, [FMask.Name]))
else if IsLiteral(TextPos) then
begin
Pos := TextPos + PictureCharCount(TextPos);
ItemNode := GetItem(TextPos, Pos - TextPos, pntLiteralChars);
TextPos := Pos;
end
else if IsMaskChar(TextPos) then
begin
Pos := TextPos + PictureCharCount(TextPos);
if not CountFound then
begin
while IsMaskChar(Pos) do Inc(Pos);
end;
ItemNode := GetItem(TextPos, Pos - TextPos, pntMaskChars);
TextPos := Pos;
end
else if IsGroupStart(TextPos) then
ItemNode := GetGroup(TextPos)
else if (not CountFound) and IsCaseChar(TextPos) then
begin
Pos := TextPos + PictureCharCount(TextPos);
ItemNode := GetItem(TextPos, Pos - TextPos, pntCase);
TextPos := Pos;
end
else
begin
FMask.InvalidOp(Format(StsPctInvalidChar, [FMask.Name, Picture[TextPos], TextPos]));
end;
if ItemNode <> nil then ItemNode.Count := Count;
Result := ItemNode;
end;
function TtsTextMask.GetRange(var TextPos: Integer; var Node: TtsPictureNode): Boolean;
var
Chars: Integer;
begin
Node := nil;
Result := IsRange(TextPos, Chars);
if Result then
begin
Node := CreatePictureNode(pntRange);
try
if Assigned(Node) then
begin
if Picture[TextPos] = tsPicNotChar then
begin
Node.Complement := True;
TextPos := TextPos + 1;
Chars := Chars - 1;
end;
Node.Count := 1;
Node.StartPos := TextPos;
Node.CharCount := Chars;
if Picture[Node.StartPos] = tsPicEscapeChar then
Node.StartPos := Node.StartPos + 1;
Node.EndPos := Node.StartPos + CharCount(Picture, Node.StartPos) + 1;
if Picture[Node.EndPos] = tsPicEscapeChar then
Node.EndPos := Node.EndPos;
end;
TextPos := TextPos + Chars;
except
Node.Free;
raise;
end;
end;
end;
function TtsTextMask.GetNextAlternative(var TextPos: Integer; EndChar: Char): TtsPictureNode;
var
PrevNode, StartNode, Node: TtsPictureNode;
begin
Result := nil;
if TextPos > FPictureLength then Exit;
PrevNode := nil;
StartNode := nil;
try
while (TextPos <= FPictureLength) and (Picture[TextPos] <> EndChar) and
(Picture[TextPos] <> tsPicAltChar) do
begin
Node := GetNextItem(TextPos);
if Assigned(PrevNode) then PrevNode.NextItem := Node;
if not Assigned(StartNode) then StartNode := Node;
PrevNode := Node;
end;
if (TextPos <= FPictureLength) and
(Picture[TextPos] = tsPicAltChar) then Inc(TextPos);
except
StartNode.Free;
raise;
end;
Result := StartNode;
end;
function TtsTextMask.GetGroup(var TextPos: Integer): TtsPictureNode;
var
PrevNode, GroupNode, Node: TtsPictureNode;
EndChar: Char;
Optional: Boolean;
begin
Result := nil;
if TextPos > FPictureLength then Exit;
PrevNode := nil;
GroupNode := CreatePictureNode(pntGroup);
try
Optional := GetGroupType(TextPos, EndChar);
if Assigned(GroupNode) then
begin
GroupNode.Count := 1;
GroupNode.Optional := Optional;
end;
if GetRange(TextPos, Node) then
begin
if Assigned(Node) and Assigned(GroupNode) then
GroupNode.SubItems := Node;
end
else
begin
while (TextPos <= FPictureLength) and
(Picture[TextPos] <> EndChar) do
begin
Node := GetNextAlternative(TextPos, EndChar);
if Assigned(Node) and Assigned(GroupNode) then
begin
if not Assigned(PrevNode)
then GroupNode.SubItems := Node
else PrevNode.NextAlternative := Node;
end;
PrevNode := Node;
end;
end;
CheckGroupEnd(TextPos, EndChar);
except
GroupNode.Free;
raise;
end;
Result := GroupNode;
end;