Skip to content

Latest commit

 

History

History
1137 lines (1052 loc) · 30.3 KB

SDL_string.c

File metadata and controls

1137 lines (1052 loc) · 30.3 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
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
*/
/* This file contains portable string manipulation functions for SDL */
Feb 9, 2006
Feb 9, 2006
26
#include "SDL_stdinc.h"
Feb 9, 2006
Feb 9, 2006
29
30
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
static size_t SDL_ScanLong(const char *text, int radix, long *valuep)
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 9, 2006
Feb 9, 2006
48
if ( SDL_isdigit(*text) ) {
Feb 9, 2006
Feb 9, 2006
50
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
51
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
52
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
if ( negative && value ) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
Feb 7, 2006
Feb 7, 2006
72
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
73
74
75
76
77
78
79
80
81
82
static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
{
const char *textstart = text;
unsigned long value = 0;
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 9, 2006
Feb 9, 2006
83
if ( SDL_isdigit(*text) ) {
Feb 9, 2006
Feb 9, 2006
85
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
86
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
87
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
*valuep = value;
}
return (text - textstart);
}
#endif
#ifdef SDL_HAS_64BIT_TYPE
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 9, 2006
Feb 9, 2006
120
if ( SDL_isdigit(*text) ) {
Feb 9, 2006
Feb 9, 2006
122
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
123
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
124
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
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
151
152
153
154
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
#ifndef HAVE_SSCANF
static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
{
const char *textstart = text;
Uint64 value = 0;
if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
text += 2;
}
for ( ; ; ) {
int v;
Feb 9, 2006
Feb 9, 2006
155
if ( SDL_isdigit(*text) ) {
Feb 9, 2006
Feb 9, 2006
157
} else if ( radix == 16 && SDL_isupperhex(*text) ) {
158
v = 10 + (*text - 'A');
Feb 9, 2006
Feb 9, 2006
159
} else if ( radix == 16 && SDL_islowerhex(*text) ) {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
v = 10 + (*text - 'a');
} else {
break;
}
value *= radix;
value += v;
++text;
}
if ( valuep ) {
*valuep = value;
}
return (text - textstart);
}
#endif
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
176
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static size_t SDL_ScanFloat(const char *text, double *valuep)
{
const char *textstart = text;
unsigned long lvalue = 0;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
if ( *text == '-' ) {
negative = SDL_TRUE;
++text;
}
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
value += lvalue;
if ( *text == '.' ) {
int mult = 10;
++text;
Feb 9, 2006
Feb 9, 2006
193
while ( SDL_isdigit(*text) ) {
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
lvalue = *text - '0';
value += (double)lvalue / mult;
mult *= 10;
++text;
}
}
if ( valuep ) {
if ( negative && value ) {
*valuep = -value;
} else {
*valuep = value;
}
}
return (text - textstart);
}
#endif
#ifndef SDL_memset
void *SDL_memset(void *dst, int c, size_t len)
{
size_t left = (len % 4);
if ( len >= 4 ) {
Uint32 value = 0;
Uint32 *dstp = (Uint32 *)dst;
int i;
for (i = 0; i < 4; ++i) {
value <<= 8;
value |= c;
}
len /= 4;
while ( len-- ) {
*dstp++ = value;
}
}
if ( left > 0 ) {
Uint8 value = (Uint8)c;
Uint8 *dstp = (Uint8 *)dst;
switch(left) {
case 3:
*dstp++ = value;
case 2:
*dstp++ = value;
case 1:
*dstp++ = value;
}
}
return dst;
}
#endif
#ifndef SDL_memcpy
void *SDL_memcpy(void *dst, const void *src, size_t len)
{
char *srcp = (char *)src;
char *dstp = (char *)dst;
while ( len-- ) {
*dstp++ = *srcp++;
}
return dst;
}
#endif
#ifndef SDL_revcpy
void *SDL_revcpy(void *dst, const void *src, size_t len)
{
char *srcp = (char *)src;
char *dstp = (char *)dst;
srcp += len;
dstp += len;
while ( len-- ) {
*dstp-- = *srcp--;
}
return dst;
}
#endif
#ifndef SDL_memcmp
int SDL_memcmp(const void *s1, const void *s2, size_t len)
{
char *s1p = (char *)s1;
char *s2p = (char *)s2;
while ( len-- ) {
if ( *s1p != *s2p ) {
return (*s1p - *s2p);
}
++s1p;
++s2p;
}
return 0;
}
#endif
#ifndef HAVE_STRLEN
size_t SDL_strlen(const char *string)
{
size_t len = 0;
while ( *string++ ) {
++len;
}
return len;
}
#endif
#ifndef HAVE_STRCPY
char *SDL_strcpy(char *dst, const char *src)
{
char *dstp = dst;
while ( *src ) {
*dstp++ = *src++;
}
*dstp = '\0';
return dst;
}
#endif
#ifndef HAVE_STRNCPY
char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
{
char *dstp = dst;
while ( maxlen-- && *src ) {
*dstp++ = *src++;
}
*dstp = '\0';
return dst;
}
#endif
Feb 7, 2006
Feb 7, 2006
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef HAVE_STRDUP
char *SDL_strdup(const char *string)
{
size_t len = SDL_strlen(string);
char *newstr = SDL_malloc(len+1);
if ( newstr ) {
SDL_strcpy(newstr, string);
}
return newstr;
}
#endif
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#ifndef HAVE__STRREV
char *SDL_strrev(char *string)
{
size_t len = SDL_strlen(string);
char *a = &string[0];
char *b = &string[len-1];
len /= 2;
while ( len-- ) {
char c = *a;
*a++ = *b;
*b-- = c;
}
return string;
}
#endif
#ifndef HAVE__STRUPR
char *SDL_strupr(char *string)
{
char *bufp = string;
while ( *bufp ) {
Feb 10, 2006
Feb 10, 2006
356
*bufp = SDL_toupper(*bufp);
Feb 7, 2006
Feb 7, 2006
357
++bufp;
358
359
360
361
362
363
364
365
366
367
}
return string;
}
#endif
#ifndef HAVE__STRLWR
char *SDL_strlwr(char *string)
{
char *bufp = string;
while ( *bufp ) {
Feb 10, 2006
Feb 10, 2006
368
*bufp = SDL_tolower(*bufp);
Feb 7, 2006
Feb 7, 2006
369
++bufp;
370
371
372
373
374
375
376
377
378
379
380
381
}
return string;
}
#endif
#ifndef HAVE_STRCHR
char *SDL_strchr(const char *string, int c)
{
while ( *string ) {
if ( *string == c ) {
return (char *)string;
}
Feb 7, 2006
Feb 7, 2006
382
++string;
383
384
385
386
387
388
389
390
}
return NULL;
}
#endif
#ifndef HAVE_STRRCHR
char *SDL_strrchr(const char *string, int c)
{
Feb 7, 2006
Feb 7, 2006
391
const char *bufp = string + SDL_strlen(string) - 1;
392
393
394
395
while ( bufp >= string ) {
if ( *bufp == c ) {
return (char *)bufp;
}
Feb 7, 2006
Feb 7, 2006
396
--bufp;
397
398
399
400
401
402
403
404
}
return NULL;
}
#endif
#ifndef HAVE_STRSTR
char *SDL_strstr(const char *haystack, const char *needle)
{
Feb 7, 2006
Feb 7, 2006
405
size_t length = SDL_strlen(needle);
Feb 7, 2006
Feb 7, 2006
407
if ( SDL_strncmp(haystack, needle, length) == 0 ) {
408
409
return (char *)haystack;
}
Feb 7, 2006
Feb 7, 2006
410
++haystack;
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
}
return NULL;
}
#endif
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
!defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
static const char ntoa_table[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
#endif /* ntoa() conversion table */
#ifndef HAVE__LTOA
char *SDL_ltoa(long value, char *string, int radix)
{
char *bufp = string;
if ( value < 0 ) {
*bufp++ = '-';
value = -value;
}
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if ( *string == '-' ) {
Feb 7, 2006
Feb 7, 2006
447
SDL_strrev(string+1);
Feb 7, 2006
Feb 7, 2006
449
SDL_strrev(string);
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
}
return string;
}
#endif
#ifndef HAVE__ULTOA
char *SDL_ultoa(unsigned long value, char *string, int radix)
{
char *bufp = string;
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Feb 7, 2006
Feb 7, 2006
472
SDL_strrev(string);
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
return string;
}
#endif
#ifndef HAVE_STRTOL
long SDL_strtol(const char *string, char **endp, int base)
{
size_t len;
long value;
len = SDL_ScanLong(string, base ? base : 10, &value);
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
#ifdef SDL_HAS_64BIT_TYPE
#ifndef HAVE__I64TOA
char *SDL_lltoa(Sint64 value, char *string, int radix)
{
char *bufp = string;
if ( value < 0 ) {
*bufp++ = '-';
value = -value;
}
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
if ( *string == '-' ) {
Feb 7, 2006
Feb 7, 2006
515
SDL_strrev(string+1);
Feb 7, 2006
Feb 7, 2006
517
SDL_strrev(string);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
}
return string;
}
#endif
#ifndef HAVE__UI64TOA
char *SDL_ulltoa(Uint64 value, char *string, int radix)
{
char *bufp = string;
if ( value ) {
while ( value > 0 ) {
*bufp++ = ntoa_table[value % radix];
value /= radix;
}
} else {
*bufp++ = '0';
}
*bufp = '\0';
/* The numbers went into the string backwards. :) */
Feb 7, 2006
Feb 7, 2006
540
SDL_strrev(string);
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
return string;
}
#endif
#ifndef HAVE_STRTOLL
Sint64 SDL_strtoll(const char *string, char **endp, int base)
{
size_t len;
Sint64 value;
len = SDL_ScanLongLong(string, base ? base : 10, &value);
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
#endif /* SDL_HAS_64BIT_TYPE */
Feb 7, 2006
Feb 7, 2006
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#ifndef HAVE_STRTOD
double SDL_strtod(const char *string, char **endp)
{
size_t len;
double value;
len = SDL_ScanFloat(string, &value);
if ( endp ) {
*endp = (char *)string + len;
}
return value;
}
#endif
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#ifndef HAVE_STRCMP
int SDL_strcmp(const char *str1, const char *str2)
{
while (*str1 && *str2) {
if ( *str1 != *str2 )
break;
++str1;
++str2;
}
return (int)((unsigned char)*str1 - (unsigned char)*str2);
}
#endif
#ifndef HAVE_STRNCMP
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
{
while ( *str1 && *str2 && maxlen ) {
if ( *str1 != *str2 )
break;
++str1;
++str2;
--maxlen;
}
if ( ! maxlen ) {
return 0;
}
return (int)((unsigned char)*str1 - (unsigned char)*str2);
}
#endif
#ifndef HAVE_STRCASECMP
int SDL_strcasecmp(const char *str1, const char *str2)
{
char a = 0;
char b = 0;
while (*str1 && *str2) {
Feb 10, 2006
Feb 10, 2006
612
613
a = SDL_tolower(*str1);
b = SDL_tolower(*str2);
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
if ( a != b )
break;
++str1;
++str2;
}
return (int)((unsigned char)a - (unsigned char)b);
}
#endif
#ifndef HAVE_SSCANF
int SDL_sscanf(const char *text, const char *fmt, ...)
{
va_list ap;
int retval = 0;
va_start(ap, fmt);
while ( *fmt ) {
if ( *fmt == ' ' ) {
Feb 9, 2006
Feb 9, 2006
632
while ( SDL_isspace(*text) ) {
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
++text;
}
++fmt;
continue;
}
if ( *fmt == '%' ) {
SDL_bool done = SDL_FALSE;
long count = 0;
int radix = 10;
enum {
DO_SHORT,
DO_INT,
DO_LONG,
DO_LONGLONG
} inttype = DO_INT;
SDL_bool suppress = SDL_FALSE;
++fmt;
if ( *fmt == '%' ) {
if ( *text == '%' ) {
++text;
++fmt;
continue;
}
break;
}
if ( *fmt == '*' ) {
suppress = SDL_TRUE;
++fmt;
}
fmt += SDL_ScanLong(fmt, 10, &count);
if ( *fmt == 'c' ) {
if ( ! count ) {
count = 1;
}
if ( suppress ) {
while ( count-- ) {
++text;
}
} else {
char *valuep = va_arg(ap, char*);
while ( count-- ) {
*valuep++ = *text++;
}
++retval;
}
continue;
}
Feb 9, 2006
Feb 9, 2006
683
while ( SDL_isspace(*text) ) {
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
++text;
}
/* FIXME: implement more of the format specifiers */
while (!done) {
switch(*fmt) {
case '*':
suppress = SDL_TRUE;
break;
case 'h':
if ( inttype > DO_SHORT ) {
++inttype;
}
break;
case 'l':
if ( inttype < DO_LONGLONG ) {
++inttype;
}
break;
case 'I':
if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
fmt += 2;
inttype = DO_LONGLONG;
}
break;
case 'i':
{
int index = 0;
if ( text[index] == '-' ) {
++index;
}
if ( text[index] == '0' ) {
Feb 10, 2006
Feb 10, 2006
716
if ( SDL_tolower(text[index+1]) == 'x' ) {
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
radix = 16;
} else {
radix = 8;
}
}
}
/* Fall through to %d handling */
case 'd':
#ifdef SDL_HAS_64BIT_TYPE
if ( inttype == DO_LONGLONG ) {
Sint64 value;
text += SDL_ScanLongLong(text, radix, &value);
if ( ! suppress ) {
Sint64 *valuep = va_arg(ap, Sint64*);
*valuep = value;
++retval;
}
}
else
#endif /* SDL_HAS_64BIT_TYPE */
{
long value;
text += SDL_ScanLong(text, radix, &value);
if ( ! suppress ) {
switch (inttype) {
case DO_SHORT:
{ short* valuep = va_arg(ap, short*);
*valuep = (short)value;
}
break;
case DO_INT:
{ int* valuep = va_arg(ap, int*);
*valuep = (int)value;
}
break;
case DO_LONG:
{ long* valuep = va_arg(ap, long*);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
}
++retval;
}
}
done = SDL_TRUE;
break;
case 'o':
if ( radix == 10 ) {
radix = 8;
}
/* Fall through to unsigned handling */
case 'x':
case 'X':
if ( radix == 10 ) {
radix = 16;
}
/* Fall through to unsigned handling */
case 'u':
#ifdef SDL_HAS_64BIT_TYPE
if ( inttype == DO_LONGLONG ) {
Uint64 value;
text += SDL_ScanUnsignedLongLong(text, radix, &value);
if ( ! suppress ) {
Uint64 *valuep = va_arg(ap, Uint64*);
*valuep = value;
++retval;
}
}
else
#endif /* SDL_HAS_64BIT_TYPE */
{
unsigned long value;
text += SDL_ScanUnsignedLong(text, radix, &value);
if ( ! suppress ) {
switch (inttype) {
case DO_SHORT:
{ short* valuep = va_arg(ap, short*);
*valuep = (short)value;
}
break;
case DO_INT:
{ int* valuep = va_arg(ap, int*);
*valuep = (int)value;
}
break;
case DO_LONG:
{ long* valuep = va_arg(ap, long*);
*valuep = value;
}
break;
case DO_LONGLONG:
/* Handled above */
break;
}
++retval;
}
}
done = SDL_TRUE;
break;
case 'p':
{
unsigned long value;
text += SDL_ScanUnsignedLong(text, 16, &value);
if ( ! suppress ) {
void** valuep = va_arg(ap, void**);
*valuep = (void*)value;
++retval;
}
}
done = SDL_TRUE;
break;
case 'f':
{
double value;
text += SDL_ScanFloat(text, &value);
if ( ! suppress ) {
float* valuep = va_arg(ap, float*);
*valuep = (float)value;
++retval;
}
}
done = SDL_TRUE;
break;
case 's':
if ( suppress ) {
Feb 9, 2006
Feb 9, 2006
845
while ( !SDL_isspace(*text) ) {
846
847
848
849
850
851
852
853
854
++text;
if ( count ) {
if ( --count == 0 ) {
break;
}
}
}
} else {
char *valuep = va_arg(ap, char*);
Feb 9, 2006
Feb 9, 2006
855
while ( !SDL_isspace(*text) ) {
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
*valuep++ = *text++;
if ( count ) {
if ( --count == 0 ) {
break;
}
}
}
*valuep = '\0';
++retval;
}
done = SDL_TRUE;
break;
default:
done = SDL_TRUE;
break;
}
++fmt;
}
continue;
}
if ( *text == *fmt ) {
++text;
++fmt;
continue;
}
/* Text didn't match format specifier */
break;
}
va_end(ap);
return retval;
}
#endif
#ifndef HAVE_SNPRINTF
int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
{
va_list ap;
int retval;
va_start(ap, fmt);
retval = SDL_vsnprintf(text, maxlen, fmt, ap);
va_end(ap);
return retval;
}
#endif
#ifndef HAVE_VSNPRINTF
static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
{
char num[130];
size_t size;
Feb 7, 2006
Feb 7, 2006
910
SDL_ltoa(value, num, radix);
911
912
913
914
size = SDL_strlen(num);
if ( size > maxlen ) {
size = maxlen;
}
Feb 7, 2006
Feb 7, 2006
915
SDL_strncpy(text, num, size);
916
917
918
919
920
921
922
923
return size;
}
static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen)
{
char num[130];
size_t size;
Feb 7, 2006
Feb 7, 2006
924
SDL_ultoa(value, num, radix);
925
926
927
928
size = SDL_strlen(num);
if ( size > maxlen ) {
size = maxlen;
}
Feb 7, 2006
Feb 7, 2006
929
SDL_strncpy(text, num, size);
930
931
932
933
934
935
936
937
938
return size;
}
#ifdef SDL_HAS_64BIT_TYPE
static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
{
char num[130];
size_t size;
Feb 7, 2006
Feb 7, 2006
939
SDL_lltoa(value, num, radix);
940
941
942
943
size = SDL_strlen(num);
if ( size > maxlen ) {
size = maxlen;
}
Feb 7, 2006
Feb 7, 2006
944
SDL_strncpy(text, num, size);
945
946
947
948
949
950
951
952
return size;
}
static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
{
char num[130];
size_t size;
Feb 7, 2006
Feb 7, 2006
953
SDL_ulltoa(value, num, radix);
954
955
956
957
size = SDL_strlen(num);
if ( size > maxlen ) {
size = maxlen;
}
Feb 7, 2006
Feb 7, 2006
958
SDL_strncpy(text, num, size);
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
return size;
}
#endif /* SDL_HAS_64BIT_TYPE */
static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen)
{
char *textstart = text;
if ( arg ) {
/* This isn't especially accurate, but hey, it's easy. :) */
const double precision = 0.00000001;
size_t len;
unsigned long value;
if ( arg < 0 ) {
*text++ = '-';
--maxlen;
arg = -arg;
}
value = (unsigned long)arg;
len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
text += len;
maxlen -= len;
arg -= value;
if ( arg > precision && maxlen ) {
int mult = 10;
*text++ = '.';
while ( (arg > precision) && maxlen ) {
value = (unsigned long)(arg * mult);
len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
text += len;
maxlen -= len;
arg -= (double)value / mult;
mult *= 10;
}
}
} else {
*text++ = '0';
}
return (text - textstart);
}
static size_t SDL_PrintString(char *text, const char *string, size_t maxlen)
{