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