Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1348 lines (1249 loc) · 33.7 KB

SDL_string.c

File metadata and controls

1348 lines (1249 loc) · 33.7 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 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
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
Jul 10, 2006
Jul 10, 2006
33
34
static size_t
SDL_ScanLong(const char *text, int radix, long *valuep)
35
36
37
38
39
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
Jul 10, 2006
Jul 10, 2006
40
if (*text == '-') {
41
42
43
negative = SDL_TRUE;
++text;
}
Jul 10, 2006
Jul 10, 2006
44
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
47
for (;;) {
Feb 15, 2007
Feb 15, 2007
49
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
51
} else if (radix == 16 && SDL_isupperhex(*text)) {
52
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
53
} else if (radix == 16 && SDL_islowerhex(*text)) {
54
55
56
57
58
59
60
61
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
62
63
if (valuep) {
if (negative && value) {
64
65
66
67
68
69
70
71
72
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
73
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
Jul 10, 2006
Jul 10, 2006
74
75
static size_t
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
76
77
78
79
{
const char *textstart = text;
unsigned long value = 0;
Jul 10, 2006
Jul 10, 2006
80
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
83
for (;;) {
Feb 15, 2007
Feb 15, 2007
85
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
87
} else if (radix == 16 && SDL_isupperhex(*text)) {
88
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
89
} else if (radix == 16 && SDL_islowerhex(*text)) {
90
91
92
93
94
95
96
97
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
98
if (valuep) {
99
100
101
102
103
104
*valuep = value;
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
105
#ifndef HAVE_SSCANF
Jul 10, 2006
Jul 10, 2006
106
107
static size_t
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
Mar 1, 2006
Mar 1, 2006
108
109
110
111
{
const char *textstart = text;
uintptr_t value = 0;
Jul 10, 2006
Jul 10, 2006
112
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Mar 1, 2006
Mar 1, 2006
113
114
text += 2;
}
Jul 10, 2006
Jul 10, 2006
115
for (;;) {
Mar 1, 2006
Mar 1, 2006
116
int v;
Feb 15, 2007
Feb 15, 2007
117
if (SDL_isdigit((unsigned char) *text)) {
Mar 1, 2006
Mar 1, 2006
118
v = *text - '0';
Jul 10, 2006
Jul 10, 2006
119
} else if (radix == 16 && SDL_isupperhex(*text)) {
Mar 1, 2006
Mar 1, 2006
120
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
121
} else if (radix == 16 && SDL_islowerhex(*text)) {
Mar 1, 2006
Mar 1, 2006
122
123
124
125
126
127
128
129
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
130
if (valuep) {
Mar 1, 2006
Mar 1, 2006
131
132
133
134
135
136
*valuep = value;
}
return (text - textstart);
}
#endif
137
138
#ifdef SDL_HAS_64BIT_TYPE
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
Jul 10, 2006
Jul 10, 2006
139
140
static size_t
SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
141
142
143
144
145
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
Jul 10, 2006
Jul 10, 2006
146
if (*text == '-') {
147
148
149
negative = SDL_TRUE;
++text;
}
Jul 10, 2006
Jul 10, 2006
150
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
153
for (;;) {
Feb 15, 2007
Feb 15, 2007
155
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
157
} else if (radix == 16 && SDL_isupperhex(*text)) {
158
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
159
} else if (radix == 16 && SDL_islowerhex(*text)) {
160
161
162
163
164
165
166
167
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
168
169
if (valuep) {
if (negative && value) {
170
171
172
173
174
175
176
177
178
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
179
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
Jul 10, 2006
Jul 10, 2006
180
181
static size_t
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
182
183
184
185
{
const char *textstart = text;
Uint64 value = 0;
Jul 10, 2006
Jul 10, 2006
186
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
189
for (;;) {
Feb 15, 2007
Feb 15, 2007
191
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
193
} else if (radix == 16 && SDL_isupperhex(*text)) {
194
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
195
} else if (radix == 16 && SDL_islowerhex(*text)) {
196
197
198
199
200
201
202
203
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
204
if (valuep) {
205
206
207
208
209
210
211
*valuep = value;
}
return (text - textstart);
}
#endif
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
212
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
Jul 10, 2006
Jul 10, 2006
213
214
static size_t
SDL_ScanFloat(const char *text, double *valuep)
215
216
217
218
219
220
{
const char *textstart = text;
unsigned long lvalue = 0;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
Jul 10, 2006
Jul 10, 2006
221
if (*text == '-') {
222
223
224
225
226
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
Jul 10, 2006
Jul 10, 2006
227
if (*text == '.') {
228
229
int mult = 10;
++text;
Feb 15, 2007
Feb 15, 2007
230
while (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
232
value += (double) lvalue / mult;
233
234
235
236
mult *= 10;
++text;
}
}
Jul 10, 2006
Jul 10, 2006
237
238
if (valuep) {
if (negative && value) {
239
240
241
242
243
244
245
246
247
248
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#ifndef SDL_memset
Jul 10, 2006
Jul 10, 2006
249
250
void *
SDL_memset(void *dst, int c, size_t len)
251
252
{
size_t left = (len % 4);
Jul 10, 2006
Jul 10, 2006
253
if (len >= 4) {
Jul 10, 2006
Jul 10, 2006
255
Uint32 *dstp = (Uint32 *) dst;
256
257
258
259
260
261
int i;
for (i = 0; i < 4; ++i) {
value <<= 8;
value |= c;
}
len /= 4;
Jul 10, 2006
Jul 10, 2006
262
while (len--) {
263
264
265
*dstp++ = value;
}
}
Jul 10, 2006
Jul 10, 2006
266
267
268
269
270
if (left > 0) {
Uint8 value = (Uint8) c;
Uint8 *dstp = (Uint8 *) dst;
switch (left) {
case 3:
Jul 10, 2006
Jul 10, 2006
272
case 2:
Jul 10, 2006
Jul 10, 2006
274
case 1:
275
276
277
278
279
280
281
282
*dstp++ = value;
}
}
return dst;
}
#endif
#ifndef SDL_memcpy
Jul 10, 2006
Jul 10, 2006
283
284
void *
SDL_memcpy(void *dst, const void *src, size_t len)
Jul 10, 2006
Jul 10, 2006
286
287
288
char *srcp = (char *) src;
char *dstp = (char *) dst;
while (len--) {
289
290
291
292
293
294
295
*dstp++ = *srcp++;
}
return dst;
}
#endif
#ifndef SDL_revcpy
Jul 10, 2006
Jul 10, 2006
296
297
void *
SDL_revcpy(void *dst, const void *src, size_t len)
Jul 10, 2006
Jul 10, 2006
299
300
char *srcp = (char *) src;
char *dstp = (char *) dst;
Jun 14, 2007
Jun 14, 2007
301
302
srcp += len - 1;
dstp += len - 1;
Jul 10, 2006
Jul 10, 2006
303
while (len--) {
304
305
306
307
308
309
310
*dstp-- = *srcp--;
}
return dst;
}
#endif
#ifndef SDL_memcmp
Jul 10, 2006
Jul 10, 2006
311
312
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
Jul 10, 2006
Jul 10, 2006
314
315
316
317
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
Jul 10, 2006
Jul 10, 2006
319
320
321
}
++s1p;
++s2p;
322
323
324
325
326
327
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
Jul 10, 2006
Jul 10, 2006
328
329
size_t
SDL_strlen(const char *string)
Jul 10, 2006
Jul 10, 2006
332
while (*string++) {
333
334
335
336
337
338
++len;
}
return len;
}
#endif
Jul 13, 2006
Jul 13, 2006
339
340
#ifndef HAVE_WCSLEN
size_t
Jul 14, 2006
Jul 14, 2006
341
SDL_wcslen(const wchar_t * string)
Jul 13, 2006
Jul 13, 2006
342
343
344
345
346
347
348
349
350
{
size_t len = 0;
while (*string++) {
++len;
}
return len;
}
#endif
Feb 19, 2006
Feb 19, 2006
351
#ifndef HAVE_STRLCPY
Jul 10, 2006
Jul 10, 2006
352
353
size_t
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
355
size_t srclen = SDL_strlen(src);
Jul 10, 2006
Jul 10, 2006
356
357
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
Feb 19, 2006
Feb 19, 2006
358
359
SDL_memcpy(dst, src, len);
dst[len] = '\0';
Feb 19, 2006
Feb 19, 2006
361
return srclen;
Feb 19, 2006
Feb 19, 2006
365
#ifndef HAVE_STRLCAT
Jul 10, 2006
Jul 10, 2006
366
367
size_t
SDL_strlcat(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
369
370
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
Jul 10, 2006
Jul 10, 2006
371
372
if (dstlen < maxlen) {
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
Jul 10, 2006
Jul 10, 2006
374
return dstlen + srclen;
Feb 7, 2006
Feb 7, 2006
378
#ifndef HAVE_STRDUP
Jul 10, 2006
Jul 10, 2006
379
380
char *
SDL_strdup(const char *string)
Feb 7, 2006
Feb 7, 2006
381
{
Jul 10, 2006
Jul 10, 2006
382
size_t len = SDL_strlen(string) + 1;
Feb 19, 2006
Feb 19, 2006
383
char *newstr = SDL_malloc(len);
Jul 10, 2006
Jul 10, 2006
384
if (newstr) {
Feb 19, 2006
Feb 19, 2006
385
SDL_strlcpy(newstr, string, len);
Feb 7, 2006
Feb 7, 2006
386
387
388
389
390
}
return newstr;
}
#endif
Jul 10, 2006
Jul 10, 2006
392
393
char *
SDL_strrev(char *string)
394
395
396
{
size_t len = SDL_strlen(string);
char *a = &string[0];
Jul 10, 2006
Jul 10, 2006
397
char *b = &string[len - 1];
Jul 10, 2006
Jul 10, 2006
399
while (len--) {
400
401
402
403
404
405
406
407
408
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifndef HAVE__STRUPR
Jul 10, 2006
Jul 10, 2006
409
410
char *
SDL_strupr(char *string)
411
412
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
413
while (*bufp) {
Feb 15, 2007
Feb 15, 2007
414
*bufp = SDL_toupper((unsigned char) *bufp);
Jul 10, 2006
Jul 10, 2006
415
++bufp;
416
417
418
419
420
421
}
return string;
}
#endif
#ifndef HAVE__STRLWR
Jul 10, 2006
Jul 10, 2006
422
423
char *
SDL_strlwr(char *string)
424
425
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
426
while (*bufp) {
Feb 15, 2007
Feb 15, 2007
427
*bufp = SDL_tolower((unsigned char) *bufp);
Jul 10, 2006
Jul 10, 2006
428
++bufp;
429
430
431
432
433
434
}
return string;
}
#endif
#ifndef HAVE_STRCHR
Jul 10, 2006
Jul 10, 2006
435
436
char *
SDL_strchr(const char *string, int c)
Jul 10, 2006
Jul 10, 2006
438
439
440
while (*string) {
if (*string == c) {
return (char *) string;
Jul 10, 2006
Jul 10, 2006
442
++string;
443
444
445
446
447
448
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
Jul 10, 2006
Jul 10, 2006
449
450
char *
SDL_strrchr(const char *string, int c)
Feb 7, 2006
Feb 7, 2006
452
const char *bufp = string + SDL_strlen(string) - 1;
Jul 10, 2006
Jul 10, 2006
453
454
455
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
Jul 10, 2006
Jul 10, 2006
457
--bufp;
458
459
460
461
462
463
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
Jul 10, 2006
Jul 10, 2006
464
465
char *
SDL_strstr(const char *haystack, const char *needle)
Feb 7, 2006
Feb 7, 2006
467
size_t length = SDL_strlen(needle);
Jul 10, 2006
Jul 10, 2006
468
469
470
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *) haystack;
Jul 10, 2006
Jul 10, 2006
472
++haystack;
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
}
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
Jul 10, 2006
Jul 10, 2006
489
490
char *
SDL_ltoa(long value, char *string, int radix)
491
492
493
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
494
if (value < 0) {
495
496
497
*bufp++ = '-';
value = -value;
}
Jul 10, 2006
Jul 10, 2006
498
499
if (value) {
while (value > 0) {
500
501
502
503
504
505
506
507
508
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Jul 10, 2006
Jul 10, 2006
509
510
if (*string == '-') {
SDL_strrev(string + 1);
Feb 7, 2006
Feb 7, 2006
512
SDL_strrev(string);
513
514
515
516
517
518
519
}
return string;
}
#endif
#ifndef HAVE__ULTOA
Jul 10, 2006
Jul 10, 2006
520
521
char *
SDL_ultoa(unsigned long value, char *string, int radix)
522
523
524
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
525
526
if (value) {
while (value > 0) {
527
528
529
530
531
532
533
534
535
*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
536
SDL_strrev(string);
537
538
539
540
541
542
return string;
}
#endif
#ifndef HAVE_STRTOL
Jul 10, 2006
Jul 10, 2006
543
544
long
SDL_strtol(const char *string, char **endp, int base)
545
546
547
548
{
size_t len;
long value;
Jul 10, 2006
Jul 10, 2006
549
550
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
551
552
553
554
555
556
557
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
558
559
if (endp) {
*endp = (char *) string + len;
560
561
562
563
564
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
565
#ifndef HAVE_STRTOUL
Jul 10, 2006
Jul 10, 2006
566
567
unsigned long
SDL_strtoul(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
568
569
570
571
{
size_t len;
unsigned long value;
Jul 10, 2006
Jul 10, 2006
572
573
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
574
575
576
577
578
579
580
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
581
582
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
583
584
585
586
587
}
return value;
}
#endif
588
589
590
#ifdef SDL_HAS_64BIT_TYPE
#ifndef HAVE__I64TOA
Jul 10, 2006
Jul 10, 2006
591
592
char *
SDL_lltoa(Sint64 value, char *string, int radix)
593
594
595
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
596
if (value < 0) {
597
598
599
*bufp++ = '-';
value = -value;
}
Jul 10, 2006
Jul 10, 2006
600
601
if (value) {
while (value > 0) {
602
603
604
605
606
607
608
609
610
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Jul 10, 2006
Jul 10, 2006
611
612
if (*string == '-') {
SDL_strrev(string + 1);
Feb 7, 2006
Feb 7, 2006
614
SDL_strrev(string);
615
616
617
618
619
620
621
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
Jul 10, 2006
Jul 10, 2006
622
623
char *
SDL_ulltoa(Uint64 value, char *string, int radix)
624
625
626
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
627
628
if (value) {
while (value > 0) {
629
630
631
632
633
634
635
636
637
*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
638
SDL_strrev(string);
639
640
641
642
643
644
return string;
}
#endif
#ifndef HAVE_STRTOLL
Jul 10, 2006
Jul 10, 2006
645
646
Sint64
SDL_strtoll(const char *string, char **endp, int base)
647
648
649
650
{
size_t len;
Sint64 value;
Jul 10, 2006
Jul 10, 2006
651
652
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
653
654
655
656
657
658
659
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
660
661
if (endp) {
*endp = (char *) string + len;
662
663
664
665
666
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
667
#ifndef HAVE_STRTOULL
Jul 10, 2006
Jul 10, 2006
668
669
Uint64
SDL_strtoull(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
670
671
672
673
{
size_t len;
Uint64 value;
Jul 10, 2006
Jul 10, 2006
674
675
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
676
677
678
679
680
681
682
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
683
684
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
685
686
687
688
689
}
return value;
}
#endif
690
691
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
692
#ifndef HAVE_STRTOD
Jul 10, 2006
Jul 10, 2006
693
694
double
SDL_strtod(const char *string, char **endp)
Feb 7, 2006
Feb 7, 2006
695
696
697
698
699
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
Jul 10, 2006
Jul 10, 2006
700
701
if (endp) {
*endp = (char *) string + len;
Feb 7, 2006
Feb 7, 2006
702
703
704
705
706
}
return value;
}
#endif
Jul 10, 2006
Jul 10, 2006
708
709
int
SDL_strcmp(const char *str1, const char *str2)
710
711
{
while (*str1 && *str2) {
Jul 10, 2006
Jul 10, 2006
712
if (*str1 != *str2)
713
714
715
716
break;
++str1;
++str2;
}
Jul 10, 2006
Jul 10, 2006
717
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
718
719
720
721
}
#endif
#ifndef HAVE_STRNCMP
Jul 10, 2006
Jul 10, 2006
722
723
int
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
Jul 10, 2006
Jul 10, 2006
725
726
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
727
728
729
730
731
break;
++str1;
++str2;
--maxlen;
}
Jul 10, 2006
Jul 10, 2006
732
if (!maxlen) {
Jul 10, 2006
Jul 10, 2006
735
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
Mar 13, 2006
Mar 13, 2006
739
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
Jul 10, 2006
Jul 10, 2006
740
741
int
SDL_strcasecmp(const char *str1, const char *str2)
742
743
744
{
char a = 0;
char b = 0;
Jul 10, 2006
Jul 10, 2006
745
while (*str1 && *str2) {
Feb 15, 2007
Feb 15, 2007
746
747
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Jul 10, 2006
Jul 10, 2006
748
if (a != b)
749
750
751
752
break;
++str1;
++str2;
}
Jul 19, 2006
Jul 19, 2006
753
754
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
Jul 10, 2006
Jul 10, 2006
755
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
759
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
Jul 10, 2006
Jul 10, 2006
760
761
int
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
Mar 13, 2006
Mar 13, 2006
762
763
764
{
char a = 0;
char b = 0;
Jul 10, 2006
Jul 10, 2006
765
while (*str1 && *str2 && maxlen) {
Feb 15, 2007
Feb 15, 2007
766
767
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Jul 10, 2006
Jul 10, 2006
768
if (a != b)
Mar 13, 2006
Mar 13, 2006
769
770
771
772
773
break;
++str1;
++str2;
--maxlen;
}
Jul 19, 2006
Jul 19, 2006
774
775
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
Jul 10, 2006
Jul 10, 2006
776
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
777
778
779
}
#endif
Jul 10, 2006
Jul 10, 2006
781
782
int
SDL_sscanf(const char *text, const char *fmt, ...)
783
784
785
786
787
{
va_list ap;
int retval = 0;
va_start(ap, fmt);
Jul 10, 2006
Jul 10, 2006
788
789
while (*fmt) {
if (*fmt == ' ') {
Feb 15, 2007
Feb 15, 2007
790
while (SDL_isspace((unsigned char) *text)) {
791
792
793
794
795
++text;
}
++fmt;
continue;
}
Jul 10, 2006
Jul 10, 2006
796
if (*fmt == '%') {
797
798
799
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
Jul 10, 2006
Jul 10, 2006
800
801
enum
{
802
803
804
805
806
807
808
809
DO_SHORT,
DO_INT,
DO_LONG,
DO_LONGLONG
} inttype = DO_INT;
SDL_bool suppress = SDL_FALSE;
++fmt;
Jul 10, 2006
Jul 10, 2006
810
811
if (*fmt == '%') {
if (*text == '%') {
812
813
814
815
816
817
++text;
++fmt;
continue;
}
break;
}
Jul 10, 2006
Jul 10, 2006
818
if (*fmt == '*') {
819
820
821
822
823
suppress = SDL_TRUE;
++fmt;
}
fmt += SDL_ScanLong(fmt, 10, &count);
Jul 10, 2006
Jul 10, 2006
824
825
if (*fmt == 'c') {
if (!count) {
Jul 10, 2006
Jul 10, 2006
828
829
if (suppress) {
while (count--) {
830
831
832
++text;
}
} else {
Jul 10, 2006
Jul 10, 2006
833
834
char *valuep = va_arg(ap, char *);
while (count--) {
835
836
837
838
839
840
841
*valuep++ = *text++;
}
++retval;
}
continue;
}
Feb 15, 2007
Feb 15, 2007
842
while (SDL_isspace((unsigned char) *text)) {
843
844
845
846
847
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
Jul 10, 2006
Jul 10, 2006
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
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;
Jul 10, 2006
Jul 10, 2006
874
if (text[index] == '0') {
Jun 14, 2007
Jun 14, 2007
875
876
if (SDL_tolower((unsigned char) text[index + 1])
== 'x') {
Jul 10, 2006
Jul 10, 2006
877
878
879
radix = 16;
} else {
radix = 8;
Jul 10, 2006
Jul 10, 2006
882
883
884
}
/* Fall through to %d handling */
case 'd':
885
#ifdef SDL_HAS_64BIT_TYPE
Jul 10, 2006
Jul 10, 2006
886
887
888
889
890
891
892
if (inttype == DO_LONGLONG) {
Sint64 value;
text += SDL_ScanLongLong(text, radix, &value);
if (!suppress) {
Sint64 *valuep = va_arg(ap, Sint64 *);
*valuep = value;
++retval;
Jul 10, 2006
Jul 10, 2006
894
} else
895
#endif /* SDL_HAS_64BIT_TYPE */
Jul 10, 2006
Jul 10, 2006
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
{
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;
Jul 10, 2006
Jul 10, 2006
912
913
914
915
916
917
918
919
920
921
break;
case DO_LONG:
{
long *valuep = va_arg(ap, long *);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
Jul 10, 2006
Jul 10, 2006
923
++retval;
Jul 10, 2006
Jul 10, 2006
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
}
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':
940
#ifdef SDL_HAS_64BIT_TYPE
Jul 10, 2006
Jul 10, 2006
941
942
943
944
945
946
947
if (inttype == DO_LONGLONG) {
Uint64 value;
text += SDL_ScanUnsignedLongLong(text, radix, &value);
if (!suppress) {
Uint64 *valuep = va_arg(ap, Uint64 *);
*valuep = value;
++retval;
Jul 10, 2006
Jul 10, 2006
949
} else
950
#endif /* SDL_HAS_64BIT_TYPE */
Jul 10, 2006
Jul 10, 2006
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
{
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;
Jul 10, 2006
Jul 10, 2006
967
968
969
970
971
972
973
974
975
976
break;
case DO_LONG:
{
long *valuep = va_arg(ap, long *);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
Jul 10, 2006
Jul 10, 2006
978
++retval;
Jul 10, 2006
Jul 10, 2006
980
981
982
983
984
985
986
987
988
989
990
}
done = SDL_TRUE;
break;
case 'p':
{
uintptr_t value;
text += SDL_ScanUintPtrT(text, 16, &value);
if (!suppress) {
void **valuep = va_arg(ap, void **);
*valuep = (void *) value;
++retval;
Jul 10, 2006
Jul 10, 2006
992
993
994
995
996
997
998
999
1000
}
done = SDL_TRUE;
break;
case 'f':
{
double value;
text += SDL_ScanFloat(text, &value);
if (!suppress) {
float *valuep = va_arg(ap, float *);