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

Latest commit

 

History

History
1299 lines (1205 loc) · 32.5 KB

SDL_string.c

File metadata and controls

1299 lines (1205 loc) · 32.5 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
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)
May 28, 2006
May 28, 2006
33
static size_t
May 29, 2006
May 29, 2006
34
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;
May 28, 2006
May 28, 2006
40
if (*text == '-') {
41
42
43
negative = SDL_TRUE;
++text;
}
May 29, 2006
May 29, 2006
44
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
May 28, 2006
May 28, 2006
47
for (;;) {
May 29, 2006
May 29, 2006
49
if (SDL_isdigit(*text)) {
May 29, 2006
May 29, 2006
51
} else if (radix == 16 && SDL_isupperhex(*text)) {
52
v = 10 + (*text - 'A');
May 29, 2006
May 29, 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;
}
May 28, 2006
May 28, 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)
May 28, 2006
May 28, 2006
74
static size_t
May 29, 2006
May 29, 2006
75
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
76
77
78
79
{
const char *textstart = text;
unsigned long value = 0;
May 29, 2006
May 29, 2006
80
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
May 28, 2006
May 28, 2006
83
for (;;) {
May 29, 2006
May 29, 2006
85
if (SDL_isdigit(*text)) {
May 29, 2006
May 29, 2006
87
} else if (radix == 16 && SDL_isupperhex(*text)) {
88
v = 10 + (*text - 'A');
May 29, 2006
May 29, 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;
}
May 28, 2006
May 28, 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
May 28, 2006
May 28, 2006
106
static size_t
May 29, 2006
May 29, 2006
107
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;
May 29, 2006
May 29, 2006
112
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Mar 1, 2006
Mar 1, 2006
113
114
text += 2;
}
May 28, 2006
May 28, 2006
115
for (;;) {
Mar 1, 2006
Mar 1, 2006
116
int v;
May 29, 2006
May 29, 2006
117
if (SDL_isdigit(*text)) {
Mar 1, 2006
Mar 1, 2006
118
v = *text - '0';
May 29, 2006
May 29, 2006
119
} else if (radix == 16 && SDL_isupperhex(*text)) {
Mar 1, 2006
Mar 1, 2006
120
v = 10 + (*text - 'A');
May 29, 2006
May 29, 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;
}
May 28, 2006
May 28, 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)
May 28, 2006
May 28, 2006
139
static size_t
May 29, 2006
May 29, 2006
140
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;
May 28, 2006
May 28, 2006
146
if (*text == '-') {
147
148
149
negative = SDL_TRUE;
++text;
}
May 29, 2006
May 29, 2006
150
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
May 28, 2006
May 28, 2006
153
for (;;) {
May 29, 2006
May 29, 2006
155
if (SDL_isdigit(*text)) {
May 29, 2006
May 29, 2006
157
} else if (radix == 16 && SDL_isupperhex(*text)) {
158
v = 10 + (*text - 'A');
May 29, 2006
May 29, 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;
}
May 28, 2006
May 28, 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)
May 28, 2006
May 28, 2006
180
static size_t
May 29, 2006
May 29, 2006
181
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
182
183
184
185
{
const char *textstart = text;
Uint64 value = 0;
May 29, 2006
May 29, 2006
186
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
May 28, 2006
May 28, 2006
189
for (;;) {
May 29, 2006
May 29, 2006
191
if (SDL_isdigit(*text)) {
May 29, 2006
May 29, 2006
193
} else if (radix == 16 && SDL_isupperhex(*text)) {
194
v = 10 + (*text - 'A');
May 29, 2006
May 29, 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;
}
May 28, 2006
May 28, 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)
May 28, 2006
May 28, 2006
213
static size_t
May 29, 2006
May 29, 2006
214
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;
May 28, 2006
May 28, 2006
221
if (*text == '-') {
222
223
224
negative = SDL_TRUE;
++text;
}
May 29, 2006
May 29, 2006
225
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
May 28, 2006
May 28, 2006
227
if (*text == '.') {
228
229
int mult = 10;
++text;
May 29, 2006
May 29, 2006
230
while (SDL_isdigit(*text)) {
May 28, 2006
May 28, 2006
232
value += (double) lvalue / mult;
233
234
235
236
mult *= 10;
++text;
}
}
May 28, 2006
May 28, 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
May 28, 2006
May 28, 2006
249
void *
May 29, 2006
May 29, 2006
250
SDL_memset(void *dst, int c, size_t len)
251
252
{
size_t left = (len % 4);
May 28, 2006
May 28, 2006
253
if (len >= 4) {
May 28, 2006
May 28, 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;
May 28, 2006
May 28, 2006
262
while (len--) {
263
264
265
*dstp++ = value;
}
}
May 28, 2006
May 28, 2006
266
267
268
269
270
if (left > 0) {
Uint8 value = (Uint8) c;
Uint8 *dstp = (Uint8 *) dst;
switch (left) {
case 3:
May 28, 2006
May 28, 2006
272
case 2:
May 28, 2006
May 28, 2006
274
case 1:
275
276
277
278
279
280
281
282
*dstp++ = value;
}
}
return dst;
}
#endif
#ifndef SDL_memcpy
May 28, 2006
May 28, 2006
283
void *
May 29, 2006
May 29, 2006
284
SDL_memcpy(void *dst, const void *src, size_t len)
May 28, 2006
May 28, 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
May 28, 2006
May 28, 2006
296
void *
May 29, 2006
May 29, 2006
297
SDL_revcpy(void *dst, const void *src, size_t len)
May 28, 2006
May 28, 2006
299
300
char *srcp = (char *) src;
char *dstp = (char *) dst;
301
302
srcp += len;
dstp += len;
May 28, 2006
May 28, 2006
303
while (len--) {
304
305
306
307
308
309
310
*dstp-- = *srcp--;
}
return dst;
}
#endif
#ifndef SDL_memcmp
May 28, 2006
May 28, 2006
311
int
May 29, 2006
May 29, 2006
312
SDL_memcmp(const void *s1, const void *s2, size_t len)
May 28, 2006
May 28, 2006
314
315
316
317
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
May 28, 2006
May 28, 2006
319
320
321
}
++s1p;
++s2p;
322
323
324
325
326
327
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
May 28, 2006
May 28, 2006
328
size_t
May 29, 2006
May 29, 2006
329
SDL_strlen(const char *string)
May 28, 2006
May 28, 2006
332
while (*string++) {
333
334
335
336
337
338
++len;
}
return len;
}
#endif
Feb 19, 2006
Feb 19, 2006
339
#ifndef HAVE_STRLCPY
May 28, 2006
May 28, 2006
340
size_t
May 29, 2006
May 29, 2006
341
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
May 29, 2006
May 29, 2006
343
size_t srclen = SDL_strlen(src);
May 28, 2006
May 28, 2006
344
if (maxlen > 0) {
May 29, 2006
May 29, 2006
345
346
size_t len = SDL_min(srclen, maxlen - 1);
SDL_memcpy(dst, src, len);
Feb 19, 2006
Feb 19, 2006
347
dst[len] = '\0';
Feb 19, 2006
Feb 19, 2006
349
return srclen;
Feb 19, 2006
Feb 19, 2006
353
#ifndef HAVE_STRLCAT
May 28, 2006
May 28, 2006
354
size_t
May 29, 2006
May 29, 2006
355
SDL_strlcat(char *dst, const char *src, size_t maxlen)
May 29, 2006
May 29, 2006
357
358
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
May 28, 2006
May 28, 2006
359
if (dstlen < maxlen) {
May 29, 2006
May 29, 2006
360
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
May 28, 2006
May 28, 2006
362
return dstlen + srclen;
Feb 7, 2006
Feb 7, 2006
366
#ifndef HAVE_STRDUP
May 28, 2006
May 28, 2006
367
char *
May 29, 2006
May 29, 2006
368
SDL_strdup(const char *string)
Feb 7, 2006
Feb 7, 2006
369
{
May 29, 2006
May 29, 2006
370
371
size_t len = SDL_strlen(string) + 1;
char *newstr = SDL_malloc(len);
May 28, 2006
May 28, 2006
372
if (newstr) {
May 29, 2006
May 29, 2006
373
SDL_strlcpy(newstr, string, len);
Feb 7, 2006
Feb 7, 2006
374
375
376
377
378
}
return newstr;
}
#endif
May 28, 2006
May 28, 2006
380
char *
May 29, 2006
May 29, 2006
381
SDL_strrev(char *string)
May 29, 2006
May 29, 2006
383
size_t len = SDL_strlen(string);
May 28, 2006
May 28, 2006
385
char *b = &string[len - 1];
May 28, 2006
May 28, 2006
387
while (len--) {
388
389
390
391
392
393
394
395
396
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifndef HAVE__STRUPR
May 28, 2006
May 28, 2006
397
char *
May 29, 2006
May 29, 2006
398
SDL_strupr(char *string)
399
400
{
char *bufp = string;
May 28, 2006
May 28, 2006
401
while (*bufp) {
May 29, 2006
May 29, 2006
402
*bufp = SDL_toupper(*bufp);
May 28, 2006
May 28, 2006
403
++bufp;
404
405
406
407
408
409
}
return string;
}
#endif
#ifndef HAVE__STRLWR
May 28, 2006
May 28, 2006
410
char *
May 29, 2006
May 29, 2006
411
SDL_strlwr(char *string)
412
413
{
char *bufp = string;
May 28, 2006
May 28, 2006
414
while (*bufp) {
May 29, 2006
May 29, 2006
415
*bufp = SDL_tolower(*bufp);
May 28, 2006
May 28, 2006
416
++bufp;
417
418
419
420
421
422
}
return string;
}
#endif
#ifndef HAVE_STRCHR
May 28, 2006
May 28, 2006
423
char *
May 29, 2006
May 29, 2006
424
SDL_strchr(const char *string, int c)
May 28, 2006
May 28, 2006
426
427
428
while (*string) {
if (*string == c) {
return (char *) string;
May 28, 2006
May 28, 2006
430
++string;
431
432
433
434
435
436
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
May 28, 2006
May 28, 2006
437
char *
May 29, 2006
May 29, 2006
438
SDL_strrchr(const char *string, int c)
May 29, 2006
May 29, 2006
440
const char *bufp = string + SDL_strlen(string) - 1;
May 28, 2006
May 28, 2006
441
442
443
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
May 28, 2006
May 28, 2006
445
--bufp;
446
447
448
449
450
451
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
May 28, 2006
May 28, 2006
452
char *
May 29, 2006
May 29, 2006
453
SDL_strstr(const char *haystack, const char *needle)
May 29, 2006
May 29, 2006
455
size_t length = SDL_strlen(needle);
May 28, 2006
May 28, 2006
456
while (*haystack) {
May 29, 2006
May 29, 2006
457
if (SDL_strncmp(haystack, needle, length) == 0) {
May 28, 2006
May 28, 2006
458
return (char *) haystack;
May 28, 2006
May 28, 2006
460
++haystack;
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
May 28, 2006
May 28, 2006
477
char *
May 29, 2006
May 29, 2006
478
SDL_ltoa(long value, char *string, int radix)
479
480
481
{
char *bufp = string;
May 28, 2006
May 28, 2006
482
if (value < 0) {
483
484
485
*bufp++ = '-';
value = -value;
}
May 28, 2006
May 28, 2006
486
487
if (value) {
while (value > 0) {
488
489
490
491
492
493
494
495
496
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
May 28, 2006
May 28, 2006
497
if (*string == '-') {
May 29, 2006
May 29, 2006
498
SDL_strrev(string + 1);
May 29, 2006
May 29, 2006
500
SDL_strrev(string);
501
502
503
504
505
506
507
}
return string;
}
#endif
#ifndef HAVE__ULTOA
May 28, 2006
May 28, 2006
508
char *
May 29, 2006
May 29, 2006
509
SDL_ultoa(unsigned long value, char *string, int radix)
510
511
512
{
char *bufp = string;
May 28, 2006
May 28, 2006
513
514
if (value) {
while (value > 0) {
515
516
517
518
519
520
521
522
523
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
May 29, 2006
May 29, 2006
524
SDL_strrev(string);
525
526
527
528
529
530
return string;
}
#endif
#ifndef HAVE_STRTOL
May 28, 2006
May 28, 2006
531
long
May 29, 2006
May 29, 2006
532
SDL_strtol(const char *string, char **endp, int base)
533
534
535
536
{
size_t len;
long value;
May 29, 2006
May 29, 2006
537
len = SDL_ScanLong(string, base ? base : 10, &value);
May 28, 2006
May 28, 2006
538
539
if (endp) {
*endp = (char *) string + len;
540
541
542
543
544
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
545
#ifndef HAVE_STRTOUL
May 28, 2006
May 28, 2006
546
unsigned long
May 29, 2006
May 29, 2006
547
SDL_strtoul(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
548
549
550
551
{
size_t len;
unsigned long value;
May 29, 2006
May 29, 2006
552
len = SDL_ScanUnsignedLong(string, base ? base : 10, &value);
May 28, 2006
May 28, 2006
553
554
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
555
556
557
558
559
}
return value;
}
#endif
560
561
562
#ifdef SDL_HAS_64BIT_TYPE
#ifndef HAVE__I64TOA
May 28, 2006
May 28, 2006
563
char *
May 29, 2006
May 29, 2006
564
SDL_lltoa(Sint64 value, char *string, int radix)
565
566
567
{
char *bufp = string;
May 28, 2006
May 28, 2006
568
if (value < 0) {
569
570
571
*bufp++ = '-';
value = -value;
}
May 28, 2006
May 28, 2006
572
573
if (value) {
while (value > 0) {
574
575
576
577
578
579
580
581
582
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
May 28, 2006
May 28, 2006
583
if (*string == '-') {
May 29, 2006
May 29, 2006
584
SDL_strrev(string + 1);
May 29, 2006
May 29, 2006
586
SDL_strrev(string);
587
588
589
590
591
592
593
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
May 28, 2006
May 28, 2006
594
char *
May 29, 2006
May 29, 2006
595
SDL_ulltoa(Uint64 value, char *string, int radix)
596
597
598
{
char *bufp = string;
May 28, 2006
May 28, 2006
599
600
if (value) {
while (value > 0) {
601
602
603
604
605
606
607
608
609
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
May 29, 2006
May 29, 2006
610
SDL_strrev(string);
611
612
613
614
615
616
return string;
}
#endif
#ifndef HAVE_STRTOLL
May 28, 2006
May 28, 2006
617
Sint64
May 29, 2006
May 29, 2006
618
SDL_strtoll(const char *string, char **endp, int base)
619
620
621
622
{
size_t len;
Sint64 value;
May 29, 2006
May 29, 2006
623
len = SDL_ScanLongLong(string, base ? base : 10, &value);
May 28, 2006
May 28, 2006
624
625
if (endp) {
*endp = (char *) string + len;
626
627
628
629
630
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
631
#ifndef HAVE_STRTOULL
May 28, 2006
May 28, 2006
632
Uint64
May 29, 2006
May 29, 2006
633
SDL_strtoull(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
634
635
636
637
{
size_t len;
Uint64 value;
May 29, 2006
May 29, 2006
638
len = SDL_ScanUnsignedLongLong(string, base ? base : 10, &value);
May 28, 2006
May 28, 2006
639
640
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
641
642
643
644
645
}
return value;
}
#endif
646
647
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
648
#ifndef HAVE_STRTOD
May 28, 2006
May 28, 2006
649
double
May 29, 2006
May 29, 2006
650
SDL_strtod(const char *string, char **endp)
Feb 7, 2006
Feb 7, 2006
651
652
653
654
{
size_t len;
double value;
May 29, 2006
May 29, 2006
655
len = SDL_ScanFloat(string, &value);
May 28, 2006
May 28, 2006
656
657
if (endp) {
*endp = (char *) string + len;
Feb 7, 2006
Feb 7, 2006
658
659
660
661
662
}
return value;
}
#endif
May 28, 2006
May 28, 2006
664
int
May 29, 2006
May 29, 2006
665
SDL_strcmp(const char *str1, const char *str2)
666
667
{
while (*str1 && *str2) {
May 28, 2006
May 28, 2006
668
if (*str1 != *str2)
669
670
671
672
break;
++str1;
++str2;
}
May 28, 2006
May 28, 2006
673
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
674
675
676
677
}
#endif
#ifndef HAVE_STRNCMP
May 28, 2006
May 28, 2006
678
int
May 29, 2006
May 29, 2006
679
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
May 28, 2006
May 28, 2006
681
682
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
683
684
685
686
687
break;
++str1;
++str2;
--maxlen;
}
May 28, 2006
May 28, 2006
688
if (!maxlen) {
May 28, 2006
May 28, 2006
691
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
Mar 13, 2006
Mar 13, 2006
695
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
May 28, 2006
May 28, 2006
696
int
May 29, 2006
May 29, 2006
697
SDL_strcasecmp(const char *str1, const char *str2)
698
699
700
{
char a = 0;
char b = 0;
May 28, 2006
May 28, 2006
701
while (*str1 && *str2) {
May 29, 2006
May 29, 2006
702
703
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
May 28, 2006
May 28, 2006
704
if (a != b)
705
706
707
708
break;
++str1;
++str2;
}
May 28, 2006
May 28, 2006
709
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
713
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
May 28, 2006
May 28, 2006
714
int
May 29, 2006
May 29, 2006
715
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
Mar 13, 2006
Mar 13, 2006
716
717
718
{
char a = 0;
char b = 0;
May 28, 2006
May 28, 2006
719
while (*str1 && *str2 && maxlen) {
May 29, 2006
May 29, 2006
720
721
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
May 28, 2006
May 28, 2006
722
if (a != b)
Mar 13, 2006
Mar 13, 2006
723
724
725
726
727
break;
++str1;
++str2;
--maxlen;
}
May 28, 2006
May 28, 2006
728
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
729
730
731
}
#endif
May 28, 2006
May 28, 2006
733
int
May 29, 2006
May 29, 2006
734
SDL_sscanf(const char *text, const char *fmt, ...)
735
736
737
738
{
va_list ap;
int retval = 0;
May 29, 2006
May 29, 2006
739
va_start(ap, fmt);
May 28, 2006
May 28, 2006
740
741
while (*fmt) {
if (*fmt == ' ') {
May 29, 2006
May 29, 2006
742
while (SDL_isspace(*text)) {
743
744
745
746
747
++text;
}
++fmt;
continue;
}
May 28, 2006
May 28, 2006
748
if (*fmt == '%') {
749
750
751
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
May 28, 2006
May 28, 2006
752
753
enum
{
754
755
756
757
758
759
760
761
DO_SHORT,
DO_INT,
DO_LONG,
DO_LONGLONG
} inttype = DO_INT;
SDL_bool suppress = SDL_FALSE;
++fmt;
May 28, 2006
May 28, 2006
762
763
if (*fmt == '%') {
if (*text == '%') {
764
765
766
767
768
769
++text;
++fmt;
continue;
}
break;
}
May 28, 2006
May 28, 2006
770
if (*fmt == '*') {
771
772
773
suppress = SDL_TRUE;
++fmt;
}
May 29, 2006
May 29, 2006
774
fmt += SDL_ScanLong(fmt, 10, &count);
May 28, 2006
May 28, 2006
776
777
if (*fmt == 'c') {
if (!count) {
May 28, 2006
May 28, 2006
780
781
if (suppress) {
while (count--) {
782
783
784
++text;
}
} else {
May 29, 2006
May 29, 2006
785
char *valuep = va_arg(ap, char *);
May 28, 2006
May 28, 2006
786
while (count--) {
787
788
789
790
791
792
793
*valuep++ = *text++;
}
++retval;
}
continue;
}
May 29, 2006
May 29, 2006
794
while (SDL_isspace(*text)) {
795
796
797
798
799
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
May 28, 2006
May 28, 2006
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
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':
May 29, 2006
May 29, 2006
815
if (SDL_strncmp(fmt, "I64", 3) == 0) {
May 28, 2006
May 28, 2006
816
817
818
819
820
821
822
823
824
fmt += 2;
inttype = DO_LONGLONG;
}
break;
case 'i':
{
int index = 0;
if (text[index] == '-') {
++index;
May 28, 2006
May 28, 2006
826
if (text[index] == '0') {
May 29, 2006
May 29, 2006
827
if (SDL_tolower(text[index + 1]) == 'x') {
May 28, 2006
May 28, 2006
828
829
830
radix = 16;
} else {
radix = 8;
May 28, 2006
May 28, 2006
833
834
835
}
/* Fall through to %d handling */
case 'd':
836
#ifdef SDL_HAS_64BIT_TYPE
May 28, 2006
May 28, 2006
837
838
if (inttype == DO_LONGLONG) {
Sint64 value;
May 29, 2006
May 29, 2006
839
text += SDL_ScanLongLong(text, radix, &value);
May 28, 2006
May 28, 2006
840
if (!suppress) {
May 29, 2006
May 29, 2006
841
Sint64 *valuep = va_arg(ap, Sint64 *);
May 28, 2006
May 28, 2006
842
843
*valuep = value;
++retval;
May 28, 2006
May 28, 2006
845
} else
846
#endif /* SDL_HAS_64BIT_TYPE */
May 28, 2006
May 28, 2006
847
848
{
long value;
May 29, 2006
May 29, 2006
849
text += SDL_ScanLong(text, radix, &value);
May 28, 2006
May 28, 2006
850
851
852
853
if (!suppress) {
switch (inttype) {
case DO_SHORT:
{
May 29, 2006
May 29, 2006
854
short *valuep = va_arg(ap, short *);
May 28, 2006
May 28, 2006
855
856
857
858
859
*valuep = (short) value;
}
break;
case DO_INT:
{
May 29, 2006
May 29, 2006
860
int *valuep = va_arg(ap, int *);
May 28, 2006
May 28, 2006
861
*valuep = (int) value;
May 28, 2006
May 28, 2006
863
864
865
break;
case DO_LONG:
{
May 29, 2006
May 29, 2006
866
long *valuep = va_arg(ap, long *);
May 28, 2006
May 28, 2006
867
868
869
870
871
872
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
May 28, 2006
May 28, 2006
874
++retval;
May 28, 2006
May 28, 2006
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
}
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':
891
#ifdef SDL_HAS_64BIT_TYPE
May 28, 2006
May 28, 2006
892
893
if (inttype == DO_LONGLONG) {
Uint64 value;
May 29, 2006
May 29, 2006
894
text += SDL_ScanUnsignedLongLong(text, radix, &value);
May 28, 2006
May 28, 2006
895
if (!suppress) {
May 29, 2006
May 29, 2006
896
Uint64 *valuep = va_arg(ap, Uint64 *);
May 28, 2006
May 28, 2006
897
898
*valuep = value;
++retval;
May 28, 2006
May 28, 2006
900
} else
901
#endif /* SDL_HAS_64BIT_TYPE */
May 28, 2006
May 28, 2006
902
903
{
unsigned long value;
May 29, 2006
May 29, 2006
904
text += SDL_ScanUnsignedLong(text, radix, &value);
May 28, 2006
May 28, 2006
905
906
907
908
if (!suppress) {
switch (inttype) {
case DO_SHORT:
{
May 29, 2006
May 29, 2006
909
short *valuep = va_arg(ap, short *);
May 28, 2006
May 28, 2006
910
911
912
913
914
*valuep = (short) value;
}
break;
case DO_INT:
{
May 29, 2006
May 29, 2006
915
int *valuep = va_arg(ap, int *);
May 28, 2006
May 28, 2006
916
*valuep = (int) value;
May 28, 2006
May 28, 2006
918
919
920
break;
case DO_LONG:
{
May 29, 2006
May 29, 2006
921
long *valuep = va_arg(ap, long *);
May 28, 2006
May 28, 2006
922
923
924
925
926
927
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
May 28, 2006
May 28, 2006
929
++retval;
May 28, 2006
May 28, 2006
931
932
933
934
935
936
}
done = SDL_TRUE;
break;
case 'p':
{
uintptr_t value;
May 29, 2006
May 29, 2006
937
text += SDL_ScanUintPtrT(text, 16, &value);
May 28, 2006
May 28, 2006
938
if (!suppress) {
May 29, 2006
May 29, 2006
939
void **valuep = va_arg(ap, void **);
May 28, 2006
May 28, 2006
940
941
*valuep = (void *) value;
++retval;
May 28, 2006
May 28, 2006
943
944
945
946
947
948
}
done = SDL_TRUE;
break;
case 'f':
{
double value;
May 29, 2006
May 29, 2006
949
text += SDL_ScanFloat(text, &value);
May 28, 2006
May 28, 2006
950
if (!suppress) {
May 29, 2006
May 29, 2006
951
float *valuep = va_arg(ap, float *);
May 28, 2006
May 28, 2006
952
953
*valuep = (float) value;
++retval;
May 28, 2006
May 28, 2006
955
956
957
958
959
}
done = SDL_TRUE;
break;
case 's':
if (suppress) {
May 29, 2006
May 29, 2006
960
while (!SDL_isspace(*text)) {
May 28, 2006
May 28, 2006
961
962
963
964
++text;
if (count) {
if (--count == 0) {
break;
May 28, 2006
May 28, 2006
967
968
}
} else {
May 29, 2006
May 29, 2006
969
970
char *valuep = va_arg(ap, char *);
while (!SDL_isspace(*text)) {
May 28, 2006
May 28, 2006
971
972
973
974
*valuep++ = *text++;
if (count) {
if (--count == 0) {
break;
May 28, 2006
May 28, 2006
978
979
980
981
982
983
984
985
*valuep = '\0';
++retval;
}
done = SDL_TRUE;
break;
default:
done = SDL_TRUE;
break;
986
987
988
989
990
}
++fmt;
}
continue;
}
May 28, 2006
May 28, 2006
991
if (*text == *fmt) {
992
993
994
995
996
997
998
++text;
++fmt;
continue;
}
/* Text didn't match format specifier */
break;
}
May 29, 2006
May 29, 2006
999
va_end(ap);