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

Latest commit

 

History

History
1646 lines (1524 loc) · 41.3 KB

SDL_string.c

File metadata and controls

1646 lines (1524 loc) · 41.3 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.
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:
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.
*/
#include "SDL_config.h"
/* This file contains portable string manipulation functions for SDL */
#include "SDL_stdinc.h"
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
static 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;
}
Jul 6, 2013
Jul 6, 2013
46
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
if (SDL_isdigit((unsigned char) *text)) {
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) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Jul 6, 2013
Jul 6, 2013
87
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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;
if (SDL_isdigit((unsigned char) *text)) {
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
Jul 6, 2013
Jul 6, 2013
119
#ifndef HAVE_SSCANF
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;
if (SDL_isdigit((unsigned char) *text)) {
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
Jul 6, 2013
Jul 6, 2013
151
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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;
if (SDL_isdigit((unsigned char) *text)) {
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) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Jul 6, 2013
Jul 6, 2013
192
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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;
if (SDL_isdigit((unsigned char) *text)) {
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
Jul 6, 2013
Jul 6, 2013
224
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
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
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;
while (SDL_isdigit((unsigned char) *text)) {
lvalue = *text - '0';
value += (double) lvalue / mult;
mult *= 10;
++text;
}
}
if (valuep) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
void *
SDL_memset(void *dst, int c, size_t len)
{
Jul 6, 2013
Jul 6, 2013
263
264
265
#if defined(HAVE_MEMSET)
return memset(dst, c, len);
#else
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
size_t left = (len % 4);
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;
}
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = value1;
case 2:
*dstp1++ = value1;
case 1:
*dstp1++ = value1;
}
return dst;
Jul 6, 2013
Jul 6, 2013
289
#endif /* HAVE_MEMSET */
Mar 15, 2013
Mar 15, 2013
291
292
293
294
void *
SDL_memcpy(void *dst, const void *src, size_t len)
{
May 27, 2013
May 27, 2013
295
296
297
298
299
#ifdef __GNUC__
/* Presumably this is well tuned for speed.
On my machine this is twice as fast as the C code below.
*/
return __builtin_memcpy(dst, src, len);
Jul 6, 2013
Jul 6, 2013
300
301
302
303
304
#elif defined(HAVE_MEMCPY)
return memcpy(dst, src, len);
#elif defined(HAVE_BCOPY)
bcopy(src, dst, len);
return dst;
May 27, 2013
May 27, 2013
305
306
307
308
309
310
311
312
313
#else
/* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
using Uint32* pointers, so we need to make sure the pointers are
aligned before we loop using them.
*/
if (((intptr_t)src & 0x3) || ((intptr_t)dst & 0x3)) {
/* Do an unaligned byte copy */
Uint8 *srcp1 = (Uint8 *)src;
Uint8 *dstp1 = (Uint8 *)dst;
May 27, 2013
May 27, 2013
315
316
317
318
319
320
321
while (len--) {
*dstp1++ = *srcp1++;
}
} else {
size_t left = (len % 4);
Uint32 *srcp4, *dstp4;
Uint8 *srcp1, *dstp1;
May 27, 2013
May 27, 2013
323
324
325
326
327
328
srcp4 = (Uint32 *) src;
dstp4 = (Uint32 *) dst;
len /= 4;
while (len--) {
*dstp4++ = *srcp4++;
}
May 27, 2013
May 27, 2013
330
331
332
333
334
335
336
337
338
339
340
srcp1 = (Uint8 *) srcp4;
dstp1 = (Uint8 *) dstp4;
switch (left) {
case 3:
*dstp1++ = *srcp1++;
case 2:
*dstp1++ = *srcp1++;
case 1:
*dstp1++ = *srcp1++;
}
}
May 27, 2013
May 27, 2013
342
#endif /* __GNUC__ */
343
344
345
346
347
}
void *
SDL_memmove(void *dst, const void *src, size_t len)
{
Jul 6, 2013
Jul 6, 2013
348
349
350
#if defined(HAVE_MEMMOVE)
return memmove(dst, src, len);
#else
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
char *srcp = (char *) src;
char *dstp = (char *) dst;
if (src < dst) {
srcp += len - 1;
dstp += len - 1;
while (len--) {
*dstp-- = *srcp--;
}
} else {
while (len--) {
*dstp++ = *srcp++;
}
}
return dst;
Jul 6, 2013
Jul 6, 2013
366
#endif /* HAVE_MEMMOVE */
367
368
369
370
371
}
int
SDL_memcmp(const void *s1, const void *s2, size_t len)
{
Jul 6, 2013
Jul 6, 2013
372
373
374
#if defined(HAVE_MEMCMP)
return memcmp(s1, s2, len);
#else
375
376
377
378
379
380
381
382
383
384
char *s1p = (char *) s1;
char *s2p = (char *) s2;
while (len--) {
if (*s1p != *s2p) {
return (*s1p - *s2p);
}
++s1p;
++s2p;
}
return 0;
Jul 6, 2013
Jul 6, 2013
385
#endif /* HAVE_MEMCMP */
386
387
388
389
390
}
size_t
SDL_strlen(const char *string)
{
Jul 6, 2013
Jul 6, 2013
391
#if defined(HAVE_STRLEN)
Jul 6, 2013
Jul 6, 2013
392
return strlen(string);
Jul 6, 2013
Jul 6, 2013
393
#else
394
395
396
397
398
size_t len = 0;
while (*string++) {
++len;
}
return len;
Jul 6, 2013
Jul 6, 2013
399
#endif /* HAVE_STRLEN */
400
401
402
403
404
}
size_t
SDL_wcslen(const wchar_t * string)
{
Jul 6, 2013
Jul 6, 2013
405
#if defined(HAVE_WCSLEN)
Jul 6, 2013
Jul 6, 2013
406
return wcslen(string);
Jul 6, 2013
Jul 6, 2013
407
#else
408
409
410
411
412
size_t len = 0;
while (*string++) {
++len;
}
return len;
Jul 6, 2013
Jul 6, 2013
413
#endif /* HAVE_WCSLEN */
414
415
416
417
418
}
size_t
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
419
420
421
#if defined(HAVE_WCSLCPY)
return wcslcpy(dst, src, maxlen);
#else
422
423
424
425
426
427
428
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;
Jul 6, 2013
Jul 6, 2013
429
#endif /* HAVE_WCSLCPY */
430
431
432
433
434
}
size_t
SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
435
436
437
#if defined(HAVE_WCSLCAT)
return wcslcat(dst, src, maxlen);
#else
438
439
440
441
442
443
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;
Jul 6, 2013
Jul 6, 2013
444
#endif /* HAVE_WCSLCAT */
445
446
447
448
449
}
size_t
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
450
451
452
#if defined(HAVE_STRLCPY)
return strlcpy(dst, src, maxlen);
#else
453
454
455
456
457
458
459
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';
}
return srclen;
Jul 6, 2013
Jul 6, 2013
460
#endif /* HAVE_STRLCPY */
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
}
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);
size_t i = 0;
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;
}
size_t
SDL_strlcat(char *dst, const char *src, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
498
499
500
#if defined(HAVE_STRLCAT)
return strlcat(dst, src, maxlen);
#else
501
502
503
504
505
506
size_t dstlen = SDL_strlen(dst);
size_t srclen = SDL_strlen(src);
if (dstlen < maxlen) {
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
}
return dstlen + srclen;
Jul 6, 2013
Jul 6, 2013
507
#endif /* HAVE_STRLCAT */
508
509
510
511
512
}
char *
SDL_strdup(const char *string)
{
Jul 6, 2013
Jul 6, 2013
513
#if defined(HAVE_STRDUP)
Jul 6, 2013
Jul 6, 2013
514
return strdup(string);
Jul 6, 2013
Jul 6, 2013
515
#else
516
517
518
519
520
521
size_t len = SDL_strlen(string) + 1;
char *newstr = SDL_malloc(len);
if (newstr) {
SDL_strlcpy(newstr, string, len);
}
return newstr;
Jul 6, 2013
Jul 6, 2013
522
#endif /* HAVE_STRDUP */
523
524
525
526
527
}
char *
SDL_strrev(char *string)
{
Jul 6, 2013
Jul 6, 2013
528
#if defined(HAVE__STRREV)
Jul 6, 2013
Jul 6, 2013
529
return _strrev(string);
Jul 6, 2013
Jul 6, 2013
530
#else
531
532
533
534
535
536
537
538
539
540
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;
Jul 6, 2013
Jul 6, 2013
541
#endif /* HAVE__STRREV */
542
543
544
545
546
}
char *
SDL_strupr(char *string)
{
Jul 6, 2013
Jul 6, 2013
547
#if defined(HAVE__STRUPR)
Jul 6, 2013
Jul 6, 2013
548
return _strupr(string);
Jul 6, 2013
Jul 6, 2013
549
#else
550
551
552
553
554
555
char *bufp = string;
while (*bufp) {
*bufp = SDL_toupper((unsigned char) *bufp);
++bufp;
}
return string;
Jul 6, 2013
Jul 6, 2013
556
#endif /* HAVE__STRUPR */
557
558
559
560
561
}
char *
SDL_strlwr(char *string)
{
Jul 6, 2013
Jul 6, 2013
562
#if defined(HAVE__STRLWR)
Jul 6, 2013
Jul 6, 2013
563
return _strlwr(string);
Jul 6, 2013
Jul 6, 2013
564
#else
565
566
567
568
569
570
char *bufp = string;
while (*bufp) {
*bufp = SDL_tolower((unsigned char) *bufp);
++bufp;
}
return string;
Jul 6, 2013
Jul 6, 2013
571
#endif /* HAVE__STRLWR */
572
573
574
575
576
}
char *
SDL_strchr(const char *string, int c)
{
Jul 6, 2013
Jul 6, 2013
577
#ifdef HAVE_STRCHR
Jul 6, 2013
Jul 6, 2013
578
return SDL_const_cast(char*,strchr(string, c));
Jul 6, 2013
Jul 6, 2013
579
#elif defined(HAVE_INDEX)
Jul 6, 2013
Jul 6, 2013
580
return SDL_const_cast(char*,index(string, c));
Jul 6, 2013
Jul 6, 2013
581
#else
582
583
584
585
586
587
588
while (*string) {
if (*string == c) {
return (char *) string;
}
++string;
}
return NULL;
Jul 6, 2013
Jul 6, 2013
589
#endif /* HAVE_STRCHR */
590
591
592
593
594
}
char *
SDL_strrchr(const char *string, int c)
{
Jul 6, 2013
Jul 6, 2013
595
#ifdef HAVE_STRRCHR
Jul 6, 2013
Jul 6, 2013
596
return SDL_const_cast(char*,strrchr(string, c));
Jul 6, 2013
Jul 6, 2013
597
#elif defined(HAVE_RINDEX)
Jul 6, 2013
Jul 6, 2013
598
return SDL_const_cast(char*,rindex(string, c));
Jul 6, 2013
Jul 6, 2013
599
#else
600
601
602
603
604
605
606
607
const char *bufp = string + SDL_strlen(string) - 1;
while (bufp >= string) {
if (*bufp == c) {
return (char *) bufp;
}
--bufp;
}
return NULL;
Jul 6, 2013
Jul 6, 2013
608
#endif /* HAVE_STRRCHR */
609
610
611
612
613
}
char *
SDL_strstr(const char *haystack, const char *needle)
{
Jul 6, 2013
Jul 6, 2013
614
615
616
#if defined(HAVE_STRSTR)
return SDL_const_cast(char*,strstr(haystack, needle));
#else
617
618
619
620
621
622
623
624
size_t length = SDL_strlen(needle);
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *) haystack;
}
++haystack;
}
return NULL;
Jul 6, 2013
Jul 6, 2013
625
#endif /* HAVE_STRSTR */
Jul 6, 2013
Jul 6, 2013
628
629
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
!defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
630
631
632
633
634
635
636
637
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 */
Jul 6, 2013
Jul 6, 2013
638
char *
Jul 6, 2013
Jul 6, 2013
639
SDL_itoa(int value, char *string, int radix)
Jul 6, 2013
Jul 6, 2013
640
641
{
#ifdef HAVE_ITOA
Jul 6, 2013
Jul 6, 2013
642
return itoa(value, string, radix);
Mar 15, 2013
Mar 15, 2013
643
#else
Jul 6, 2013
Jul 6, 2013
644
return SDL_ltoa((long)value, string, radix);
Jul 6, 2013
Jul 6, 2013
645
646
647
648
#endif /* HAVE_ITOA */
}
char *
Jul 6, 2013
Jul 6, 2013
649
SDL_uitoa(unsigned int value, char *string, int radix)
Jul 6, 2013
Jul 6, 2013
650
651
{
#ifdef HAVE__UITOA
Jul 6, 2013
Jul 6, 2013
652
return _uitoa(value, string, radix);
Jul 6, 2013
Jul 6, 2013
653
#else
Jul 6, 2013
Jul 6, 2013
654
return SDL_ultoa((unsigned long)value, string, radix);
Jul 6, 2013
Jul 6, 2013
655
656
657
#endif /* HAVE__UITOA */
}
658
659
660
char *
SDL_ltoa(long value, char *string, int radix)
{
Jul 6, 2013
Jul 6, 2013
661
#if defined(HAVE__LTOA)
Jul 6, 2013
Jul 6, 2013
662
return _ltoa(value, string, radix);
Jul 6, 2013
Jul 6, 2013
663
#else
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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 == '-') {
SDL_strrev(string + 1);
} else {
SDL_strrev(string);
}
return string;
Jul 6, 2013
Jul 6, 2013
688
#endif /* HAVE__LTOA */
689
690
691
692
693
}
char *
SDL_ultoa(unsigned long value, char *string, int radix)
{
Jul 6, 2013
Jul 6, 2013
694
#if defined(HAVE__ULTOA)
Jul 6, 2013
Jul 6, 2013
695
return _ultoa(value, string, radix);
Jul 6, 2013
Jul 6, 2013
696
#else
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
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. :) */
SDL_strrev(string);
return string;
Jul 6, 2013
Jul 6, 2013
713
#endif /* HAVE__ULTOA */
714
715
716
717
718
}
char *
SDL_lltoa(Sint64 value, char *string, int radix)
{
Jul 6, 2013
Jul 6, 2013
719
#if defined(HAVE__I64TOA)
Jul 6, 2013
Jul 6, 2013
720
return _i64toa(value, string, radix);
Jul 6, 2013
Jul 6, 2013
721
#else
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
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 == '-') {
SDL_strrev(string + 1);
} else {
SDL_strrev(string);
}
return string;
Jul 6, 2013
Jul 6, 2013
746
#endif /* HAVE__I64TOA */
747
748
749
750
751
}
char *
SDL_ulltoa(Uint64 value, char *string, int radix)
{
Jul 6, 2013
Jul 6, 2013
752
#if defined(HAVE__UI64TOA)
Jul 6, 2013
Jul 6, 2013
753
return _ui64toa(value, string, radix);
Jul 6, 2013
Jul 6, 2013
754
#else
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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. :) */
SDL_strrev(string);
return string;
Jul 6, 2013
Jul 6, 2013
771
772
773
#endif /* HAVE__UI64TOA */
}
Jul 6, 2013
Jul 6, 2013
774
int SDL_atoi(const char *string)
Jul 6, 2013
Jul 6, 2013
775
776
{
#ifdef HAVE_ATOI
Jul 6, 2013
Jul 6, 2013
777
return atoi(string);
Jul 6, 2013
Jul 6, 2013
778
#else
Jul 6, 2013
Jul 6, 2013
779
return SDL_strtol(string, NULL, 0);
Jul 6, 2013
Jul 6, 2013
780
781
782
#endif /* HAVE_ATOI */
}
Jul 6, 2013
Jul 6, 2013
783
double SDL_atof(const char *string)
Jul 6, 2013
Jul 6, 2013
784
785
{
#ifdef HAVE_ATOF
Jul 6, 2013
Jul 6, 2013
786
return (double) atof(string);
Jul 6, 2013
Jul 6, 2013
787
#else
Jul 6, 2013
Jul 6, 2013
788
return SDL_strtod(string, NULL);
Jul 6, 2013
Jul 6, 2013
789
790
791
792
793
794
795
#endif /* HAVE_ATOF */
}
long
SDL_strtol(const char *string, char **endp, int base)
{
#if defined(HAVE_STRTOL)
Jul 6, 2013
Jul 6, 2013
796
return strtol(string, endp, base);
Jul 6, 2013
Jul 6, 2013
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#else
size_t len;
long value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
#endif /* HAVE_STRTOL */
Jul 6, 2013
Jul 6, 2013
817
818
819
820
unsigned long
SDL_strtoul(const char *string, char **endp, int base)
{
#if defined(HAVE_STRTOUL)
Jul 6, 2013
Jul 6, 2013
821
return strtoul(string, endp, base);
Mar 15, 2013
Mar 15, 2013
822
#else
Jul 6, 2013
Jul 6, 2013
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
size_t len;
unsigned long value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
#endif /* HAVE_STRTOUL */
}
842
843
844
Sint64
SDL_strtoll(const char *string, char **endp, int base)
{
Jul 6, 2013
Jul 6, 2013
845
#if defined(HAVE_STRTOLL)
Jul 6, 2013
Jul 6, 2013
846
return strtoll(string, endp, base);
Jul 6, 2013
Jul 6, 2013
847
#else
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
size_t len;
Sint64 value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
Jul 6, 2013
Jul 6, 2013
864
#endif /* HAVE_STRTOLL */
865
866
867
868
869
}
Uint64
SDL_strtoull(const char *string, char **endp, int base)
{
Jul 6, 2013
Jul 6, 2013
870
#if defined(HAVE_STRTOULL)
Jul 6, 2013
Jul 6, 2013
871
return strtoull(string, endp, base);
Jul 6, 2013
Jul 6, 2013
872
#else
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
size_t len;
Uint64 value;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, base, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
Jul 6, 2013
Jul 6, 2013
889
#endif /* HAVE_STRTOULL */
890
891
892
893
894
}
double
SDL_strtod(const char *string, char **endp)
{
Jul 6, 2013
Jul 6, 2013
895
#if defined(HAVE_STRTOD)
Jul 6, 2013
Jul 6, 2013
896
return strtod(string, endp);
Jul 6, 2013
Jul 6, 2013
897
#else
898
899
900
901
902
903
904
905
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
if (endp) {
*endp = (char *) string + len;
}
return value;
Jul 6, 2013
Jul 6, 2013
906
#endif /* HAVE_STRTOD */
907
908
909
910
911
}
int
SDL_strcmp(const char *str1, const char *str2)
{
Jul 6, 2013
Jul 6, 2013
912
913
914
#if defined(HAVE_STRCMP)
return strcmp(str1, str2);
#else
915
916
917
918
919
920
921
while (*str1 && *str2) {
if (*str1 != *str2)
break;
++str1;
++str2;
}
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
Jul 6, 2013
Jul 6, 2013
922
#endif /* HAVE_STRCMP */
923
924
925
926
927
}
int
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
928
929
930
#if defined(HAVE_STRNCMP)
return strncmp(str1, str2, maxlen);
#else
931
932
933
934
935
936
937
938
939
940
941
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
break;
++str1;
++str2;
--maxlen;
}
if (!maxlen) {
return 0;
}
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
Jul 6, 2013
Jul 6, 2013
942
#endif /* HAVE_STRNCMP */
943
944
945
946
947
}
int
SDL_strcasecmp(const char *str1, const char *str2)
{
Jul 6, 2013
Jul 6, 2013
948
949
950
951
952
#ifdef HAVE_STRCASECMP
return strcasecmp(str1, str2);
#elif defined(HAVE__STRICMP)
return _stricmp(str1, str2);
#else
953
954
955
char a = 0;
char b = 0;
while (*str1 && *str2) {
Jul 6, 2013
Jul 6, 2013
956
957
a = SDL_toupper((unsigned char) *str1);
b = SDL_toupper((unsigned char) *str2);
958
959
960
961
962
if (a != b)
break;
++str1;
++str2;
}
Jul 6, 2013
Jul 6, 2013
963
964
a = SDL_toupper(*str1);
b = SDL_toupper(*str2);
965
return (int) ((unsigned char) a - (unsigned char) b);
Jul 6, 2013
Jul 6, 2013
966
#endif /* HAVE_STRCASECMP */
967
968
969
970
971
}
int
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
{
Jul 6, 2013
Jul 6, 2013
972
#ifdef HAVE_STRNCASECMP
Jul 6, 2013
Jul 6, 2013
973
return strncasecmp(str1, str2, maxlen);
Jul 6, 2013
Jul 6, 2013
974
#elif defined(HAVE__STRNICMP)
Jul 6, 2013
Jul 6, 2013
975
return _strnicmp(str1, str2, maxlen);
Jul 6, 2013
Jul 6, 2013
976
#else
977
978
979
980
981
982
983
984
985
986
987
char a = 0;
char b = 0;
while (*str1 && *str2 && maxlen) {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
if (a != b)
break;
++str1;
++str2;
--maxlen;
}
Dec 23, 2012
Dec 23, 2012
988
989
990
991
992
993
994
if (maxlen == 0) {
return 0;
} else {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
return (int) ((unsigned char) a - (unsigned char) b);
}
Jul 6, 2013
Jul 6, 2013
995
#endif /* HAVE_STRNCASECMP */
Jul 6, 2013
Jul 6, 2013
998
#ifdef HAVE_SSCANF
Mar 15, 2013
Mar 15, 2013
999
1000
int
SDL_sscanf(const char *text, const char *fmt, ...)