forked from tmcdos/IDR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanf_c.pas
1475 lines (1403 loc) · 50.9 KB
/
Scanf_c.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
(* E. Sorokin 2001, Ver 1.4 *)
(* Version for Delphi 4 and above *)
(* Assembler-level scanner routines and core functions for scanf and DeFormat *)
{$WRITEABLECONST OFF} {$EXTENDEDSYNTAX ON}
// {$DEFINE SCANF_CASE_SENSITIVE} // uncomment to make scanf case-insensitive.
{$DEFINE DEFORMAT_CASE_SENSITIVE} // comment out to make DeFormat case-insensitive
// {$DEFINE SCANF_EXCEPTIONS} // uncomment to generate exceptions in scanf_core
// {$DEFINE DEFORMAT_EXCEPTIONS} // uncomment to generate exceptions in DeFormat_core
unit Scanf_c;
interface
uses Classes;
const scOK = 1;
scOverflow = 8;
scEOF = -1;
function Scanf_core(var Buffer: PChar; var Format : PChar;
Pointers : array of Pointer) : Integer;
function Scanf_stream(Inp : TStream; var Format : PChar;
Pointers : array of Pointer)
: Integer;
function DeFormat_core(var Buffer : PChar; BufLen: Cardinal;
var Format : PChar; FmtLen: Cardinal;
Args: array of const;
DecSep, ThSep : char): Cardinal;
function StrToCurrF_core(var Buffer : PChar; BufLen : Cardinal;
var Res : Currency;
CurrStr : PChar; CurrF, NegCurrF : byte;
DecSep, ThSep : char) : Integer;
// Scans Str, assuming it starts with first digit or decimal point (no sign!)
// EShift is the integer, added to the decimal exponent
// (should be 4 for Currency and 0 otherwise, but may be used for any other purpose).
// On success returns scOK, conversion result on st(0)
// On error returns 0, st(0) undefined.
// In $Q+ mode, returns scOverflow bit is set on overflow, conversion result on st(0).
function Ext_scanner(var Str : PChar; Width : cardinal; EShift : integer; DecSep, ThSep : char) : integer;
resourcestring
SInvalidInteger = '''%s'' is not a valid integer value';
SInvalidFloat = '''%s'' is not a valid floating point value';
SInvalidFormat = 'Format ''%s'' invalid or incompatible with argument';
SArgumentMissing = 'No argument for format ''%s''';
SOverflow = 'Floating point overflow';
SIntOverflow = 'Integer overflow';
SInvalidCurrency = '''%s'' is not a valid currency value';
implementation
uses SysUtils;
function Hex_scanner(var Str : PChar; var P : int64; Width : cardinal) : integer; register;
asm
// Validity checks
JECXZ @@ret {Width was 0}
PUSH EBX {Save registers to be preserved}
PUSH ESI
PUSH EDI
// Initialize
{$IFOPT Q+}
PUSH EBP
XOR EBP,EBP {EBP used for overflow tracking}
{$ENDIF}
PUSH EAX {Put @Str on stack }
MOV EAX,[EAX]
OR EAX,EAX
JE @@Str0 {Str was NIL, quit}
XOR EBX,EBX
XOR ESI,ESI {EDI:ESI will be a 64-bit accumulator}
XOR EDI,EDI
// Main loop start
@@Loop: MOV BL,[EAX] {Get char}
CMP BL,'a'
JB @@upcase
SUB BL,'a' - 'A'
@@upcase:
SUB BL,'0'
CMP BL, 9
JBE @@digOk
SUB BL,'A' - '0'
CMP BL,5
JA @@StopScan {found illegal char}
ADD BL,10
//-------- Multiply and add start
@@digOk:
SHLD EDI,ESI,4 {64-bit unsigned multiplication by 16}
{$IFOPT Q+}
ADC EBP,0 {If CF was set at least once, EDX > 0}
{$ENDIF}
SHL ESI,4
OR ESI,EBX {Add BL}
//-------- Multiply and add end
INC EAX
DEC ECX
JNZ @@Loop
// Main loop end
@@StopScan:
POP EBX {Original @Str}
XOR ECX,ECX
CMP [EBX],EAX {Were there valid chars?}
JE @@Exit {No valid characters found}
MOV [EBX],EAX {Store new Str}
INC ECX {Set 1 as Result}
OR EDX,EDX
JZ @@Exit {Pointer was NIL, no assignment}
MOV [EDX+4],EDI {store high dword of _int64}
MOV [EDX],ESI {store low dword}
@@Exit:
{$IFOPT Q+}
OR EBP,EBP
POP EBP
JNA @@NoOverflow
OR ECX,scOverflow
@@NoOverflow:
{$ENDIF}
POP EDI {restore registers to be preserved}
POP ESI
POP EBX
@@ret:
MOV EAX,ECX {set return code to Result}
RET
@@Str0:
XOR ECX,ECX
POP EBX
JMP @@Exit
end;
function Oct_scanner(var Str : PChar; var P : int64; Width : cardinal) : integer; register;
asm
// Validity checks
JECXZ @@ret {Width was 0}
PUSH EBX {Save registers to be preserved}
PUSH ESI
PUSH EDI
// Initialize
{$IFOPT Q+}
PUSH EBP
XOR EBP,EBP {EBP used for overflow tracking}
{$ENDIF}
PUSH EAX {Put @Str on stack }
MOV EAX,[EAX]
OR EAX,EAX
JE @@Str0 {Str was NIL, quit}
XOR EBX,EBX
XOR ESI,ESI {EDI:ESI will be a 64-bit accumulator}
XOR EDI,EDI
// Main loop start
@@loop: MOV BL,[EAX] {Get char}
SUB BL,'0'+8
ADD BL,8
JNC @@StopScan {found illegal char}
SHLD EDI,ESI,3 {64-bit unsigned multiplication by 8}
{$IFOPT Q+}
ADC EBP,0 {Once CF set, EDX > 0}
{$ENDIF}
SHL ESI,3
OR ESI,EBX {Add BL}
INC EAX
DEC ECX
JNZ @@loop
// Main loop end
@@StopScan:
POP EBX {Original @Str}
XOR ECX,ECX
CMP [EBX],EAX {Were there valid chars?}
JE @@Exit {No valid characters found}
MOV [EBX],EAX {Store new Str}
INC ECX {Set 1 as Result}
OR EDX,EDX
JZ @@Exit {Pointer was NIL, no assignment}
MOV [EDX+4],EDI {store high dword of _int64}
MOV [EDX],ESI {store low dword}
@@Exit:
{$IFOPT Q+}
OR EBP,EBP
POP EBP
JNA @@NoOverflow
OR ECX,scOverflow
@@NoOverflow:
{$ENDIF}
POP EDI {restore registers to be preserved}
POP ESI
POP EBX
@@ret:
MOV EAX,ECX {set return code to Result}
RET
@@Str0:
XOR ECX,ECX
POP EBX
JMP @@Exit
end;
function Dec_scanner(var Str : PChar; var P : int64; Width : cardinal) : integer; register;
asm
// Validity checks
JECXZ @@ret {Width was 0}
PUSH EBX {Save registers to be preserved}
PUSH ESI
PUSH EDI
// Initialize
{$IFOPT Q+}
PUSH EBP
XOR EBP,EBP {EBP used for overflow tracking}
{$ENDIF}
PUSH EAX {Put @Str on stack }
MOV EAX,[EAX]
OR EAX,EAX
JE @@Str0 {Str was NIL, quit}
PUSH EDX {Put P on stack }
MOV ESI,EAX {use ESI for Str}
XOR EBX,EBX
XOR EDI,EDI {EDI:EAX will be a 64-bit accumulator}
XOR EAX,EAX
// Main loop start
@@loop: MOV BL,[ESI] {Get char}
SUB BL,'0'+10
ADD BL,10
JNC @@StopScan {found illegal char}
//=== Unsigned multiplication by 10 start
{$IFOPT Q+}
CMP EDI,$33333333 { $FFFFFFFF div 5}
JNA @@Less
INC EBP
@@Less:
{$ENDIF}
LEA EDI,[EDI+4*EDI]
MOV EDX,10
MUL EDX
ADD EAX,EBX {Add BL}
ADC EDX,0
{$IFOPT Q+}
SHL EDI,1
ADC EBP,0
ADD EDI,EDX
ADC EBP,0
{$ELSE}
LEA EDI,[EDI*2+EDX]
{$ENDIF}
//=== Unsigned multiplication by 10 end
INC ESI
DEC ECX
JNZ @@loop
// Main loop end
@@StopScan:
POP EDX {Get back P}
POP EBX {Original @Str}
XOR ECX,ECX
CMP [EBX],ESI {Were there valid chars?}
JE @@Exit {No valid characters found}
MOV [EBX],ESI {Store new Str}
INC ECX {Set 1 as Result}
OR EDX,EDX
JZ @@Exit {Pointer was NIL, no assignment}
MOV [EDX+4],EDI {store high dword of _int64}
MOV [EDX],EAX {store low dword}
@@Exit:
{$IFOPT Q+}
OR EBP,EBP
POP EBP
JNA @@NoOverflow
OR ECX,scOverflow
@@NoOverflow:
{$ENDIF}
POP EDI {restore registers to be preserved}
POP ESI
POP EBX
@@ret:
MOV EAX,ECX {set return code to Result}
RET
@@Str0:
XOR ECX,ECX
POP EBX
JMP @@Exit
end;
const single10 : single = 10.0;
single1000 : single = 1000.0;
// 10.0 and 1000.0 have exact representations, single precision is sufficient
// FMUL with single is the fastest [The 386 Book].
function Ext_scanner(var Str : PChar; Width : cardinal; EShift : integer; DecSep, ThSep : char) : integer; register;
var Tmp : integer;
SaveCW : word;
NewCW : word;
asm
// Initialization start
or edx,edx // check Width
jz @@ret
fnstcw SaveCW // Save FPU control word
push edi
push esi
push ebx
fnclex // just in case some garbage is there
mov NewCW,$33f // full precision, ignore errors
push eax // remember @str point on stack
mov esi,[eax] // ESI will be Str
fldcw NewCW
fldz // initialize accumulator
push ecx // remember EFactor
xor ecx,ecx // fractional part length
xor edi,edi // exponent
xor ebx,ebx // general pusrpose
// Initialization end
// Integer part start
xor eax,eax // inititate integer loop
@@intloop:
mov al,[esi]
sub al,'0'+10
add al,10
jnc @@ThSep // two checks in one!
inc esi // accept
fmul single10
inc ebx // use ebx as digit counter
mov Tmp,eax
fiadd Tmp
dec edx
jnz @@intloop
jmp @@OK
// Integer part end
//============ ThSep block ========================
// edi counts thousand groups, edi=0 means no thousand groups
@@ThSep:
cmp ThSep,0
je @@DecSep // ThSep=#0, forget it
sub ebx,1+3 // ebx must be 1, 2 or 3
add ebx,3
jnc @@bDecSep
sub edx,4
jc @@ThDecSep // Less than 4 characters left, ThSep not allowed
mov ebx,[esi]
@@Thloop:
xor eax,eax
cmp bl, ThSep
jne @@ThDecSep
inc edi
shr ebx,8 // first digit in bl
inc esi
sub bl,'0'+10
add bl,10
jnc @@error
mov al,bl
shr ebx,8 // second digit in bl
inc esi
sub bl,'0'+10
add bl,10
jnc @@error
lea eax,[eax+eax*4]
add eax,eax
add al,bl // al <= 90, no carry possible
shr ebx,8 // third digit in bl
inc esi
sub bl,'0'+10
add bl,10
jnc @@error
fmul single1000 // finally...
lea eax,[eax+eax*4]
lea eax,[ebx+eax*2]
mov Tmp,eax
inc esi
fiadd Tmp
sub edx,4 // Try next thousand group
jc @@ThDecSep // Less than 4 characters left, exit Thloop
mov ebx,[esi]
jmp @@Thloop
//============ End of ThSep block ====================
// Fractional part start
@@ThdecSep:
add edx,4 // compensate edx check in ThSep block
mov al,[esi]
sub al,'0'
@@bdecSep:
inc ebx // compensate ebx check in ThSep block
@@decSep:
mov ah,DecSep
sub ah,'0'
cmp al,ah
jne @@E // not DecSep, try E
inc esi
dec edx
jz @@OK // edx=0, finished anyway
xor eax,eax // initiate fraction loop
mov ecx,edx // remember edx in ecx
@@fracloop:
mov al,[esi]
sub al,'0'+10
add al,10
jnc @@fracEnd // two checks in one!
inc esi
fmul single10
mov Tmp,eax
fiadd Tmp
dec edx
jnz @@fracloop
xor edi,edi // bring edi back to 0
jmp @@OK // edx = 0, nothing to subtract from ecx
@@fracEnd:
sub ecx,edx // ecx now is number of digits in a fraction part
// Fractional part end
// Exponent start
@@E:
cmp edi,0
mov edi,0
jnz @@OK // edi was not 0, no exp
add ebx,ecx
jz @@Error // ebx=0 and ecx=0 mean no digits found, error
mov al,[esi]
and al,$df // Upcase(al)
cmp al,'E'
jnz @@OK
inc esi
dec edx
jz @@OK // width reached
mov al,[esi]
xor ebx,ebx // use ebx as '-' flag
cmp al,'+'
jz @@pmyes
cmp al,'-'
jnz @@doexp
dec ebx
@@pmyes:
inc esi
dec edx
jz @@OK // width reached
@@doexp:
xor eax,eax // inititate exponent loop
@@exploop:
mov al,[esi]
sub al,'0'+10
add al,10
jnc @@EndExp // two checks in one!
inc esi // accept
lea edi,[edi+4*edi] // edi:=edi*5
lea edi,[2*edi+eax] // edi = edi + edi + eax
dec edx
jnz @@exploop
@@EndExp:
cmp ebx,0 // ebx <> 0 means '-'
jz @@OK
neg edi
// Exponent end
// Combine parts start
// At this point, edi is the exponent and ecx is the fractional part length
@@OK:
mov eax,edi // get exponent into eax
sub eax,ecx // decrease by fractional part length
pop ecx // restore EFactor
add eax,ecx // add to exponent
jz @@NoPower
call FPower10 // multiply st(0) by eax^10
@@NoPower:
mov edx,1 // return Result=1
{$IFOPT Q+}
fstsw ax
and eax,8 // FPU overflow mask
or edx,eax // simply add to edx
{$ENDIF}
jmp @@finish
// Combine parts end
@@error:
fstp st(0) // clear up FPU stack
pop eax // clear up stack
xor edx,edx // set Result to 0
@@finish:
fnclex // clear up
fldcw SaveCW // restore FPU control word
pop eax
mov [eax],esi // store new Str
pop ebx
pop esi
pop edi
@@ret:
mov eax,edx // set result
end;
const singleINFasLongint : longint = $7f800000; // infinity
q_singleNANasLongint : longint = $7fC00000; // quiet NAN
var singleINF : single absolute singleINFasLongint;
q_singleNAN : single absolute q_singleNANasLongint;
type TCharSet = set of AnsiChar;
PInt = ^Integer;
TscRec = packed record // Has size of an integer
Case byte of
0: ( Typ : byte; Size : char; Flags : word;);
1: ( SizeType : word; iFlags : smallInt;);
end;
// Somewhat complicated structure of possible variable types.
const scIllegal = -1; // illegal value, no assignment possible
scChar = vtChar;
scPChar = vtPChar;
scInteger = vtInteger;
scFloat = vtExtended;
scCurrency= vtCurrency;
scCharSet = $7e;
scNspec = $7f; // "n" specifier
scUpperL = Ord('L') shl 8; // Size specifiers
scLowerL = Ord('l') shl 8;
scUpperH = Ord('H') shl 8;
scLowerh = Ord('h') shl 8;
scShortInt= scInteger + scUpperH;
scSmallInt= scInteger + scLowerH;
scLongInt = scInteger + scLowerL;
scInt64 = scInteger + scUpperL;
scReal = scFloat + scUpperH;
scSingle = scFloat + scLowerH;
scDouble = scFloat + scLowerL;
scExtended= scFloat + scUpperL;
scAnsi = scLowerL;
scShort = scLowerH;
scShortSet= scCharSet + scShort;
scAnsiSet = scCharSet + scAnsi;
scString = scPChar + scShort;
scAnsiStr = scPChar + scAnsi;
// Flags constants
scHex = $1;
scDecimal = $2;
scOctal = $4;
scBaseMask = scHex or scDecimal or scOctal;
sc1000Sep = $10;
scIntSpec = $20; // l8, l16, l32, l64 can be used only with integer types
scUnsigned = $40; // Not really implemented, as in BC++
scFormatted = $80; // Formatted currency in DeFormat
scNoAssign =$8000; // Just negative bit
defWidth = MaxInt; // no width limit by default
const
CurrFmts : array [0..3] of string =
( '%s%%m', // 0 = $1
'%%m%s', // 1 = 1$
'%s %%m', // 2 = $ 1
'%%m %s' // 3 = 1 $
);
NegCurrFmts : array [0..15] of string =
( '(%s%%m)', // 0 = ($1)
'-%s%%m', // 1 = -$1
'%s%%m', // 2 = $-1
'%s%%m-', // 3 = $1-
'(%%m%s)', // 4 = (1$)
'%%m%s', // 5 = -1$
'%%m-%s', // 6 = 1-$
'%%m%s-', // 7 = 1$-
'%%m %s', // 8 = -1 $
'-%s %%m', // 9 = -$ 1
'%%m %s-', // 10 = 1 $-
'%s%%m-', // 11 = $ 1-
'%s %%m', // 12 = $ -1
'%%m- %s', // 13 = 1- $
'(%s %%m)', // 14 = ($ 1)
'(%%m %s)' // 15 = (1 $)
);
function StrToCurrF_core(var Buffer : PChar; BufLen : Cardinal;
var Res : currency;
CurrStr : PChar; CurrF, NegCurrF : byte;
DecSep, ThSep : char) : Integer;
var Fmt : string;
FPtr : PChar;
begin
Fmt:=Format( NegCurrFmts[NegCurrF], [CurrStr]); // Try negative first
FPtr:=PChar(Fmt);
{$IFDEF DEFORMAT_EXCEPTIONS}
try
{$ENDIF}
Result:=DeFormat_core(Buffer, Buflen, FPtr, Length(Fmt), [@Res], DecSep, ThSep);
{$IFDEF DEFORMAT_EXCEPTIONS}
except on EConvertError do {nothing}; end;
{$ENDIF}
if (Result=1) then
Case NegCurrF of
1,4,9,14,15 : Res:=-Res;
2,5,8,12 : {Negative and positive formats are compatible} ;
else{3,6,7,10,11,13:}
If Fptr^=#0 then Res:=-Res {scanned FPtr to the end, minus was there!}
else Result:=0; {Something gone wrong!}
end;
If (Result <> 1) then begin {negative did not work, try positive}
Fmt:=Format(CurrFmts[CurrF],[CurrStr]);
FPtr:=PChar(Fmt);
Result:=DeFormat_core(Buffer, Buflen, FPtr, Length(Fmt), [@Res], DecSep, ThSep);
end;
end;
// *********** Common routines *******************************************//
// Scan numerical string (float, decimal, $hex or 0x..)
// Str : points to the first char in a string
// Width : maximum length and assumed to not exceed actual length
// FType : type and length of result, see scXXXX constants
// P : where to put the conversion result if scNoAssign is not set.
// Result : 0 on error, > 0 on success (e.g. scOverflow in $Q+ mode)
function NumScan(var Str : PChar; Width : integer;
FType : integer; P : Pointer;
DecSep, ThSep : char) : integer;
var X : extended;
i64 : int64 absolute X;
L : longint absolute X;
Cur : Currency absolute X;
{$IFOPT Q+} SaveCW, NewCW : word; {$ENDIF}
Negative : boolean;
Label doHex, doDec, doOct, Cont;
begin
With TscRec(FType) do begin
Negative:=False;
Case Str^ of // Get sign, if any
'-' : begin Negative:=True; Inc(Str); Dec(Width); end;
'+' : begin Inc(Str); Dec(Width); end;
end;
Result:=0; // keep compiler happy...
Case Typ of
scInteger :
begin
Case (Flags and scBaseMask) of
scHex : doHex: Result:=Hex_Scanner( Str, i64, Width);
scOctal : doOct: Result:=Oct_Scanner( Str, i64, Width);
scDecimal : doDec: Result:=Dec_Scanner( Str, i64, Width);
else // Base decision block
Case Str^ of
'$' : begin Inc(Str); Dec(Width); goto doHex; end;
'0' : If (Str[1] = 'x') or (Str[1] = 'X') then begin
Inc(Str,2); Dec(Width,2); goto doHex;
end else
if (Flags and scOctal <> 0) then goto doOct else goto doDec;
else goto doDec;
end;
end;
if (Result <= 0) then Exit;
if Negative then
asm
neg dword ptr [X][4]
neg dword ptr [X]
sbb dword ptr [X][4],0
end;
end;
scCurrency :
begin
Result:=Ext_Scanner(Str, Width, 4, DecSep, ThSep);
if Result > 0 then begin
{$IFOPT Q-}
if iFlags >= 0 then begin
if Negative then asm fchs; end;
asm
mov eax, [P]
fistp qword ptr [eax];
fnstsw ax
fnclex // Clear possible exceptions
end;
end else asm fstp st(0); end; // Just clear the FPU stack
{$ELSE}
if Negative then asm fchs; end;
asm
fstcw SaveCW
mov NewCW,$33f // Mask exceptions
fldcw NewCW
fistp qword ptr [X];
fnstsw ax
and eax,8+1 // FPU overflow and invalidop mask
jz @@OK
or [Result],8
@@OK: fldcw SaveCW
fnclex // Clear exceptions
end;
if iFlags <> 0 then Currency(P^):=Cur;
{$ENDIF}
end;
Exit;
end;
scFloat :
begin
if (Width >=3) then begin // Check NAN and INF
Case ( (PInt(Str))^ and $00ffffff) of
$004e414e {'NAN'#0}: begin if Negative then X:=-q_singleNAN else X:=q_singleNAN; Inc(Str,3); Result:=scOK; goto Cont; end;
$00464e49 {'INF'#0}: begin if Negative then X:=-singleINF else X:=singleINF; Inc(Str,3); Result:=scOK; goto Cont; end;
end;
end;
Result:=Ext_Scanner(Str, Width, 0, DecSep, Char(Ord(ThSep)*Integer( (Flags and sc1000Sep) <> 0)));
if (Result <= 0) or (FType < 0) then Exit else begin
if Negative then asm fchs; end;
asm fstp [X]; end;
end;
Cont:
end;
end; {Case Typ}
if (FType > 0) then begin // If scNoAssign not set, assign
Case SizeType of
{$IFOPT R-}
scShortInt : ShortInt(P^):=L;
scSmallInt : SmallInt(P^):=L;
scInteger, scLongInt : LongInt(P^) :=L;
{$ELSE} //signed and unsigned have different ranges
scShortInt : If (Flags and scUnsigned) = 0 then ShortInt(P^):=L else Byte(P^):=L;
scSmallInt : If (Flags and scUnsigned) = 0 then SmallInt(P^):=L else Word(P^):=L;
scInteger, scLongInt : If (Flags and scUnsigned) = 0 then LongInt(P^) :=L else LongWord(P^):=L;
{$ENDIF}
scSingle : Single(P^):=X;
scReal : real48(P^):=X;
scDouble : Double(P^):=X;
scExtended : Extended(P^):=X;
scInt64 : int64(P^):=i64;
end;
end;
end;
end;
// Parse a search-set specifier in format string
// Return value : set of allowed characters
// Format is updated to the first unparsed character
// On error result is False
function ParseSet(var Format : PChar; FmtEnd : PChar; var Res : TCharSet) : boolean;
type T32Int = array [0..7] of integer;
var TI : T32Int absolute Res;
i : integer;
Neg : boolean;
begin // Assume that Format points to a character after opening "["
Res:=[]; Result:=False;
If Format >= FmtEnd then Exit;
If Format^='^' then begin // Check for '^'
Neg:=True;
Inc(Format);
If Format >= FmtEnd then Exit;
end else Neg:=False;
Case Format^ of // Check special cases: first ']' or '-'
']','-' :
begin
// Res:=Res+[Format^];
TI[Byte(Format^) shr 5]:=TI[Byte(Format^) shr 5] or (1 shl ( Byte(Format^) and 31));
Inc(Format);
end;
end;
while Format < FmtEnd do
if Format^ = ']' then begin
Result:=True;
Inc(Format);
Break;
end else
if (Format[1]='-') and (Format[2] <> ']') then begin // Range
Res:=Res+[Format^..Format[2]];
Inc(Format,3);
end else begin
// Res:=Res + [Format^];
TI[Byte(Format^) shr 5]:=TI[Byte(Format^) shr 5] or (1 shl ( Byte(Format^) and 31));
Inc(Format);
end;
if Neg then
// Res:=[#0..#255]-Res;
for i:=0 to 7 do TI[i]:= not TI[i];
end;
// Process [size] type portions of a format specifier
// On error, scIllegal is returned
function GetSizeType(var Format : PChar) : TscRec;
var FType : TscRec;
PW : ^word absolute Format;
begin
Integer(Result):=scIllegal; Integer(FType):=0;// Initialization
With FType do begin
Case Format^ of // check size specifiers
'H','h','L' :
begin
Size:=Format^;
Inc(Format);
end;
'l' :
begin
Inc(Format);
Case Format^ of
'8' : begin Size:='H'; Inc(Format); Flags:=Flags or scIntSpec; end;
'l' : begin Size:='L'; Inc(Format); end;
else
Case PW^ of
$3631 {'16'} : begin Size:='h'; Inc(Format,2); Flags:=Flags or scIntSpec; end;
$3332 {'32'} : begin Size:='l'; Inc(Format,2); Flags:=Flags or scIntSpec; end;
$3436 {'64'} : begin Size:='L'; Inc(Format,2); Flags:=Flags or scIntSpec; end;
else Size:='l';
end;
end;
end;
end;
Case UpCase(Format^) of
#0 : Exit;
'[' : Typ:=scCharSet;
'I' : begin Typ:=scInteger; Flags:=Flags or scDecimal or scHex or scOctal; end;
'D' : begin Typ:=scInteger; Flags:=Flags or scDecimal; end;
'U' : begin Typ:=scInteger; Flags:=Flags or scDecimal or scUnsigned; end;
'X' : begin Typ:=scInteger; Flags:=Flags or scHex; end;
'O' : begin Typ:=scInteger; Flags:=Flags or scOctal; end;
'F','G','E' : begin Typ:=scFloat; Flags:=Flags or scDecimal; end;
'N' : Typ:=scNspec;
'S' : Typ:=scPChar;
// No size specs. allowed:
'C' : if Size=#0 then Typ:=scChar else Exit;
'P' : if Size = #0 then begin
Typ:=scInteger; Flags:=Flags or scHex;
end else Exit;
'M' : if Size=#0 then begin
Typ:=scCurrency;
if Format^='M' then Flags:=Flags or scFormatted;
end else Exit;
else begin Dec(Format); Exit; end;
end;
Case Typ of
scPChar, scCharSet :
Case Size of
'H', 'L' : Exit; // 'H' and 'L' not allowed with strings
end;
end;
Inc(Format);
end; { With Ftype}
Result:=FType;
end;
// *********** DeFormat ***********************************************//
function DeFormat_core(var Buffer : PChar; BufLen: Cardinal;
var Format : PChar; FmtLen: Cardinal;
Args: array of TVarRec;
DecSep, ThSep : char): Cardinal;
var // Many of these will be optimized out
Count : cardinal;
Index, Width : integer;
Buf, Marker, Fmt : PChar;
{$IFDEF DEFORMAT_EXCEPTIONS} FMarker : PChar; {$ENDIF}
Temp : integer;
Ptr : Pointer;
BufEnd : PChar absolute BufLen;
FmtEnd : PChar absolute FmtLen;
SCR : TscRec;
FType : integer absolute SCR;
theSet : TCharSet;
Label BadArg, BadFormat, CopyStr, DoFloat;
function ScanToFormat : boolean;
begin
ScanToFormat:=False;
while (Fmt < FmtEnd) and (Buf < BufEnd) do begin
case Fmt^ of
'%' : begin
Inc(Fmt);
if Fmt^='%' then // this is "%%", check for "%" in Buf
If Buf^ <> '%' then Exit
else begin Inc(Buf); Inc(Fmt); end
else begin
ScanToFormat:=True; // Start of Fmt specifier found.
Exit;
end;
end;
#0..#32 : begin
while (Buf < BufEnd) and (Buf^ <=' ') do Inc(Buf);
while (Fmt < FmtEnd) and (Fmt^ <=' ') do Inc(Fmt);
end;
else begin
{$IFNDEF DEFORMAT_CASE_SENSITIVE}
if Buf^ <> Fmt^ then Exit;
{$ELSE}
if UpCase(Buf^) <> UpCase(Fmt^) then Exit;
{$ENDIF}
Inc(Buf);
Inc(Fmt);
end;
end;
end; {While}
end;
// Returns: -1 in case of '*' error
// 0 on success
// 1 in case of numerical input error
function GetNumFields : integer;
var Temp : cardinal;
GotTemp, GotIndex : boolean;
begin
GotIndex:=False;
Temp:=0; // not really necessary, but this will suppress compiler's warning.
repeat
GotTemp:=False;
if Fmt^='*' then begin // Get indirect entry into Temp
Inc(Fmt);
If (Index <= High(Args)) and (Args[Index].VType=vtInteger) then
begin
Temp:=Args[Index].VInteger;
Inc(Index);
GotTemp:=True;
end else begin Result:=-1; Exit; end;
end else begin // Get explicit numerical entry into Temp
if Fmt^='-' then Inc(Fmt); // minus is ignored on scanning
Temp:=0; //start simple decimal input loop
while (Fmt^ >= '0') and (Fmt^ <= '9') do begin
Temp:=Temp*10+Ord(Fmt^)-Ord('0');
Inc(Fmt); GotTemp:=True;
end;
end;
Case Fmt^ of
':' : If (not GotIndex) and GotTemp then begin
Index:=Temp; Inc(Fmt); GotIndex:=True;
end else begin
Result:=1; Exit;
end;
'.' : begin
If GotTemp then Width:=Temp;
Inc(Fmt); // OK, now ignore precision in any form
If Fmt^='*' then begin Inc(Index); Inc(Format); end
else while (Fmt^ >= '0') and (Fmt^ <= '9') do Inc(Fmt);
Break;
end;
else begin
If GotTemp then Width:=Temp;
Break;
end;
end; {Case}
until Fmt^ <> ':';
Result:=0;
end;
begin
Buf:=Buffer; Fmt:=Format; FmtEnd:=Fmt+FmtLen; BufEnd:=Buf+BufLen; Index:=0; Result:=0; // initialization
{$IFDEF DEFORMAT_EXCEPTIONS}
try
{$ENDIF}
while ScanToFormat do // ScanToFormat returns False if end is reached
with SCR do begin
Width:=0;
{$IFDEF DEFORMAT_EXCEPTIONS} FMarker:=Fmt-1; {$ENDIF}
Case GetNumFields of
-1 : BadArg : begin
{$IFDEF DEFORMAT_EXCEPTIONS}
raise EConvertError.CreateFmt(SArgumentMissing,[Copy(FMarker, 1, Fmt-FMarker+1)]);
{$ENDIF}
Break;
end;
1 : BadFormat : begin
{$IFDEF DEFORMAT_EXCEPTIONS}
raise EConvertError.CreateFmt(SInvalidFormat, [Copy(FMarker, 1, Fmt-FMarker+2)]);
{$ENDIF}
Break;
end;
end;
SCR:=GetSizeType(Fmt);
Ptr:=NIL;
if (Ftype > 0) then begin
if Index > High(Args) then goto BadArg else Ptr:=Args[Index].VPointer;
if Ptr=NIL then Flags:=Flags or scNoAssign; // DeFormat treats NIL as NoAssign
end;
if Typ = scNSpec then begin
Flags:=Flags or sc1000Sep;
Typ:=scFloat;
end;
Case Typ of
scPChar, scFloat, scInteger, scCurrency : // Skip blank space for these formats
begin
while (Buf <= BufEnd) and (Buf^ <= ' ') do Inc(Buf);
if Buf >= BufEnd then Break;
end;
scCharSet : if not ParseSet(Fmt, FmtEnd, theSet) then goto BadFormat;
scChar : if Width = 0 then Inc(Width);
end;
if Width = 0 then Width:=defWidth;
if Width > BufEnd-Buf then Width := BufEnd-Buf; // Set maximum width
Marker:=Buf;
Case Typ of
scChar : begin
if FType > 0 then
Case Args[Index].VType of
vtPChar, vtPointer : Move(Buf^, Ptr^, Width);
else goto BadArg;
end;
Inc(Buf, Width);
end;
scCharSet : begin
for Count:=1 to Width do if (Buf^ in theSet) then Inc(Buf) else Break;
goto CopyStr;