-
Notifications
You must be signed in to change notification settings - Fork 51
/
enum.cpp
767 lines (750 loc) · 17.3 KB
/
enum.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
/*
* 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 <string.h>
#include "sscanf.h"
#include "args.h"
#include "utils.h"
#include "specifiers.h"
#include "data.h"
extern logprintf_t
logprintf;
#define SAVE_VALUE(m) \
if (doSave) \
*cptr++ = m
#define SAVE_VALUE_F(m) \
if (doSave) { \
float f = (float)m; \
*cptr++ = amx_ftoc(f); }
// Macros for the regular values.
#define DO(m,n) \
{ m b; \
if (Do##n(&string, &b)) { \
SAVE_VALUE((cell)b); \
break; } \
*input = string; \
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; } \
*input = string; \
return SSCANF_FAIL_RETURN; }
#define OPTIONAL_INVALID \
SscanfWarning(4, "Optional types invalid in enum specifiers, consider using 'E'.")
#define DX(m,n) \
OPTIONAL_INVALID;
#define DXF(m,n) \
OPTIONAL_INVALID;
extern AMX *
g_aCurAMX;
extern E_SSCANF_OPTIONS
gOptions;
bool
DoK(AMX * amx, char ** defaults, char ** input, cell * cptr, bool optional, bool all);
int
DoEnumValues(char * format, char ** input, cell * cptr, bool defaults, struct args_s & args)
{
// If cptr is NULL we never save - regardless of quiet sections.
bool
doSave = cptr != NULL;
char *
string = *input;
// Copied directly from the main loop, just with different macros.
while (*string)
{
if (!*format)
{
// Happy with this return.
*input = string;
return SSCANF_TRUE_RETURN;
}
else if (IsWhitespace(*format))
{
++format;
}
else
{
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 if (cptr)
{
// 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
{
if (cptr)
{
doSave = true;
}
else
{
SscanfWarning(22, "Can't remove quiet in enum.");
}
}
continue;
case 'P':
{
ResetDelimiter();
char *
t = GetMultiType(&format);
if (t) AddDelimiters(t);
else return SSCANF_FAIL_RETURN;
continue;
}
//SscanfWarning("You can't have an optional delimiter.");
// 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 'K':
OPTIONAL_INVALID;
// FALLTHROUGH
case 'k':
//DOF(double, K)
if (defaults && !(gOptions & OLD_DEFAULT_KUSTOM))
{
GetMultiType(&format);
if (doSave)
{
int
b;
DoI(&string, &b);
*cptr++ = b;
}
else
{
int
b;
DoI(&string, &b);
}
*(format - 1) = '>';
break;
}
else if (doSave)
{
if (DoK(g_aCurAMX, &format, &string, cptr, false, false))
{
*(format - 1) = '>';
++cptr;
break;
}
}
else
{
if (DoK(g_aCurAMX, &format, &string, NULL, false, false))
{
*(format - 1) = '>';
break;
}
}
*input = string;
return SSCANF_FAIL_RETURN;
case 'S':
OPTIONAL_INVALID;
// FALLTHROUGH
case 's':
{
// Get the length.
int
lole = GetLength(&format, args);
if (!lole)
{
return SSCANF_FAIL_RETURN;
}
char *
dest;
DoS(&string, &dest, lole, IsEnd(*format));
// Send the string to PAWN.
if (doSave)
{
// Save the string.
amx_SetString(cptr, dest, 0, 0, lole);
// Increase the pointer by the MAXIMUM length of
// the string - that's how enum strings work.
cptr += lole;
}
}
break;
case 'Z':
OPTIONAL_INVALID;
// FALLTHROUGH
case 'z':
{
// Get the length.
int
lole = GetLength(&format, args);
if (!lole)
{
return SSCANF_FAIL_RETURN;
}
char *
dest;
DoS(&string, &dest, lole, IsEnd(*format));
// Send the string to PAWN.
if (doSave)
{
// Save the string.
amx_SetString(cptr, dest, 1, 0, lole);
// Increase the pointer by the MAXIMUM length of
// the string - that's how enum strings work.
cptr += lole;
}
}
break;
case 'U':
DX(int, U)
// FALLTHROUGH
case 'u':
if (*format == '[')
{
SscanfWarning(54, "User arrays are not supported in enums.");
SkipLength(&format);
}
#define DoU(m,n) DoU(m,n,0)
if (defaults && !(gOptions & OLD_DEFAULT_NAME))
{
DOV(int, I)
}
else
{
DOV(int, U)
}
#undef DoU
break;
case 'Q':
DX(int, Q)
// FALLTHROUGH
case 'q':
if (*format == '[')
{
SscanfWarning(54, "User arrays are not supported in enums.");
SkipLength(&format);
}
#define DoQ(m,n) DoQ(m,n,0)
if (defaults && !(gOptions & OLD_DEFAULT_NAME))
{
DOV(int, I)
}
else
{
DOV(int, Q)
}
#undef DoQ
break;
case 'R':
DX(int, R)
// FALLTHROUGH
case 'r':
if (*format == '[')
{
SscanfWarning(54, "User arrays are not supported in enums.");
SkipLength(&format);
}
#define DoR(m,n) DoR(m,n,0)
if (defaults && !(gOptions & OLD_DEFAULT_NAME))
{
DOV(int, I)
}
else
{
DOV(int, R)
}
#undef DoR
break;
case 'A':
case 'a':
SscanfError(23, "Arrays are not supported in enums.");
*input = string;
return SSCANF_FAIL_RETURN;
case 'E':
case 'e':
SscanfError(67, "Enums are not supported in enums.");
*input = string;
return SSCANF_FAIL_RETURN;
case '\'':
// Find the end of the literal.
{
char
* str = format,
* write = format;
bool
escape = false;
while (!IsEnd(*str) && (escape || *str != '\''))
{
if (*str == '\\')
{
if (escape)
{
// "\\" - Go back a step to write this
// character over the last character (which
// just happens to be the same character).
--write;
}
escape = !escape;
}
else
{
if (*str == '\'')
{
// Overwrite the escape character with the
// quote character. Must have been
// preceeded by a slash or it wouldn't have
// got to here in the loop.
--write;
}
escape = false;
}
// Copy the string over itself to get rid of excess
// escape characters.
// Not sure if it's faster in the average case to
// always do the copy or check if it's needed.
// This write is always safe as it makes the string
// shorter, so we'll never run out of space. It
// will also not overwrite the original string.
*write++ = *str++;
}
if (*str == '\'')
{
// Correct end. Make a shorter string to search
// for.
*write = '\0';
// Find the current section of format in string.
char *
find = strstr(string, format);
if (!find)
{
// Didn't find the string.
*input = string;
SetErrorCode(1003);
return SSCANF_FAIL_RETURN;
}
// Found the string. Update the current string
// position to the length of the search term
// further along from the start of the term. Use
// "write" here as we want the escaped string
// length.
string = find + (write - format);
// Move to after the end of the search string. Use
// "str" here as we want the unescaped string
// length.
format = str + 1;
}
else
{
SscanfWarning(24, "Unclosed string literal.");
char *
find = strstr(string, format);
if (!find)
{
*input = string;
SetErrorCode(1003);
return SSCANF_FAIL_RETURN;
}
string = find + (write - format);
format = str;
}
}
break;
case '?':
SscanfError(27, "Options are not supported in enums.");
return SSCANF_FAIL_RETURN;
case '!':
SscanfError(71, "End of text is not supported in enums.");
return SSCANF_FAIL_RETURN;
case '%':
SscanfWarning(25, "sscanf specifiers do not require '%' before them.");
continue;
case '-':
{
int
len = 1;
switch (*format++)
{
case 'i':
case 'f':
case 'l':
case 'b':
case 'n':
case 'c':
case 'd':
case 'h':
case 'm':
case 'x':
case 'o':
case 'g':
break;
case 'I':
case 'F':
case 'L':
case 'B':
case 'N':
case 'C':
case 'D':
case 'H':
case 'M':
case 'X':
case 'O':
case 'G':
OPTIONAL_INVALID;
break;
case 'K':
OPTIONAL_INVALID;
case 'k':
GetMultiType(&format);
break;
case 'P':
case 'p':
SscanfWarning(52, "A minus delimiter makes no sense.");
len = 0;
break;
case '{':
case '}':
SscanfWarning(53, "A minus quiet section makes no sense.");
len = 0;
break;
case 'U':
case 'Q':
case 'R':
OPTIONAL_INVALID;
case 'u':
case 'q':
case 'r':
if (*format == '[')
{
len = GetLength(&format, args);
}
break;
case 'A':
OPTIONAL_INVALID;
case 'a':
len = GetLength(&format, args);
break;
case 'E':
OPTIONAL_INVALID;
case 'e':
SscanfError(67, "Enums are not supported in enums.");
*input = string;
return SSCANF_FAIL_RETURN;
case 'Z':
OPTIONAL_INVALID;
// FALLTHROUGH
case 'z':
len = GetLength(&format, args);
break;
case 'S':
OPTIONAL_INVALID;
// FALLTHROUGH
case 's':
len = GetLength(&format, args);
break;
case '?':
SscanfWarning(51, "A minus option makes no sense.");
len = 0;
break;
case '!':
SscanfWarning(72, "A minus end of text makes no sense.");
len = 0;
break;
case '%':
SscanfWarning(25, "sscanf specifiers do not require '%' before them.");
len = 0;
break;
case '-':
SscanfWarning(50, "A minus minus makes no sense.");
len = 0;
break;
default:
SscanfWarning(9, "Unknown format specifier '%c', skipping.", *(format - 1));
len = 0;
break;
}
if (doSave)
{
cptr += len;
}
}
break;
default:
SscanfWarning(9, "Unknown format specifier '%c', skipping.", *(format - 1));
continue;
}
// Loop cleanup - only skip one spacer so that we can detect
// multiple explicit delimiters in a row, for example:
//
// hi there
//
// is NOT multiple explicit delimiters in a row (they're
// whitespace). This however is:
//
// hi , , , there
//
SkipOneSpacer(&string);
}
}
// Save the end of the string.
*input = string;
if (*format)
{
if (*format == '}' && IsEnd(*(format + 1)))
{
return SSCANF_TRUE_RETURN;
}
// Ran out of values - check if this is an optional array at a higher
// level.
return SSCANF_CONT_RETURN;
}
return SSCANF_TRUE_RETURN;
}
bool
DoE(char ** defaults, char ** input, struct args_s & args, bool optional, bool doSave)
{
// First, get the type of the array.
char *
type = GetMultiType(defaults);
cell *
cptr = NULL;
args.Mark();
if (!type)
{
return false;
}
if (optional)
{
// Optional parameters - just collect the data for
// now and use it later.
if (**defaults == '(')
{
++(*defaults);
SkipWhitespace(defaults);
// Got the start of the values.
char *
opts = *defaults;
// Skip the defaults for now, we don't know the length yet.
bool
escape = false;
while (**defaults && (escape || **defaults != ')'))
{
if (**defaults == '\\')
{
escape = !escape;
}
else
{
escape = false;
}
++(*defaults);
}
if (**defaults)
{
if (opts == *defaults)
{
// No defaults found.
SscanfWarning(10, "Empty default values.");
optional = false;
}
// Found a valid end. Make it null for
// later array getting (easy to detect a
// null end and we'll never need to
// backtrack to here in the specifiers).
**defaults = '\0';
++(*defaults);
}
else
{
SscanfWarning(11, "Unclosed default value.");
}
if (optional)
{
// Optional parameters are always separated by commans, not
// whatever the coder may choose.
TempDelimiter(",)");
// We need to copy the old save value for optional parts. If
// we don't and save gets set to false in the middle of the
// enum then when the code is called for a second time for the
// real values then save will already be false and they won't
// get saved.
if (doSave)
{
char *
tmp = opts;
DoEnumValues(type, &tmp, NULL, true, args);
cptr = args.Next();
args.Restore();
}
// Do this twice. Once to get the lengths, once for the data.
switch (DoEnumValues(type, &opts, cptr, true, args))
{
case SSCANF_TRUE_RETURN:
break;
case SSCANF_CONT_RETURN:
SscanfError(26, "Insufficient default values.");
// FALLTHROUGH
default:
RestoreDelimiter();
return false;
}
RestoreDelimiter();
if (cptr)
args.Next();
}
}
else
{
SscanfWarning(12, "No default value found.");
optional = false;
}
}
if (input)
{
if (doSave && !cptr)
{
args.Mark();
char *
tmp = *input;
DoEnumValues(type, &tmp, NULL, false, args);
cptr = args.Next();
}
args.Restore();
switch (DoEnumValues(type, input, cptr, false, args))
{
case SSCANF_TRUE_RETURN:
if (cptr)
args.Next();
return true;
case SSCANF_CONT_RETURN:
if (optional)
{
if (cptr)
args.Next();
return true;
}
// FALLTHROUGH
default:
return false;
}
}
return true;
}