Mercurial > sdl-ios-xcode
comparison src/stdlib/SDL_string.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children | 504cb581ae1c |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
29 #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) | 29 #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) |
30 #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) | 30 #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) |
31 | 31 |
32 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) | 32 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) |
33 static size_t | 33 static size_t |
34 SDL_ScanLong (const char *text, int radix, long *valuep) | 34 SDL_ScanLong(const char *text, int radix, long *valuep) |
35 { | 35 { |
36 const char *textstart = text; | 36 const char *textstart = text; |
37 long value = 0; | 37 long value = 0; |
38 SDL_bool negative = SDL_FALSE; | 38 SDL_bool negative = SDL_FALSE; |
39 | 39 |
40 if (*text == '-') { | 40 if (*text == '-') { |
41 negative = SDL_TRUE; | 41 negative = SDL_TRUE; |
42 ++text; | 42 ++text; |
43 } | 43 } |
44 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) { | 44 if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { |
45 text += 2; | 45 text += 2; |
46 } | 46 } |
47 for (;;) { | 47 for (;;) { |
48 int v; | 48 int v; |
49 if (SDL_isdigit (*text)) { | 49 if (SDL_isdigit(*text)) { |
50 v = *text - '0'; | 50 v = *text - '0'; |
51 } else if (radix == 16 && SDL_isupperhex (*text)) { | 51 } else if (radix == 16 && SDL_isupperhex(*text)) { |
52 v = 10 + (*text - 'A'); | 52 v = 10 + (*text - 'A'); |
53 } else if (radix == 16 && SDL_islowerhex (*text)) { | 53 } else if (radix == 16 && SDL_islowerhex(*text)) { |
54 v = 10 + (*text - 'a'); | 54 v = 10 + (*text - 'a'); |
55 } else { | 55 } else { |
56 break; | 56 break; |
57 } | 57 } |
58 value *= radix; | 58 value *= radix; |
70 } | 70 } |
71 #endif | 71 #endif |
72 | 72 |
73 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) | 73 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) |
74 static size_t | 74 static size_t |
75 SDL_ScanUnsignedLong (const char *text, int radix, unsigned long *valuep) | 75 SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep) |
76 { | 76 { |
77 const char *textstart = text; | 77 const char *textstart = text; |
78 unsigned long value = 0; | 78 unsigned long value = 0; |
79 | 79 |
80 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) { | 80 if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { |
81 text += 2; | 81 text += 2; |
82 } | 82 } |
83 for (;;) { | 83 for (;;) { |
84 int v; | 84 int v; |
85 if (SDL_isdigit (*text)) { | 85 if (SDL_isdigit(*text)) { |
86 v = *text - '0'; | 86 v = *text - '0'; |
87 } else if (radix == 16 && SDL_isupperhex (*text)) { | 87 } else if (radix == 16 && SDL_isupperhex(*text)) { |
88 v = 10 + (*text - 'A'); | 88 v = 10 + (*text - 'A'); |
89 } else if (radix == 16 && SDL_islowerhex (*text)) { | 89 } else if (radix == 16 && SDL_islowerhex(*text)) { |
90 v = 10 + (*text - 'a'); | 90 v = 10 + (*text - 'a'); |
91 } else { | 91 } else { |
92 break; | 92 break; |
93 } | 93 } |
94 value *= radix; | 94 value *= radix; |
102 } | 102 } |
103 #endif | 103 #endif |
104 | 104 |
105 #ifndef HAVE_SSCANF | 105 #ifndef HAVE_SSCANF |
106 static size_t | 106 static size_t |
107 SDL_ScanUintPtrT (const char *text, int radix, uintptr_t * valuep) | 107 SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep) |
108 { | 108 { |
109 const char *textstart = text; | 109 const char *textstart = text; |
110 uintptr_t value = 0; | 110 uintptr_t value = 0; |
111 | 111 |
112 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) { | 112 if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { |
113 text += 2; | 113 text += 2; |
114 } | 114 } |
115 for (;;) { | 115 for (;;) { |
116 int v; | 116 int v; |
117 if (SDL_isdigit (*text)) { | 117 if (SDL_isdigit(*text)) { |
118 v = *text - '0'; | 118 v = *text - '0'; |
119 } else if (radix == 16 && SDL_isupperhex (*text)) { | 119 } else if (radix == 16 && SDL_isupperhex(*text)) { |
120 v = 10 + (*text - 'A'); | 120 v = 10 + (*text - 'A'); |
121 } else if (radix == 16 && SDL_islowerhex (*text)) { | 121 } else if (radix == 16 && SDL_islowerhex(*text)) { |
122 v = 10 + (*text - 'a'); | 122 v = 10 + (*text - 'a'); |
123 } else { | 123 } else { |
124 break; | 124 break; |
125 } | 125 } |
126 value *= radix; | 126 value *= radix; |
135 #endif | 135 #endif |
136 | 136 |
137 #ifdef SDL_HAS_64BIT_TYPE | 137 #ifdef SDL_HAS_64BIT_TYPE |
138 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL) | 138 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL) |
139 static size_t | 139 static size_t |
140 SDL_ScanLongLong (const char *text, int radix, Sint64 * valuep) | 140 SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep) |
141 { | 141 { |
142 const char *textstart = text; | 142 const char *textstart = text; |
143 Sint64 value = 0; | 143 Sint64 value = 0; |
144 SDL_bool negative = SDL_FALSE; | 144 SDL_bool negative = SDL_FALSE; |
145 | 145 |
146 if (*text == '-') { | 146 if (*text == '-') { |
147 negative = SDL_TRUE; | 147 negative = SDL_TRUE; |
148 ++text; | 148 ++text; |
149 } | 149 } |
150 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) { | 150 if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { |
151 text += 2; | 151 text += 2; |
152 } | 152 } |
153 for (;;) { | 153 for (;;) { |
154 int v; | 154 int v; |
155 if (SDL_isdigit (*text)) { | 155 if (SDL_isdigit(*text)) { |
156 v = *text - '0'; | 156 v = *text - '0'; |
157 } else if (radix == 16 && SDL_isupperhex (*text)) { | 157 } else if (radix == 16 && SDL_isupperhex(*text)) { |
158 v = 10 + (*text - 'A'); | 158 v = 10 + (*text - 'A'); |
159 } else if (radix == 16 && SDL_islowerhex (*text)) { | 159 } else if (radix == 16 && SDL_islowerhex(*text)) { |
160 v = 10 + (*text - 'a'); | 160 v = 10 + (*text - 'a'); |
161 } else { | 161 } else { |
162 break; | 162 break; |
163 } | 163 } |
164 value *= radix; | 164 value *= radix; |
176 } | 176 } |
177 #endif | 177 #endif |
178 | 178 |
179 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL) | 179 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL) |
180 static size_t | 180 static size_t |
181 SDL_ScanUnsignedLongLong (const char *text, int radix, Uint64 * valuep) | 181 SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep) |
182 { | 182 { |
183 const char *textstart = text; | 183 const char *textstart = text; |
184 Uint64 value = 0; | 184 Uint64 value = 0; |
185 | 185 |
186 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) { | 186 if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { |
187 text += 2; | 187 text += 2; |
188 } | 188 } |
189 for (;;) { | 189 for (;;) { |
190 int v; | 190 int v; |
191 if (SDL_isdigit (*text)) { | 191 if (SDL_isdigit(*text)) { |
192 v = *text - '0'; | 192 v = *text - '0'; |
193 } else if (radix == 16 && SDL_isupperhex (*text)) { | 193 } else if (radix == 16 && SDL_isupperhex(*text)) { |
194 v = 10 + (*text - 'A'); | 194 v = 10 + (*text - 'A'); |
195 } else if (radix == 16 && SDL_islowerhex (*text)) { | 195 } else if (radix == 16 && SDL_islowerhex(*text)) { |
196 v = 10 + (*text - 'a'); | 196 v = 10 + (*text - 'a'); |
197 } else { | 197 } else { |
198 break; | 198 break; |
199 } | 199 } |
200 value *= radix; | 200 value *= radix; |
209 #endif | 209 #endif |
210 #endif /* SDL_HAS_64BIT_TYPE */ | 210 #endif /* SDL_HAS_64BIT_TYPE */ |
211 | 211 |
212 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD) | 212 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD) |
213 static size_t | 213 static size_t |
214 SDL_ScanFloat (const char *text, double *valuep) | 214 SDL_ScanFloat(const char *text, double *valuep) |
215 { | 215 { |
216 const char *textstart = text; | 216 const char *textstart = text; |
217 unsigned long lvalue = 0; | 217 unsigned long lvalue = 0; |
218 double value = 0.0; | 218 double value = 0.0; |
219 SDL_bool negative = SDL_FALSE; | 219 SDL_bool negative = SDL_FALSE; |
220 | 220 |
221 if (*text == '-') { | 221 if (*text == '-') { |
222 negative = SDL_TRUE; | 222 negative = SDL_TRUE; |
223 ++text; | 223 ++text; |
224 } | 224 } |
225 text += SDL_ScanUnsignedLong (text, 10, &lvalue); | 225 text += SDL_ScanUnsignedLong(text, 10, &lvalue); |
226 value += lvalue; | 226 value += lvalue; |
227 if (*text == '.') { | 227 if (*text == '.') { |
228 int mult = 10; | 228 int mult = 10; |
229 ++text; | 229 ++text; |
230 while (SDL_isdigit (*text)) { | 230 while (SDL_isdigit(*text)) { |
231 lvalue = *text - '0'; | 231 lvalue = *text - '0'; |
232 value += (double) lvalue / mult; | 232 value += (double) lvalue / mult; |
233 mult *= 10; | 233 mult *= 10; |
234 ++text; | 234 ++text; |
235 } | 235 } |
245 } | 245 } |
246 #endif | 246 #endif |
247 | 247 |
248 #ifndef SDL_memset | 248 #ifndef SDL_memset |
249 void * | 249 void * |
250 SDL_memset (void *dst, int c, size_t len) | 250 SDL_memset(void *dst, int c, size_t len) |
251 { | 251 { |
252 size_t left = (len % 4); | 252 size_t left = (len % 4); |
253 if (len >= 4) { | 253 if (len >= 4) { |
254 Uint32 value = 0; | 254 Uint32 value = 0; |
255 Uint32 *dstp = (Uint32 *) dst; | 255 Uint32 *dstp = (Uint32 *) dst; |
279 } | 279 } |
280 #endif | 280 #endif |
281 | 281 |
282 #ifndef SDL_memcpy | 282 #ifndef SDL_memcpy |
283 void * | 283 void * |
284 SDL_memcpy (void *dst, const void *src, size_t len) | 284 SDL_memcpy(void *dst, const void *src, size_t len) |
285 { | 285 { |
286 char *srcp = (char *) src; | 286 char *srcp = (char *) src; |
287 char *dstp = (char *) dst; | 287 char *dstp = (char *) dst; |
288 while (len--) { | 288 while (len--) { |
289 *dstp++ = *srcp++; | 289 *dstp++ = *srcp++; |
292 } | 292 } |
293 #endif | 293 #endif |
294 | 294 |
295 #ifndef SDL_revcpy | 295 #ifndef SDL_revcpy |
296 void * | 296 void * |
297 SDL_revcpy (void *dst, const void *src, size_t len) | 297 SDL_revcpy(void *dst, const void *src, size_t len) |
298 { | 298 { |
299 char *srcp = (char *) src; | 299 char *srcp = (char *) src; |
300 char *dstp = (char *) dst; | 300 char *dstp = (char *) dst; |
301 srcp += len; | 301 srcp += len; |
302 dstp += len; | 302 dstp += len; |
307 } | 307 } |
308 #endif | 308 #endif |
309 | 309 |
310 #ifndef SDL_memcmp | 310 #ifndef SDL_memcmp |
311 int | 311 int |
312 SDL_memcmp (const void *s1, const void *s2, size_t len) | 312 SDL_memcmp(const void *s1, const void *s2, size_t len) |
313 { | 313 { |
314 char *s1p = (char *) s1; | 314 char *s1p = (char *) s1; |
315 char *s2p = (char *) s2; | 315 char *s2p = (char *) s2; |
316 while (len--) { | 316 while (len--) { |
317 if (*s1p != *s2p) { | 317 if (*s1p != *s2p) { |
324 } | 324 } |
325 #endif | 325 #endif |
326 | 326 |
327 #ifndef HAVE_STRLEN | 327 #ifndef HAVE_STRLEN |
328 size_t | 328 size_t |
329 SDL_strlen (const char *string) | 329 SDL_strlen(const char *string) |
330 { | 330 { |
331 size_t len = 0; | 331 size_t len = 0; |
332 while (*string++) { | 332 while (*string++) { |
333 ++len; | 333 ++len; |
334 } | 334 } |
336 } | 336 } |
337 #endif | 337 #endif |
338 | 338 |
339 #ifndef HAVE_STRLCPY | 339 #ifndef HAVE_STRLCPY |
340 size_t | 340 size_t |
341 SDL_strlcpy (char *dst, const char *src, size_t maxlen) | 341 SDL_strlcpy(char *dst, const char *src, size_t maxlen) |
342 { | 342 { |
343 size_t srclen = SDL_strlen (src); | 343 size_t srclen = SDL_strlen(src); |
344 if (maxlen > 0) { | 344 if (maxlen > 0) { |
345 size_t len = SDL_min (srclen, maxlen - 1); | 345 size_t len = SDL_min(srclen, maxlen - 1); |
346 SDL_memcpy (dst, src, len); | 346 SDL_memcpy(dst, src, len); |
347 dst[len] = '\0'; | 347 dst[len] = '\0'; |
348 } | 348 } |
349 return srclen; | 349 return srclen; |
350 } | 350 } |
351 #endif | 351 #endif |
352 | 352 |
353 #ifndef HAVE_STRLCAT | 353 #ifndef HAVE_STRLCAT |
354 size_t | 354 size_t |
355 SDL_strlcat (char *dst, const char *src, size_t maxlen) | 355 SDL_strlcat(char *dst, const char *src, size_t maxlen) |
356 { | 356 { |
357 size_t dstlen = SDL_strlen (dst); | 357 size_t dstlen = SDL_strlen(dst); |
358 size_t srclen = SDL_strlen (src); | 358 size_t srclen = SDL_strlen(src); |
359 if (dstlen < maxlen) { | 359 if (dstlen < maxlen) { |
360 SDL_strlcpy (dst + dstlen, src, maxlen - dstlen); | 360 SDL_strlcpy(dst + dstlen, src, maxlen - dstlen); |
361 } | 361 } |
362 return dstlen + srclen; | 362 return dstlen + srclen; |
363 } | 363 } |
364 #endif | 364 #endif |
365 | 365 |
366 #ifndef HAVE_STRDUP | 366 #ifndef HAVE_STRDUP |
367 char * | 367 char * |
368 SDL_strdup (const char *string) | 368 SDL_strdup(const char *string) |
369 { | 369 { |
370 size_t len = SDL_strlen (string) + 1; | 370 size_t len = SDL_strlen(string) + 1; |
371 char *newstr = SDL_malloc (len); | 371 char *newstr = SDL_malloc(len); |
372 if (newstr) { | 372 if (newstr) { |
373 SDL_strlcpy (newstr, string, len); | 373 SDL_strlcpy(newstr, string, len); |
374 } | 374 } |
375 return newstr; | 375 return newstr; |
376 } | 376 } |
377 #endif | 377 #endif |
378 | 378 |
379 #ifndef HAVE__STRREV | 379 #ifndef HAVE__STRREV |
380 char * | 380 char * |
381 SDL_strrev (char *string) | 381 SDL_strrev(char *string) |
382 { | 382 { |
383 size_t len = SDL_strlen (string); | 383 size_t len = SDL_strlen(string); |
384 char *a = &string[0]; | 384 char *a = &string[0]; |
385 char *b = &string[len - 1]; | 385 char *b = &string[len - 1]; |
386 len /= 2; | 386 len /= 2; |
387 while (len--) { | 387 while (len--) { |
388 char c = *a; | 388 char c = *a; |
393 } | 393 } |
394 #endif | 394 #endif |
395 | 395 |
396 #ifndef HAVE__STRUPR | 396 #ifndef HAVE__STRUPR |
397 char * | 397 char * |
398 SDL_strupr (char *string) | 398 SDL_strupr(char *string) |
399 { | 399 { |
400 char *bufp = string; | 400 char *bufp = string; |
401 while (*bufp) { | 401 while (*bufp) { |
402 *bufp = SDL_toupper (*bufp); | 402 *bufp = SDL_toupper(*bufp); |
403 ++bufp; | 403 ++bufp; |
404 } | 404 } |
405 return string; | 405 return string; |
406 } | 406 } |
407 #endif | 407 #endif |
408 | 408 |
409 #ifndef HAVE__STRLWR | 409 #ifndef HAVE__STRLWR |
410 char * | 410 char * |
411 SDL_strlwr (char *string) | 411 SDL_strlwr(char *string) |
412 { | 412 { |
413 char *bufp = string; | 413 char *bufp = string; |
414 while (*bufp) { | 414 while (*bufp) { |
415 *bufp = SDL_tolower (*bufp); | 415 *bufp = SDL_tolower(*bufp); |
416 ++bufp; | 416 ++bufp; |
417 } | 417 } |
418 return string; | 418 return string; |
419 } | 419 } |
420 #endif | 420 #endif |
421 | 421 |
422 #ifndef HAVE_STRCHR | 422 #ifndef HAVE_STRCHR |
423 char * | 423 char * |
424 SDL_strchr (const char *string, int c) | 424 SDL_strchr(const char *string, int c) |
425 { | 425 { |
426 while (*string) { | 426 while (*string) { |
427 if (*string == c) { | 427 if (*string == c) { |
428 return (char *) string; | 428 return (char *) string; |
429 } | 429 } |
433 } | 433 } |
434 #endif | 434 #endif |
435 | 435 |
436 #ifndef HAVE_STRRCHR | 436 #ifndef HAVE_STRRCHR |
437 char * | 437 char * |
438 SDL_strrchr (const char *string, int c) | 438 SDL_strrchr(const char *string, int c) |
439 { | 439 { |
440 const char *bufp = string + SDL_strlen (string) - 1; | 440 const char *bufp = string + SDL_strlen(string) - 1; |
441 while (bufp >= string) { | 441 while (bufp >= string) { |
442 if (*bufp == c) { | 442 if (*bufp == c) { |
443 return (char *) bufp; | 443 return (char *) bufp; |
444 } | 444 } |
445 --bufp; | 445 --bufp; |
448 } | 448 } |
449 #endif | 449 #endif |
450 | 450 |
451 #ifndef HAVE_STRSTR | 451 #ifndef HAVE_STRSTR |
452 char * | 452 char * |
453 SDL_strstr (const char *haystack, const char *needle) | 453 SDL_strstr(const char *haystack, const char *needle) |
454 { | 454 { |
455 size_t length = SDL_strlen (needle); | 455 size_t length = SDL_strlen(needle); |
456 while (*haystack) { | 456 while (*haystack) { |
457 if (SDL_strncmp (haystack, needle, length) == 0) { | 457 if (SDL_strncmp(haystack, needle, length) == 0) { |
458 return (char *) haystack; | 458 return (char *) haystack; |
459 } | 459 } |
460 ++haystack; | 460 ++haystack; |
461 } | 461 } |
462 return NULL; | 462 return NULL; |
473 }; | 473 }; |
474 #endif /* ntoa() conversion table */ | 474 #endif /* ntoa() conversion table */ |
475 | 475 |
476 #ifndef HAVE__LTOA | 476 #ifndef HAVE__LTOA |
477 char * | 477 char * |
478 SDL_ltoa (long value, char *string, int radix) | 478 SDL_ltoa(long value, char *string, int radix) |
479 { | 479 { |
480 char *bufp = string; | 480 char *bufp = string; |
481 | 481 |
482 if (value < 0) { | 482 if (value < 0) { |
483 *bufp++ = '-'; | 483 *bufp++ = '-'; |
493 } | 493 } |
494 *bufp = '\0'; | 494 *bufp = '\0'; |
495 | 495 |
496 /* The numbers went into the string backwards. :) */ | 496 /* The numbers went into the string backwards. :) */ |
497 if (*string == '-') { | 497 if (*string == '-') { |
498 SDL_strrev (string + 1); | 498 SDL_strrev(string + 1); |
499 } else { | 499 } else { |
500 SDL_strrev (string); | 500 SDL_strrev(string); |
501 } | 501 } |
502 | 502 |
503 return string; | 503 return string; |
504 } | 504 } |
505 #endif | 505 #endif |
506 | 506 |
507 #ifndef HAVE__ULTOA | 507 #ifndef HAVE__ULTOA |
508 char * | 508 char * |
509 SDL_ultoa (unsigned long value, char *string, int radix) | 509 SDL_ultoa(unsigned long value, char *string, int radix) |
510 { | 510 { |
511 char *bufp = string; | 511 char *bufp = string; |
512 | 512 |
513 if (value) { | 513 if (value) { |
514 while (value > 0) { | 514 while (value > 0) { |
519 *bufp++ = '0'; | 519 *bufp++ = '0'; |
520 } | 520 } |
521 *bufp = '\0'; | 521 *bufp = '\0'; |
522 | 522 |
523 /* The numbers went into the string backwards. :) */ | 523 /* The numbers went into the string backwards. :) */ |
524 SDL_strrev (string); | 524 SDL_strrev(string); |
525 | 525 |
526 return string; | 526 return string; |
527 } | 527 } |
528 #endif | 528 #endif |
529 | 529 |
530 #ifndef HAVE_STRTOL | 530 #ifndef HAVE_STRTOL |
531 long | 531 long |
532 SDL_strtol (const char *string, char **endp, int base) | 532 SDL_strtol(const char *string, char **endp, int base) |
533 { | 533 { |
534 size_t len; | 534 size_t len; |
535 long value; | 535 long value; |
536 | 536 |
537 len = SDL_ScanLong (string, base ? base : 10, &value); | 537 len = SDL_ScanLong(string, base ? base : 10, &value); |
538 if (endp) { | 538 if (endp) { |
539 *endp = (char *) string + len; | 539 *endp = (char *) string + len; |
540 } | 540 } |
541 return value; | 541 return value; |
542 } | 542 } |
543 #endif | 543 #endif |
544 | 544 |
545 #ifndef HAVE_STRTOUL | 545 #ifndef HAVE_STRTOUL |
546 unsigned long | 546 unsigned long |
547 SDL_strtoul (const char *string, char **endp, int base) | 547 SDL_strtoul(const char *string, char **endp, int base) |
548 { | 548 { |
549 size_t len; | 549 size_t len; |
550 unsigned long value; | 550 unsigned long value; |
551 | 551 |
552 len = SDL_ScanUnsignedLong (string, base ? base : 10, &value); | 552 len = SDL_ScanUnsignedLong(string, base ? base : 10, &value); |
553 if (endp) { | 553 if (endp) { |
554 *endp = (char *) string + len; | 554 *endp = (char *) string + len; |
555 } | 555 } |
556 return value; | 556 return value; |
557 } | 557 } |
559 | 559 |
560 #ifdef SDL_HAS_64BIT_TYPE | 560 #ifdef SDL_HAS_64BIT_TYPE |
561 | 561 |
562 #ifndef HAVE__I64TOA | 562 #ifndef HAVE__I64TOA |
563 char * | 563 char * |
564 SDL_lltoa (Sint64 value, char *string, int radix) | 564 SDL_lltoa(Sint64 value, char *string, int radix) |
565 { | 565 { |
566 char *bufp = string; | 566 char *bufp = string; |
567 | 567 |
568 if (value < 0) { | 568 if (value < 0) { |
569 *bufp++ = '-'; | 569 *bufp++ = '-'; |
579 } | 579 } |
580 *bufp = '\0'; | 580 *bufp = '\0'; |
581 | 581 |
582 /* The numbers went into the string backwards. :) */ | 582 /* The numbers went into the string backwards. :) */ |
583 if (*string == '-') { | 583 if (*string == '-') { |
584 SDL_strrev (string + 1); | 584 SDL_strrev(string + 1); |
585 } else { | 585 } else { |
586 SDL_strrev (string); | 586 SDL_strrev(string); |
587 } | 587 } |
588 | 588 |
589 return string; | 589 return string; |
590 } | 590 } |
591 #endif | 591 #endif |
592 | 592 |
593 #ifndef HAVE__UI64TOA | 593 #ifndef HAVE__UI64TOA |
594 char * | 594 char * |
595 SDL_ulltoa (Uint64 value, char *string, int radix) | 595 SDL_ulltoa(Uint64 value, char *string, int radix) |
596 { | 596 { |
597 char *bufp = string; | 597 char *bufp = string; |
598 | 598 |
599 if (value) { | 599 if (value) { |
600 while (value > 0) { | 600 while (value > 0) { |
605 *bufp++ = '0'; | 605 *bufp++ = '0'; |
606 } | 606 } |
607 *bufp = '\0'; | 607 *bufp = '\0'; |
608 | 608 |
609 /* The numbers went into the string backwards. :) */ | 609 /* The numbers went into the string backwards. :) */ |
610 SDL_strrev (string); | 610 SDL_strrev(string); |
611 | 611 |
612 return string; | 612 return string; |
613 } | 613 } |
614 #endif | 614 #endif |
615 | 615 |
616 #ifndef HAVE_STRTOLL | 616 #ifndef HAVE_STRTOLL |
617 Sint64 | 617 Sint64 |
618 SDL_strtoll (const char *string, char **endp, int base) | 618 SDL_strtoll(const char *string, char **endp, int base) |
619 { | 619 { |
620 size_t len; | 620 size_t len; |
621 Sint64 value; | 621 Sint64 value; |
622 | 622 |
623 len = SDL_ScanLongLong (string, base ? base : 10, &value); | 623 len = SDL_ScanLongLong(string, base ? base : 10, &value); |
624 if (endp) { | 624 if (endp) { |
625 *endp = (char *) string + len; | 625 *endp = (char *) string + len; |
626 } | 626 } |
627 return value; | 627 return value; |
628 } | 628 } |
629 #endif | 629 #endif |
630 | 630 |
631 #ifndef HAVE_STRTOULL | 631 #ifndef HAVE_STRTOULL |
632 Uint64 | 632 Uint64 |
633 SDL_strtoull (const char *string, char **endp, int base) | 633 SDL_strtoull(const char *string, char **endp, int base) |
634 { | 634 { |
635 size_t len; | 635 size_t len; |
636 Uint64 value; | 636 Uint64 value; |
637 | 637 |
638 len = SDL_ScanUnsignedLongLong (string, base ? base : 10, &value); | 638 len = SDL_ScanUnsignedLongLong(string, base ? base : 10, &value); |
639 if (endp) { | 639 if (endp) { |
640 *endp = (char *) string + len; | 640 *endp = (char *) string + len; |
641 } | 641 } |
642 return value; | 642 return value; |
643 } | 643 } |
645 | 645 |
646 #endif /* SDL_HAS_64BIT_TYPE */ | 646 #endif /* SDL_HAS_64BIT_TYPE */ |
647 | 647 |
648 #ifndef HAVE_STRTOD | 648 #ifndef HAVE_STRTOD |
649 double | 649 double |
650 SDL_strtod (const char *string, char **endp) | 650 SDL_strtod(const char *string, char **endp) |
651 { | 651 { |
652 size_t len; | 652 size_t len; |
653 double value; | 653 double value; |
654 | 654 |
655 len = SDL_ScanFloat (string, &value); | 655 len = SDL_ScanFloat(string, &value); |
656 if (endp) { | 656 if (endp) { |
657 *endp = (char *) string + len; | 657 *endp = (char *) string + len; |
658 } | 658 } |
659 return value; | 659 return value; |
660 } | 660 } |
661 #endif | 661 #endif |
662 | 662 |
663 #ifndef HAVE_STRCMP | 663 #ifndef HAVE_STRCMP |
664 int | 664 int |
665 SDL_strcmp (const char *str1, const char *str2) | 665 SDL_strcmp(const char *str1, const char *str2) |
666 { | 666 { |
667 while (*str1 && *str2) { | 667 while (*str1 && *str2) { |
668 if (*str1 != *str2) | 668 if (*str1 != *str2) |
669 break; | 669 break; |
670 ++str1; | 670 ++str1; |
674 } | 674 } |
675 #endif | 675 #endif |
676 | 676 |
677 #ifndef HAVE_STRNCMP | 677 #ifndef HAVE_STRNCMP |
678 int | 678 int |
679 SDL_strncmp (const char *str1, const char *str2, size_t maxlen) | 679 SDL_strncmp(const char *str1, const char *str2, size_t maxlen) |
680 { | 680 { |
681 while (*str1 && *str2 && maxlen) { | 681 while (*str1 && *str2 && maxlen) { |
682 if (*str1 != *str2) | 682 if (*str1 != *str2) |
683 break; | 683 break; |
684 ++str1; | 684 ++str1; |
692 } | 692 } |
693 #endif | 693 #endif |
694 | 694 |
695 #if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP) | 695 #if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP) |
696 int | 696 int |
697 SDL_strcasecmp (const char *str1, const char *str2) | 697 SDL_strcasecmp(const char *str1, const char *str2) |
698 { | 698 { |
699 char a = 0; | 699 char a = 0; |
700 char b = 0; | 700 char b = 0; |
701 while (*str1 && *str2) { | 701 while (*str1 && *str2) { |
702 a = SDL_tolower (*str1); | 702 a = SDL_tolower(*str1); |
703 b = SDL_tolower (*str2); | 703 b = SDL_tolower(*str2); |
704 if (a != b) | 704 if (a != b) |
705 break; | 705 break; |
706 ++str1; | 706 ++str1; |
707 ++str2; | 707 ++str2; |
708 } | 708 } |
710 } | 710 } |
711 #endif | 711 #endif |
712 | 712 |
713 #if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP) | 713 #if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP) |
714 int | 714 int |
715 SDL_strncasecmp (const char *str1, const char *str2, size_t maxlen) | 715 SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) |
716 { | 716 { |
717 char a = 0; | 717 char a = 0; |
718 char b = 0; | 718 char b = 0; |
719 while (*str1 && *str2 && maxlen) { | 719 while (*str1 && *str2 && maxlen) { |
720 a = SDL_tolower (*str1); | 720 a = SDL_tolower(*str1); |
721 b = SDL_tolower (*str2); | 721 b = SDL_tolower(*str2); |
722 if (a != b) | 722 if (a != b) |
723 break; | 723 break; |
724 ++str1; | 724 ++str1; |
725 ++str2; | 725 ++str2; |
726 --maxlen; | 726 --maxlen; |
729 } | 729 } |
730 #endif | 730 #endif |
731 | 731 |
732 #ifndef HAVE_SSCANF | 732 #ifndef HAVE_SSCANF |
733 int | 733 int |
734 SDL_sscanf (const char *text, const char *fmt, ...) | 734 SDL_sscanf(const char *text, const char *fmt, ...) |
735 { | 735 { |
736 va_list ap; | 736 va_list ap; |
737 int retval = 0; | 737 int retval = 0; |
738 | 738 |
739 va_start (ap, fmt); | 739 va_start(ap, fmt); |
740 while (*fmt) { | 740 while (*fmt) { |
741 if (*fmt == ' ') { | 741 if (*fmt == ' ') { |
742 while (SDL_isspace (*text)) { | 742 while (SDL_isspace(*text)) { |
743 ++text; | 743 ++text; |
744 } | 744 } |
745 ++fmt; | 745 ++fmt; |
746 continue; | 746 continue; |
747 } | 747 } |
769 } | 769 } |
770 if (*fmt == '*') { | 770 if (*fmt == '*') { |
771 suppress = SDL_TRUE; | 771 suppress = SDL_TRUE; |
772 ++fmt; | 772 ++fmt; |
773 } | 773 } |
774 fmt += SDL_ScanLong (fmt, 10, &count); | 774 fmt += SDL_ScanLong(fmt, 10, &count); |
775 | 775 |
776 if (*fmt == 'c') { | 776 if (*fmt == 'c') { |
777 if (!count) { | 777 if (!count) { |
778 count = 1; | 778 count = 1; |
779 } | 779 } |
780 if (suppress) { | 780 if (suppress) { |
781 while (count--) { | 781 while (count--) { |
782 ++text; | 782 ++text; |
783 } | 783 } |
784 } else { | 784 } else { |
785 char *valuep = va_arg (ap, char *); | 785 char *valuep = va_arg(ap, char *); |
786 while (count--) { | 786 while (count--) { |
787 *valuep++ = *text++; | 787 *valuep++ = *text++; |
788 } | 788 } |
789 ++retval; | 789 ++retval; |
790 } | 790 } |
791 continue; | 791 continue; |
792 } | 792 } |
793 | 793 |
794 while (SDL_isspace (*text)) { | 794 while (SDL_isspace(*text)) { |
795 ++text; | 795 ++text; |
796 } | 796 } |
797 | 797 |
798 /* FIXME: implement more of the format specifiers */ | 798 /* FIXME: implement more of the format specifiers */ |
799 while (!done) { | 799 while (!done) { |
810 if (inttype < DO_LONGLONG) { | 810 if (inttype < DO_LONGLONG) { |
811 ++inttype; | 811 ++inttype; |
812 } | 812 } |
813 break; | 813 break; |
814 case 'I': | 814 case 'I': |
815 if (SDL_strncmp (fmt, "I64", 3) == 0) { | 815 if (SDL_strncmp(fmt, "I64", 3) == 0) { |
816 fmt += 2; | 816 fmt += 2; |
817 inttype = DO_LONGLONG; | 817 inttype = DO_LONGLONG; |
818 } | 818 } |
819 break; | 819 break; |
820 case 'i': | 820 case 'i': |
822 int index = 0; | 822 int index = 0; |
823 if (text[index] == '-') { | 823 if (text[index] == '-') { |
824 ++index; | 824 ++index; |
825 } | 825 } |
826 if (text[index] == '0') { | 826 if (text[index] == '0') { |
827 if (SDL_tolower (text[index + 1]) == 'x') { | 827 if (SDL_tolower(text[index + 1]) == 'x') { |
828 radix = 16; | 828 radix = 16; |
829 } else { | 829 } else { |
830 radix = 8; | 830 radix = 8; |
831 } | 831 } |
832 } | 832 } |
834 /* Fall through to %d handling */ | 834 /* Fall through to %d handling */ |
835 case 'd': | 835 case 'd': |
836 #ifdef SDL_HAS_64BIT_TYPE | 836 #ifdef SDL_HAS_64BIT_TYPE |
837 if (inttype == DO_LONGLONG) { | 837 if (inttype == DO_LONGLONG) { |
838 Sint64 value; | 838 Sint64 value; |
839 text += SDL_ScanLongLong (text, radix, &value); | 839 text += SDL_ScanLongLong(text, radix, &value); |
840 if (!suppress) { | 840 if (!suppress) { |
841 Sint64 *valuep = va_arg (ap, Sint64 *); | 841 Sint64 *valuep = va_arg(ap, Sint64 *); |
842 *valuep = value; | 842 *valuep = value; |
843 ++retval; | 843 ++retval; |
844 } | 844 } |
845 } else | 845 } else |
846 #endif /* SDL_HAS_64BIT_TYPE */ | 846 #endif /* SDL_HAS_64BIT_TYPE */ |
847 { | 847 { |
848 long value; | 848 long value; |
849 text += SDL_ScanLong (text, radix, &value); | 849 text += SDL_ScanLong(text, radix, &value); |
850 if (!suppress) { | 850 if (!suppress) { |
851 switch (inttype) { | 851 switch (inttype) { |
852 case DO_SHORT: | 852 case DO_SHORT: |
853 { | 853 { |
854 short *valuep = va_arg (ap, short *); | 854 short *valuep = va_arg(ap, short *); |
855 *valuep = (short) value; | 855 *valuep = (short) value; |
856 } | 856 } |
857 break; | 857 break; |
858 case DO_INT: | 858 case DO_INT: |
859 { | 859 { |
860 int *valuep = va_arg (ap, int *); | 860 int *valuep = va_arg(ap, int *); |
861 *valuep = (int) value; | 861 *valuep = (int) value; |
862 } | 862 } |
863 break; | 863 break; |
864 case DO_LONG: | 864 case DO_LONG: |
865 { | 865 { |
866 long *valuep = va_arg (ap, long *); | 866 long *valuep = va_arg(ap, long *); |
867 *valuep = value; | 867 *valuep = value; |
868 } | 868 } |
869 break; | 869 break; |
870 case DO_LONGLONG: | 870 case DO_LONGLONG: |
871 /* Handled above */ | 871 /* Handled above */ |
889 /* Fall through to unsigned handling */ | 889 /* Fall through to unsigned handling */ |
890 case 'u': | 890 case 'u': |
891 #ifdef SDL_HAS_64BIT_TYPE | 891 #ifdef SDL_HAS_64BIT_TYPE |
892 if (inttype == DO_LONGLONG) { | 892 if (inttype == DO_LONGLONG) { |
893 Uint64 value; | 893 Uint64 value; |
894 text += | 894 text += SDL_ScanUnsignedLongLong(text, radix, &value); |
895 SDL_ScanUnsignedLongLong (text, radix, &value); | |
896 if (!suppress) { | 895 if (!suppress) { |
897 Uint64 *valuep = va_arg (ap, Uint64 *); | 896 Uint64 *valuep = va_arg(ap, Uint64 *); |
898 *valuep = value; | 897 *valuep = value; |
899 ++retval; | 898 ++retval; |
900 } | 899 } |
901 } else | 900 } else |
902 #endif /* SDL_HAS_64BIT_TYPE */ | 901 #endif /* SDL_HAS_64BIT_TYPE */ |
903 { | 902 { |
904 unsigned long value; | 903 unsigned long value; |
905 text += SDL_ScanUnsignedLong (text, radix, &value); | 904 text += SDL_ScanUnsignedLong(text, radix, &value); |
906 if (!suppress) { | 905 if (!suppress) { |
907 switch (inttype) { | 906 switch (inttype) { |
908 case DO_SHORT: | 907 case DO_SHORT: |
909 { | 908 { |
910 short *valuep = va_arg (ap, short *); | 909 short *valuep = va_arg(ap, short *); |
911 *valuep = (short) value; | 910 *valuep = (short) value; |
912 } | 911 } |
913 break; | 912 break; |
914 case DO_INT: | 913 case DO_INT: |
915 { | 914 { |
916 int *valuep = va_arg (ap, int *); | 915 int *valuep = va_arg(ap, int *); |
917 *valuep = (int) value; | 916 *valuep = (int) value; |
918 } | 917 } |
919 break; | 918 break; |
920 case DO_LONG: | 919 case DO_LONG: |
921 { | 920 { |
922 long *valuep = va_arg (ap, long *); | 921 long *valuep = va_arg(ap, long *); |
923 *valuep = value; | 922 *valuep = value; |
924 } | 923 } |
925 break; | 924 break; |
926 case DO_LONGLONG: | 925 case DO_LONGLONG: |
927 /* Handled above */ | 926 /* Handled above */ |
933 done = SDL_TRUE; | 932 done = SDL_TRUE; |
934 break; | 933 break; |
935 case 'p': | 934 case 'p': |
936 { | 935 { |
937 uintptr_t value; | 936 uintptr_t value; |
938 text += SDL_ScanUintPtrT (text, 16, &value); | 937 text += SDL_ScanUintPtrT(text, 16, &value); |
939 if (!suppress) { | 938 if (!suppress) { |
940 void **valuep = va_arg (ap, void **); | 939 void **valuep = va_arg(ap, void **); |
941 *valuep = (void *) value; | 940 *valuep = (void *) value; |
942 ++retval; | 941 ++retval; |
943 } | 942 } |
944 } | 943 } |
945 done = SDL_TRUE; | 944 done = SDL_TRUE; |
946 break; | 945 break; |
947 case 'f': | 946 case 'f': |
948 { | 947 { |
949 double value; | 948 double value; |
950 text += SDL_ScanFloat (text, &value); | 949 text += SDL_ScanFloat(text, &value); |
951 if (!suppress) { | 950 if (!suppress) { |
952 float *valuep = va_arg (ap, float *); | 951 float *valuep = va_arg(ap, float *); |
953 *valuep = (float) value; | 952 *valuep = (float) value; |
954 ++retval; | 953 ++retval; |
955 } | 954 } |
956 } | 955 } |
957 done = SDL_TRUE; | 956 done = SDL_TRUE; |
958 break; | 957 break; |
959 case 's': | 958 case 's': |
960 if (suppress) { | 959 if (suppress) { |
961 while (!SDL_isspace (*text)) { | 960 while (!SDL_isspace(*text)) { |
962 ++text; | 961 ++text; |
963 if (count) { | 962 if (count) { |
964 if (--count == 0) { | 963 if (--count == 0) { |
965 break; | 964 break; |
966 } | 965 } |
967 } | 966 } |
968 } | 967 } |
969 } else { | 968 } else { |
970 char *valuep = va_arg (ap, char *); | 969 char *valuep = va_arg(ap, char *); |
971 while (!SDL_isspace (*text)) { | 970 while (!SDL_isspace(*text)) { |
972 *valuep++ = *text++; | 971 *valuep++ = *text++; |
973 if (count) { | 972 if (count) { |
974 if (--count == 0) { | 973 if (--count == 0) { |
975 break; | 974 break; |
976 } | 975 } |
995 continue; | 994 continue; |
996 } | 995 } |
997 /* Text didn't match format specifier */ | 996 /* Text didn't match format specifier */ |
998 break; | 997 break; |
999 } | 998 } |
1000 va_end (ap); | 999 va_end(ap); |
1001 | 1000 |
1002 return retval; | 1001 return retval; |
1003 } | 1002 } |
1004 #endif | 1003 #endif |
1005 | 1004 |
1006 #ifndef HAVE_SNPRINTF | 1005 #ifndef HAVE_SNPRINTF |
1007 int | 1006 int |
1008 SDL_snprintf (char *text, size_t maxlen, const char *fmt, ...) | 1007 SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) |
1009 { | 1008 { |
1010 va_list ap; | 1009 va_list ap; |
1011 int retval; | 1010 int retval; |
1012 | 1011 |
1013 va_start (ap, fmt); | 1012 va_start(ap, fmt); |
1014 retval = SDL_vsnprintf (text, maxlen, fmt, ap); | 1013 retval = SDL_vsnprintf(text, maxlen, fmt, ap); |
1015 va_end (ap); | 1014 va_end(ap); |
1016 | 1015 |
1017 return retval; | 1016 return retval; |
1018 } | 1017 } |
1019 #endif | 1018 #endif |
1020 | 1019 |
1021 #ifndef HAVE_VSNPRINTF | 1020 #ifndef HAVE_VSNPRINTF |
1022 static size_t | 1021 static size_t |
1023 SDL_PrintLong (char *text, long value, int radix, size_t maxlen) | 1022 SDL_PrintLong(char *text, long value, int radix, size_t maxlen) |
1024 { | 1023 { |
1025 char num[130]; | 1024 char num[130]; |
1026 size_t size; | 1025 size_t size; |
1027 | 1026 |
1028 SDL_ltoa (value, num, radix); | 1027 SDL_ltoa(value, num, radix); |
1029 size = SDL_strlen (num); | 1028 size = SDL_strlen(num); |
1030 if (size >= maxlen) { | 1029 if (size >= maxlen) { |
1031 size = maxlen - 1; | 1030 size = maxlen - 1; |
1032 } | 1031 } |
1033 SDL_strlcpy (text, num, size + 1); | 1032 SDL_strlcpy(text, num, size + 1); |
1034 | 1033 |
1035 return size; | 1034 return size; |
1036 } | 1035 } |
1037 | 1036 |
1038 static size_t | 1037 static size_t |
1039 SDL_PrintUnsignedLong (char *text, unsigned long value, int radix, | 1038 SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, |
1040 size_t maxlen) | 1039 size_t maxlen) |
1041 { | 1040 { |
1042 char num[130]; | 1041 char num[130]; |
1043 size_t size; | 1042 size_t size; |
1044 | 1043 |
1045 SDL_ultoa (value, num, radix); | 1044 SDL_ultoa(value, num, radix); |
1046 size = SDL_strlen (num); | 1045 size = SDL_strlen(num); |
1047 if (size >= maxlen) { | 1046 if (size >= maxlen) { |
1048 size = maxlen - 1; | 1047 size = maxlen - 1; |
1049 } | 1048 } |
1050 SDL_strlcpy (text, num, size + 1); | 1049 SDL_strlcpy(text, num, size + 1); |
1051 | 1050 |
1052 return size; | 1051 return size; |
1053 } | 1052 } |
1054 | 1053 |
1055 #ifdef SDL_HAS_64BIT_TYPE | 1054 #ifdef SDL_HAS_64BIT_TYPE |
1056 static size_t | 1055 static size_t |
1057 SDL_PrintLongLong (char *text, Sint64 value, int radix, size_t maxlen) | 1056 SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen) |
1058 { | 1057 { |
1059 char num[130]; | 1058 char num[130]; |
1060 size_t size; | 1059 size_t size; |
1061 | 1060 |
1062 SDL_lltoa (value, num, radix); | 1061 SDL_lltoa(value, num, radix); |
1063 size = SDL_strlen (num); | 1062 size = SDL_strlen(num); |
1064 if (size >= maxlen) { | 1063 if (size >= maxlen) { |
1065 size = maxlen - 1; | 1064 size = maxlen - 1; |
1066 } | 1065 } |
1067 SDL_strlcpy (text, num, size + 1); | 1066 SDL_strlcpy(text, num, size + 1); |
1068 | 1067 |
1069 return size; | 1068 return size; |
1070 } | 1069 } |
1071 | 1070 |
1072 static size_t | 1071 static size_t |
1073 SDL_PrintUnsignedLongLong (char *text, Uint64 value, int radix, size_t maxlen) | 1072 SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen) |
1074 { | 1073 { |
1075 char num[130]; | 1074 char num[130]; |
1076 size_t size; | 1075 size_t size; |
1077 | 1076 |
1078 SDL_ulltoa (value, num, radix); | 1077 SDL_ulltoa(value, num, radix); |
1079 size = SDL_strlen (num); | 1078 size = SDL_strlen(num); |
1080 if (size >= maxlen) { | 1079 if (size >= maxlen) { |
1081 size = maxlen - 1; | 1080 size = maxlen - 1; |
1082 } | 1081 } |
1083 SDL_strlcpy (text, num, size + 1); | 1082 SDL_strlcpy(text, num, size + 1); |
1084 | 1083 |
1085 return size; | 1084 return size; |
1086 } | 1085 } |
1087 #endif /* SDL_HAS_64BIT_TYPE */ | 1086 #endif /* SDL_HAS_64BIT_TYPE */ |
1088 static size_t | 1087 static size_t |
1089 SDL_PrintFloat (char *text, double arg, size_t maxlen) | 1088 SDL_PrintFloat(char *text, double arg, size_t maxlen) |
1090 { | 1089 { |
1091 char *textstart = text; | 1090 char *textstart = text; |
1092 if (arg) { | 1091 if (arg) { |
1093 /* This isn't especially accurate, but hey, it's easy. :) */ | 1092 /* This isn't especially accurate, but hey, it's easy. :) */ |
1094 const double precision = 0.00000001; | 1093 const double precision = 0.00000001; |
1099 *text++ = '-'; | 1098 *text++ = '-'; |
1100 --maxlen; | 1099 --maxlen; |
1101 arg = -arg; | 1100 arg = -arg; |
1102 } | 1101 } |
1103 value = (unsigned long) arg; | 1102 value = (unsigned long) arg; |
1104 len = SDL_PrintUnsignedLong (text, value, 10, maxlen); | 1103 len = SDL_PrintUnsignedLong(text, value, 10, maxlen); |
1105 text += len; | 1104 text += len; |
1106 maxlen -= len; | 1105 maxlen -= len; |
1107 arg -= value; | 1106 arg -= value; |
1108 if (arg > precision && maxlen) { | 1107 if (arg > precision && maxlen) { |
1109 int mult = 10; | 1108 int mult = 10; |
1110 *text++ = '.'; | 1109 *text++ = '.'; |
1111 while ((arg > precision) && maxlen) { | 1110 while ((arg > precision) && maxlen) { |
1112 value = (unsigned long) (arg * mult); | 1111 value = (unsigned long) (arg * mult); |
1113 len = SDL_PrintUnsignedLong (text, value, 10, maxlen); | 1112 len = SDL_PrintUnsignedLong(text, value, 10, maxlen); |
1114 text += len; | 1113 text += len; |
1115 maxlen -= len; | 1114 maxlen -= len; |
1116 arg -= (double) value / mult; | 1115 arg -= (double) value / mult; |
1117 mult *= 10; | 1116 mult *= 10; |
1118 } | 1117 } |
1122 } | 1121 } |
1123 return (text - textstart); | 1122 return (text - textstart); |
1124 } | 1123 } |
1125 | 1124 |
1126 static size_t | 1125 static size_t |
1127 SDL_PrintString (char *text, const char *string, size_t maxlen) | 1126 SDL_PrintString(char *text, const char *string, size_t maxlen) |
1128 { | 1127 { |
1129 char *textstart = text; | 1128 char *textstart = text; |
1130 while (*string && maxlen--) { | 1129 while (*string && maxlen--) { |
1131 *text++ = *string++; | 1130 *text++ = *string++; |
1132 } | 1131 } |
1133 return (text - textstart); | 1132 return (text - textstart); |
1134 } | 1133 } |
1135 | 1134 |
1136 int | 1135 int |
1137 SDL_vsnprintf (char *text, size_t maxlen, const char *fmt, va_list ap) | 1136 SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) |
1138 { | 1137 { |
1139 char *textstart = text; | 1138 char *textstart = text; |
1140 if (maxlen <= 0) { | 1139 if (maxlen <= 0) { |
1141 return 0; | 1140 return 0; |
1142 } | 1141 } |
1166 len = 1; | 1165 len = 1; |
1167 done = SDL_TRUE; | 1166 done = SDL_TRUE; |
1168 break; | 1167 break; |
1169 case 'c': | 1168 case 'c': |
1170 /* char is promoted to int when passed through (...) */ | 1169 /* char is promoted to int when passed through (...) */ |
1171 *text = (char) va_arg (ap, int); | 1170 *text = (char) va_arg(ap, int); |
1172 len = 1; | 1171 len = 1; |
1173 done = SDL_TRUE; | 1172 done = SDL_TRUE; |
1174 break; | 1173 break; |
1175 case 'h': | 1174 case 'h': |
1176 /* short is promoted to int when passed through (...) */ | 1175 /* short is promoted to int when passed through (...) */ |
1179 if (inttype < DO_LONGLONG) { | 1178 if (inttype < DO_LONGLONG) { |
1180 ++inttype; | 1179 ++inttype; |
1181 } | 1180 } |
1182 break; | 1181 break; |
1183 case 'I': | 1182 case 'I': |
1184 if (SDL_strncmp (fmt, "I64", 3) == 0) { | 1183 if (SDL_strncmp(fmt, "I64", 3) == 0) { |
1185 fmt += 2; | 1184 fmt += 2; |
1186 inttype = DO_LONGLONG; | 1185 inttype = DO_LONGLONG; |
1187 } | 1186 } |
1188 break; | 1187 break; |
1189 case 'i': | 1188 case 'i': |
1190 case 'd': | 1189 case 'd': |
1191 switch (inttype) { | 1190 switch (inttype) { |
1192 case DO_INT: | 1191 case DO_INT: |
1193 len = | 1192 len = |
1194 SDL_PrintLong (text, | 1193 SDL_PrintLong(text, |
1195 (long) va_arg (ap, int), | 1194 (long) va_arg(ap, int), |
1196 radix, maxlen); | 1195 radix, maxlen); |
1197 break; | 1196 break; |
1198 case DO_LONG: | 1197 case DO_LONG: |
1199 len = | 1198 len = |
1200 SDL_PrintLong (text, va_arg (ap, long), | 1199 SDL_PrintLong(text, va_arg(ap, long), |
1201 radix, maxlen); | 1200 radix, maxlen); |
1202 break; | 1201 break; |
1203 case DO_LONGLONG: | 1202 case DO_LONGLONG: |
1204 #ifdef SDL_HAS_64BIT_TYPE | 1203 #ifdef SDL_HAS_64BIT_TYPE |
1205 len = | 1204 len = |
1206 SDL_PrintLongLong (text, | 1205 SDL_PrintLongLong(text, |
1207 va_arg (ap, Sint64), | 1206 va_arg(ap, Sint64), |
1208 radix, maxlen); | 1207 radix, maxlen); |
1209 #else | 1208 #else |
1210 len = | 1209 len = |
1211 SDL_PrintLong (text, va_arg (ap, long), | 1210 SDL_PrintLong(text, va_arg(ap, long), |
1212 radix, maxlen); | 1211 radix, maxlen); |
1213 #endif | 1212 #endif |
1214 break; | 1213 break; |
1215 } | 1214 } |
1216 done = SDL_TRUE; | 1215 done = SDL_TRUE; |
1217 break; | 1216 break; |
1233 } | 1232 } |
1234 /* Fall through to unsigned handling */ | 1233 /* Fall through to unsigned handling */ |
1235 case 'u': | 1234 case 'u': |
1236 switch (inttype) { | 1235 switch (inttype) { |
1237 case DO_INT: | 1236 case DO_INT: |
1238 len = SDL_PrintUnsignedLong (text, (unsigned long) | 1237 len = SDL_PrintUnsignedLong(text, (unsigned long) |
1239 va_arg (ap, | 1238 va_arg(ap, |
1240 unsigned | 1239 unsigned |
1241 int), | 1240 int), |
1242 radix, maxlen); | 1241 radix, maxlen); |
1243 break; | 1242 break; |
1244 case DO_LONG: | 1243 case DO_LONG: |
1245 len = | 1244 len = |
1246 SDL_PrintUnsignedLong (text, | 1245 SDL_PrintUnsignedLong(text, |
1247 va_arg (ap, | 1246 va_arg(ap, |
1248 unsigned | 1247 unsigned |
1249 long), | 1248 long), |
1250 radix, maxlen); | 1249 radix, maxlen); |
1251 break; | 1250 break; |
1252 case DO_LONGLONG: | 1251 case DO_LONGLONG: |
1253 #ifdef SDL_HAS_64BIT_TYPE | 1252 #ifdef SDL_HAS_64BIT_TYPE |
1254 len = | 1253 len = |
1255 SDL_PrintUnsignedLongLong (text, | 1254 SDL_PrintUnsignedLongLong(text, |
1256 va_arg (ap, | 1255 va_arg(ap, |
1257 Uint64), | 1256 Uint64), |
1258 radix, maxlen); | 1257 radix, maxlen); |
1259 #else | 1258 #else |
1260 len = | 1259 len = |
1261 SDL_PrintUnsignedLong (text, | 1260 SDL_PrintUnsignedLong(text, |
1262 va_arg (ap, | 1261 va_arg(ap, |
1263 unsigned | 1262 unsigned |
1264 long), | 1263 long), |
1265 radix, maxlen); | 1264 radix, maxlen); |
1266 #endif | 1265 #endif |
1267 break; | 1266 break; |
1268 } | 1267 } |
1269 if (do_lowercase) { | 1268 if (do_lowercase) { |
1270 SDL_strlwr (text); | 1269 SDL_strlwr(text); |
1271 } | 1270 } |
1272 done = SDL_TRUE; | 1271 done = SDL_TRUE; |
1273 break; | 1272 break; |
1274 case 'f': | 1273 case 'f': |
1275 len = SDL_PrintFloat (text, va_arg (ap, double), maxlen); | 1274 len = SDL_PrintFloat(text, va_arg(ap, double), maxlen); |
1276 done = SDL_TRUE; | 1275 done = SDL_TRUE; |
1277 break; | 1276 break; |
1278 case 's': | 1277 case 's': |
1279 len = SDL_PrintString (text, va_arg (ap, char *), maxlen); | 1278 len = SDL_PrintString(text, va_arg(ap, char *), maxlen); |
1280 done = SDL_TRUE; | 1279 done = SDL_TRUE; |
1281 break; | 1280 break; |
1282 default: | 1281 default: |
1283 done = SDL_TRUE; | 1282 done = SDL_TRUE; |
1284 break; | 1283 break; |