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

Latest commit

 

History

History
1413 lines (1303 loc) · 35 KB

SDL_string.c

File metadata and controls

1413 lines (1303 loc) · 35 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 8, 2011
Apr 8, 2011
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.
Apr 8, 2011
Apr 8, 2011
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:
Apr 8, 2011
Apr 8, 2011
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.
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
22
23
24
/* This file contains portable string manipulation functions for SDL */
Feb 9, 2006
Feb 9, 2006
25
#include "SDL_stdinc.h"
Feb 9, 2006
Feb 9, 2006
28
29
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
Jul 13, 2010
Jul 13, 2010
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)
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
54
if (*text == '-') {
55
56
57
negative = SDL_TRUE;
++text;
}
Jul 10, 2006
Jul 10, 2006
58
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
61
for (;;) {
Feb 15, 2007
Feb 15, 2007
63
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
65
} else if (radix == 16 && SDL_isupperhex(*text)) {
66
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
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;
}
Jul 10, 2006
Jul 10, 2006
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
Mar 1, 2006
Mar 1, 2006
87
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
94
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
97
for (;;) {
Feb 15, 2007
Feb 15, 2007
99
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
101
} else if (radix == 16 && SDL_isupperhex(*text)) {
102
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
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;
}
Jul 10, 2006
Jul 10, 2006
112
if (valuep) {
113
114
115
116
117
118
*valuep = value;
}
return (text - textstart);
}
#endif
Mar 1, 2006
Mar 1, 2006
119
#ifndef HAVE_SSCANF
Jul 10, 2006
Jul 10, 2006
120
121
static size_t
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
Mar 1, 2006
Mar 1, 2006
122
123
124
125
{
const char *textstart = text;
uintptr_t value = 0;
Jul 10, 2006
Jul 10, 2006
126
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Mar 1, 2006
Mar 1, 2006
127
128
text += 2;
}
Jul 10, 2006
Jul 10, 2006
129
for (;;) {
Mar 1, 2006
Mar 1, 2006
130
int v;
Feb 15, 2007
Feb 15, 2007
131
if (SDL_isdigit((unsigned char) *text)) {
Mar 1, 2006
Mar 1, 2006
132
v = *text - '0';
Jul 10, 2006
Jul 10, 2006
133
} else if (radix == 16 && SDL_isupperhex(*text)) {
Mar 1, 2006
Mar 1, 2006
134
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
135
} else if (radix == 16 && SDL_islowerhex(*text)) {
Mar 1, 2006
Mar 1, 2006
136
137
138
139
140
141
142
143
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
Jul 10, 2006
Jul 10, 2006
144
if (valuep) {
Mar 1, 2006
Mar 1, 2006
145
146
147
148
149
150
*valuep = value;
}
return (text - textstart);
}
#endif
151
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
159
if (*text == '-') {
160
161
162
negative = SDL_TRUE;
++text;
}
Jul 10, 2006
Jul 10, 2006
163
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
166
for (;;) {
Feb 15, 2007
Feb 15, 2007
168
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
170
} else if (radix == 16 && SDL_isupperhex(*text)) {
171
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
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;
}
Jul 10, 2006
Jul 10, 2006
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
Mar 1, 2006
Mar 1, 2006
192
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
199
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
Jul 10, 2006
Jul 10, 2006
202
for (;;) {
Feb 15, 2007
Feb 15, 2007
204
if (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
206
} else if (radix == 16 && SDL_isupperhex(*text)) {
207
v = 10 + (*text - 'A');
Jul 10, 2006
Jul 10, 2006
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;
}
Jul 10, 2006
Jul 10, 2006
217
if (valuep) {
218
219
220
221
222
223
*valuep = value;
}
return (text - textstart);
}
#endif
Feb 7, 2006
Feb 7, 2006
224
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
233
if (*text == '-') {
234
235
236
237
238
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
Jul 10, 2006
Jul 10, 2006
239
if (*text == '.') {
240
241
int mult = 10;
++text;
Feb 15, 2007
Feb 15, 2007
242
while (SDL_isdigit((unsigned char) *text)) {
Jul 10, 2006
Jul 10, 2006
244
value += (double) lvalue / mult;
245
246
247
248
mult *= 10;
++text;
}
}
Jul 10, 2006
Jul 10, 2006
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
Jul 10, 2006
Jul 10, 2006
261
262
void *
SDL_memset(void *dst, int c, size_t len)
263
264
{
size_t left = (len % 4);
Feb 16, 2011
Feb 16, 2011
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;
Feb 16, 2011
Feb 16, 2011
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;
Feb 16, 2011
Feb 16, 2011
285
286
287
288
289
290
return dst;
}
#endif
#ifndef SDL_memcpy
Jul 10, 2006
Jul 10, 2006
291
292
void *
SDL_memcpy(void *dst, const void *src, size_t len)
Feb 16, 2011
Feb 16, 2011
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;
Jul 10, 2006
Jul 10, 2006
301
while (len--) {
Feb 16, 2011
Feb 16, 2011
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++;
Feb 16, 2011
Feb 16, 2011
315
316
317
318
319
return dst;
}
#endif
Feb 16, 2011
Feb 16, 2011
320
#ifndef SDL_memmove
Jul 10, 2006
Jul 10, 2006
321
void *
Feb 16, 2011
Feb 16, 2011
322
SDL_memmove(void *dst, const void *src, size_t len)
Jul 10, 2006
Jul 10, 2006
324
325
char *srcp = (char *) src;
char *dstp = (char *) dst;
Feb 16, 2011
Feb 16, 2011
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
Jul 10, 2006
Jul 10, 2006
343
344
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
Jul 10, 2006
Jul 10, 2006
346
347
348
349
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
Jul 10, 2006
Jul 10, 2006
351
352
353
}
++s1p;
++s2p;
354
355
356
357
358
359
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
Jul 10, 2006
Jul 10, 2006
360
361
size_t
SDL_strlen(const char *string)
Jul 10, 2006
Jul 10, 2006
364
while (*string++) {
365
366
367
368
369
370
++len;
}
return len;
}
#endif
Jul 13, 2006
Jul 13, 2006
371
372
#ifndef HAVE_WCSLEN
size_t
Jul 14, 2006
Jul 14, 2006
373
SDL_wcslen(const wchar_t * string)
Jul 13, 2006
Jul 13, 2006
374
375
376
377
378
379
380
381
382
{
size_t len = 0;
while (*string++) {
++len;
}
return len;
}
#endif
Aug 3, 2010
Aug 3, 2010
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
Feb 19, 2006
Feb 19, 2006
410
#ifndef HAVE_STRLCPY
Jul 10, 2006
Jul 10, 2006
411
412
size_t
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
414
size_t srclen = SDL_strlen(src);
Jul 10, 2006
Jul 10, 2006
415
416
if (maxlen > 0) {
size_t len = SDL_min(srclen, maxlen - 1);
Feb 19, 2006
Feb 19, 2006
417
418
SDL_memcpy(dst, src, len);
dst[len] = '\0';
Feb 19, 2006
Feb 19, 2006
420
return srclen;
Jul 13, 2010
Jul 13, 2010
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);
Jan 22, 2011
Jan 22, 2011
428
size_t i = 0;
Jul 13, 2010
Jul 13, 2010
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;
}
Feb 19, 2006
Feb 19, 2006
456
#ifndef HAVE_STRLCAT
Jul 10, 2006
Jul 10, 2006
457
458
size_t
SDL_strlcat(char *dst, const char *src, size_t maxlen)
Feb 19, 2006
Feb 19, 2006
460
461
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
Jul 10, 2006
Jul 10, 2006
462
463
if (dstlen < maxlen) {
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
Jul 10, 2006
Jul 10, 2006
465
return dstlen + srclen;
Feb 7, 2006
Feb 7, 2006
469
#ifndef HAVE_STRDUP
Jul 10, 2006
Jul 10, 2006
470
471
char *
SDL_strdup(const char *string)
Feb 7, 2006
Feb 7, 2006
472
{
Jul 10, 2006
Jul 10, 2006
473
size_t len = SDL_strlen(string) + 1;
Feb 19, 2006
Feb 19, 2006
474
char *newstr = SDL_malloc(len);
Jul 10, 2006
Jul 10, 2006
475
if (newstr) {
Feb 19, 2006
Feb 19, 2006
476
SDL_strlcpy(newstr, string, len);
Feb 7, 2006
Feb 7, 2006
477
478
479
480
481
}
return newstr;
}
#endif
Jul 10, 2006
Jul 10, 2006
483
484
char *
SDL_strrev(char *string)
485
486
487
{
size_t len = SDL_strlen(string);
char *a = &string[0];
Jul 10, 2006
Jul 10, 2006
488
char *b = &string[len - 1];
Jul 10, 2006
Jul 10, 2006
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
Jul 10, 2006
Jul 10, 2006
500
501
char *
SDL_strupr(char *string)
502
503
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
504
while (*bufp) {
Feb 15, 2007
Feb 15, 2007
505
*bufp = SDL_toupper((unsigned char) *bufp);
Jul 10, 2006
Jul 10, 2006
506
++bufp;
507
508
509
510
511
512
}
return string;
}
#endif
#ifndef HAVE__STRLWR
Jul 10, 2006
Jul 10, 2006
513
514
char *
SDL_strlwr(char *string)
515
516
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
517
while (*bufp) {
Feb 15, 2007
Feb 15, 2007
518
*bufp = SDL_tolower((unsigned char) *bufp);
Jul 10, 2006
Jul 10, 2006
519
++bufp;
520
521
522
523
524
525
}
return string;
}
#endif
#ifndef HAVE_STRCHR
Jul 10, 2006
Jul 10, 2006
526
527
char *
SDL_strchr(const char *string, int c)
Jul 10, 2006
Jul 10, 2006
529
530
531
while (*string) {
if (*string == c) {
return (char *) string;
Jul 10, 2006
Jul 10, 2006
533
++string;
534
535
536
537
538
539
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
Jul 10, 2006
Jul 10, 2006
540
541
char *
SDL_strrchr(const char *string, int c)
Feb 7, 2006
Feb 7, 2006
543
const char *bufp = string + SDL_strlen(string) - 1;
Jul 10, 2006
Jul 10, 2006
544
545
546
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
Jul 10, 2006
Jul 10, 2006
548
--bufp;
549
550
551
552
553
554
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
Jul 10, 2006
Jul 10, 2006
555
556
char *
SDL_strstr(const char *haystack, const char *needle)
Feb 7, 2006
Feb 7, 2006
558
size_t length = SDL_strlen(needle);
Jul 10, 2006
Jul 10, 2006
559
560
561
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *) haystack;
Jul 10, 2006
Jul 10, 2006
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
Jul 10, 2006
Jul 10, 2006
580
581
char *
SDL_ltoa(long value, char *string, int radix)
582
583
584
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
585
if (value < 0) {
586
587
588
*bufp++ = '-';
value = -value;
}
Jul 10, 2006
Jul 10, 2006
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. :) */
Jul 10, 2006
Jul 10, 2006
600
601
if (*string == '-') {
SDL_strrev(string + 1);
Feb 7, 2006
Feb 7, 2006
603
SDL_strrev(string);
604
605
606
607
608
609
610
}
return string;
}
#endif
#ifndef HAVE__ULTOA
Jul 10, 2006
Jul 10, 2006
611
612
char *
SDL_ultoa(unsigned long value, char *string, int radix)
613
614
615
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
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. :) */
Feb 7, 2006
Feb 7, 2006
627
SDL_strrev(string);
628
629
630
631
632
633
return string;
}
#endif
#ifndef HAVE_STRTOL
Jul 10, 2006
Jul 10, 2006
634
635
long
SDL_strtol(const char *string, char **endp, int base)
636
637
638
639
{
size_t len;
long value;
Jul 10, 2006
Jul 10, 2006
640
641
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
642
643
644
645
646
647
648
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
649
650
if (endp) {
*endp = (char *) string + len;
651
652
653
654
655
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
656
#ifndef HAVE_STRTOUL
Jul 10, 2006
Jul 10, 2006
657
658
unsigned long
SDL_strtoul(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
659
660
661
662
{
size_t len;
unsigned long value;
Jul 10, 2006
Jul 10, 2006
663
664
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
665
666
667
668
669
670
671
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
672
673
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
674
675
676
677
678
}
return value;
}
#endif
Jul 10, 2006
Jul 10, 2006
680
681
char *
SDL_lltoa(Sint64 value, char *string, int radix)
682
683
684
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
685
if (value < 0) {
686
687
688
*bufp++ = '-';
value = -value;
}
Jul 10, 2006
Jul 10, 2006
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. :) */
Jul 10, 2006
Jul 10, 2006
700
701
if (*string == '-') {
SDL_strrev(string + 1);
Feb 7, 2006
Feb 7, 2006
703
SDL_strrev(string);
704
705
706
707
708
709
710
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
Jul 10, 2006
Jul 10, 2006
711
712
char *
SDL_ulltoa(Uint64 value, char *string, int radix)
713
714
715
{
char *bufp = string;
Jul 10, 2006
Jul 10, 2006
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. :) */
Feb 7, 2006
Feb 7, 2006
727
SDL_strrev(string);
728
729
730
731
732
733
return string;
}
#endif
#ifndef HAVE_STRTOLL
Jul 10, 2006
Jul 10, 2006
734
735
Sint64
SDL_strtoll(const char *string, char **endp, int base)
736
737
738
739
{
size_t len;
Sint64 value;
Jul 10, 2006
Jul 10, 2006
740
741
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
742
743
744
745
746
747
748
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
749
750
if (endp) {
*endp = (char *) string + len;
751
752
753
754
755
}
return value;
}
#endif
Mar 1, 2006
Mar 1, 2006
756
#ifndef HAVE_STRTOULL
Jul 10, 2006
Jul 10, 2006
757
758
Uint64
SDL_strtoull(const char *string, char **endp, int base)
Mar 1, 2006
Mar 1, 2006
759
760
761
762
{
size_t len;
Uint64 value;
Jul 10, 2006
Jul 10, 2006
763
764
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
Jun 20, 2006
Jun 20, 2006
765
766
767
768
769
770
771
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
Jul 10, 2006
Jul 10, 2006
772
773
if (endp) {
*endp = (char *) string + len;
Mar 1, 2006
Mar 1, 2006
774
775
776
777
778
}
return value;
}
#endif
Feb 7, 2006
Feb 7, 2006
779
#ifndef HAVE_STRTOD
Jul 10, 2006
Jul 10, 2006
780
781
double
SDL_strtod(const char *string, char **endp)
Feb 7, 2006
Feb 7, 2006
782
783
784
785
786
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
Jul 10, 2006
Jul 10, 2006
787
788
if (endp) {
*endp = (char *) string + len;
Feb 7, 2006
Feb 7, 2006
789
790
791
792
793
}
return value;
}
#endif
Jul 10, 2006
Jul 10, 2006
795
796
int
SDL_strcmp(const char *str1, const char *str2)
797
798
{
while (*str1 && *str2) {
Jul 10, 2006
Jul 10, 2006
799
if (*str1 != *str2)
800
801
802
803
break;
++str1;
++str2;
}
Jul 10, 2006
Jul 10, 2006
804
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
805
806
807
808
}
#endif
#ifndef HAVE_STRNCMP
Jul 10, 2006
Jul 10, 2006
809
810
int
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
Jul 10, 2006
Jul 10, 2006
812
813
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
814
815
816
817
818
break;
++str1;
++str2;
--maxlen;
}
Jul 10, 2006
Jul 10, 2006
819
if (!maxlen) {
Jul 10, 2006
Jul 10, 2006
822
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
Mar 13, 2006
Mar 13, 2006
826
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
Jul 10, 2006
Jul 10, 2006
827
828
int
SDL_strcasecmp(const char *str1, const char *str2)
829
830
831
{
char a = 0;
char b = 0;
Jul 10, 2006
Jul 10, 2006
832
while (*str1 && *str2) {
Feb 15, 2007
Feb 15, 2007
833
834
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Jul 10, 2006
Jul 10, 2006
835
if (a != b)
836
837
838
839
break;
++str1;
++str2;
}
Jul 19, 2006
Jul 19, 2006
840
841
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
Jul 10, 2006
Jul 10, 2006
842
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
846
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
Jul 10, 2006
Jul 10, 2006
847
848
int
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
Mar 13, 2006
Mar 13, 2006
849
850
851
{
char a = 0;
char b = 0;
Jul 10, 2006
Jul 10, 2006
852
while (*str1 && *str2 && maxlen) {
Feb 15, 2007
Feb 15, 2007
853
854
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Jul 10, 2006
Jul 10, 2006
855
if (a != b)
Mar 13, 2006
Mar 13, 2006
856
857
858
859
860
break;
++str1;
++str2;
--maxlen;
}
Mar 7, 2011
Mar 7, 2011
861
862
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
Jul 10, 2006
Jul 10, 2006
863
return (int) ((unsigned char) a - (unsigned char) b);
Mar 13, 2006
Mar 13, 2006
864
865
866
}
#endif
Jul 10, 2006
Jul 10, 2006
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);
Jul 10, 2006
Jul 10, 2006
875
876
while (*fmt) {
if (*fmt == ' ') {
Feb 15, 2007
Feb 15, 2007
877
while (SDL_isspace((unsigned char) *text)) {
878
879
880
881
882
++text;
}
++fmt;
continue;
}
Jul 10, 2006
Jul 10, 2006
883
if (*fmt == '%') {
884
885
886
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
897
898
if (*fmt == '%') {
if (*text == '%') {
899
900
901
902
903
904
++text;
++fmt;
continue;
}
break;
}
Jul 10, 2006
Jul 10, 2006
905
if (*fmt == '*') {
906
907
908
909
910
suppress = SDL_TRUE;
++fmt;
}
fmt += SDL_ScanLong(fmt, 10, &count);
Jul 10, 2006
Jul 10, 2006
911
912
if (*fmt == 'c') {
if (!count) {
Jul 10, 2006
Jul 10, 2006
915
916
if (suppress) {
while (count--) {
917
918
919
++text;
}
} else {
Jul 10, 2006
Jul 10, 2006
920
921
char *valuep = va_arg(ap, char *);
while (count--) {
922
923
924
925
926
927
928
*valuep++ = *text++;
}
++retval;
}
continue;
}
Feb 15, 2007
Feb 15, 2007
929
while (SDL_isspace((unsigned char) *text)) {
930
931
932
933
934
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
961
if (text[index] == '0') {
Jun 14, 2007
Jun 14, 2007
962
963
if (SDL_tolower((unsigned char) text[index + 1])
== 'x') {
Jul 10, 2006
Jul 10, 2006
964
965
966
radix = 16;
} else {
radix = 8;
Jul 10, 2006
Jul 10, 2006
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;
Mar 25, 2011
Mar 25, 2011
980
} else {
Jul 10, 2006
Jul 10, 2006
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;
Jul 10, 2006
Jul 10, 2006
996
997
998
999
1000
break;
case DO_LONG:
{
long *valuep = va_arg(ap, long *);
*valuep = value;