-
Notifications
You must be signed in to change notification settings - Fork 49
/
sscanf.cpp
2923 lines (2793 loc) · 73.8 KB
/
sscanf.cpp
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
/*
* sscanf 2.15.1
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the sscanf 2.0 SA:MP plugin.
*
* The Initial Developer of the Original Code is Alex "Y_Less" Cole.
* Portions created by the Initial Developer are Copyright (c) 2022
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Cheaterman
* DEntisT
* Emmet_
* karimcambridge
* kalacsparty
* Kirima
* leHeix
* maddinat0r
* Southclaws
* Y_Less
* ziggi
*
* Special Thanks to:
*
* SA:MP Team past, present, and future.
* maddinat0r, for hosting the repo for a very long time.
* Emmet_, for his efforts in maintaining it for almost a year.
*/
#include <malloc.h>
#include <string.h>
#include <stdarg.h>
#include "sscanf.h"
#include "args.h"
#include "specifiers.h"
#include "utils.h"
#include "data.h"
#include "array.h"
#include "enum.h"
#include "amx/plugincommon.h"
#include <sdk.hpp>
#include <Server/Components/Pawn/pawn.hpp>
#include "subhook/subhook.h"
#define DEFER_STRINGISE(n) #n
#define STRINGISE(n) DEFER_STRINGISE(n)
#define SSCANF_VERSION STRINGISE(SSCANF_VERSION_MAJOR) "." STRINGISE(SSCANF_VERSION_MINOR) "." STRINGISE(SSCANF_VERSION_BUILD)
//----------------------------------------------------------
logprintf_t
logprintf,
real_logprintf;
AMX_NATIVE
SetPlayerName = NULL;
typedef int AMXAPI(*amx_Register_t)(AMX* amx, const AMX_NATIVE_INFO* nativelist, int number);
subhook_t amx_Register_hook;
// These are the pointers to all the functions currently used by sscanf. If
// more are added, this table will need to be updated.
static void *
NPC_AMX_FUNCTIONS[] = {
NULL, // PLUGIN_AMX_EXPORT_Align16
NULL, // PLUGIN_AMX_EXPORT_Align32
NULL, // PLUGIN_AMX_EXPORT_Align64
NULL, // PLUGIN_AMX_EXPORT_Allot
NULL, // PLUGIN_AMX_EXPORT_Callback
NULL, // PLUGIN_AMX_EXPORT_Cleanup
NULL, // PLUGIN_AMX_EXPORT_Clone
(void *)&npcamx_Exec, // PLUGIN_AMX_EXPORT_Exec
NULL, // PLUGIN_AMX_EXPORT_FindNative
(void *)&npcamx_FindPublic, // PLUGIN_AMX_EXPORT_FindPublic
NULL, // PLUGIN_AMX_EXPORT_FindPubVar
NULL, // PLUGIN_AMX_EXPORT_FindTagId
NULL, // PLUGIN_AMX_EXPORT_Flags
(void *)&npcamx_GetAddr, // PLUGIN_AMX_EXPORT_GetAddr
NULL, // PLUGIN_AMX_EXPORT_GetNative
NULL, // PLUGIN_AMX_EXPORT_GetPublic
NULL, // PLUGIN_AMX_EXPORT_GetPubVar
(void *)&npcamx_GetString, // PLUGIN_AMX_EXPORT_GetString
NULL, // PLUGIN_AMX_EXPORT_GetTag
NULL, // PLUGIN_AMX_EXPORT_GetUserData
NULL, // PLUGIN_AMX_EXPORT_Init
NULL, // PLUGIN_AMX_EXPORT_InitJIT
NULL, // PLUGIN_AMX_EXPORT_MemInfo
NULL, // PLUGIN_AMX_EXPORT_NameLength
NULL, // PLUGIN_AMX_EXPORT_NativeInfo
NULL, // PLUGIN_AMX_EXPORT_NumNatives
NULL, // PLUGIN_AMX_EXPORT_NumPublics
NULL, // PLUGIN_AMX_EXPORT_NumPubVars
NULL, // PLUGIN_AMX_EXPORT_NumTags
NULL, // PLUGIN_AMX_EXPORT_Push
NULL, // PLUGIN_AMX_EXPORT_PushArray
(void *)&npcamx_PushString, // PLUGIN_AMX_EXPORT_PushString
NULL, // PLUGIN_AMX_EXPORT_RaiseError
(void *)&npcamx_Register, // PLUGIN_AMX_EXPORT_Register
(void *)&npcamx_Release, // PLUGIN_AMX_EXPORT_Release
NULL, // PLUGIN_AMX_EXPORT_SetCallback
NULL, // PLUGIN_AMX_EXPORT_SetDebugHook
(void *)&npcamx_SetString, // PLUGIN_AMX_EXPORT_SetString
NULL, // PLUGIN_AMX_EXPORT_SetUserData
(void *)&npcamx_StrLen, // PLUGIN_AMX_EXPORT_StrLen
NULL, // PLUGIN_AMX_EXPORT_UTF8Check
NULL, // PLUGIN_AMX_EXPORT_UTF8Get
NULL, // PLUGIN_AMX_EXPORT_UTF8Len
NULL, // PLUGIN_AMX_EXPORT_UTF8Put
};
extern "C" void *
pAMXFunctions;
extern unsigned int
g_iTrueMax,
g_iInvalid,
g_iMaxPlayerName;
extern int *
g_iConnected;
extern int *
g_iNPC;
extern char *
g_szPlayerNames;
extern int
gAlpha,
gForms;
extern E_SSCANF_OPTIONS
gOptions;
AMX *
g_aCurAMX;
//extern char
// ** g_pServer;
#define SAVE_VALUE(m) \
if (doSave) \
*args.Next() = m
#define SAVE_VALUE_F(m) \
if (doSave) { \
float f = (float)m; \
*args.Next() = amx_ftoc(f); }
static char
emptyString[1] = { '\0' };
// Based on amx_StrParam but using 0 length strings. This can't be inline as
// it uses alloca - it could be written to use malloc instead, but that would
// require memory free code all over the place!
#define STR_PARAM(amx,param,result) \
do { \
cell * amx_cstr_; int amx_length_; \
amx_GetAddr((amx), (param), &amx_cstr_); \
amx_StrLen(amx_cstr_, &amx_length_); \
if (amx_length_ > 0) { \
if (((result) = (char *)alloca((amx_length_ + 1) * sizeof (*(result)))) != NULL) \
amx_GetString((result), amx_cstr_, sizeof (*(result)) > 1, amx_length_ + 1); \
else { \
SscanfError(43, "Unable to allocate memory."); \
return SSCANF_FAIL_RETURN; } } \
else (result) = emptyString; } \
while (false)
#define SAFE_STR_PARAM(amx,param,result) \
do { \
cell * amx_cstr_; int amx_length_; \
amx_GetAddr((amx), (param), &amx_cstr_); \
amx_StrLen(amx_cstr_, &amx_length_); \
if (amx_length_ > 0) { \
if (((result) = (char *)alloca((amx_length_ + 1) * sizeof (*(result)))) != NULL) \
amx_GetString((result), amx_cstr_, sizeof (*(result)) > 1, amx_length_ + 1); \
else { \
logprintf("sscanf error: Unable to allocate memory."); \
SetErrorCode(43); \
return SSCANF_FAIL_RETURN; } } \
else (result) = emptyString; } \
while (false)
#define LENGTH_STR_PARAM(amx,param,result,length) \
do { \
cell * amx_cstr_; \
amx_GetAddr((amx), (param), &amx_cstr_); \
amx_StrLen(amx_cstr_, &length); \
if (length > 0) { \
if (((result) = (char *)alloca((length + 1) * sizeof (*(result)))) != NULL) \
amx_GetString((result), amx_cstr_, sizeof (*(result)) > 1, length + 1); \
else { \
logprintf("sscanf error: Unable to allocate memory."); \
SetErrorCode(43); \
return SSCANF_FAIL_RETURN; } } \
else (result) = emptyString; } \
while (false)
// Macros for the regular values.
#define DO(m,n) \
{m b; \
if (Do##n(&string, &b)) { \
SAVE_VALUE((cell)b); \
break; } \
return SSCANF_FAIL_RETURN; }
#define DOV(m,n) \
{m b; \
Do##n(&string, &b); \
SAVE_VALUE((cell)b); }
#define DOF(m,n) \
{m b; \
if (Do##n(&string, &b)) { \
SAVE_VALUE_F(b) \
break; } \
return SSCANF_FAIL_RETURN; }
// Macros for the default values. None of these have ifs as the return value
// of GetReturnDefault is always true - we don't penalise users for the
// mistakes of the coder - they will get warning messages if they get the
// format wrong, and I don't know of any mistakes which aren't warned about
// (admittedly a silly statement as if I did I would have fixed them).
#define DE(m,n) \
{m b; \
switch (Do##n##D(&format, &b)) \
{ \
case -1: \
b = (m)*args.Next(); \
if (!args.HasMore()) { \
SscanfWarning(47, "Format specifier does not match parameter count."); \
return SSCANF_FAIL_RETURN; } \
case 1: \
SAVE_VALUE((cell)b); \
} \
break; }
#define DEF(m,n) \
{m b; \
switch (Do##n##D(&format, &b)) \
{ \
case -1: \
b = amx_ctof(*args.Next()); \
if (!args.HasMore()) { \
SscanfWarning(47, "Format specifier does not match parameter count."); \
return SSCANF_FAIL_RETURN; } \
case 1: \
SAVE_VALUE_F(b); \
} \
break; }
// Macros for the default values in the middle of a string so you can do:
//
// sscanf("hello, , 10", "p<,>sI(42)i", str, var0, var1);
//
// Note that optional parameters in the middle of a string only work with
// explicit (i.e. not whitespace) delimiters.
#define DX(m,n) \
if (IsDelimiter(*string)) { \
m b; \
switch (Do##n##D(&format, &b)) \
{ \
case -1: \
b = (m)*args.Next(); \
if (!args.HasMore()) { \
SscanfWarning(47, "Format specifier does not match parameter count."); \
return SSCANF_FAIL_RETURN; } \
case 1: \
SAVE_VALUE((cell)b); \
} \
break; } \
if (SkipDefault(&format)) \
args.Next();
#define DXF(m,n) \
if (IsDelimiter(*string)) { \
m b; \
switch (Do##n##D(&format, &b)) \
{ \
case -1: \
b = amx_ctof(*args.Next()); \
if (!args.HasMore()) { \
SscanfWarning(47, "Format specifier does not match parameter count."); \
return SSCANF_FAIL_RETURN; } \
case 1: \
SAVE_VALUE_F(b); \
} \
break; } \
if (SkipDefault(&format)) \
args.Next();
bool
DoK(AMX * amx, char ** defaults, char ** input, cell * cptr, bool optional, bool all);
void
SetOptions(char *, cell);
cell
GetOptions(char *);
char
* gFormat = 0;
const char
* gCallFile = 0;
int
gCallLine = 0;
cell
* gCallResolve = 0;
bool SscanfErrLine()
{
if (gCallResolve == 0)
{
gCallFile = "sscanf";
gCallLine = -1;
}
else if (gCallFile == 0)
{
// Extract the filename.
int length;
amx_StrLen(gCallResolve, &length);
if (length > 0)
{
if ((gCallFile = (const char *)malloc((length + 1))) != NULL)
{
amx_GetString((char *)gCallFile, gCallResolve, false, length + 1);
}
else
{
logprintf("sscanf error: Unable to allocate memory.");
SetErrorCode(43);
gCallFile = "sscanf";
gCallLine = -1;
gCallResolve = 0;
}
}
else
{
gCallFile = "sscanf";
gCallLine = -1;
gCallResolve = 0;
}
}
return gCallLine > 0;
}
static cell
Sscanf(AMX * amx, char * string, char * format, cell const * params, const int paramCount)
{
struct args_s
args{ amx, params, 0, 0, paramCount };
// Check for CallRemoteFunction style null strings and correct.
if (string[0] == '\1' && string[1] == '\0')
{
string[0] = '\0';
}
// Save the default options so we can have local modifications.
InitialiseDelimiter();
// Skip leading space.
SkipWhitespace(&string);
bool
doSave;
// Code for the rare cases where the WHOLE format is quiet.
if (*format == '{')
{
++format;
doSave = false;
}
else
{
doSave = true;
}
g_aCurAMX = amx;
// Now do the main loop as long as there are variables to store the data in
// and input string remaining to get the data from.
while (*string && (args.HasMore() || !doSave))
{
if (!*format)
{
// End of the format string - if we're here we've got all the
// parameters but there is extra string or variables, which may
// indicate their code needs fixing, for example:
// sscanf(data, "ii", var0, var1, var3, var4);
// There is only two format specifiers, but four returns. This may
// also be reached if there is too much input data, but that is
// considered OK as that is likely a user's fault.
if (args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
}
if (!doSave)
{
// Started a quiet section but never explicitly ended it.
SscanfWarning(48, "Unclosed quiet section.");
}
return SSCANF_TRUE_RETURN;
}
else if (IsWhitespace(*format))
{
++format;
}
else
{
IncErrorSpecifier();
switch (*format++)
{
case 'L':
DX(bool, L)
// FALLTHROUGH
case 'l':
DOV(bool, L)
break;
case 'B':
DX(int, B)
// FALLTHROUGH
case 'b':
DO(int, B)
case 'N':
DX(int, N)
// FALLTHROUGH
case 'n':
DO(int, N)
case 'C':
DX(char, C)
// FALLTHROUGH
case 'c':
DO(char, C)
case 'I':
case 'D':
DX(int, I)
// FALLTHROUGH
case 'i':
case 'd':
DO(int, I)
case 'H':
case 'X':
DX(int, H)
// FALLTHROUGH
case 'h':
case 'x':
DO(int, H)
case 'M':
DX(unsigned int, M)
// FALLTHROUGH
case 'm':
DO(unsigned int, M)
case 'O':
DX(int, O)
// FALLTHROUGH
case 'o':
DO(int, O)
case 'F':
DXF(double, F)
// FALLTHROUGH
case 'f':
DOF(double, F)
case 'G':
DXF(double, G)
// FALLTHROUGH
case 'g':
DOF(double, G)
case '{':
if (doSave)
{
doSave = false;
}
else
{
// Already in a quiet section.
SscanfWarning(20, "Can't have nestled quiet sections.");
}
continue;
case '}':
if (doSave)
{
SscanfWarning(21, "Not in a quiet section.");
}
else
{
doSave = true;
}
continue;
case 'P':
{
ResetDelimiter();
char *
t = GetMultiType(&format);
if (t) AddDelimiters(t);
else return SSCANF_FAIL_RETURN;
continue;
}
// FALLTHROUGH
case 'p':
// 'P' doesn't exist.
// Theoretically, for compatibility, this should be:
// p<delimiter>, but that will break backwards
// compatibility with anyone doing "p<" to use '<' as a
// delimiter (doesn't matter how rare that may be). Also,
// writing deprecation code and both the new and old code
// is more trouble than it's worth, and it's slow.
// UPDATE: I wrote the "GetSingleType" code for 'a' and
// figured out a way to support legacy and new code, while
// still maintaining support for the legacy "p<" separator,
// so here it is:
ResetDelimiter();
AddDelimiter(GetSingleType(&format));
continue;
case 'S':
if (IsDelimiter(*string))
{
char *
dest;
int
length;
DoSD(&format, &dest, &length, args);
// Send the string to PAWN.
if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
amx_SetString(args.Next(), dest, 0, 0, length);
}
break;
}
// Implicit "else".
if (SkipDefault(&format))
{
SscanfError(64, "(*) is not supported in strings/arrays yet.");
args.Next();
}
// FALLTHROUGH
case 's':
{
// Get the length.
int
length = GetLength(&format, args);
char *
dest;
DoS(&string, &dest, length, IsEnd(*format) || (!doSave && *format == '}' && IsEnd(*(format + 1))));
// Send the string to PAWN.
if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
amx_SetString(args.Next(), dest, 0, 0, length);
}
}
break;
case 'Z':
if (IsDelimiter(*string))
{
char *
dest;
int
length;
DoSD(&format, &dest, &length, args);
// Send the string to PAWN.
if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
amx_SetString(args.Next(), dest, 1, 0, length);
}
break;
}
// Implicit "else".
if (SkipDefault(&format))
{
SscanfError(64, "(*) is not supported in strings/arrays yet.");
args.Next();
}
// FALLTHROUGH
case 'z':
{
// Get the length.
int
length = GetLength(&format, args);
char *
dest;
DoS(&string, &dest, length, IsEnd(*format) || (!doSave && *format == '}' && IsEnd(*(format + 1))));
// Send the string to PAWN.
if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
amx_SetString(args.Next(), dest, 1, 0, length);
}
}
break;
case 'U':
if (IsDelimiter(*string))
{
int
b;
switch (DoUD(&format, &b))
{
case -1:
b = *args.Next();
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
}
if (*format == '[')
{
int
len = GetLength(&format, args);
if (gOptions & OLD_DEFAULT_NAME)
{
// Incompatible combination.
SscanfError(55, "'U(name)[len]' is incompatible with OLD_DEFAULT_NAME.");
return SSCANF_FAIL_RETURN;
}
else if (len < 2)
{
SscanfError(56, "'U(num)[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
cell *
cptr = args.Next();
*cptr++ = b;
*cptr = g_iInvalid;
}
}
else
{
SAVE_VALUE((cell)b);
}
break;
}
if (SkipDefault(&format))
args.Next();
// FALLTHROUGH
case 'u':
if (*format == '[')
{
int
len = GetLength(&format, args);
if (len < 2)
{
SscanfError(57, "'u[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else
{
int
b = -1;
E_SSCANF_OPTIONS
od = gOptions;
if (doSave)
{
char *
tstr;
// Don't detect multiple results.
gOptions = (E_SSCANF_OPTIONS)(gOptions & ~CELLMIN_ON_MATCHES);
cell *
cptr = args.Next();
while (--len)
{
tstr = string;
if (!DoU(&tstr, &b, b + 1))
{
*cptr++ = b;
b = g_iInvalid;
break;
}
if (b == g_iInvalid) break;
*cptr++ = b;
}
if (b == g_iInvalid)
{
*cptr = g_iInvalid;
}
else
{
tstr = string;
DoU(&tstr, &b, b + 1);
if (b == g_iInvalid) *cptr = g_iInvalid;
else *cptr = 0x80000000;
}
// Restore results detection.
gOptions = od;
string = tstr;
}
else
{
DoU(&string, &b, 0);
}
}
}
else
{
int
b;
DoU(&string, &b, 0);
SAVE_VALUE((cell)b);
}
break;
case 'Q':
if (IsDelimiter(*string))
{
int
b;
switch (DoQD(&format, &b))
{
case -1:
b = *args.Next();
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
}
if (*format == '[')
{
int
len = GetLength(&format, args);
if (gOptions & OLD_DEFAULT_NAME)
{
// Incompatible combination.
SscanfError(58, "'Q(name)[len]' is incompatible with OLD_DEFAULT_NAME.");
return SSCANF_FAIL_RETURN;
}
else if (len < 2)
{
SscanfError(59, "'Q(num)[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
cell *
cptr = args.Next();
*cptr++ = b;
*cptr = g_iInvalid;
}
}
else
{
SAVE_VALUE((cell)b);
}
break;
}
if (SkipDefault(&format))
args.Next();
// FALLTHROUGH
case 'q':
if (*format == '[')
{
int
len = GetLength(&format, args);
if (len < 2)
{
SscanfError(60, "'q[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else
{
int
b = -1;
E_SSCANF_OPTIONS
od = gOptions;
if (doSave)
{
char *
tstr;
// Don't detect multiple results.
gOptions = (E_SSCANF_OPTIONS)(gOptions & ~CELLMIN_ON_MATCHES);
cell *
cptr = args.Next();
while (--len)
{
tstr = string;
if (!DoQ(&tstr, &b, b + 1))
{
*cptr++ = b;
b = g_iInvalid;
break;
}
if (b == g_iInvalid) break;
*cptr++ = b;
}
if (b == g_iInvalid)
{
*cptr = g_iInvalid;
}
else
{
tstr = string;
DoQ(&tstr, &b, b + 1);
if (b == g_iInvalid) *cptr = g_iInvalid;
else *cptr = 0x80000000;
}
// Restore results detection.
gOptions = od;
string = tstr;
}
else
{
DoQ(&string, &b, 0);
}
}
}
else
{
int
b;
DoQ(&string, &b, 0);
SAVE_VALUE((cell)b);
}
break;
case 'R':
if (IsDelimiter(*string))
{
int
b;
switch (DoRD(&format, &b))
{
case -1:
b = *args.Next();
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
}
if (*format == '[')
{
int
len = GetLength(&format, args);
if (gOptions & OLD_DEFAULT_NAME)
{
// Incompatible combination.
SscanfError(61, "'R(name)[len]' is incompatible with OLD_DEFAULT_NAME.");
return SSCANF_FAIL_RETURN;
}
else if (len < 2)
{
SscanfError(62, "'R(num)[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else if (doSave)
{
if (!args.HasMore())
{
SscanfWarning(47, "Format specifier does not match parameter count.");
return SSCANF_FAIL_RETURN;
}
cell *
cptr = args.Next();
*cptr++ = b;
*cptr = g_iInvalid;
}
}
else
{
SAVE_VALUE((cell)b);
}
break;
}
if (SkipDefault(&format))
args.Next();
// FALLTHROUGH
case 'r':
if (*format == '[')
{
int
len = GetLength(&format, args);
if (len < 2)
{
SscanfError(63, "'r[len]' length under 2.");
return SSCANF_FAIL_RETURN;
}
else
{
int
b = -1;
E_SSCANF_OPTIONS
od = gOptions;
if (doSave)
{
char *
tstr;
cell *
cptr = args.Next();
// Don't detect multiple results.
gOptions = (E_SSCANF_OPTIONS)(gOptions & ~CELLMIN_ON_MATCHES);
while (--len)
{
tstr = string;
if (!DoR(&tstr, &b, b + 1))
{
*cptr++ = b;
b = g_iInvalid;
break;
}
if (b == g_iInvalid) break;
*cptr++ = b;
}
if (b == g_iInvalid)
{
*cptr = g_iInvalid;
}
else
{
tstr = string;
DoR(&tstr, &b, b + 1);
if (b == g_iInvalid) *cptr = g_iInvalid;
else *cptr = 0x80000000;
}
// Restore results detection.
gOptions = od;
string = tstr;
}
else
{
DoR(&string, &b, 0);
}
}
}
else
{
int
b;
DoR(&string, &b, 0);
SAVE_VALUE((cell)b);
}
break;
case 'A':
// We need the default values here.
if (DoA(&format, &string, args, true, doSave))
{
break;
}
return SSCANF_FAIL_RETURN;
case 'a':
if (DoA(&format, &string, args, false, doSave))
{
break;
}
return SSCANF_FAIL_RETURN;
case 'E':
// We need the default values here.
if (DoE(&format, &string, args, true, doSave))
{
break;
}
return SSCANF_FAIL_RETURN;
case 'e':
if (DoE(&format, &string, args, false, doSave))
{
break;
}
return SSCANF_FAIL_RETURN;
case 'K':