comparison src/stdlib/SDL_string.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
28 28
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 SDL_ScanLong(const char *text, int radix, long *valuep) 33 static size_t
34 SDL_ScanLong (const char *text, int radix, long *valuep)
34 { 35 {
35 const char *textstart = text; 36 const char *textstart = text;
36 long value = 0; 37 long value = 0;
37 SDL_bool negative = SDL_FALSE; 38 SDL_bool negative = SDL_FALSE;
38 39
39 if ( *text == '-' ) { 40 if (*text == '-') {
40 negative = SDL_TRUE; 41 negative = SDL_TRUE;
41 ++text; 42 ++text;
42 } 43 }
43 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 44 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) {
44 text += 2; 45 text += 2;
45 } 46 }
46 for ( ; ; ) { 47 for (;;) {
47 int v; 48 int v;
48 if ( SDL_isdigit(*text) ) { 49 if (SDL_isdigit (*text)) {
49 v = *text - '0'; 50 v = *text - '0';
50 } else if ( radix == 16 && SDL_isupperhex(*text) ) { 51 } else if (radix == 16 && SDL_isupperhex (*text)) {
51 v = 10 + (*text - 'A'); 52 v = 10 + (*text - 'A');
52 } else if ( radix == 16 && SDL_islowerhex(*text) ) { 53 } else if (radix == 16 && SDL_islowerhex (*text)) {
53 v = 10 + (*text - 'a'); 54 v = 10 + (*text - 'a');
54 } else { 55 } else {
55 break; 56 break;
56 } 57 }
57 value *= radix; 58 value *= radix;
58 value += v; 59 value += v;
59 ++text; 60 ++text;
60 } 61 }
61 if ( valuep ) { 62 if (valuep) {
62 if ( negative && value ) { 63 if (negative && value) {
63 *valuep = -value; 64 *valuep = -value;
64 } else { 65 } else {
65 *valuep = value; 66 *valuep = value;
66 } 67 }
67 } 68 }
68 return (text - textstart); 69 return (text - textstart);
69 } 70 }
70 #endif 71 #endif
71 72
72 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) 73 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
73 static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep) 74 static size_t
75 SDL_ScanUnsignedLong (const char *text, int radix, unsigned long *valuep)
74 { 76 {
75 const char *textstart = text; 77 const char *textstart = text;
76 unsigned long value = 0; 78 unsigned long value = 0;
77 79
78 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 80 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) {
79 text += 2; 81 text += 2;
80 } 82 }
81 for ( ; ; ) { 83 for (;;) {
82 int v; 84 int v;
83 if ( SDL_isdigit(*text) ) { 85 if (SDL_isdigit (*text)) {
84 v = *text - '0'; 86 v = *text - '0';
85 } else if ( radix == 16 && SDL_isupperhex(*text) ) { 87 } else if (radix == 16 && SDL_isupperhex (*text)) {
86 v = 10 + (*text - 'A'); 88 v = 10 + (*text - 'A');
87 } else if ( radix == 16 && SDL_islowerhex(*text) ) { 89 } else if (radix == 16 && SDL_islowerhex (*text)) {
88 v = 10 + (*text - 'a'); 90 v = 10 + (*text - 'a');
89 } else { 91 } else {
90 break; 92 break;
91 } 93 }
92 value *= radix; 94 value *= radix;
93 value += v; 95 value += v;
94 ++text; 96 ++text;
95 } 97 }
96 if ( valuep ) { 98 if (valuep) {
97 *valuep = value; 99 *valuep = value;
98 } 100 }
99 return (text - textstart); 101 return (text - textstart);
100 } 102 }
101 #endif 103 #endif
102 104
103 #ifndef HAVE_SSCANF 105 #ifndef HAVE_SSCANF
104 static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep) 106 static size_t
107 SDL_ScanUintPtrT (const char *text, int radix, uintptr_t * valuep)
105 { 108 {
106 const char *textstart = text; 109 const char *textstart = text;
107 uintptr_t value = 0; 110 uintptr_t value = 0;
108 111
109 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 112 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) {
110 text += 2; 113 text += 2;
111 } 114 }
112 for ( ; ; ) { 115 for (;;) {
113 int v; 116 int v;
114 if ( SDL_isdigit(*text) ) { 117 if (SDL_isdigit (*text)) {
115 v = *text - '0'; 118 v = *text - '0';
116 } else if ( radix == 16 && SDL_isupperhex(*text) ) { 119 } else if (radix == 16 && SDL_isupperhex (*text)) {
117 v = 10 + (*text - 'A'); 120 v = 10 + (*text - 'A');
118 } else if ( radix == 16 && SDL_islowerhex(*text) ) { 121 } else if (radix == 16 && SDL_islowerhex (*text)) {
119 v = 10 + (*text - 'a'); 122 v = 10 + (*text - 'a');
120 } else { 123 } else {
121 break; 124 break;
122 } 125 }
123 value *= radix; 126 value *= radix;
124 value += v; 127 value += v;
125 ++text; 128 ++text;
126 } 129 }
127 if ( valuep ) { 130 if (valuep) {
128 *valuep = value; 131 *valuep = value;
129 } 132 }
130 return (text - textstart); 133 return (text - textstart);
131 } 134 }
132 #endif 135 #endif
133 136
134 #ifdef SDL_HAS_64BIT_TYPE 137 #ifdef SDL_HAS_64BIT_TYPE
135 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL) 138 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
136 static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep) 139 static size_t
140 SDL_ScanLongLong (const char *text, int radix, Sint64 * valuep)
137 { 141 {
138 const char *textstart = text; 142 const char *textstart = text;
139 Sint64 value = 0; 143 Sint64 value = 0;
140 SDL_bool negative = SDL_FALSE; 144 SDL_bool negative = SDL_FALSE;
141 145
142 if ( *text == '-' ) { 146 if (*text == '-') {
143 negative = SDL_TRUE; 147 negative = SDL_TRUE;
144 ++text; 148 ++text;
145 } 149 }
146 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 150 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) {
147 text += 2; 151 text += 2;
148 } 152 }
149 for ( ; ; ) { 153 for (;;) {
150 int v; 154 int v;
151 if ( SDL_isdigit(*text) ) { 155 if (SDL_isdigit (*text)) {
152 v = *text - '0'; 156 v = *text - '0';
153 } else if ( radix == 16 && SDL_isupperhex(*text) ) { 157 } else if (radix == 16 && SDL_isupperhex (*text)) {
154 v = 10 + (*text - 'A'); 158 v = 10 + (*text - 'A');
155 } else if ( radix == 16 && SDL_islowerhex(*text) ) { 159 } else if (radix == 16 && SDL_islowerhex (*text)) {
156 v = 10 + (*text - 'a'); 160 v = 10 + (*text - 'a');
157 } else { 161 } else {
158 break; 162 break;
159 } 163 }
160 value *= radix; 164 value *= radix;
161 value += v; 165 value += v;
162 ++text; 166 ++text;
163 } 167 }
164 if ( valuep ) { 168 if (valuep) {
165 if ( negative && value ) { 169 if (negative && value) {
166 *valuep = -value; 170 *valuep = -value;
167 } else { 171 } else {
168 *valuep = value; 172 *valuep = value;
169 } 173 }
170 } 174 }
171 return (text - textstart); 175 return (text - textstart);
172 } 176 }
173 #endif 177 #endif
174 178
175 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL) 179 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
176 static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep) 180 static size_t
181 SDL_ScanUnsignedLongLong (const char *text, int radix, Uint64 * valuep)
177 { 182 {
178 const char *textstart = text; 183 const char *textstart = text;
179 Uint64 value = 0; 184 Uint64 value = 0;
180 185
181 if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { 186 if (radix == 16 && SDL_strncmp (text, "0x", 2) == 0) {
182 text += 2; 187 text += 2;
183 } 188 }
184 for ( ; ; ) { 189 for (;;) {
185 int v; 190 int v;
186 if ( SDL_isdigit(*text) ) { 191 if (SDL_isdigit (*text)) {
187 v = *text - '0'; 192 v = *text - '0';
188 } else if ( radix == 16 && SDL_isupperhex(*text) ) { 193 } else if (radix == 16 && SDL_isupperhex (*text)) {
189 v = 10 + (*text - 'A'); 194 v = 10 + (*text - 'A');
190 } else if ( radix == 16 && SDL_islowerhex(*text) ) { 195 } else if (radix == 16 && SDL_islowerhex (*text)) {
191 v = 10 + (*text - 'a'); 196 v = 10 + (*text - 'a');
192 } else { 197 } else {
193 break; 198 break;
194 } 199 }
195 value *= radix; 200 value *= radix;
196 value += v; 201 value += v;
197 ++text; 202 ++text;
198 } 203 }
199 if ( valuep ) { 204 if (valuep) {
200 *valuep = value; 205 *valuep = value;
201 } 206 }
202 return (text - textstart); 207 return (text - textstart);
203 } 208 }
204 #endif 209 #endif
205 #endif /* SDL_HAS_64BIT_TYPE */ 210 #endif /* SDL_HAS_64BIT_TYPE */
206 211
207 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD) 212 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
208 static size_t SDL_ScanFloat(const char *text, double *valuep) 213 static size_t
214 SDL_ScanFloat (const char *text, double *valuep)
209 { 215 {
210 const char *textstart = text; 216 const char *textstart = text;
211 unsigned long lvalue = 0; 217 unsigned long lvalue = 0;
212 double value = 0.0; 218 double value = 0.0;
213 SDL_bool negative = SDL_FALSE; 219 SDL_bool negative = SDL_FALSE;
214 220
215 if ( *text == '-' ) { 221 if (*text == '-') {
216 negative = SDL_TRUE; 222 negative = SDL_TRUE;
217 ++text; 223 ++text;
218 } 224 }
219 text += SDL_ScanUnsignedLong(text, 10, &lvalue); 225 text += SDL_ScanUnsignedLong (text, 10, &lvalue);
220 value += lvalue; 226 value += lvalue;
221 if ( *text == '.' ) { 227 if (*text == '.') {
222 int mult = 10; 228 int mult = 10;
223 ++text; 229 ++text;
224 while ( SDL_isdigit(*text) ) { 230 while (SDL_isdigit (*text)) {
225 lvalue = *text - '0'; 231 lvalue = *text - '0';
226 value += (double)lvalue / mult; 232 value += (double) lvalue / mult;
227 mult *= 10; 233 mult *= 10;
228 ++text; 234 ++text;
229 } 235 }
230 } 236 }
231 if ( valuep ) { 237 if (valuep) {
232 if ( negative && value ) { 238 if (negative && value) {
233 *valuep = -value; 239 *valuep = -value;
234 } else { 240 } else {
235 *valuep = value; 241 *valuep = value;
236 } 242 }
237 } 243 }
238 return (text - textstart); 244 return (text - textstart);
239 } 245 }
240 #endif 246 #endif
241 247
242 #ifndef SDL_memset 248 #ifndef SDL_memset
243 void *SDL_memset(void *dst, int c, size_t len) 249 void *
250 SDL_memset (void *dst, int c, size_t len)
244 { 251 {
245 size_t left = (len % 4); 252 size_t left = (len % 4);
246 if ( len >= 4 ) { 253 if (len >= 4) {
247 Uint32 value = 0; 254 Uint32 value = 0;
248 Uint32 *dstp = (Uint32 *)dst; 255 Uint32 *dstp = (Uint32 *) dst;
249 int i; 256 int i;
250 for (i = 0; i < 4; ++i) { 257 for (i = 0; i < 4; ++i) {
251 value <<= 8; 258 value <<= 8;
252 value |= c; 259 value |= c;
253 } 260 }
254 len /= 4; 261 len /= 4;
255 while ( len-- ) { 262 while (len--) {
256 *dstp++ = value; 263 *dstp++ = value;
257 } 264 }
258 } 265 }
259 if ( left > 0 ) { 266 if (left > 0) {
260 Uint8 value = (Uint8)c; 267 Uint8 value = (Uint8) c;
261 Uint8 *dstp = (Uint8 *)dst; 268 Uint8 *dstp = (Uint8 *) dst;
262 switch(left) { 269 switch (left) {
263 case 3: 270 case 3:
264 *dstp++ = value; 271 *dstp++ = value;
265 case 2: 272 case 2:
266 *dstp++ = value; 273 *dstp++ = value;
267 case 1: 274 case 1:
268 *dstp++ = value; 275 *dstp++ = value;
269 } 276 }
270 } 277 }
271 return dst; 278 return dst;
272 } 279 }
273 #endif 280 #endif
274 281
275 #ifndef SDL_memcpy 282 #ifndef SDL_memcpy
276 void *SDL_memcpy(void *dst, const void *src, size_t len) 283 void *
277 { 284 SDL_memcpy (void *dst, const void *src, size_t len)
278 char *srcp = (char *)src; 285 {
279 char *dstp = (char *)dst; 286 char *srcp = (char *) src;
280 while ( len-- ) { 287 char *dstp = (char *) dst;
288 while (len--) {
281 *dstp++ = *srcp++; 289 *dstp++ = *srcp++;
282 } 290 }
283 return dst; 291 return dst;
284 } 292 }
285 #endif 293 #endif
286 294
287 #ifndef SDL_revcpy 295 #ifndef SDL_revcpy
288 void *SDL_revcpy(void *dst, const void *src, size_t len) 296 void *
289 { 297 SDL_revcpy (void *dst, const void *src, size_t len)
290 char *srcp = (char *)src; 298 {
291 char *dstp = (char *)dst; 299 char *srcp = (char *) src;
300 char *dstp = (char *) dst;
292 srcp += len; 301 srcp += len;
293 dstp += len; 302 dstp += len;
294 while ( len-- ) { 303 while (len--) {
295 *dstp-- = *srcp--; 304 *dstp-- = *srcp--;
296 } 305 }
297 return dst; 306 return dst;
298 } 307 }
299 #endif 308 #endif
300 309
301 #ifndef SDL_memcmp 310 #ifndef SDL_memcmp
302 int SDL_memcmp(const void *s1, const void *s2, size_t len) 311 int
303 { 312 SDL_memcmp (const void *s1, const void *s2, size_t len)
304 char *s1p = (char *)s1; 313 {
305 char *s2p = (char *)s2; 314 char *s1p = (char *) s1;
306 while ( len-- ) { 315 char *s2p = (char *) s2;
307 if ( *s1p != *s2p ) { 316 while (len--) {
317 if (*s1p != *s2p) {
308 return (*s1p - *s2p); 318 return (*s1p - *s2p);
309 } 319 }
310 ++s1p; 320 ++s1p;
311 ++s2p; 321 ++s2p;
312 } 322 }
313 return 0; 323 return 0;
314 } 324 }
315 #endif 325 #endif
316 326
317 #ifndef HAVE_STRLEN 327 #ifndef HAVE_STRLEN
318 size_t SDL_strlen(const char *string) 328 size_t
329 SDL_strlen (const char *string)
319 { 330 {
320 size_t len = 0; 331 size_t len = 0;
321 while ( *string++ ) { 332 while (*string++) {
322 ++len; 333 ++len;
323 } 334 }
324 return len; 335 return len;
325 } 336 }
326 #endif 337 #endif
327 338
328 #ifndef HAVE_STRLCPY 339 #ifndef HAVE_STRLCPY
329 size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) 340 size_t
330 { 341 SDL_strlcpy (char *dst, const char *src, size_t maxlen)
331 size_t srclen = SDL_strlen(src); 342 {
332 if ( maxlen > 0 ) { 343 size_t srclen = SDL_strlen (src);
333 size_t len = SDL_min(srclen, maxlen-1); 344 if (maxlen > 0) {
334 SDL_memcpy(dst, src, len); 345 size_t len = SDL_min (srclen, maxlen - 1);
346 SDL_memcpy (dst, src, len);
335 dst[len] = '\0'; 347 dst[len] = '\0';
336 } 348 }
337 return srclen; 349 return srclen;
338 } 350 }
339 #endif 351 #endif
340 352
341 #ifndef HAVE_STRLCAT 353 #ifndef HAVE_STRLCAT
342 size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) 354 size_t
343 { 355 SDL_strlcat (char *dst, const char *src, size_t maxlen)
344 size_t dstlen = SDL_strlen(dst); 356 {
345 size_t srclen = SDL_strlen(src); 357 size_t dstlen = SDL_strlen (dst);
346 if ( dstlen < maxlen ) { 358 size_t srclen = SDL_strlen (src);
347 SDL_strlcpy(dst+dstlen, src, maxlen-dstlen); 359 if (dstlen < maxlen) {
348 } 360 SDL_strlcpy (dst + dstlen, src, maxlen - dstlen);
349 return dstlen+srclen; 361 }
362 return dstlen + srclen;
350 } 363 }
351 #endif 364 #endif
352 365
353 #ifndef HAVE_STRDUP 366 #ifndef HAVE_STRDUP
354 char *SDL_strdup(const char *string) 367 char *
355 { 368 SDL_strdup (const char *string)
356 size_t len = SDL_strlen(string)+1; 369 {
357 char *newstr = SDL_malloc(len); 370 size_t len = SDL_strlen (string) + 1;
358 if ( newstr ) { 371 char *newstr = SDL_malloc (len);
359 SDL_strlcpy(newstr, string, len); 372 if (newstr) {
373 SDL_strlcpy (newstr, string, len);
360 } 374 }
361 return newstr; 375 return newstr;
362 } 376 }
363 #endif 377 #endif
364 378
365 #ifndef HAVE__STRREV 379 #ifndef HAVE__STRREV
366 char *SDL_strrev(char *string) 380 char *
367 { 381 SDL_strrev (char *string)
368 size_t len = SDL_strlen(string); 382 {
383 size_t len = SDL_strlen (string);
369 char *a = &string[0]; 384 char *a = &string[0];
370 char *b = &string[len-1]; 385 char *b = &string[len - 1];
371 len /= 2; 386 len /= 2;
372 while ( len-- ) { 387 while (len--) {
373 char c = *a; 388 char c = *a;
374 *a++ = *b; 389 *a++ = *b;
375 *b-- = c; 390 *b-- = c;
376 } 391 }
377 return string; 392 return string;
378 } 393 }
379 #endif 394 #endif
380 395
381 #ifndef HAVE__STRUPR 396 #ifndef HAVE__STRUPR
382 char *SDL_strupr(char *string) 397 char *
398 SDL_strupr (char *string)
383 { 399 {
384 char *bufp = string; 400 char *bufp = string;
385 while ( *bufp ) { 401 while (*bufp) {
386 *bufp = SDL_toupper(*bufp); 402 *bufp = SDL_toupper (*bufp);
387 ++bufp; 403 ++bufp;
388 } 404 }
389 return string; 405 return string;
390 } 406 }
391 #endif 407 #endif
392 408
393 #ifndef HAVE__STRLWR 409 #ifndef HAVE__STRLWR
394 char *SDL_strlwr(char *string) 410 char *
411 SDL_strlwr (char *string)
395 { 412 {
396 char *bufp = string; 413 char *bufp = string;
397 while ( *bufp ) { 414 while (*bufp) {
398 *bufp = SDL_tolower(*bufp); 415 *bufp = SDL_tolower (*bufp);
399 ++bufp; 416 ++bufp;
400 } 417 }
401 return string; 418 return string;
402 } 419 }
403 #endif 420 #endif
404 421
405 #ifndef HAVE_STRCHR 422 #ifndef HAVE_STRCHR
406 char *SDL_strchr(const char *string, int c) 423 char *
407 { 424 SDL_strchr (const char *string, int c)
408 while ( *string ) { 425 {
409 if ( *string == c ) { 426 while (*string) {
410 return (char *)string; 427 if (*string == c) {
411 } 428 return (char *) string;
412 ++string; 429 }
430 ++string;
413 } 431 }
414 return NULL; 432 return NULL;
415 } 433 }
416 #endif 434 #endif
417 435
418 #ifndef HAVE_STRRCHR 436 #ifndef HAVE_STRRCHR
419 char *SDL_strrchr(const char *string, int c) 437 char *
420 { 438 SDL_strrchr (const char *string, int c)
421 const char *bufp = string + SDL_strlen(string) - 1; 439 {
422 while ( bufp >= string ) { 440 const char *bufp = string + SDL_strlen (string) - 1;
423 if ( *bufp == c ) { 441 while (bufp >= string) {
424 return (char *)bufp; 442 if (*bufp == c) {
425 } 443 return (char *) bufp;
426 --bufp; 444 }
445 --bufp;
427 } 446 }
428 return NULL; 447 return NULL;
429 } 448 }
430 #endif 449 #endif
431 450
432 #ifndef HAVE_STRSTR 451 #ifndef HAVE_STRSTR
433 char *SDL_strstr(const char *haystack, const char *needle) 452 char *
434 { 453 SDL_strstr (const char *haystack, const char *needle)
435 size_t length = SDL_strlen(needle); 454 {
436 while ( *haystack ) { 455 size_t length = SDL_strlen (needle);
437 if ( SDL_strncmp(haystack, needle, length) == 0 ) { 456 while (*haystack) {
438 return (char *)haystack; 457 if (SDL_strncmp (haystack, needle, length) == 0) {
439 } 458 return (char *) haystack;
440 ++haystack; 459 }
460 ++haystack;
441 } 461 }
442 return NULL; 462 return NULL;
443 } 463 }
444 #endif 464 #endif
445 465
452 'U', 'V', 'W', 'X', 'Y', 'Z' 472 'U', 'V', 'W', 'X', 'Y', 'Z'
453 }; 473 };
454 #endif /* ntoa() conversion table */ 474 #endif /* ntoa() conversion table */
455 475
456 #ifndef HAVE__LTOA 476 #ifndef HAVE__LTOA
457 char *SDL_ltoa(long value, char *string, int radix) 477 char *
478 SDL_ltoa (long value, char *string, int radix)
458 { 479 {
459 char *bufp = string; 480 char *bufp = string;
460 481
461 if ( value < 0 ) { 482 if (value < 0) {
462 *bufp++ = '-'; 483 *bufp++ = '-';
463 value = -value; 484 value = -value;
464 } 485 }
465 if ( value ) { 486 if (value) {
466 while ( value > 0 ) { 487 while (value > 0) {
467 *bufp++ = ntoa_table[value % radix]; 488 *bufp++ = ntoa_table[value % radix];
468 value /= radix; 489 value /= radix;
469 } 490 }
470 } else { 491 } else {
471 *bufp++ = '0'; 492 *bufp++ = '0';
472 } 493 }
473 *bufp = '\0'; 494 *bufp = '\0';
474 495
475 /* The numbers went into the string backwards. :) */ 496 /* The numbers went into the string backwards. :) */
476 if ( *string == '-' ) { 497 if (*string == '-') {
477 SDL_strrev(string+1); 498 SDL_strrev (string + 1);
478 } else { 499 } else {
479 SDL_strrev(string); 500 SDL_strrev (string);
480 } 501 }
481 502
482 return string; 503 return string;
483 } 504 }
484 #endif 505 #endif
485 506
486 #ifndef HAVE__ULTOA 507 #ifndef HAVE__ULTOA
487 char *SDL_ultoa(unsigned long value, char *string, int radix) 508 char *
509 SDL_ultoa (unsigned long value, char *string, int radix)
488 { 510 {
489 char *bufp = string; 511 char *bufp = string;
490 512
491 if ( value ) { 513 if (value) {
492 while ( value > 0 ) { 514 while (value > 0) {
493 *bufp++ = ntoa_table[value % radix]; 515 *bufp++ = ntoa_table[value % radix];
494 value /= radix; 516 value /= radix;
495 } 517 }
496 } else { 518 } else {
497 *bufp++ = '0'; 519 *bufp++ = '0';
498 } 520 }
499 *bufp = '\0'; 521 *bufp = '\0';
500 522
501 /* The numbers went into the string backwards. :) */ 523 /* The numbers went into the string backwards. :) */
502 SDL_strrev(string); 524 SDL_strrev (string);
503 525
504 return string; 526 return string;
505 } 527 }
506 #endif 528 #endif
507 529
508 #ifndef HAVE_STRTOL 530 #ifndef HAVE_STRTOL
509 long SDL_strtol(const char *string, char **endp, int base) 531 long
532 SDL_strtol (const char *string, char **endp, int base)
510 { 533 {
511 size_t len; 534 size_t len;
512 long value; 535 long value;
513 536
514 len = SDL_ScanLong(string, base ? base : 10, &value); 537 len = SDL_ScanLong (string, base ? base : 10, &value);
515 if ( endp ) { 538 if (endp) {
516 *endp = (char *)string + len; 539 *endp = (char *) string + len;
517 } 540 }
518 return value; 541 return value;
519 } 542 }
520 #endif 543 #endif
521 544
522 #ifndef HAVE_STRTOUL 545 #ifndef HAVE_STRTOUL
523 unsigned long SDL_strtoul(const char *string, char **endp, int base) 546 unsigned long
547 SDL_strtoul (const char *string, char **endp, int base)
524 { 548 {
525 size_t len; 549 size_t len;
526 unsigned long value; 550 unsigned long value;
527 551
528 len = SDL_ScanUnsignedLong(string, base ? base : 10, &value); 552 len = SDL_ScanUnsignedLong (string, base ? base : 10, &value);
529 if ( endp ) { 553 if (endp) {
530 *endp = (char *)string + len; 554 *endp = (char *) string + len;
531 } 555 }
532 return value; 556 return value;
533 } 557 }
534 #endif 558 #endif
535 559
536 #ifdef SDL_HAS_64BIT_TYPE 560 #ifdef SDL_HAS_64BIT_TYPE
537 561
538 #ifndef HAVE__I64TOA 562 #ifndef HAVE__I64TOA
539 char *SDL_lltoa(Sint64 value, char *string, int radix) 563 char *
564 SDL_lltoa (Sint64 value, char *string, int radix)
540 { 565 {
541 char *bufp = string; 566 char *bufp = string;
542 567
543 if ( value < 0 ) { 568 if (value < 0) {
544 *bufp++ = '-'; 569 *bufp++ = '-';
545 value = -value; 570 value = -value;
546 } 571 }
547 if ( value ) { 572 if (value) {
548 while ( value > 0 ) { 573 while (value > 0) {
549 *bufp++ = ntoa_table[value % radix]; 574 *bufp++ = ntoa_table[value % radix];
550 value /= radix; 575 value /= radix;
551 } 576 }
552 } else { 577 } else {
553 *bufp++ = '0'; 578 *bufp++ = '0';
554 } 579 }
555 *bufp = '\0'; 580 *bufp = '\0';
556 581
557 /* The numbers went into the string backwards. :) */ 582 /* The numbers went into the string backwards. :) */
558 if ( *string == '-' ) { 583 if (*string == '-') {
559 SDL_strrev(string+1); 584 SDL_strrev (string + 1);
560 } else { 585 } else {
561 SDL_strrev(string); 586 SDL_strrev (string);
562 } 587 }
563 588
564 return string; 589 return string;
565 } 590 }
566 #endif 591 #endif
567 592
568 #ifndef HAVE__UI64TOA 593 #ifndef HAVE__UI64TOA
569 char *SDL_ulltoa(Uint64 value, char *string, int radix) 594 char *
595 SDL_ulltoa (Uint64 value, char *string, int radix)
570 { 596 {
571 char *bufp = string; 597 char *bufp = string;
572 598
573 if ( value ) { 599 if (value) {
574 while ( value > 0 ) { 600 while (value > 0) {
575 *bufp++ = ntoa_table[value % radix]; 601 *bufp++ = ntoa_table[value % radix];
576 value /= radix; 602 value /= radix;
577 } 603 }
578 } else { 604 } else {
579 *bufp++ = '0'; 605 *bufp++ = '0';
580 } 606 }
581 *bufp = '\0'; 607 *bufp = '\0';
582 608
583 /* The numbers went into the string backwards. :) */ 609 /* The numbers went into the string backwards. :) */
584 SDL_strrev(string); 610 SDL_strrev (string);
585 611
586 return string; 612 return string;
587 } 613 }
588 #endif 614 #endif
589 615
590 #ifndef HAVE_STRTOLL 616 #ifndef HAVE_STRTOLL
591 Sint64 SDL_strtoll(const char *string, char **endp, int base) 617 Sint64
618 SDL_strtoll (const char *string, char **endp, int base)
592 { 619 {
593 size_t len; 620 size_t len;
594 Sint64 value; 621 Sint64 value;
595 622
596 len = SDL_ScanLongLong(string, base ? base : 10, &value); 623 len = SDL_ScanLongLong (string, base ? base : 10, &value);
597 if ( endp ) { 624 if (endp) {
598 *endp = (char *)string + len; 625 *endp = (char *) string + len;
599 } 626 }
600 return value; 627 return value;
601 } 628 }
602 #endif 629 #endif
603 630
604 #ifndef HAVE_STRTOULL 631 #ifndef HAVE_STRTOULL
605 Uint64 SDL_strtoull(const char *string, char **endp, int base) 632 Uint64
633 SDL_strtoull (const char *string, char **endp, int base)
606 { 634 {
607 size_t len; 635 size_t len;
608 Uint64 value; 636 Uint64 value;
609 637
610 len = SDL_ScanUnsignedLongLong(string, base ? base : 10, &value); 638 len = SDL_ScanUnsignedLongLong (string, base ? base : 10, &value);
611 if ( endp ) { 639 if (endp) {
612 *endp = (char *)string + len; 640 *endp = (char *) string + len;
613 } 641 }
614 return value; 642 return value;
615 } 643 }
616 #endif 644 #endif
617 645
618 #endif /* SDL_HAS_64BIT_TYPE */ 646 #endif /* SDL_HAS_64BIT_TYPE */
619 647
620 #ifndef HAVE_STRTOD 648 #ifndef HAVE_STRTOD
621 double SDL_strtod(const char *string, char **endp) 649 double
650 SDL_strtod (const char *string, char **endp)
622 { 651 {
623 size_t len; 652 size_t len;
624 double value; 653 double value;
625 654
626 len = SDL_ScanFloat(string, &value); 655 len = SDL_ScanFloat (string, &value);
627 if ( endp ) { 656 if (endp) {
628 *endp = (char *)string + len; 657 *endp = (char *) string + len;
629 } 658 }
630 return value; 659 return value;
631 } 660 }
632 #endif 661 #endif
633 662
634 #ifndef HAVE_STRCMP 663 #ifndef HAVE_STRCMP
635 int SDL_strcmp(const char *str1, const char *str2) 664 int
665 SDL_strcmp (const char *str1, const char *str2)
636 { 666 {
637 while (*str1 && *str2) { 667 while (*str1 && *str2) {
638 if ( *str1 != *str2 ) 668 if (*str1 != *str2)
639 break; 669 break;
640 ++str1; 670 ++str1;
641 ++str2; 671 ++str2;
642 } 672 }
643 return (int)((unsigned char)*str1 - (unsigned char)*str2); 673 return (int) ((unsigned char) *str1 - (unsigned char) *str2);
644 } 674 }
645 #endif 675 #endif
646 676
647 #ifndef HAVE_STRNCMP 677 #ifndef HAVE_STRNCMP
648 int SDL_strncmp(const char *str1, const char *str2, size_t maxlen) 678 int
649 { 679 SDL_strncmp (const char *str1, const char *str2, size_t maxlen)
650 while ( *str1 && *str2 && maxlen ) { 680 {
651 if ( *str1 != *str2 ) 681 while (*str1 && *str2 && maxlen) {
682 if (*str1 != *str2)
652 break; 683 break;
653 ++str1; 684 ++str1;
654 ++str2; 685 ++str2;
655 --maxlen; 686 --maxlen;
656 } 687 }
657 if ( ! maxlen ) { 688 if (!maxlen) {
658 return 0; 689 return 0;
659 } 690 }
660 return (int)((unsigned char)*str1 - (unsigned char)*str2); 691 return (int) ((unsigned char) *str1 - (unsigned char) *str2);
661 } 692 }
662 #endif 693 #endif
663 694
664 #if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP) 695 #if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
665 int SDL_strcasecmp(const char *str1, const char *str2) 696 int
697 SDL_strcasecmp (const char *str1, const char *str2)
666 { 698 {
667 char a = 0; 699 char a = 0;
668 char b = 0; 700 char b = 0;
669 while ( *str1 && *str2 ) { 701 while (*str1 && *str2) {
670 a = SDL_tolower(*str1); 702 a = SDL_tolower (*str1);
671 b = SDL_tolower(*str2); 703 b = SDL_tolower (*str2);
672 if ( a != b ) 704 if (a != b)
673 break; 705 break;
674 ++str1; 706 ++str1;
675 ++str2; 707 ++str2;
676 } 708 }
677 return (int)((unsigned char)a - (unsigned char)b); 709 return (int) ((unsigned char) a - (unsigned char) b);
678 } 710 }
679 #endif 711 #endif
680 712
681 #if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP) 713 #if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
682 int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen) 714 int
715 SDL_strncasecmp (const char *str1, const char *str2, size_t maxlen)
683 { 716 {
684 char a = 0; 717 char a = 0;
685 char b = 0; 718 char b = 0;
686 while ( *str1 && *str2 && maxlen ) { 719 while (*str1 && *str2 && maxlen) {
687 a = SDL_tolower(*str1); 720 a = SDL_tolower (*str1);
688 b = SDL_tolower(*str2); 721 b = SDL_tolower (*str2);
689 if ( a != b ) 722 if (a != b)
690 break; 723 break;
691 ++str1; 724 ++str1;
692 ++str2; 725 ++str2;
693 --maxlen; 726 --maxlen;
694 } 727 }
695 return (int)((unsigned char)a - (unsigned char)b); 728 return (int) ((unsigned char) a - (unsigned char) b);
696 } 729 }
697 #endif 730 #endif
698 731
699 #ifndef HAVE_SSCANF 732 #ifndef HAVE_SSCANF
700 int SDL_sscanf(const char *text, const char *fmt, ...) 733 int
734 SDL_sscanf (const char *text, const char *fmt, ...)
701 { 735 {
702 va_list ap; 736 va_list ap;
703 int retval = 0; 737 int retval = 0;
704 738
705 va_start(ap, fmt); 739 va_start (ap, fmt);
706 while ( *fmt ) { 740 while (*fmt) {
707 if ( *fmt == ' ' ) { 741 if (*fmt == ' ') {
708 while ( SDL_isspace(*text) ) { 742 while (SDL_isspace (*text)) {
709 ++text; 743 ++text;
710 } 744 }
711 ++fmt; 745 ++fmt;
712 continue; 746 continue;
713 } 747 }
714 if ( *fmt == '%' ) { 748 if (*fmt == '%') {
715 SDL_bool done = SDL_FALSE; 749 SDL_bool done = SDL_FALSE;
716 long count = 0; 750 long count = 0;
717 int radix = 10; 751 int radix = 10;
718 enum { 752 enum
753 {
719 DO_SHORT, 754 DO_SHORT,
720 DO_INT, 755 DO_INT,
721 DO_LONG, 756 DO_LONG,
722 DO_LONGLONG 757 DO_LONGLONG
723 } inttype = DO_INT; 758 } inttype = DO_INT;
724 SDL_bool suppress = SDL_FALSE; 759 SDL_bool suppress = SDL_FALSE;
725 760
726 ++fmt; 761 ++fmt;
727 if ( *fmt == '%' ) { 762 if (*fmt == '%') {
728 if ( *text == '%' ) { 763 if (*text == '%') {
729 ++text; 764 ++text;
730 ++fmt; 765 ++fmt;
731 continue; 766 continue;
732 } 767 }
733 break; 768 break;
734 } 769 }
735 if ( *fmt == '*' ) { 770 if (*fmt == '*') {
736 suppress = SDL_TRUE; 771 suppress = SDL_TRUE;
737 ++fmt; 772 ++fmt;
738 } 773 }
739 fmt += SDL_ScanLong(fmt, 10, &count); 774 fmt += SDL_ScanLong (fmt, 10, &count);
740 775
741 if ( *fmt == 'c' ) { 776 if (*fmt == 'c') {
742 if ( ! count ) { 777 if (!count) {
743 count = 1; 778 count = 1;
744 } 779 }
745 if ( suppress ) { 780 if (suppress) {
746 while ( count-- ) { 781 while (count--) {
747 ++text; 782 ++text;
748 } 783 }
749 } else { 784 } else {
750 char *valuep = va_arg(ap, char*); 785 char *valuep = va_arg (ap, char *);
751 while ( count-- ) { 786 while (count--) {
752 *valuep++ = *text++; 787 *valuep++ = *text++;
753 } 788 }
754 ++retval; 789 ++retval;
755 } 790 }
756 continue; 791 continue;
757 } 792 }
758 793
759 while ( SDL_isspace(*text) ) { 794 while (SDL_isspace (*text)) {
760 ++text; 795 ++text;
761 } 796 }
762 797
763 /* FIXME: implement more of the format specifiers */ 798 /* FIXME: implement more of the format specifiers */
764 while (!done) { 799 while (!done) {
765 switch(*fmt) { 800 switch (*fmt) {
766 case '*': 801 case '*':
767 suppress = SDL_TRUE; 802 suppress = SDL_TRUE;
768 break; 803 break;
769 case 'h': 804 case 'h':
770 if ( inttype > DO_SHORT ) { 805 if (inttype > DO_SHORT) {
771 ++inttype; 806 ++inttype;
807 }
808 break;
809 case 'l':
810 if (inttype < DO_LONGLONG) {
811 ++inttype;
812 }
813 break;
814 case 'I':
815 if (SDL_strncmp (fmt, "I64", 3) == 0) {
816 fmt += 2;
817 inttype = DO_LONGLONG;
818 }
819 break;
820 case 'i':
821 {
822 int index = 0;
823 if (text[index] == '-') {
824 ++index;
772 } 825 }
773 break; 826 if (text[index] == '0') {
774 case 'l': 827 if (SDL_tolower (text[index + 1]) == 'x') {
775 if ( inttype < DO_LONGLONG ) { 828 radix = 16;
776 ++inttype; 829 } else {
830 radix = 8;
831 }
777 } 832 }
778 break; 833 }
779 case 'I': 834 /* Fall through to %d handling */
780 if ( SDL_strncmp(fmt, "I64", 3) == 0 ) { 835 case 'd':
781 fmt += 2; 836 #ifdef SDL_HAS_64BIT_TYPE
782 inttype = DO_LONGLONG; 837 if (inttype == DO_LONGLONG) {
838 Sint64 value;
839 text += SDL_ScanLongLong (text, radix, &value);
840 if (!suppress) {
841 Sint64 *valuep = va_arg (ap, Sint64 *);
842 *valuep = value;
843 ++retval;
783 } 844 }
784 break; 845 } else
785 case 'i': 846 #endif /* SDL_HAS_64BIT_TYPE */
786 { 847 {
787 int index = 0; 848 long value;
788 if ( text[index] == '-' ) { 849 text += SDL_ScanLong (text, radix, &value);
789 ++index; 850 if (!suppress) {
851 switch (inttype) {
852 case DO_SHORT:
853 {
854 short *valuep = va_arg (ap, short *);
855 *valuep = (short) value;
856 }
857 break;
858 case DO_INT:
859 {
860 int *valuep = va_arg (ap, int *);
861 *valuep = (int) value;
862 }
863 break;
864 case DO_LONG:
865 {
866 long *valuep = va_arg (ap, long *);
867 *valuep = value;
868 }
869 break;
870 case DO_LONGLONG:
871 /* Handled above */
872 break;
790 } 873 }
791 if ( text[index] == '0' ) { 874 ++retval;
792 if ( SDL_tolower(text[index+1]) == 'x' ) { 875 }
793 radix = 16; 876 }
794 } else { 877 done = SDL_TRUE;
795 radix = 8; 878 break;
879 case 'o':
880 if (radix == 10) {
881 radix = 8;
882 }
883 /* Fall through to unsigned handling */
884 case 'x':
885 case 'X':
886 if (radix == 10) {
887 radix = 16;
888 }
889 /* Fall through to unsigned handling */
890 case 'u':
891 #ifdef SDL_HAS_64BIT_TYPE
892 if (inttype == DO_LONGLONG) {
893 Uint64 value;
894 text +=
895 SDL_ScanUnsignedLongLong (text, radix, &value);
896 if (!suppress) {
897 Uint64 *valuep = va_arg (ap, Uint64 *);
898 *valuep = value;
899 ++retval;
900 }
901 } else
902 #endif /* SDL_HAS_64BIT_TYPE */
903 {
904 unsigned long value;
905 text += SDL_ScanUnsignedLong (text, radix, &value);
906 if (!suppress) {
907 switch (inttype) {
908 case DO_SHORT:
909 {
910 short *valuep = va_arg (ap, short *);
911 *valuep = (short) value;
912 }
913 break;
914 case DO_INT:
915 {
916 int *valuep = va_arg (ap, int *);
917 *valuep = (int) value;
918 }
919 break;
920 case DO_LONG:
921 {
922 long *valuep = va_arg (ap, long *);
923 *valuep = value;
924 }
925 break;
926 case DO_LONGLONG:
927 /* Handled above */
928 break;
929 }
930 ++retval;
931 }
932 }
933 done = SDL_TRUE;
934 break;
935 case 'p':
936 {
937 uintptr_t value;
938 text += SDL_ScanUintPtrT (text, 16, &value);
939 if (!suppress) {
940 void **valuep = va_arg (ap, void **);
941 *valuep = (void *) value;
942 ++retval;
943 }
944 }
945 done = SDL_TRUE;
946 break;
947 case 'f':
948 {
949 double value;
950 text += SDL_ScanFloat (text, &value);
951 if (!suppress) {
952 float *valuep = va_arg (ap, float *);
953 *valuep = (float) value;
954 ++retval;
955 }
956 }
957 done = SDL_TRUE;
958 break;
959 case 's':
960 if (suppress) {
961 while (!SDL_isspace (*text)) {
962 ++text;
963 if (count) {
964 if (--count == 0) {
965 break;
796 } 966 }
797 } 967 }
798 } 968 }
799 /* Fall through to %d handling */ 969 } else {
800 case 'd': 970 char *valuep = va_arg (ap, char *);
801 #ifdef SDL_HAS_64BIT_TYPE 971 while (!SDL_isspace (*text)) {
802 if ( inttype == DO_LONGLONG ) { 972 *valuep++ = *text++;
803 Sint64 value; 973 if (count) {
804 text += SDL_ScanLongLong(text, radix, &value); 974 if (--count == 0) {
805 if ( ! suppress ) { 975 break;
806 Sint64 *valuep = va_arg(ap, Sint64*); 976 }
807 *valuep = value;
808 ++retval;
809 } 977 }
810 } 978 }
811 else 979 *valuep = '\0';
812 #endif /* SDL_HAS_64BIT_TYPE */ 980 ++retval;
813 { 981 }
814 long value; 982 done = SDL_TRUE;
815 text += SDL_ScanLong(text, radix, &value); 983 break;
816 if ( ! suppress ) { 984 default:
817 switch (inttype) { 985 done = SDL_TRUE;
818 case DO_SHORT: 986 break;
819 { short* valuep = va_arg(ap, short*);
820 *valuep = (short)value;
821 }
822 break;
823 case DO_INT:
824 { int* valuep = va_arg(ap, int*);
825 *valuep = (int)value;
826 }
827 break;
828 case DO_LONG:
829 { long* valuep = va_arg(ap, long*);
830 *valuep = value;
831 }
832 break;
833 case DO_LONGLONG:
834 /* Handled above */
835 break;
836 }
837 ++retval;
838 }
839 }
840 done = SDL_TRUE;
841 break;
842 case 'o':
843 if ( radix == 10 ) {
844 radix = 8;
845 }
846 /* Fall through to unsigned handling */
847 case 'x':
848 case 'X':
849 if ( radix == 10 ) {
850 radix = 16;
851 }
852 /* Fall through to unsigned handling */
853 case 'u':
854 #ifdef SDL_HAS_64BIT_TYPE
855 if ( inttype == DO_LONGLONG ) {
856 Uint64 value;
857 text += SDL_ScanUnsignedLongLong(text, radix, &value);
858 if ( ! suppress ) {
859 Uint64 *valuep = va_arg(ap, Uint64*);
860 *valuep = value;
861 ++retval;
862 }
863 }
864 else
865 #endif /* SDL_HAS_64BIT_TYPE */
866 {
867 unsigned long value;
868 text += SDL_ScanUnsignedLong(text, radix, &value);
869 if ( ! suppress ) {
870 switch (inttype) {
871 case DO_SHORT:
872 { short* valuep = va_arg(ap, short*);
873 *valuep = (short)value;
874 }
875 break;
876 case DO_INT:
877 { int* valuep = va_arg(ap, int*);
878 *valuep = (int)value;
879 }
880 break;
881 case DO_LONG:
882 { long* valuep = va_arg(ap, long*);
883 *valuep = value;
884 }
885 break;
886 case DO_LONGLONG:
887 /* Handled above */
888 break;
889 }
890 ++retval;
891 }
892 }
893 done = SDL_TRUE;
894 break;
895 case 'p':
896 {
897 uintptr_t value;
898 text += SDL_ScanUintPtrT(text, 16, &value);
899 if ( ! suppress ) {
900 void** valuep = va_arg(ap, void**);
901 *valuep = (void*)value;
902 ++retval;
903 }
904 }
905 done = SDL_TRUE;
906 break;
907 case 'f':
908 {
909 double value;
910 text += SDL_ScanFloat(text, &value);
911 if ( ! suppress ) {
912 float* valuep = va_arg(ap, float*);
913 *valuep = (float)value;
914 ++retval;
915 }
916 }
917 done = SDL_TRUE;
918 break;
919 case 's':
920 if ( suppress ) {
921 while ( !SDL_isspace(*text) ) {
922 ++text;
923 if ( count ) {
924 if ( --count == 0 ) {
925 break;
926 }
927 }
928 }
929 } else {
930 char *valuep = va_arg(ap, char*);
931 while ( !SDL_isspace(*text) ) {
932 *valuep++ = *text++;
933 if ( count ) {
934 if ( --count == 0 ) {
935 break;
936 }
937 }
938 }
939 *valuep = '\0';
940 ++retval;
941 }
942 done = SDL_TRUE;
943 break;
944 default:
945 done = SDL_TRUE;
946 break;
947 } 987 }
948 ++fmt; 988 ++fmt;
949 } 989 }
950 continue; 990 continue;
951 } 991 }
952 if ( *text == *fmt ) { 992 if (*text == *fmt) {
953 ++text; 993 ++text;
954 ++fmt; 994 ++fmt;
955 continue; 995 continue;
956 } 996 }
957 /* Text didn't match format specifier */ 997 /* Text didn't match format specifier */
958 break; 998 break;
959 } 999 }
960 va_end(ap); 1000 va_end (ap);
961 1001
962 return retval; 1002 return retval;
963 } 1003 }
964 #endif 1004 #endif
965 1005
966 #ifndef HAVE_SNPRINTF 1006 #ifndef HAVE_SNPRINTF
967 int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...) 1007 int
1008 SDL_snprintf (char *text, size_t maxlen, const char *fmt, ...)
968 { 1009 {
969 va_list ap; 1010 va_list ap;
970 int retval; 1011 int retval;
971 1012
972 va_start(ap, fmt); 1013 va_start (ap, fmt);
973 retval = SDL_vsnprintf(text, maxlen, fmt, ap); 1014 retval = SDL_vsnprintf (text, maxlen, fmt, ap);
974 va_end(ap); 1015 va_end (ap);
975 1016
976 return retval; 1017 return retval;
977 } 1018 }
978 #endif 1019 #endif
979 1020
980 #ifndef HAVE_VSNPRINTF 1021 #ifndef HAVE_VSNPRINTF
981 static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen) 1022 static size_t
1023 SDL_PrintLong (char *text, long value, int radix, size_t maxlen)
982 { 1024 {
983 char num[130]; 1025 char num[130];
984 size_t size; 1026 size_t size;
985 1027
986 SDL_ltoa(value, num, radix); 1028 SDL_ltoa (value, num, radix);
987 size = SDL_strlen(num); 1029 size = SDL_strlen (num);
988 if ( size >= maxlen ) { 1030 if (size >= maxlen) {
989 size = maxlen-1; 1031 size = maxlen - 1;
990 } 1032 }
991 SDL_strlcpy(text, num, size+1); 1033 SDL_strlcpy (text, num, size + 1);
992 1034
993 return size; 1035 return size;
994 } 1036 }
995 static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen) 1037
1038 static size_t
1039 SDL_PrintUnsignedLong (char *text, unsigned long value, int radix,
1040 size_t maxlen)
996 { 1041 {
997 char num[130]; 1042 char num[130];
998 size_t size; 1043 size_t size;
999 1044
1000 SDL_ultoa(value, num, radix); 1045 SDL_ultoa (value, num, radix);
1001 size = SDL_strlen(num); 1046 size = SDL_strlen (num);
1002 if ( size >= maxlen ) { 1047 if (size >= maxlen) {
1003 size = maxlen-1; 1048 size = maxlen - 1;
1004 } 1049 }
1005 SDL_strlcpy(text, num, size+1); 1050 SDL_strlcpy (text, num, size + 1);
1006 1051
1007 return size; 1052 return size;
1008 } 1053 }
1054
1009 #ifdef SDL_HAS_64BIT_TYPE 1055 #ifdef SDL_HAS_64BIT_TYPE
1010 static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen) 1056 static size_t
1057 SDL_PrintLongLong (char *text, Sint64 value, int radix, size_t maxlen)
1011 { 1058 {
1012 char num[130]; 1059 char num[130];
1013 size_t size; 1060 size_t size;
1014 1061
1015 SDL_lltoa(value, num, radix); 1062 SDL_lltoa (value, num, radix);
1016 size = SDL_strlen(num); 1063 size = SDL_strlen (num);
1017 if ( size >= maxlen ) { 1064 if (size >= maxlen) {
1018 size = maxlen-1; 1065 size = maxlen - 1;
1019 } 1066 }
1020 SDL_strlcpy(text, num, size+1); 1067 SDL_strlcpy (text, num, size + 1);
1021 1068
1022 return size; 1069 return size;
1023 } 1070 }
1024 static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen) 1071
1072 static size_t
1073 SDL_PrintUnsignedLongLong (char *text, Uint64 value, int radix, size_t maxlen)
1025 { 1074 {
1026 char num[130]; 1075 char num[130];
1027 size_t size; 1076 size_t size;
1028 1077
1029 SDL_ulltoa(value, num, radix); 1078 SDL_ulltoa (value, num, radix);
1030 size = SDL_strlen(num); 1079 size = SDL_strlen (num);
1031 if ( size >= maxlen ) { 1080 if (size >= maxlen) {
1032 size = maxlen-1; 1081 size = maxlen - 1;
1033 } 1082 }
1034 SDL_strlcpy(text, num, size+1); 1083 SDL_strlcpy (text, num, size + 1);
1035 1084
1036 return size; 1085 return size;
1037 } 1086 }
1038 #endif /* SDL_HAS_64BIT_TYPE */ 1087 #endif /* SDL_HAS_64BIT_TYPE */
1039 static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen) 1088 static size_t
1089 SDL_PrintFloat (char *text, double arg, size_t maxlen)
1040 { 1090 {
1041 char *textstart = text; 1091 char *textstart = text;
1042 if ( arg ) { 1092 if (arg) {
1043 /* This isn't especially accurate, but hey, it's easy. :) */ 1093 /* This isn't especially accurate, but hey, it's easy. :) */
1044 const double precision = 0.00000001; 1094 const double precision = 0.00000001;
1045 size_t len; 1095 size_t len;
1046 unsigned long value; 1096 unsigned long value;
1047 1097
1048 if ( arg < 0 ) { 1098 if (arg < 0) {
1049 *text++ = '-'; 1099 *text++ = '-';
1050 --maxlen; 1100 --maxlen;
1051 arg = -arg; 1101 arg = -arg;
1052 } 1102 }
1053 value = (unsigned long)arg; 1103 value = (unsigned long) arg;
1054 len = SDL_PrintUnsignedLong(text, value, 10, maxlen); 1104 len = SDL_PrintUnsignedLong (text, value, 10, maxlen);
1055 text += len; 1105 text += len;
1056 maxlen -= len; 1106 maxlen -= len;
1057 arg -= value; 1107 arg -= value;
1058 if ( arg > precision && maxlen ) { 1108 if (arg > precision && maxlen) {
1059 int mult = 10; 1109 int mult = 10;
1060 *text++ = '.'; 1110 *text++ = '.';
1061 while ( (arg > precision) && maxlen ) { 1111 while ((arg > precision) && maxlen) {
1062 value = (unsigned long)(arg * mult); 1112 value = (unsigned long) (arg * mult);
1063 len = SDL_PrintUnsignedLong(text, value, 10, maxlen); 1113 len = SDL_PrintUnsignedLong (text, value, 10, maxlen);
1064 text += len; 1114 text += len;
1065 maxlen -= len; 1115 maxlen -= len;
1066 arg -= (double)value / mult; 1116 arg -= (double) value / mult;
1067 mult *= 10; 1117 mult *= 10;
1068 } 1118 }
1069 } 1119 }
1070 } else { 1120 } else {
1071 *text++ = '0'; 1121 *text++ = '0';
1072 } 1122 }
1073 return (text - textstart); 1123 return (text - textstart);
1074 } 1124 }
1075 static size_t SDL_PrintString(char *text, const char *string, size_t maxlen) 1125
1126 static size_t
1127 SDL_PrintString (char *text, const char *string, size_t maxlen)
1076 { 1128 {
1077 char *textstart = text; 1129 char *textstart = text;
1078 while ( *string && maxlen-- ) { 1130 while (*string && maxlen--) {
1079 *text++ = *string++; 1131 *text++ = *string++;
1080 } 1132 }
1081 return (text - textstart); 1133 return (text - textstart);
1082 } 1134 }
1083 int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap) 1135
1136 int
1137 SDL_vsnprintf (char *text, size_t maxlen, const char *fmt, va_list ap)
1084 { 1138 {
1085 char *textstart = text; 1139 char *textstart = text;
1086 if ( maxlen <= 0 ) { 1140 if (maxlen <= 0) {
1087 return 0; 1141 return 0;
1088 } 1142 }
1089 --maxlen; /* For the trailing '\0' */ 1143 --maxlen; /* For the trailing '\0' */
1090 while ( *fmt && maxlen ) { 1144 while (*fmt && maxlen) {
1091 if ( *fmt == '%' ) { 1145 if (*fmt == '%') {
1092 SDL_bool done = SDL_FALSE; 1146 SDL_bool done = SDL_FALSE;
1093 size_t len = 0; 1147 size_t len = 0;
1094 SDL_bool do_lowercase = SDL_FALSE; 1148 SDL_bool do_lowercase = SDL_FALSE;
1095 int radix = 10; 1149 int radix = 10;
1096 enum { 1150 enum
1151 {
1097 DO_INT, 1152 DO_INT,
1098 DO_LONG, 1153 DO_LONG,
1099 DO_LONGLONG 1154 DO_LONGLONG
1100 } inttype = DO_INT; 1155 } inttype = DO_INT;
1101 1156
1102 ++fmt; 1157 ++fmt;
1103 /* FIXME: implement more of the format specifiers */ 1158 /* FIXME: implement more of the format specifiers */
1104 while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) { 1159 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
1105 ++fmt; 1160 ++fmt;
1106 } 1161 }
1107 while (!done) { 1162 while (!done) {
1108 switch(*fmt) { 1163 switch (*fmt) {
1109 case '%': 1164 case '%':
1110 *text = '%'; 1165 *text = '%';
1111 len = 1; 1166 len = 1;
1112 done = SDL_TRUE; 1167 done = SDL_TRUE;
1168 break;
1169 case 'c':
1170 /* char is promoted to int when passed through (...) */
1171 *text = (char) va_arg (ap, int);
1172 len = 1;
1173 done = SDL_TRUE;
1174 break;
1175 case 'h':
1176 /* short is promoted to int when passed through (...) */
1177 break;
1178 case 'l':
1179 if (inttype < DO_LONGLONG) {
1180 ++inttype;
1181 }
1182 break;
1183 case 'I':
1184 if (SDL_strncmp (fmt, "I64", 3) == 0) {
1185 fmt += 2;
1186 inttype = DO_LONGLONG;
1187 }
1188 break;
1189 case 'i':
1190 case 'd':
1191 switch (inttype) {
1192 case DO_INT:
1193 len =
1194 SDL_PrintLong (text,
1195 (long) va_arg (ap, int),
1196 radix, maxlen);
1113 break; 1197 break;
1114 case 'c': 1198 case DO_LONG:
1115 /* char is promoted to int when passed through (...) */ 1199 len =
1116 *text = (char)va_arg(ap, int); 1200 SDL_PrintLong (text, va_arg (ap, long),
1117 len = 1; 1201 radix, maxlen);
1118 done = SDL_TRUE;
1119 break; 1202 break;
1120 case 'h': 1203 case DO_LONGLONG:
1121 /* short is promoted to int when passed through (...) */ 1204 #ifdef SDL_HAS_64BIT_TYPE
1205 len =
1206 SDL_PrintLongLong (text,
1207 va_arg (ap, Sint64),
1208 radix, maxlen);
1209 #else
1210 len =
1211 SDL_PrintLong (text, va_arg (ap, long),
1212 radix, maxlen);
1213 #endif
1122 break; 1214 break;
1123 case 'l': 1215 }
1124 if ( inttype < DO_LONGLONG ) { 1216 done = SDL_TRUE;
1125 ++inttype; 1217 break;
1126 } 1218 case 'p':
1219 case 'x':
1220 do_lowercase = SDL_TRUE;
1221 /* Fall through to 'X' handling */
1222 case 'X':
1223 if (radix == 10) {
1224 radix = 16;
1225 }
1226 if (*fmt == 'p') {
1227 inttype = DO_LONG;
1228 }
1229 /* Fall through to unsigned handling */
1230 case 'o':
1231 if (radix == 10) {
1232 radix = 8;
1233 }
1234 /* Fall through to unsigned handling */
1235 case 'u':
1236 switch (inttype) {
1237 case DO_INT:
1238 len = SDL_PrintUnsignedLong (text, (unsigned long)
1239 va_arg (ap,
1240 unsigned
1241 int),
1242 radix, maxlen);
1127 break; 1243 break;
1128 case 'I': 1244 case DO_LONG:
1129 if ( SDL_strncmp(fmt, "I64", 3) == 0 ) { 1245 len =
1130 fmt += 2; 1246 SDL_PrintUnsignedLong (text,
1131 inttype = DO_LONGLONG; 1247 va_arg (ap,
1132 } 1248 unsigned
1249 long),
1250 radix, maxlen);
1133 break; 1251 break;
1134 case 'i': 1252 case DO_LONGLONG:
1135 case 'd':
1136 switch (inttype) {
1137 case DO_INT:
1138 len = SDL_PrintLong(text, (long)va_arg(ap, int), radix, maxlen);
1139 break;
1140 case DO_LONG:
1141 len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
1142 break;
1143 case DO_LONGLONG:
1144 #ifdef SDL_HAS_64BIT_TYPE 1253 #ifdef SDL_HAS_64BIT_TYPE
1145 len = SDL_PrintLongLong(text, va_arg(ap, Sint64), radix, maxlen); 1254 len =
1255 SDL_PrintUnsignedLongLong (text,
1256 va_arg (ap,
1257 Uint64),
1258 radix, maxlen);
1146 #else 1259 #else
1147 len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen); 1260 len =
1148 #endif 1261 SDL_PrintUnsignedLong (text,
1149 break; 1262 va_arg (ap,
1150 } 1263 unsigned
1151 done = SDL_TRUE; 1264 long),
1265 radix, maxlen);
1266 #endif
1152 break; 1267 break;
1153 case 'p': 1268 }
1154 case 'x': 1269 if (do_lowercase) {
1155 do_lowercase = SDL_TRUE; 1270 SDL_strlwr (text);
1156 /* Fall through to 'X' handling */ 1271 }
1157 case 'X': 1272 done = SDL_TRUE;
1158 if ( radix == 10 ) { 1273 break;
1159 radix = 16; 1274 case 'f':
1160 } 1275 len = SDL_PrintFloat (text, va_arg (ap, double), maxlen);
1161 if ( *fmt == 'p' ) { 1276 done = SDL_TRUE;
1162 inttype = DO_LONG; 1277 break;
1163 } 1278 case 's':
1164 /* Fall through to unsigned handling */ 1279 len = SDL_PrintString (text, va_arg (ap, char *), maxlen);
1165 case 'o': 1280 done = SDL_TRUE;
1166 if ( radix == 10 ) { 1281 break;
1167 radix = 8; 1282 default:
1168 } 1283 done = SDL_TRUE;
1169 /* Fall through to unsigned handling */ 1284 break;
1170 case 'u':
1171 switch (inttype) {
1172 case DO_INT:
1173 len = SDL_PrintUnsignedLong(text, (unsigned long)va_arg(ap, unsigned int), radix, maxlen);
1174 break;
1175 case DO_LONG:
1176 len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
1177 break;
1178 case DO_LONGLONG:
1179 #ifdef SDL_HAS_64BIT_TYPE
1180 len = SDL_PrintUnsignedLongLong(text, va_arg(ap, Uint64), radix, maxlen);
1181 #else
1182 len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
1183 #endif
1184 break;
1185 }
1186 if ( do_lowercase ) {
1187 SDL_strlwr(text);
1188 }
1189 done = SDL_TRUE;
1190 break;
1191 case 'f':
1192 len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
1193 done = SDL_TRUE;
1194 break;
1195 case 's':
1196 len = SDL_PrintString(text, va_arg(ap, char*), maxlen);
1197 done = SDL_TRUE;
1198 break;
1199 default:
1200 done = SDL_TRUE;
1201 break;
1202 } 1285 }
1203 ++fmt; 1286 ++fmt;
1204 } 1287 }
1205 text += len; 1288 text += len;
1206 maxlen -= len; 1289 maxlen -= len;
1212 *text = '\0'; 1295 *text = '\0';
1213 1296
1214 return (text - textstart); 1297 return (text - textstart);
1215 } 1298 }
1216 #endif 1299 #endif
1300 /* vi: set ts=4 sw=4 expandtab: */