This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_string.c
1414 lines (1303 loc) · 34.9 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
/* This file contains portable string manipulation functions for SDL */
26
#include "SDL_stdinc.h"
27
28
29
30
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
int UTF8_TrailingBytes(unsigned char c)
{
if (c >= 0xC0 && c<= 0xDF)
return 1;
else if (c >= 0xE0 && c <= 0xEF)
return 2;
else if (c >= 0xF0 && c <= 0xF4)
return 3;
else
return 0;
}
47
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
48
49
static size_t
SDL_ScanLong(const char *text, int radix, long *valuep)
50
51
52
53
54
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
55
if (*text == '-') {
56
57
58
negative = SDL_TRUE;
++text;
}
59
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
60
61
text += 2;
}
62
for (;;) {
63
int v;
64
if (SDL_isdigit((unsigned char) *text)) {
65
v = *text - '0';
66
} else if (radix == 16 && SDL_isupperhex(*text)) {
67
v = 10 + (*text - 'A');
68
} else if (radix == 16 && SDL_islowerhex(*text)) {
69
70
71
72
73
74
75
76
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
77
78
if (valuep) {
if (negative && value) {
79
80
81
82
83
84
85
86
87
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
88
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
89
90
static size_t
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
91
92
93
94
{
const char *textstart = text;
unsigned long value = 0;
95
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
96
97
text += 2;
}
98
for (;;) {
99
int v;
100
if (SDL_isdigit((unsigned char) *text)) {
101
v = *text - '0';
102
} else if (radix == 16 && SDL_isupperhex(*text)) {
103
v = 10 + (*text - 'A');
104
} else if (radix == 16 && SDL_islowerhex(*text)) {
105
106
107
108
109
110
111
112
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
113
if (valuep) {
114
115
116
117
118
119
*valuep = value;
}
return (text - textstart);
}
#endif
120
#ifndef HAVE_SSCANF
121
122
static size_t
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
123
124
125
126
{
const char *textstart = text;
uintptr_t value = 0;
127
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
128
129
text += 2;
}
130
for (;;) {
131
int v;
132
if (SDL_isdigit((unsigned char) *text)) {
133
v = *text - '0';
134
} else if (radix == 16 && SDL_isupperhex(*text)) {
135
v = 10 + (*text - 'A');
136
} else if (radix == 16 && SDL_islowerhex(*text)) {
137
138
139
140
141
142
143
144
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
145
if (valuep) {
146
147
148
149
150
151
*valuep = value;
}
return (text - textstart);
}
#endif
152
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
153
154
static size_t
SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
155
156
157
158
159
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
160
if (*text == '-') {
161
162
163
negative = SDL_TRUE;
++text;
}
164
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
165
166
text += 2;
}
167
for (;;) {
168
int v;
169
if (SDL_isdigit((unsigned char) *text)) {
170
v = *text - '0';
171
} else if (radix == 16 && SDL_isupperhex(*text)) {
172
v = 10 + (*text - 'A');
173
} else if (radix == 16 && SDL_islowerhex(*text)) {
174
175
176
177
178
179
180
181
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
182
183
if (valuep) {
if (negative && value) {
184
185
186
187
188
189
190
191
192
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
193
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
194
195
static size_t
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
196
197
198
199
{
const char *textstart = text;
Uint64 value = 0;
200
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
201
202
text += 2;
}
203
for (;;) {
204
int v;
205
if (SDL_isdigit((unsigned char) *text)) {
206
v = *text - '0';
207
} else if (radix == 16 && SDL_isupperhex(*text)) {
208
v = 10 + (*text - 'A');
209
} else if (radix == 16 && SDL_islowerhex(*text)) {
210
211
212
213
214
215
216
217
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
218
if (valuep) {
219
220
221
222
223
224
*valuep = value;
}
return (text - textstart);
}
#endif
225
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
226
227
static size_t
SDL_ScanFloat(const char *text, double *valuep)
228
229
230
231
232
233
{
const char *textstart = text;
unsigned long lvalue = 0;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
234
if (*text == '-') {
235
236
237
238
239
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
240
if (*text == '.') {
241
242
int mult = 10;
++text;
243
while (SDL_isdigit((unsigned char) *text)) {
244
lvalue = *text - '0';
245
value += (double) lvalue / mult;
246
247
248
249
mult *= 10;
++text;
}
}
250
251
if (valuep) {
if (negative && value) {
252
253
254
255
256
257
258
259
260
261
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#ifndef SDL_memset
262
263
void *
SDL_memset(void *dst, int c, size_t len)
264
265
{
size_t left = (len % 4);
266
267
268
269
270
271
272
273
274
Uint32 *dstp4;
Uint8 *dstp1;
Uint32 value4 = (c | (c << 8) | (c << 16) | (c << 24));
Uint8 value1 = (Uint8) c;
dstp4 = (Uint32 *) dst;
len /= 4;
while (len--) {
*dstp4++ = value4;
275
}
276
277
278
279
280
281
282
283
284
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = value1;
case 2:
*dstp1++ = value1;
case 1:
*dstp1++ = value1;
285
}
286
287
288
289
290
291
return dst;
}
#endif
#ifndef SDL_memcpy
292
293
void *
SDL_memcpy(void *dst, const void *src, size_t len)
294
{
295
296
297
298
299
300
301
size_t left = (len % 4);
Uint32 *srcp4, *dstp4;
Uint8 *srcp1, *dstp1;
srcp4 = (Uint32 *) src;
dstp4 = (Uint32 *) dst;
len /= 4;
302
while (len--) {
303
304
305
306
307
308
309
310
311
312
313
314
*dstp4++ = *srcp4++;
}
srcp1 = (Uint8 *) srcp4;
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = *srcp1++;
case 2:
*dstp1++ = *srcp1++;
case 1:
*dstp1++ = *srcp1++;
315
}
316
317
318
319
320
return dst;
}
#endif
321
#ifndef SDL_memmove
322
void *
323
SDL_memmove(void *dst, const void *src, size_t len)
324
{
325
326
char *srcp = (char *) src;
char *dstp = (char *) dst;
327
328
329
330
331
332
333
334
335
336
337
if (src < dst) {
srcp += len - 1;
dstp += len - 1;
while (len--) {
*dstp-- = *srcp--;
}
} else {
while (len--) {
*dstp++ = *srcp++;
}
338
339
340
341
342
343
}
return dst;
}
#endif
#ifndef SDL_memcmp
344
345
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
346
{
347
348
349
350
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
351
return (*s1p - *s2p);
352
353
354
}
++s1p;
++s2p;
355
356
357
358
359
360
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
361
362
size_t
SDL_strlen(const char *string)
363
364
{
size_t len = 0;
365
while (*string++) {
366
367
368
369
370
371
++len;
}
return len;
}
#endif
372
373
#ifndef HAVE_WCSLEN
size_t
374
SDL_wcslen(const wchar_t * string)
375
376
377
378
379
380
381
382
383
{
size_t len = 0;
while (*string++) {
++len;
}
return len;
}
#endif
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
#ifndef HAVE_WCSLCPY
size_t
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
size_t srclen = SDL_wcslen(src);
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
SDL_memcpy(dst, src, len * sizeof(wchar_t));
dst[len] = '\0';
}
return srclen;
}
#endif
#ifndef HAVE_WCSLCAT
size_t
SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
size_t dstlen = SDL_wcslen(dst);
size_t srclen = SDL_wcslen(src);
if (dstlen < maxlen) {
SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
}
return dstlen + srclen;
}
#endif
411
#ifndef HAVE_STRLCPY
412
413
size_t
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
414
{
415
size_t srclen = SDL_strlen(src);
416
417
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
418
419
SDL_memcpy(dst, src, len);
dst[len] = '\0';
420
}
421
return srclen;
422
423
424
}
#endif
425
426
427
428
size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes)
{
size_t src_bytes = SDL_strlen(src);
size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
429
size_t i = 0;
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
char trailing_bytes = 0;
if (bytes)
{
unsigned char c = (unsigned char)src[bytes - 1];
if (UTF8_IsLeadByte(c))
--bytes;
else if (UTF8_IsTrailingByte(c))
{
for (i = bytes - 1; i != 0; --i)
{
c = (unsigned char)src[i];
trailing_bytes = UTF8_TrailingBytes(c);
if (trailing_bytes)
{
if (bytes - i != trailing_bytes + 1)
bytes = i;
break;
}
}
}
SDL_memcpy(dst, src, bytes);
}
dst[bytes] = '\0';
return bytes;
}
457
#ifndef HAVE_STRLCAT
458
459
size_t
SDL_strlcat(char *dst, const char *src, size_t maxlen)
460
{
461
462
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
463
464
if (dstlen < maxlen) {
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
465
}
466
return dstlen + srclen;
467
468
469
}
#endif
470
#ifndef HAVE_STRDUP
471
472
char *
SDL_strdup(const char *string)
473
{
474
size_t len = SDL_strlen(string) + 1;
475
char *newstr = SDL_malloc(len);
476
if (newstr) {
477
SDL_strlcpy(newstr, string, len);
478
479
480
481
482
}
return newstr;
}
#endif
483
#ifndef HAVE__STRREV
484
485
char *
SDL_strrev(char *string)
486
487
488
{
size_t len = SDL_strlen(string);
char *a = &string[0];
489
char *b = &string[len - 1];
490
len /= 2;
491
while (len--) {
492
493
494
495
496
497
498
499
500
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifndef HAVE__STRUPR
501
502
char *
SDL_strupr(char *string)
503
504
{
char *bufp = string;
505
while (*bufp) {
506
*bufp = SDL_toupper((unsigned char) *bufp);
507
++bufp;
508
509
510
511
512
513
}
return string;
}
#endif
#ifndef HAVE__STRLWR
514
515
char *
SDL_strlwr(char *string)
516
517
{
char *bufp = string;
518
while (*bufp) {
519
*bufp = SDL_tolower((unsigned char) *bufp);
520
++bufp;
521
522
523
524
525
526
}
return string;
}
#endif
#ifndef HAVE_STRCHR
527
528
char *
SDL_strchr(const char *string, int c)
529
{
530
531
532
while (*string) {
if (*string == c) {
return (char *) string;
533
}
534
++string;
535
536
537
538
539
540
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
541
542
char *
SDL_strrchr(const char *string, int c)
543
{
544
const char *bufp = string + SDL_strlen(string) - 1;
545
546
547
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
548
}
549
--bufp;
550
551
552
553
554
555
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
556
557
char *
SDL_strstr(const char *haystack, const char *needle)
558
{
559
size_t length = SDL_strlen(needle);
560
561
562
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *) haystack;
563
}
564
++haystack;
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
}
return NULL;
}
#endif
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
!defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
static const char ntoa_table[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
#endif /* ntoa() conversion table */
#ifndef HAVE__LTOA
581
582
char *
SDL_ltoa(long value, char *string, int radix)
583
584
585
{
char *bufp = string;
586
if (value < 0) {
587
588
589
*bufp++ = '-';
value = -value;
}
590
591
if (value) {
while (value > 0) {
592
593
594
595
596
597
598
599
600
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
601
602
if (*string == '-') {
SDL_strrev(string + 1);
603
} else {
604
SDL_strrev(string);
605
606
607
608
609
610
611
}
return string;
}
#endif
#ifndef HAVE__ULTOA
612
613
char *
SDL_ultoa(unsigned long value, char *string, int radix)
614
615
616
{
char *bufp = string;
617
618
if (value) {
while (value > 0) {
619
620
621
622
623
624
625
626
627
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
628
SDL_strrev(string);
629
630
631
632
633
634
return string;
}
#endif
#ifndef HAVE_STRTOL
635
636
long
SDL_strtol(const char *string, char **endp, int base)
637
638
639
640
{
size_t len;
long value;
641
642
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
643
644
645
646
647
648
649
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
650
651
if (endp) {
*endp = (char *) string + len;
652
653
654
655
656
}
return value;
}
#endif
657
#ifndef HAVE_STRTOUL
658
659
unsigned long
SDL_strtoul(const char *string, char **endp, int base)
660
661
662
663
{
size_t len;
unsigned long value;
664
665
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
666
667
668
669
670
671
672
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
673
674
if (endp) {
*endp = (char *) string + len;
675
676
677
678
679
}
return value;
}
#endif
680
#ifndef HAVE__I64TOA
681
682
char *
SDL_lltoa(Sint64 value, char *string, int radix)
683
684
685
{
char *bufp = string;
686
if (value < 0) {
687
688
689
*bufp++ = '-';
value = -value;
}
690
691
if (value) {
while (value > 0) {
692
693
694
695
696
697
698
699
700
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
701
702
if (*string == '-') {
SDL_strrev(string + 1);
703
} else {
704
SDL_strrev(string);
705
706
707
708
709
710
711
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
712
713
char *
SDL_ulltoa(Uint64 value, char *string, int radix)
714
715
716
{
char *bufp = string;
717
718
if (value) {
while (value > 0) {
719
720
721
722
723
724
725
726
727
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
728
SDL_strrev(string);
729
730
731
732
733
734
return string;
}
#endif
#ifndef HAVE_STRTOLL
735
736
Sint64
SDL_strtoll(const char *string, char **endp, int base)
737
738
739
740
{
size_t len;
Sint64 value;
741
742
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
743
744
745
746
747
748
749
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
750
751
if (endp) {
*endp = (char *) string + len;
752
753
754
755
756
}
return value;
}
#endif
757
#ifndef HAVE_STRTOULL
758
759
Uint64
SDL_strtoull(const char *string, char **endp, int base)
760
761
762
763
{
size_t len;
Uint64 value;
764
765
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
766
767
768
769
770
771
772
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
773
774
if (endp) {
*endp = (char *) string + len;
775
776
777
778
779
}
return value;
}
#endif
780
#ifndef HAVE_STRTOD
781
782
double
SDL_strtod(const char *string, char **endp)
783
784
785
786
787
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
788
789
if (endp) {
*endp = (char *) string + len;
790
791
792
793
794
}
return value;
}
#endif
795
#ifndef HAVE_STRCMP
796
797
int
SDL_strcmp(const char *str1, const char *str2)
798
799
{
while (*str1 && *str2) {
800
if (*str1 != *str2)
801
802
803
804
break;
++str1;
++str2;
}
805
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
806
807
808
809
}
#endif
#ifndef HAVE_STRNCMP
810
811
int
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
812
{
813
814
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
815
816
817
818
819
break;
++str1;
++str2;
--maxlen;
}
820
if (!maxlen) {
821
822
return 0;
}
823
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
824
825
826
}
#endif
827
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
828
829
int
SDL_strcasecmp(const char *str1, const char *str2)
830
831
832
{
char a = 0;
char b = 0;
833
while (*str1 && *str2) {
834
835
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
836
if (a != b)
837
838
839
840
break;
++str1;
++str2;
}
841
842
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
843
return (int) ((unsigned char) a - (unsigned char) b);
844
845
846
}
#endif
847
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
848
849
int
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
850
851
852
{
char a = 0;
char b = 0;
853
while (*str1 && *str2 && maxlen) {
854
855
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
856
if (a != b)
857
858
859
860
861
break;
++str1;
++str2;
--maxlen;
}
862
863
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
864
return (int) ((unsigned char) a - (unsigned char) b);
865
866
867
}
#endif
868
#ifndef HAVE_SSCANF
869
870
int
SDL_sscanf(const char *text, const char *fmt, ...)
871
872
873
874
875
{
va_list ap;
int retval = 0;
va_start(ap, fmt);
876
877
while (*fmt) {
if (*fmt == ' ') {
878
while (SDL_isspace((unsigned char) *text)) {
879
880
881
882
883
++text;
}
++fmt;
continue;
}
884
if (*fmt == '%') {
885
886
887
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
888
889
enum
{
890
891
892
893
894
895
896
897
DO_SHORT,
DO_INT,
DO_LONG,
DO_LONGLONG
} inttype = DO_INT;
SDL_bool suppress = SDL_FALSE;
++fmt;
898
899
if (*fmt == '%') {
if (*text == '%') {
900
901
902
903
904
905
++text;
++fmt;
continue;
}
break;
}
906
if (*fmt == '*') {
907
908
909
910
911
suppress = SDL_TRUE;
++fmt;
}
fmt += SDL_ScanLong(fmt, 10, &count);
912
913
if (*fmt == 'c') {
if (!count) {
914
915
count = 1;
}
916
917
if (suppress) {
while (count--) {
918
919
920
++text;
}
} else {
921
922
char *valuep = va_arg(ap, char *);
while (count--) {
923
924
925
926
927
928
929
*valuep++ = *text++;
}
++retval;
}
continue;
}
930
while (SDL_isspace((unsigned char) *text)) {
931
932
933
934
935
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
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
switch (*fmt) {
case '*':
suppress = SDL_TRUE;
break;
case 'h':
if (inttype > DO_SHORT) {
++inttype;
}
break;
case 'l':
if (inttype < DO_LONGLONG) {
++inttype;
}
break;
case 'I':
if (SDL_strncmp(fmt, "I64", 3) == 0) {
fmt += 2;
inttype = DO_LONGLONG;
}
break;
case 'i':
{
int index = 0;
if (text[index] == '-') {
++index;
961
}
962
if (text[index] == '0') {
963
964
if (SDL_tolower((unsigned char) text[index + 1])
== 'x') {
965
966
967
radix = 16;
} else {
radix = 8;
968
969
}
}
970
971
972
973
974
975
976
977
978
979
}
/* Fall through to %d handling */
case 'd':
if (inttype == DO_LONGLONG) {
Sint64 value;
text += SDL_ScanLongLong(text, radix, &value);
if (!suppress) {
Sint64 *valuep = va_arg(ap, Sint64 *);
*valuep = value;
++retval;
980
}
981
} else {
982
983
984
985
986
987
988
989
990
991
992
993
994
995
long value;
text += SDL_ScanLong(text, radix, &value);
if (!suppress) {
switch (inttype) {
case DO_SHORT:
{
short *valuep = va_arg(ap, short *);
*valuep = (short) value;
}
break;
case DO_INT:
{
int *valuep = va_arg(ap, int *);
*valuep = (int) value;
996
}
997
998
999
1000
break;
case DO_LONG:
{
long *valuep = va_arg(ap, long *);