Skip to content

Latest commit

 

History

History
1266 lines (1173 loc) · 33.9 KB

SDL_string.c

File metadata and controls

1266 lines (1173 loc) · 33.9 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 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
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
23
24
25
/* This file contains portable string manipulation functions for SDL */
Feb 9, 2006
Feb 9, 2006
26
#include "SDL_stdinc.h"
Feb 9, 2006
Feb 9, 2006
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
47
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
static size_t SDL_ScanLong(const char *text, int radix, long *valuep)
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 15, 2007
Feb 15, 2007
48
if ( SDL_isdigit((unsigned char) *text) ) {
Feb 9, 2006
Feb 9, 2006
50
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
51
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
52
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
if ( negative && value ) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
72
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
73
74
75
76
77
78
79
80
81
82
static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
{
const char *textstart = text;
unsigned long value = 0;
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 15, 2007
Feb 15, 2007
83
if ( SDL_isdigit((unsigned char) *text) ) {
Feb 9, 2006
Feb 9, 2006
85
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
86
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
87
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
*valuep = value;
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
103
104
105
106
107
108
109
110
111
112
113
#ifndef HAVE_SSCANF
static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
{
const char *textstart = text;
uintptr_t value = 0;
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 15, 2007
Feb 15, 2007
114
if ( SDL_isdigit((unsigned char) *text) ) {
Mar 1, 2006
Mar 1, 2006
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
v = *text - '0';
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
v = 10 + (*text - 'A');
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
*valuep = value;
}
return (text - textstart);
}
#endif
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifdef SDL_HAS_64BIT_TYPE
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 15, 2007
Feb 15, 2007
151
if ( SDL_isdigit((unsigned char) *text) ) {
Feb 9, 2006
Feb 9, 2006
153
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
154
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
155
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
if ( negative && value ) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
175
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
176
177
178
179
180
181
182
183
184
185
static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
{
const char *textstart = text;
Uint64 value = 0;
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 15, 2007
Feb 15, 2007
186
if ( SDL_isdigit((unsigned char) *text) ) {
Feb 9, 2006
Feb 9, 2006
188
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
189
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
190
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
*valuep = value;
}
return (text - textstart);
}
#endif
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
207
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
static size_t SDL_ScanFloat(const char *text, double *valuep)
{
const char *textstart = text;
unsigned long lvalue = 0;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
if ( *text == '.' ) {
int mult = 10;
++text;
Feb 15, 2007
Feb 15, 2007
224
while ( SDL_isdigit((unsigned char) *text) ) {
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
lvalue = *text - '0';
value += (double)lvalue / mult;
mult *= 10;
++text;
}
}
if ( valuep ) {
if ( negative && value ) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#ifndef SDL_memset
void *SDL_memset(void *dst, int c, size_t len)
{
size_t left = (len % 4);
if ( len >= 4 ) {
Uint32 value = 0;
Uint32 *dstp = (Uint32 *)dst;
int i;
for (i = 0; i < 4; ++i) {
value <<= 8;
value |= c;
}
len /= 4;
while ( len-- ) {
*dstp++ = value;
}
}
if ( left > 0 ) {
Uint8 value = (Uint8)c;
Uint8 *dstp = (Uint8 *)dst;
switch(left) {
case 3:
*dstp++ = value;
case 2:
*dstp++ = value;
case 1:
*dstp++ = value;
}
}
return dst;
}
#endif
#ifndef SDL_memcpy
void *SDL_memcpy(void *dst, const void *src, size_t len)
{
char *srcp = (char *)src;
char *dstp = (char *)dst;
while ( len-- ) {
*dstp++ = *srcp++;
}
return dst;
}
#endif
#ifndef SDL_revcpy
void *SDL_revcpy(void *dst, const void *src, size_t len)
{
char *srcp = (char *)src;
char *dstp = (char *)dst;
Jun 4, 2007
Jun 4, 2007
292
293
srcp += len-1;
dstp += len-1;
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
while ( len-- ) {
*dstp-- = *srcp--;
}
return dst;
}
#endif
#ifndef SDL_memcmp
int SDL_memcmp(const void *s1, const void *s2, size_t len)
{
char *s1p = (char *)s1;
char *s2p = (char *)s2;
while ( len-- ) {
if ( *s1p != *s2p ) {
return (*s1p - *s2p);
}
++s1p;
++s2p;
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
size_t SDL_strlen(const char *string)
{
size_t len = 0;
while ( *string++ ) {
++len;
}
return len;
}
#endif
Feb 19, 2006
Feb 19, 2006
328
329
#ifndef HAVE_STRLCPY
size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
331
332
333
334
335
size_t srclen = SDL_strlen(src);
if ( maxlen > 0 ) {
size_t len = SDL_min(srclen, maxlen-1);
SDL_memcpy(dst, src, len);
dst[len] = '\0';
Feb 19, 2006
Feb 19, 2006
337
return srclen;
Feb 19, 2006
Feb 19, 2006
341
342
#ifndef HAVE_STRLCAT
size_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
344
345
346
347
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
if ( dstlen < maxlen ) {
SDL_strlcpy(dst+dstlen, src, maxlen-dstlen);
Feb 19, 2006
Feb 19, 2006
349
return dstlen+srclen;
Feb 7, 2006
Feb 7, 2006
353
354
355
#ifndef HAVE_STRDUP
char *SDL_strdup(const char *string)
{
Feb 19, 2006
Feb 19, 2006
356
357
size_t len = SDL_strlen(string)+1;
char *newstr = SDL_malloc(len);
Feb 7, 2006
Feb 7, 2006
358
if ( newstr ) {
Feb 19, 2006
Feb 19, 2006
359
SDL_strlcpy(newstr, string, len);
Feb 7, 2006
Feb 7, 2006
360
361
362
363
364
}
return newstr;
}
#endif
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef HAVE__STRREV
char *SDL_strrev(char *string)
{
size_t len = SDL_strlen(string);
char *a = &string[0];
char *b = &string[len-1];
len /= 2;
while ( len-- ) {
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifndef HAVE__STRUPR
char *SDL_strupr(char *string)
{
char *bufp = string;
while ( *bufp ) {
Feb 15, 2007
Feb 15, 2007
386
*bufp = SDL_toupper((unsigned char) *bufp);
Feb 7, 2006
Feb 7, 2006
387
++bufp;
388
389
390
391
392
393
394
395
396
397
}
return string;
}
#endif
#ifndef HAVE__STRLWR
char *SDL_strlwr(char *string)
{
char *bufp = string;
while ( *bufp ) {
Feb 15, 2007
Feb 15, 2007
398
*bufp = SDL_tolower((unsigned char) *bufp);
Feb 7, 2006
Feb 7, 2006
399
++bufp;
400
401
402
403
404
405
406
407
408
409
410
411
}
return string;
}
#endif
#ifndef HAVE_STRCHR
char *SDL_strchr(const char *string, int c)
{
while ( *string ) {
if ( *string == c ) {
return (char *)string;
}
Feb 7, 2006
Feb 7, 2006
412
++string;
413
414
415
416
417
418
419
420
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
char *SDL_strrchr(const char *string, int c)
{
Feb 7, 2006
Feb 7, 2006
421
const char *bufp = string + SDL_strlen(string) - 1;
422
423
424
425
while ( bufp >= string ) {
if ( *bufp == c ) {
return (char *)bufp;
}
Feb 7, 2006
Feb 7, 2006
426
--bufp;
427
428
429
430
431
432
433
434
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
char *SDL_strstr(const char *haystack, const char *needle)
{
Feb 7, 2006
Feb 7, 2006
435
size_t length = SDL_strlen(needle);
Feb 7, 2006
Feb 7, 2006
437
if ( SDL_strncmp(haystack, needle, length) == 0 ) {
438
439
return (char *)haystack;
}
Feb 7, 2006
Feb 7, 2006
440
++haystack;
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
}
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
char *SDL_ltoa(long value, char *string, int radix)
{
char *bufp = string;
if ( value < 0 ) {
*bufp++ = '-';
value = -value;
}
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if ( *string == '-' ) {
Feb 7, 2006
Feb 7, 2006
477
SDL_strrev(string+1);
Feb 7, 2006
Feb 7, 2006
479
SDL_strrev(string);
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
}
return string;
}
#endif
#ifndef HAVE__ULTOA
char *SDL_ultoa(unsigned long value, char *string, int radix)
{
char *bufp = string;
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Feb 7, 2006
Feb 7, 2006
502
SDL_strrev(string);
503
504
505
506
507
508
509
510
511
512
513
return string;
}
#endif
#ifndef HAVE_STRTOL
long SDL_strtol(const char *string, char **endp, int base)
{
size_t len;
long value;
Jun 20, 2006
Jun 20, 2006
514
515
516
517
518
519
520
521
522
if ( !base ) {
if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
523
524
525
526
527
528
529
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
530
531
532
533
534
535
#ifndef HAVE_STRTOUL
unsigned long SDL_strtoul(const char *string, char **endp, int base)
{
size_t len;
unsigned long value;
Jun 20, 2006
Jun 20, 2006
536
537
538
539
540
541
542
543
544
if ( !base ) {
if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
Mar 1, 2006
Mar 1, 2006
545
546
547
548
549
550
551
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#ifdef SDL_HAS_64BIT_TYPE
#ifndef HAVE__I64TOA
char *SDL_lltoa(Sint64 value, char *string, int radix)
{
char *bufp = string;
if ( value < 0 ) {
*bufp++ = '-';
value = -value;
}
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if ( *string == '-' ) {
Feb 7, 2006
Feb 7, 2006
575
SDL_strrev(string+1);
Feb 7, 2006
Feb 7, 2006
577
SDL_strrev(string);
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
char *SDL_ulltoa(Uint64 value, char *string, int radix)
{
char *bufp = string;
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Feb 7, 2006
Feb 7, 2006
600
SDL_strrev(string);
601
602
603
604
605
return string;
}
#endif
Mar 24, 2018
Mar 24, 2018
606
#if !defined(HAVE_STRTOLL) && !defined(HAVE__STRTOI64)
607
608
609
610
611
Sint64 SDL_strtoll(const char *string, char **endp, int base)
{
size_t len;
Sint64 value;
Jun 20, 2006
Jun 20, 2006
612
613
614
615
616
617
618
619
620
if ( !base ) {
if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
621
622
623
624
625
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
Jul 1, 2018
Jul 1, 2018
626
627
628
629
630
631
632
633
634
#elif defined(__WIN32__) /* so that the export won't be missing */
#undef SDL_strtoll
DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *string, char **endp, int base) {
#ifdef HAVE__STRTOI64
return _strtoi64 (string, endp, base);
#else
return strtoll (string, endp, base);
#endif
}
Mar 24, 2018
Mar 24, 2018
637
#if !defined(HAVE_STRTOULL) && !defined(HAVE__STRTOUI64)
Mar 1, 2006
Mar 1, 2006
638
639
640
641
642
Uint64 SDL_strtoull(const char *string, char **endp, int base)
{
size_t len;
Uint64 value;
Jun 20, 2006
Jun 20, 2006
643
644
645
646
647
648
649
650
651
if ( !base ) {
if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
Mar 1, 2006
Mar 1, 2006
652
653
654
655
656
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
Jul 1, 2018
Jul 1, 2018
657
658
659
660
661
662
663
664
665
#elif defined(__WIN32__) /* so that the export won't be missing */
#undef SDL_strtoull
DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *string, char **endp, int base) {
#ifdef HAVE__STRTOUI64
return _strtoui64(string, endp, base);
#else
return strtoull(string, endp, base);
#endif
}
Mar 1, 2006
Mar 1, 2006
666
667
#endif
668
669
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#ifndef HAVE_STRTOD
double SDL_strtod(const char *string, char **endp)
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
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
#ifndef HAVE_STRCMP
int SDL_strcmp(const char *str1, const char *str2)
{
while (*str1 && *str2) {
if ( *str1 != *str2 )
break;
++str1;
++str2;
}
return (int)((unsigned char)*str1 - (unsigned char)*str2);
}
#endif
#ifndef HAVE_STRNCMP
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
{
while ( *str1 && *str2 && maxlen ) {
if ( *str1 != *str2 )
break;
++str1;
++str2;
--maxlen;
}
if ( ! maxlen ) {
return 0;
}
return (int)((unsigned char)*str1 - (unsigned char)*str2);
}
#endif
Mar 13, 2006
Mar 13, 2006
714
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
715
716
717
718
int SDL_strcasecmp(const char *str1, const char *str2)
{
char a = 0;
char b = 0;
Mar 13, 2006
Mar 13, 2006
719
while ( *str1 && *str2 ) {
Feb 15, 2007
Feb 15, 2007
720
721
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
722
723
724
725
726
727
728
729
730
if ( a != b )
break;
++str1;
++str2;
}
return (int)((unsigned char)a - (unsigned char)b);
}
#endif
Mar 13, 2006
Mar 13, 2006
731
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
Mar 13, 2006
Mar 13, 2006
732
733
734
735
736
int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
{
char a = 0;
char b = 0;
while ( *str1 && *str2 && maxlen ) {
Feb 15, 2007
Feb 15, 2007
737
738
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Mar 13, 2006
Mar 13, 2006
739
740
741
742
743
744
745
746
747
748
if ( a != b )
break;
++str1;
++str2;
--maxlen;
}
return (int)((unsigned char)a - (unsigned char)b);
}
#endif
749
750
751
752
753
754
755
756
757
#ifndef HAVE_SSCANF
int SDL_sscanf(const char *text, const char *fmt, ...)
{
va_list ap;
int retval = 0;
va_start(ap, fmt);
while ( *fmt ) {
if ( *fmt == ' ' ) {
Feb 15, 2007
Feb 15, 2007
758
while ( SDL_isspace((unsigned char) *text) ) {
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
++text;
}
++fmt;
continue;
}
if ( *fmt == '%' ) {
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
enum {
DO_SHORT,
DO_INT,
DO_LONG,
DO_LONGLONG
} inttype = DO_INT;
SDL_bool suppress = SDL_FALSE;
++fmt;
if ( *fmt == '%' ) {
if ( *text == '%' ) {
++text;
++fmt;
continue;
}
break;
}
if ( *fmt == '*' ) {
suppress = SDL_TRUE;
++fmt;
}
fmt += SDL_ScanLong(fmt, 10, &count);
if ( *fmt == 'c' ) {
if ( ! count ) {
count = 1;
}
if ( suppress ) {
while ( count-- ) {
++text;
}
} else {
char *valuep = va_arg(ap, char*);
while ( count-- ) {
*valuep++ = *text++;
}
++retval;
}
continue;
}
Feb 15, 2007
Feb 15, 2007
809
while ( SDL_isspace((unsigned char) *text) ) {
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
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
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;
}
if ( text[index] == '0' ) {
Feb 15, 2007
Feb 15, 2007
842
if ( SDL_tolower((unsigned char) text[index+1]) == 'x' ) {
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
radix = 16;
} else {
radix = 8;
}
}
}
/* Fall through to %d handling */
case 'd':
#ifdef SDL_HAS_64BIT_TYPE
if ( inttype == DO_LONGLONG ) {
Sint64 value;
text += SDL_ScanLongLong(text, radix, &value);
if ( ! suppress ) {
Sint64 *valuep = va_arg(ap, Sint64*);
*valuep = value;
++retval;
}
}
else
#endif /* SDL_HAS_64BIT_TYPE */
{
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;
}
break;
case DO_LONG:
{ long* valuep = va_arg(ap, long*);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
}
++retval;
}
}
done = SDL_TRUE;
break;
case 'o':
if ( radix == 10 ) {
radix = 8;
}
/* Fall through to unsigned handling */
case 'x':
case 'X':
if ( radix == 10 ) {
radix = 16;
}
/* Fall through to unsigned handling */
case 'u':
#ifdef SDL_HAS_64BIT_TYPE
if ( inttype == DO_LONGLONG ) {
Uint64 value;
text += SDL_ScanUnsignedLongLong(text, radix, &value);
if ( ! suppress ) {
Uint64 *valuep = va_arg(ap, Uint64*);
*valuep = value;
++retval;
}
}
else
#endif /* SDL_HAS_64BIT_TYPE */
{
unsigned long value;
text += SDL_ScanUnsignedLong(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;
}
break;
case DO_LONG:
{ long* valuep = va_arg(ap, long*);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
}
++retval;
}
}
done = SDL_TRUE;
break;
case 'p':
{
Mar 1, 2006
Mar 1, 2006
947
948
uintptr_t value;
text += SDL_ScanUintPtrT(text, 16, &value);
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
if ( ! suppress ) {
void** valuep = va_arg(ap, void**);
*valuep = (void*)value;
++retval;
}
}
done = SDL_TRUE;
break;
case 'f':
{
double value;
text += SDL_ScanFloat(text, &value);
if ( ! suppress ) {
float* valuep = va_arg(ap, float*);
*valuep = (float)value;
++retval;
}
}
done = SDL_TRUE;
break;
case 's':
if ( suppress ) {
Feb 15, 2007
Feb 15, 2007
971
while ( !SDL_isspace((unsigned char) *text) ) {
972
973
974
975
976
977
978
979
980
++text;
if ( count ) {
if ( --count == 0 ) {
break;
}
}
}
} else {
char *valuep = va_arg(ap, char*);
Feb 15, 2007
Feb 15, 2007
981
while ( !SDL_isspace((unsigned char) *text) ) {
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*valuep++ = *text++;
if ( count ) {
if ( --count == 0 ) {
break;
}
}
}
*valuep = '\0';
++retval;
}
done = SDL_TRUE;
break;
default:
done = SDL_TRUE;
break;
}
++fmt;
}
continue;