src/stdlib/SDL_string.c
changeset 1330 450721ad5436
child 1336 3692456e7b0f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/stdlib/SDL_string.c	Mon Feb 06 08:28:51 2006 +0000
     1.3 @@ -0,0 +1,1108 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +
    1.27 +/* This file contains portable string manipulation functions for SDL */
    1.28 +
    1.29 +#include "SDL_types.h"
    1.30 +#include "SDL_ctype.h"
    1.31 +#include "SDL_string.h"
    1.32 +
    1.33 +
    1.34 +#define isupperhex(X)   (((X) >= 'A') && ((X) <= 'F'))
    1.35 +#define islowerhex(X)   (((X) >= 'a') && ((X) <= 'f'))
    1.36 +
    1.37 +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
    1.38 +static size_t SDL_ScanLong(const char *text, int radix, long *valuep)
    1.39 +{
    1.40 +    const char *textstart = text;
    1.41 +    long value = 0;
    1.42 +    SDL_bool negative = SDL_FALSE;
    1.43 +
    1.44 +    if ( *text == '-' ) {
    1.45 +        negative = SDL_TRUE;
    1.46 +        ++text;
    1.47 +    }
    1.48 +    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
    1.49 +        text += 2;
    1.50 +    }
    1.51 +    for ( ; ; ) {
    1.52 +        int v;
    1.53 +        if ( isdigit(*text) ) {
    1.54 +            v = *text - '0';
    1.55 +        } else if ( radix == 16 && isupperhex(*text) ) {
    1.56 +            v = 10 + (*text - 'A');
    1.57 +        } else if ( radix == 16 && islowerhex(*text) ) {
    1.58 +            v = 10 + (*text - 'a');
    1.59 +        } else {
    1.60 +            break;
    1.61 +        }
    1.62 +        value *= radix;
    1.63 +        value += v;
    1.64 +        ++text;
    1.65 +    }
    1.66 +    if ( valuep ) {
    1.67 +        if ( negative && value ) {
    1.68 +            *valuep = -value;
    1.69 +        } else {
    1.70 +            *valuep = value;
    1.71 +        }
    1.72 +    }
    1.73 +    return (text - textstart);
    1.74 +}
    1.75 +#endif
    1.76 +
    1.77 +#ifndef HAVE_SSCANF
    1.78 +static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
    1.79 +{
    1.80 +    const char *textstart = text;
    1.81 +    unsigned long value = 0;
    1.82 +
    1.83 +    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
    1.84 +        text += 2;
    1.85 +    }
    1.86 +    for ( ; ; ) {
    1.87 +        int v;
    1.88 +        if ( isdigit(*text) ) {
    1.89 +            v = *text - '0';
    1.90 +        } else if ( radix == 16 && isupperhex(*text) ) {
    1.91 +            v = 10 + (*text - 'A');
    1.92 +        } else if ( radix == 16 && islowerhex(*text) ) {
    1.93 +            v = 10 + (*text - 'a');
    1.94 +        } else {
    1.95 +            break;
    1.96 +        }
    1.97 +        value *= radix;
    1.98 +        value += v;
    1.99 +        ++text;
   1.100 +    }
   1.101 +    if ( valuep ) {
   1.102 +        *valuep = value;
   1.103 +    }
   1.104 +    return (text - textstart);
   1.105 +}
   1.106 +#endif
   1.107 +
   1.108 +#ifdef SDL_HAS_64BIT_TYPE
   1.109 +#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
   1.110 +static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
   1.111 +{
   1.112 +    const char *textstart = text;
   1.113 +    Sint64 value = 0;
   1.114 +    SDL_bool negative = SDL_FALSE;
   1.115 +
   1.116 +    if ( *text == '-' ) {
   1.117 +        negative = SDL_TRUE;
   1.118 +        ++text;
   1.119 +    }
   1.120 +    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
   1.121 +        text += 2;
   1.122 +    }
   1.123 +    for ( ; ; ) {
   1.124 +        int v;
   1.125 +        if ( isdigit(*text) ) {
   1.126 +            v = *text - '0';
   1.127 +        } else if ( radix == 16 && isupperhex(*text) ) {
   1.128 +            v = 10 + (*text - 'A');
   1.129 +        } else if ( radix == 16 && islowerhex(*text) ) {
   1.130 +            v = 10 + (*text - 'a');
   1.131 +        } else {
   1.132 +            break;
   1.133 +        }
   1.134 +        value *= radix;
   1.135 +        value += v;
   1.136 +        ++text;
   1.137 +    }
   1.138 +    if ( valuep ) {
   1.139 +        if ( negative && value ) {
   1.140 +            *valuep = -value;
   1.141 +        } else {
   1.142 +            *valuep = value;
   1.143 +        }
   1.144 +    }
   1.145 +    return (text - textstart);
   1.146 +}
   1.147 +#endif
   1.148 +
   1.149 +#ifndef HAVE_SSCANF
   1.150 +static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
   1.151 +{
   1.152 +    const char *textstart = text;
   1.153 +    Uint64 value = 0;
   1.154 +
   1.155 +    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
   1.156 +        text += 2;
   1.157 +    }
   1.158 +    for ( ; ; ) {
   1.159 +        int v;
   1.160 +        if ( isdigit(*text) ) {
   1.161 +            v = *text - '0';
   1.162 +        } else if ( radix == 16 && isupperhex(*text) ) {
   1.163 +            v = 10 + (*text - 'A');
   1.164 +        } else if ( radix == 16 && islowerhex(*text) ) {
   1.165 +            v = 10 + (*text - 'a');
   1.166 +        } else {
   1.167 +            break;
   1.168 +        }
   1.169 +        value *= radix;
   1.170 +        value += v;
   1.171 +        ++text;
   1.172 +    }
   1.173 +    if ( valuep ) {
   1.174 +        *valuep = value;
   1.175 +    }
   1.176 +    return (text - textstart);
   1.177 +}
   1.178 +#endif
   1.179 +#endif /* SDL_HAS_64BIT_TYPE */
   1.180 +
   1.181 +#ifndef HAVE_SSCANF
   1.182 +static size_t SDL_ScanFloat(const char *text, double *valuep)
   1.183 +{
   1.184 +    const char *textstart = text;
   1.185 +    unsigned long lvalue = 0;
   1.186 +    double value = 0.0;
   1.187 +    SDL_bool negative = SDL_FALSE;
   1.188 +
   1.189 +    if ( *text == '-' ) {
   1.190 +        negative = SDL_TRUE;
   1.191 +        ++text;
   1.192 +    }
   1.193 +    text += SDL_ScanUnsignedLong(text, 10, &lvalue);
   1.194 +    value += lvalue;
   1.195 +    if ( *text == '.' ) {
   1.196 +        int mult = 10;
   1.197 +        ++text;
   1.198 +        while ( isdigit(*text) ) {
   1.199 +            lvalue = *text - '0';
   1.200 +            value += (double)lvalue / mult;
   1.201 +            mult *= 10;
   1.202 +            ++text;
   1.203 +        }
   1.204 +    }
   1.205 +    if ( valuep ) {
   1.206 +        if ( negative && value ) {
   1.207 +            *valuep = -value;
   1.208 +        } else {
   1.209 +            *valuep = value;
   1.210 +        }
   1.211 +    }
   1.212 +    return (text - textstart);
   1.213 +}
   1.214 +#endif
   1.215 +
   1.216 +#ifndef SDL_memset
   1.217 +void *SDL_memset(void *dst, int c, size_t len)
   1.218 +{
   1.219 +    size_t left = (len % 4);
   1.220 +    if ( len >= 4 ) {
   1.221 +        Uint32 value = 0;
   1.222 +        Uint32 *dstp = (Uint32 *)dst;
   1.223 +        int i;
   1.224 +        for (i = 0; i < 4; ++i) {
   1.225 +            value <<= 8;
   1.226 +            value |= c;
   1.227 +        }
   1.228 +        len /= 4;
   1.229 +        while ( len-- ) {
   1.230 +            *dstp++ = value;
   1.231 +        }
   1.232 +    }
   1.233 +    if ( left > 0 ) {
   1.234 +        Uint8 value = (Uint8)c;
   1.235 +        Uint8 *dstp = (Uint8 *)dst;
   1.236 +	switch(left) {
   1.237 +	case 3:
   1.238 +            *dstp++ = value;
   1.239 +	case 2:
   1.240 +            *dstp++ = value;
   1.241 +	case 1:
   1.242 +            *dstp++ = value;
   1.243 +        }
   1.244 +    }
   1.245 +    return dst;
   1.246 +}
   1.247 +#endif
   1.248 +
   1.249 +#ifndef SDL_memcpy
   1.250 +void *SDL_memcpy(void *dst, const void *src, size_t len)
   1.251 +{
   1.252 +    char *srcp = (char *)src;
   1.253 +    char *dstp = (char *)dst;
   1.254 +    while ( len-- ) {
   1.255 +        *dstp++ = *srcp++;
   1.256 +    }
   1.257 +    return dst;
   1.258 +}
   1.259 +#endif
   1.260 +
   1.261 +#ifndef SDL_revcpy
   1.262 +void *SDL_revcpy(void *dst, const void *src, size_t len)
   1.263 +{
   1.264 +    char *srcp = (char *)src;
   1.265 +    char *dstp = (char *)dst;
   1.266 +    srcp += len;
   1.267 +    dstp += len;
   1.268 +    while ( len-- ) {
   1.269 +        *dstp-- = *srcp--;
   1.270 +    }
   1.271 +    return dst;
   1.272 +}
   1.273 +#endif
   1.274 +
   1.275 +#ifndef SDL_memcmp
   1.276 +int SDL_memcmp(const void *s1, const void *s2, size_t len)
   1.277 +{
   1.278 +    char *s1p = (char *)s1;
   1.279 +    char *s2p = (char *)s2;
   1.280 +    while ( len-- ) {
   1.281 +        if ( *s1p != *s2p ) {
   1.282 +            return (*s1p - *s2p);
   1.283 +    }
   1.284 +    ++s1p;
   1.285 +    ++s2p;
   1.286 +    }
   1.287 +    return 0;
   1.288 +}
   1.289 +#endif
   1.290 +
   1.291 +#ifndef HAVE_STRLEN
   1.292 +size_t SDL_strlen(const char *string)
   1.293 +{
   1.294 +    size_t len = 0;
   1.295 +    while ( *string++ ) {
   1.296 +        ++len;
   1.297 +    }
   1.298 +    return len;
   1.299 +}
   1.300 +#endif
   1.301 +
   1.302 +#ifndef HAVE_STRCPY
   1.303 +char *SDL_strcpy(char *dst, const char *src)
   1.304 +{
   1.305 +    char *dstp = dst;
   1.306 +    while ( *src ) {
   1.307 +        *dstp++ = *src++;
   1.308 +    }
   1.309 +    *dstp = '\0';
   1.310 +
   1.311 +    return dst;
   1.312 +}
   1.313 +#endif
   1.314 +
   1.315 +#ifndef HAVE_STRNCPY
   1.316 +char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
   1.317 +{
   1.318 +    char *dstp = dst;
   1.319 +    while ( maxlen-- && *src ) {
   1.320 +        *dstp++ = *src++;
   1.321 +    }
   1.322 +    *dstp = '\0';
   1.323 +
   1.324 +    return dst;
   1.325 +}
   1.326 +#endif
   1.327 +
   1.328 +#ifndef HAVE__STRREV
   1.329 +char *SDL_strrev(char *string)
   1.330 +{
   1.331 +    size_t len = SDL_strlen(string);
   1.332 +    char *a = &string[0];
   1.333 +    char *b = &string[len-1];
   1.334 +    len /= 2;
   1.335 +    while ( len-- ) {
   1.336 +        char c = *a;
   1.337 +        *a++ = *b;
   1.338 +        *b-- = c;
   1.339 +    }
   1.340 +    return string;
   1.341 +}
   1.342 +#endif
   1.343 +
   1.344 +#ifndef HAVE__STRUPR
   1.345 +char *SDL_strupr(char *string)
   1.346 +{
   1.347 +    char *bufp = string;
   1.348 +    while ( *bufp ) {
   1.349 +        *bufp++ = toupper(*bufp);
   1.350 +    }
   1.351 +    return string;
   1.352 +}
   1.353 +#endif
   1.354 +
   1.355 +#ifndef HAVE__STRLWR
   1.356 +char *SDL_strlwr(char *string)
   1.357 +{
   1.358 +    char *bufp = string;
   1.359 +    while ( *bufp ) {
   1.360 +        *bufp++ = tolower(*bufp);
   1.361 +    }
   1.362 +    return string;
   1.363 +}
   1.364 +#endif
   1.365 +
   1.366 +#ifndef HAVE_STRCHR
   1.367 +char *SDL_strchr(const char *string, int c)
   1.368 +{
   1.369 +    while ( *string ) {
   1.370 +        if ( *string == c ) {
   1.371 +            return (char *)string;
   1.372 +        }
   1.373 +    }
   1.374 +    return NULL;
   1.375 +}
   1.376 +#endif
   1.377 +
   1.378 +#ifndef HAVE_STRRCHR
   1.379 +char *SDL_strrchr(const char *string, int c)
   1.380 +{
   1.381 +    const char *bufp = string + strlen(string) - 1;
   1.382 +    while ( bufp >= string ) {
   1.383 +        if ( *bufp == c ) {
   1.384 +            return (char *)bufp;
   1.385 +        }
   1.386 +    }
   1.387 +    return NULL;
   1.388 +}
   1.389 +#endif
   1.390 +
   1.391 +#ifndef HAVE_STRSTR
   1.392 +char *SDL_strstr(const char *haystack, const char *needle)
   1.393 +{
   1.394 +    size_t length = strlen(needle);
   1.395 +    while ( *haystack ) {
   1.396 +        if ( strncmp(haystack, needle, length) == 0 ) {
   1.397 +            return (char *)haystack;
   1.398 +        }
   1.399 +    }
   1.400 +    return NULL;
   1.401 +}
   1.402 +#endif
   1.403 +
   1.404 +#if !defined(HAVE__LTOA)  || !defined(HAVE__I64TOA) || \
   1.405 +    !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
   1.406 +static const char ntoa_table[] = {
   1.407 +    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.408 +    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
   1.409 +    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
   1.410 +    'U', 'V', 'W', 'X', 'Y', 'Z'
   1.411 +};
   1.412 +#endif /* ntoa() conversion table */
   1.413 +
   1.414 +#ifndef HAVE__LTOA
   1.415 +char *SDL_ltoa(long value, char *string, int radix)
   1.416 +{
   1.417 +    char *bufp = string;
   1.418 +
   1.419 +    if ( value < 0 ) {
   1.420 +        *bufp++ = '-';
   1.421 +        value = -value;
   1.422 +    }
   1.423 +    if ( value ) {
   1.424 +        while ( value > 0 ) {
   1.425 +            *bufp++ = ntoa_table[value % radix];
   1.426 +            value /= radix;
   1.427 +        }
   1.428 +    } else {
   1.429 +        *bufp++ = '0';
   1.430 +    }
   1.431 +    *bufp = '\0';
   1.432 +
   1.433 +    /* The numbers went into the string backwards. :) */
   1.434 +    if ( *string == '-' ) {
   1.435 +        _strrev(string+1);
   1.436 +    } else {
   1.437 +        _strrev(string);
   1.438 +    }
   1.439 +
   1.440 +    return string;
   1.441 +}
   1.442 +#endif
   1.443 +
   1.444 +#ifndef HAVE__ULTOA
   1.445 +char *SDL_ultoa(unsigned long value, char *string, int radix)
   1.446 +{
   1.447 +    char *bufp = string;
   1.448 +
   1.449 +    if ( value ) {
   1.450 +        while ( value > 0 ) {
   1.451 +            *bufp++ = ntoa_table[value % radix];
   1.452 +            value /= radix;
   1.453 +        }
   1.454 +    } else {
   1.455 +        *bufp++ = '0';
   1.456 +    }
   1.457 +    *bufp = '\0';
   1.458 +
   1.459 +    /* The numbers went into the string backwards. :) */
   1.460 +    _strrev(string);
   1.461 +
   1.462 +    return string;
   1.463 +}
   1.464 +#endif
   1.465 +
   1.466 +#ifndef HAVE_STRTOL
   1.467 +long SDL_strtol(const char *string, char **endp, int base)
   1.468 +{
   1.469 +    size_t len;
   1.470 +    long value;
   1.471 +
   1.472 +    len = SDL_ScanLong(string, base ? base : 10, &value);
   1.473 +    if ( endp ) {
   1.474 +        *endp = (char *)string + len;
   1.475 +    }
   1.476 +    return value;
   1.477 +}
   1.478 +#endif
   1.479 +
   1.480 +#ifdef SDL_HAS_64BIT_TYPE
   1.481 +
   1.482 +#ifndef HAVE__I64TOA
   1.483 +char *SDL_lltoa(Sint64 value, char *string, int radix)
   1.484 +{
   1.485 +    char *bufp = string;
   1.486 +
   1.487 +    if ( value < 0 ) {
   1.488 +        *bufp++ = '-';
   1.489 +        value = -value;
   1.490 +    }
   1.491 +    if ( value ) {
   1.492 +        while ( value > 0 ) {
   1.493 +            *bufp++ = ntoa_table[value % radix];
   1.494 +            value /= radix;
   1.495 +        }
   1.496 +    } else {
   1.497 +        *bufp++ = '0';
   1.498 +    }
   1.499 +    *bufp = '\0';
   1.500 +
   1.501 +    /* The numbers went into the string backwards. :) */
   1.502 +    if ( *string == '-' ) {
   1.503 +        _strrev(string+1);
   1.504 +    } else {
   1.505 +        _strrev(string);
   1.506 +    }
   1.507 +
   1.508 +    return string;
   1.509 +}
   1.510 +#endif
   1.511 +
   1.512 +#ifndef HAVE__UI64TOA
   1.513 +char *SDL_ulltoa(Uint64 value, char *string, int radix)
   1.514 +{
   1.515 +    char *bufp = string;
   1.516 +
   1.517 +    if ( value ) {
   1.518 +        while ( value > 0 ) {
   1.519 +            *bufp++ = ntoa_table[value % radix];
   1.520 +            value /= radix;
   1.521 +        }
   1.522 +    } else {
   1.523 +        *bufp++ = '0';
   1.524 +    }
   1.525 +    *bufp = '\0';
   1.526 +
   1.527 +    /* The numbers went into the string backwards. :) */
   1.528 +    _strrev(string);
   1.529 +
   1.530 +    return string;
   1.531 +}
   1.532 +#endif
   1.533 +
   1.534 +#ifndef HAVE_STRTOLL
   1.535 +Sint64 SDL_strtoll(const char *string, char **endp, int base)
   1.536 +{
   1.537 +    size_t len;
   1.538 +    Sint64 value;
   1.539 +
   1.540 +    len = SDL_ScanLongLong(string, base ? base : 10, &value);
   1.541 +    if ( endp ) {
   1.542 +        *endp = (char *)string + len;
   1.543 +    }
   1.544 +    return value;
   1.545 +}
   1.546 +#endif
   1.547 +
   1.548 +#endif /* SDL_HAS_64BIT_TYPE */
   1.549 +
   1.550 +#ifndef HAVE_STRCMP
   1.551 +int SDL_strcmp(const char *str1, const char *str2)
   1.552 +{
   1.553 +    while (*str1 && *str2) {
   1.554 +        if ( *str1 != *str2 )
   1.555 +            break;
   1.556 +        ++str1;
   1.557 +        ++str2;
   1.558 +    }
   1.559 +    return (int)((unsigned char)*str1 - (unsigned char)*str2);
   1.560 +}
   1.561 +#endif
   1.562 +
   1.563 +#ifndef HAVE_STRNCMP
   1.564 +int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
   1.565 +{
   1.566 +    while ( *str1 && *str2 && maxlen ) {
   1.567 +        if ( *str1 != *str2 )
   1.568 +            break;
   1.569 +        ++str1;
   1.570 +        ++str2;
   1.571 +        --maxlen;
   1.572 +    }
   1.573 +    if ( ! maxlen ) {
   1.574 +        return 0;
   1.575 +    }
   1.576 +    return (int)((unsigned char)*str1 - (unsigned char)*str2);
   1.577 +}
   1.578 +#endif
   1.579 +
   1.580 +#ifndef HAVE_STRCASECMP
   1.581 +int SDL_strcasecmp(const char *str1, const char *str2)
   1.582 +{
   1.583 +    char a = 0;
   1.584 +    char b = 0;
   1.585 +    while (*str1 && *str2) {
   1.586 +        a = tolower(*str1);
   1.587 +        b = tolower(*str2);
   1.588 +        if ( a != b )
   1.589 +            break;
   1.590 +        ++str1;
   1.591 +        ++str2;
   1.592 +    }
   1.593 +    return (int)((unsigned char)a - (unsigned char)b);
   1.594 +}
   1.595 +#endif
   1.596 +
   1.597 +#ifndef HAVE_SSCANF
   1.598 +int SDL_sscanf(const char *text, const char *fmt, ...)
   1.599 +{
   1.600 +    va_list ap;
   1.601 +    int retval = 0;
   1.602 +
   1.603 +    va_start(ap, fmt);
   1.604 +    while ( *fmt ) {
   1.605 +        if ( *fmt == ' ' ) {
   1.606 +            while ( isspace(*text) ) {
   1.607 +                ++text;
   1.608 +            }
   1.609 +            ++fmt;
   1.610 +            continue;
   1.611 +        }
   1.612 +        if ( *fmt == '%' ) {
   1.613 +            SDL_bool done = SDL_FALSE;
   1.614 +            long count = 0;
   1.615 +            int radix = 10;
   1.616 +            enum {
   1.617 +                DO_SHORT,
   1.618 +                DO_INT,
   1.619 +                DO_LONG,
   1.620 +                DO_LONGLONG
   1.621 +            } inttype = DO_INT;
   1.622 +            SDL_bool suppress = SDL_FALSE;
   1.623 +
   1.624 +            ++fmt;
   1.625 +            if ( *fmt == '%' ) {
   1.626 +                if ( *text == '%' ) {
   1.627 +                    ++text;
   1.628 +                    ++fmt;
   1.629 +                    continue;
   1.630 +                }
   1.631 +                break;
   1.632 +            }
   1.633 +            if ( *fmt == '*' ) {
   1.634 +                suppress = SDL_TRUE;
   1.635 +                ++fmt;
   1.636 +            }
   1.637 +            fmt += SDL_ScanLong(fmt, 10, &count);
   1.638 +
   1.639 +            if ( *fmt == 'c' ) {
   1.640 +                if ( ! count ) {
   1.641 +                    count = 1;
   1.642 +                }
   1.643 +                if ( suppress ) {
   1.644 +                    while ( count-- ) {
   1.645 +                        ++text;
   1.646 +                    }
   1.647 +                } else {
   1.648 +                    char *valuep = va_arg(ap, char*);
   1.649 +                    while ( count-- ) {
   1.650 +                        *valuep++ = *text++;
   1.651 +                    }
   1.652 +                    ++retval;
   1.653 +                }
   1.654 +                continue;
   1.655 +            }
   1.656 +
   1.657 +            while ( isspace(*text) ) {
   1.658 +                ++text;
   1.659 +            }
   1.660 +
   1.661 +            /* FIXME: implement more of the format specifiers */
   1.662 +            while (!done) {
   1.663 +                switch(*fmt) {
   1.664 +                    case '*':
   1.665 +                        suppress = SDL_TRUE;
   1.666 +                        break;
   1.667 +                    case 'h':
   1.668 +                        if ( inttype > DO_SHORT ) {
   1.669 +                            ++inttype;
   1.670 +                        }
   1.671 +                        break;
   1.672 +                    case 'l':
   1.673 +                        if ( inttype < DO_LONGLONG ) {
   1.674 +                            ++inttype;
   1.675 +                        }
   1.676 +                        break;
   1.677 +                    case 'I':
   1.678 +                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
   1.679 +                            fmt += 2;
   1.680 +                            inttype = DO_LONGLONG;
   1.681 +                        }
   1.682 +                        break;
   1.683 +                    case 'i':
   1.684 +                        {
   1.685 +                            int index = 0;
   1.686 +                            if ( text[index] == '-' ) {
   1.687 +                                ++index;
   1.688 +                            }
   1.689 +                            if ( text[index] == '0' ) {
   1.690 +                                if ( tolower(text[index+1]) == 'x' ) {
   1.691 +                                    radix = 16;
   1.692 +                                } else {
   1.693 +                                    radix = 8;
   1.694 +                                }
   1.695 +                            }
   1.696 +                        }
   1.697 +                        /* Fall through to %d handling */
   1.698 +                    case 'd':
   1.699 +#ifdef SDL_HAS_64BIT_TYPE
   1.700 +                        if ( inttype == DO_LONGLONG ) {
   1.701 +                            Sint64 value;
   1.702 +                            text += SDL_ScanLongLong(text, radix, &value);
   1.703 +                            if ( ! suppress ) {
   1.704 +                                Sint64 *valuep = va_arg(ap, Sint64*);
   1.705 +                                *valuep = value;
   1.706 +                                ++retval;
   1.707 +                            }
   1.708 +                        }
   1.709 +                        else
   1.710 +#endif /* SDL_HAS_64BIT_TYPE */
   1.711 +                        {
   1.712 +                            long value;
   1.713 +                            text += SDL_ScanLong(text, radix, &value);
   1.714 +                            if ( ! suppress ) {
   1.715 +                                switch (inttype) {
   1.716 +                                    case DO_SHORT:
   1.717 +                                        { short* valuep = va_arg(ap, short*);
   1.718 +                                            *valuep = (short)value;
   1.719 +                                        }
   1.720 +                                        break;
   1.721 +                                    case DO_INT:
   1.722 +                                        { int* valuep = va_arg(ap, int*);
   1.723 +                                            *valuep = (int)value;
   1.724 +                                        }
   1.725 +                                        break;
   1.726 +                                    case DO_LONG:
   1.727 +                                        { long* valuep = va_arg(ap, long*);
   1.728 +                                            *valuep = value;
   1.729 +                                        }
   1.730 +                                        break;
   1.731 +                                    case DO_LONGLONG:
   1.732 +                                        /* Handled above */
   1.733 +                                        break;
   1.734 +                                }
   1.735 +                                ++retval;
   1.736 +                            }
   1.737 +                        }
   1.738 +                        done = SDL_TRUE;
   1.739 +                        break;
   1.740 +                    case 'o':
   1.741 +                        if ( radix == 10 ) {
   1.742 +                            radix = 8;
   1.743 +                        }
   1.744 +                        /* Fall through to unsigned handling */
   1.745 +                    case 'x':
   1.746 +                    case 'X':
   1.747 +                        if ( radix == 10 ) {
   1.748 +                            radix = 16;
   1.749 +                        }
   1.750 +                        /* Fall through to unsigned handling */
   1.751 +                    case 'u':
   1.752 +#ifdef SDL_HAS_64BIT_TYPE
   1.753 +                        if ( inttype == DO_LONGLONG ) {
   1.754 +                            Uint64 value;
   1.755 +                            text += SDL_ScanUnsignedLongLong(text, radix, &value);
   1.756 +                            if ( ! suppress ) {
   1.757 +                                Uint64 *valuep = va_arg(ap, Uint64*);
   1.758 +                                *valuep = value;
   1.759 +                                ++retval;
   1.760 +                            }
   1.761 +                        }
   1.762 +                        else
   1.763 +#endif /* SDL_HAS_64BIT_TYPE */
   1.764 +                        {
   1.765 +                            unsigned long value;
   1.766 +                            text += SDL_ScanUnsignedLong(text, radix, &value);
   1.767 +                            if ( ! suppress ) {
   1.768 +                                switch (inttype) {
   1.769 +                                    case DO_SHORT:
   1.770 +                                        { short* valuep = va_arg(ap, short*);
   1.771 +                                            *valuep = (short)value;
   1.772 +                                        }
   1.773 +                                        break;
   1.774 +                                    case DO_INT:
   1.775 +                                        { int* valuep = va_arg(ap, int*);
   1.776 +                                            *valuep = (int)value;
   1.777 +                                        }
   1.778 +                                        break;
   1.779 +                                    case DO_LONG:
   1.780 +                                        { long* valuep = va_arg(ap, long*);
   1.781 +                                            *valuep = value;
   1.782 +                                        }
   1.783 +                                        break;
   1.784 +                                    case DO_LONGLONG:
   1.785 +                                        /* Handled above */
   1.786 +                                        break;
   1.787 +                                }
   1.788 +                                ++retval;
   1.789 +                            }
   1.790 +                        }
   1.791 +                        done = SDL_TRUE;
   1.792 +                        break;
   1.793 +                    case 'p':
   1.794 +                        {
   1.795 +                            unsigned long value;
   1.796 +                            text += SDL_ScanUnsignedLong(text, 16, &value);
   1.797 +                            if ( ! suppress ) {
   1.798 +                                void** valuep = va_arg(ap, void**);
   1.799 +                                *valuep = (void*)value;
   1.800 +                                ++retval;
   1.801 +                            }
   1.802 +                        }
   1.803 +                        done = SDL_TRUE;
   1.804 +                        break;
   1.805 +                    case 'f':
   1.806 +                        {
   1.807 +                            double value;
   1.808 +                            text += SDL_ScanFloat(text, &value);
   1.809 +                            if ( ! suppress ) {
   1.810 +                                float* valuep = va_arg(ap, float*);
   1.811 +                                *valuep = (float)value;
   1.812 +                                ++retval;
   1.813 +                            }
   1.814 +                        }
   1.815 +                        done = SDL_TRUE;
   1.816 +                        break;
   1.817 +                    case 's':
   1.818 +                        if ( suppress ) {
   1.819 +                            while ( !isspace(*text) ) {
   1.820 +                                ++text;
   1.821 +                                if ( count ) {
   1.822 +                                    if ( --count == 0 ) {
   1.823 +                                        break;
   1.824 +                                    }
   1.825 +                                }
   1.826 +                            }
   1.827 +                        } else {
   1.828 +                            char *valuep = va_arg(ap, char*);
   1.829 +                            while ( !isspace(*text) ) {
   1.830 +                                *valuep++ = *text++;
   1.831 +                                if ( count ) {
   1.832 +                                    if ( --count == 0 ) {
   1.833 +                                        break;
   1.834 +                                    }
   1.835 +                                }
   1.836 +                            }
   1.837 +                            *valuep = '\0';
   1.838 +                            ++retval;
   1.839 +                        }
   1.840 +                        done = SDL_TRUE;
   1.841 +                        break;
   1.842 +                    default:
   1.843 +                        done = SDL_TRUE;
   1.844 +                        break;
   1.845 +                }
   1.846 +                ++fmt;
   1.847 +            }
   1.848 +            continue;
   1.849 +        }
   1.850 +        if ( *text == *fmt ) {
   1.851 +            ++text;
   1.852 +            ++fmt;
   1.853 +            continue;
   1.854 +        }
   1.855 +        /* Text didn't match format specifier */
   1.856 +        break;
   1.857 +    }
   1.858 +    va_end(ap);
   1.859 +
   1.860 +    return retval;
   1.861 +}
   1.862 +#endif
   1.863 +
   1.864 +#ifndef HAVE_SNPRINTF
   1.865 +int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
   1.866 +{
   1.867 +    va_list ap;
   1.868 +    int retval;
   1.869 +
   1.870 +    va_start(ap, fmt);
   1.871 +    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
   1.872 +    va_end(ap);
   1.873 +
   1.874 +    return retval;
   1.875 +}
   1.876 +#endif
   1.877 +
   1.878 +#ifndef HAVE_VSNPRINTF
   1.879 +static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
   1.880 +{
   1.881 +    char num[130];
   1.882 +    size_t size;
   1.883 +
   1.884 +    _ltoa(value, num, radix);
   1.885 +    size = SDL_strlen(num);
   1.886 +    if ( size > maxlen ) {
   1.887 +        size = maxlen;
   1.888 +    }
   1.889 +    strncpy(text, num, size);
   1.890 +
   1.891 +    return size;
   1.892 +}
   1.893 +static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen)
   1.894 +{
   1.895 +    char num[130];
   1.896 +    size_t size;
   1.897 +
   1.898 +    _ultoa(value, num, radix);
   1.899 +    size = SDL_strlen(num);
   1.900 +    if ( size > maxlen ) {
   1.901 +        size = maxlen;
   1.902 +    }
   1.903 +    strncpy(text, num, size);
   1.904 +
   1.905 +    return size;
   1.906 +}
   1.907 +#ifdef SDL_HAS_64BIT_TYPE
   1.908 +static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
   1.909 +{
   1.910 +    char num[130];
   1.911 +    size_t size;
   1.912 +
   1.913 +    _i64toa(value, num, radix);
   1.914 +    size = SDL_strlen(num);
   1.915 +    if ( size > maxlen ) {
   1.916 +        size = maxlen;
   1.917 +    }
   1.918 +    strncpy(text, num, size);
   1.919 +
   1.920 +    return size;
   1.921 +}
   1.922 +static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
   1.923 +{
   1.924 +    char num[130];
   1.925 +    size_t size;
   1.926 +
   1.927 +    _ui64toa(value, num, radix);
   1.928 +    size = SDL_strlen(num);
   1.929 +    if ( size > maxlen ) {
   1.930 +        size = maxlen;
   1.931 +    }
   1.932 +    strncpy(text, num, size);
   1.933 +
   1.934 +    return size;
   1.935 +}
   1.936 +#endif /* SDL_HAS_64BIT_TYPE */
   1.937 +static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen)
   1.938 +{
   1.939 +    char *textstart = text;
   1.940 +    if ( arg ) {
   1.941 +        /* This isn't especially accurate, but hey, it's easy. :) */
   1.942 +        const double precision = 0.00000001;
   1.943 +        size_t len;
   1.944 +        unsigned long value;
   1.945 +
   1.946 +        if ( arg < 0 ) {
   1.947 +            *text++ = '-';
   1.948 +            --maxlen;
   1.949 +            arg = -arg;
   1.950 +        }
   1.951 +        value = (unsigned long)arg;
   1.952 +        len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
   1.953 +        text += len;
   1.954 +        maxlen -= len;
   1.955 +        arg -= value;
   1.956 +        if ( arg > precision && maxlen ) {
   1.957 +            int mult = 10;
   1.958 +            *text++ = '.';
   1.959 +            while ( (arg > precision) && maxlen ) {
   1.960 +                value = (unsigned long)(arg * mult);
   1.961 +                len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
   1.962 +                text += len;
   1.963 +                maxlen -= len;
   1.964 +                arg -= (double)value / mult;
   1.965 +                mult *= 10;
   1.966 +            }
   1.967 +        }
   1.968 +    } else {
   1.969 +        *text++ = '0';
   1.970 +    }
   1.971 +    return (text - textstart);
   1.972 +}
   1.973 +static size_t SDL_PrintString(char *text, const char *string, size_t maxlen)
   1.974 +{
   1.975 +    char *textstart = text;
   1.976 +    while ( *string && maxlen-- ) {
   1.977 +        *text++ = *string++;
   1.978 +    }
   1.979 +    return (text - textstart);
   1.980 +}
   1.981 +int  SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
   1.982 +{
   1.983 +    char *textstart = text;
   1.984 +    if ( maxlen <= 0 ) {
   1.985 +        return 0;
   1.986 +    }
   1.987 +    --maxlen; /* For the trailing '\0' */
   1.988 +    while ( *fmt && maxlen ) {
   1.989 +        if ( *fmt == '%' ) {
   1.990 +            SDL_bool done = SDL_FALSE;
   1.991 +            size_t len = 0;
   1.992 +            SDL_bool do_lowercase = SDL_FALSE;
   1.993 +            int radix = 10;
   1.994 +            enum {
   1.995 +                DO_INT,
   1.996 +                DO_LONG,
   1.997 +                DO_LONGLONG
   1.998 +            } inttype = DO_INT;
   1.999 +
  1.1000 +            ++fmt;
  1.1001 +            /* FIXME: implement more of the format specifiers */
  1.1002 +            while (!done) {
  1.1003 +                switch(*fmt) {
  1.1004 +                    case '%':
  1.1005 +                        *text = '%';
  1.1006 +                        len = 1;
  1.1007 +                        done = SDL_TRUE;
  1.1008 +                        break;
  1.1009 +                    case 'c':
  1.1010 +                        /* char is promoted to int when passed through (...) */
  1.1011 +                        *text = (char)va_arg(ap, int);
  1.1012 +                        len = 1;
  1.1013 +                        done = SDL_TRUE;
  1.1014 +                        break;
  1.1015 +                    case 'h':
  1.1016 +                        /* short is promoted to int when passed through (...) */
  1.1017 +                        break;
  1.1018 +                    case 'l':
  1.1019 +                        if ( inttype < DO_LONGLONG ) {
  1.1020 +                            ++inttype;
  1.1021 +                        }
  1.1022 +                        break;
  1.1023 +                    case 'I':
  1.1024 +                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
  1.1025 +                            fmt += 2;
  1.1026 +                            inttype = DO_LONGLONG;
  1.1027 +                        }
  1.1028 +                        break;
  1.1029 +                    case 'i':
  1.1030 +                    case 'd':
  1.1031 +                        switch (inttype) {
  1.1032 +                            case DO_INT:
  1.1033 +                                len = SDL_PrintLong(text, (long)va_arg(ap, int), radix, maxlen);
  1.1034 +                                break;
  1.1035 +                            case DO_LONG:
  1.1036 +                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
  1.1037 +                                break;
  1.1038 +                            case DO_LONGLONG:
  1.1039 +#ifdef SDL_HAS_64BIT_TYPE
  1.1040 +                                len = SDL_PrintLongLong(text, va_arg(ap, Sint64), radix, maxlen);
  1.1041 +#else
  1.1042 +                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
  1.1043 +#endif
  1.1044 +                                break;
  1.1045 +                        }
  1.1046 +                        done = SDL_TRUE;
  1.1047 +                        break;
  1.1048 +                    case 'p':
  1.1049 +                    case 'x':
  1.1050 +                        do_lowercase = SDL_TRUE;
  1.1051 +                        /* Fall through to 'X' handling */
  1.1052 +                    case 'X':
  1.1053 +                        if ( radix == 10 ) {
  1.1054 +                            radix = 16;
  1.1055 +                        }
  1.1056 +                        if ( *fmt == 'p' ) {
  1.1057 +                            inttype = DO_LONG;
  1.1058 +                        }
  1.1059 +                        /* Fall through to unsigned handling */
  1.1060 +                    case 'o':
  1.1061 +                        if ( radix == 10 ) {
  1.1062 +                            radix = 8;
  1.1063 +                        }
  1.1064 +                        /* Fall through to unsigned handling */
  1.1065 +                    case 'u':
  1.1066 +                        switch (inttype) {
  1.1067 +                            case DO_INT:
  1.1068 +                                len = SDL_PrintUnsignedLong(text, (unsigned long)va_arg(ap, unsigned int), radix, maxlen);
  1.1069 +                                break;
  1.1070 +                            case DO_LONG:
  1.1071 +                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
  1.1072 +                                break;
  1.1073 +                            case DO_LONGLONG:
  1.1074 +#ifdef SDL_HAS_64BIT_TYPE
  1.1075 +                                len = SDL_PrintUnsignedLongLong(text, va_arg(ap, Uint64), radix, maxlen);
  1.1076 +#else
  1.1077 +                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
  1.1078 +#endif
  1.1079 +                                break;
  1.1080 +                        }
  1.1081 +                        if ( do_lowercase ) {
  1.1082 +                            _strlwr(text);
  1.1083 +                        }
  1.1084 +                        done = SDL_TRUE;
  1.1085 +                        break;
  1.1086 +                    case 'f':
  1.1087 +                        len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
  1.1088 +                        done = SDL_TRUE;
  1.1089 +                        break;
  1.1090 +                    case 's':
  1.1091 +                        len = SDL_PrintString(text, va_arg(ap, char*), maxlen);
  1.1092 +                        done = SDL_TRUE;
  1.1093 +                        break;
  1.1094 +                    default:
  1.1095 +                        done = SDL_TRUE;
  1.1096 +                        break;
  1.1097 +                }
  1.1098 +                ++fmt;
  1.1099 +            }
  1.1100 +            text += len;
  1.1101 +            maxlen -= len;
  1.1102 +        } else {
  1.1103 +            *text++ = *fmt++;
  1.1104 +            --maxlen;
  1.1105 +        }
  1.1106 +    }
  1.1107 +    *text = '\0';
  1.1108 +
  1.1109 +    return (text - textstart);
  1.1110 +}
  1.1111 +#endif