src/stdlib/SDL_string.c
author Sam Lantinga
Tue, 07 Feb 2006 06:59:48 +0000
changeset 1336 3692456e7b0f
parent 1330 450721ad5436
child 1338 604d73db6802
permissions -rw-r--r--
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
slouken@1330
     1
/*
slouken@1330
     2
    SDL - Simple DirectMedia Layer
slouken@1330
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1330
     4
slouken@1330
     5
    This library is free software; you can redistribute it and/or
slouken@1330
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1330
     7
    License as published by the Free Software Foundation; either
slouken@1330
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1330
     9
slouken@1330
    10
    This library is distributed in the hope that it will be useful,
slouken@1330
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1330
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1330
    13
    Lesser General Public License for more details.
slouken@1330
    14
slouken@1330
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1330
    16
    License along with this library; if not, write to the Free Software
slouken@1330
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1330
    18
slouken@1330
    19
    Sam Lantinga
slouken@1330
    20
    slouken@libsdl.org
slouken@1330
    21
*/
slouken@1330
    22
slouken@1330
    23
slouken@1330
    24
/* This file contains portable string manipulation functions for SDL */
slouken@1330
    25
slouken@1330
    26
#include "SDL_types.h"
slouken@1330
    27
#include "SDL_ctype.h"
slouken@1330
    28
#include "SDL_string.h"
slouken@1330
    29
slouken@1330
    30
slouken@1330
    31
#define isupperhex(X)   (((X) >= 'A') && ((X) <= 'F'))
slouken@1330
    32
#define islowerhex(X)   (((X) >= 'a') && ((X) <= 'f'))
slouken@1330
    33
slouken@1330
    34
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
slouken@1330
    35
static size_t SDL_ScanLong(const char *text, int radix, long *valuep)
slouken@1330
    36
{
slouken@1330
    37
    const char *textstart = text;
slouken@1330
    38
    long value = 0;
slouken@1330
    39
    SDL_bool negative = SDL_FALSE;
slouken@1330
    40
slouken@1330
    41
    if ( *text == '-' ) {
slouken@1330
    42
        negative = SDL_TRUE;
slouken@1330
    43
        ++text;
slouken@1330
    44
    }
slouken@1330
    45
    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
slouken@1330
    46
        text += 2;
slouken@1330
    47
    }
slouken@1330
    48
    for ( ; ; ) {
slouken@1330
    49
        int v;
slouken@1330
    50
        if ( isdigit(*text) ) {
slouken@1330
    51
            v = *text - '0';
slouken@1330
    52
        } else if ( radix == 16 && isupperhex(*text) ) {
slouken@1330
    53
            v = 10 + (*text - 'A');
slouken@1330
    54
        } else if ( radix == 16 && islowerhex(*text) ) {
slouken@1330
    55
            v = 10 + (*text - 'a');
slouken@1330
    56
        } else {
slouken@1330
    57
            break;
slouken@1330
    58
        }
slouken@1330
    59
        value *= radix;
slouken@1330
    60
        value += v;
slouken@1330
    61
        ++text;
slouken@1330
    62
    }
slouken@1330
    63
    if ( valuep ) {
slouken@1330
    64
        if ( negative && value ) {
slouken@1330
    65
            *valuep = -value;
slouken@1330
    66
        } else {
slouken@1330
    67
            *valuep = value;
slouken@1330
    68
        }
slouken@1330
    69
    }
slouken@1330
    70
    return (text - textstart);
slouken@1330
    71
}
slouken@1330
    72
#endif
slouken@1330
    73
slouken@1330
    74
#ifndef HAVE_SSCANF
slouken@1330
    75
static size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
slouken@1330
    76
{
slouken@1330
    77
    const char *textstart = text;
slouken@1330
    78
    unsigned long value = 0;
slouken@1330
    79
slouken@1330
    80
    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
slouken@1330
    81
        text += 2;
slouken@1330
    82
    }
slouken@1330
    83
    for ( ; ; ) {
slouken@1330
    84
        int v;
slouken@1330
    85
        if ( isdigit(*text) ) {
slouken@1330
    86
            v = *text - '0';
slouken@1330
    87
        } else if ( radix == 16 && isupperhex(*text) ) {
slouken@1330
    88
            v = 10 + (*text - 'A');
slouken@1330
    89
        } else if ( radix == 16 && islowerhex(*text) ) {
slouken@1330
    90
            v = 10 + (*text - 'a');
slouken@1330
    91
        } else {
slouken@1330
    92
            break;
slouken@1330
    93
        }
slouken@1330
    94
        value *= radix;
slouken@1330
    95
        value += v;
slouken@1330
    96
        ++text;
slouken@1330
    97
    }
slouken@1330
    98
    if ( valuep ) {
slouken@1330
    99
        *valuep = value;
slouken@1330
   100
    }
slouken@1330
   101
    return (text - textstart);
slouken@1330
   102
}
slouken@1330
   103
#endif
slouken@1330
   104
slouken@1330
   105
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   106
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
slouken@1330
   107
static size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
slouken@1330
   108
{
slouken@1330
   109
    const char *textstart = text;
slouken@1330
   110
    Sint64 value = 0;
slouken@1330
   111
    SDL_bool negative = SDL_FALSE;
slouken@1330
   112
slouken@1330
   113
    if ( *text == '-' ) {
slouken@1330
   114
        negative = SDL_TRUE;
slouken@1330
   115
        ++text;
slouken@1330
   116
    }
slouken@1330
   117
    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
slouken@1330
   118
        text += 2;
slouken@1330
   119
    }
slouken@1330
   120
    for ( ; ; ) {
slouken@1330
   121
        int v;
slouken@1330
   122
        if ( isdigit(*text) ) {
slouken@1330
   123
            v = *text - '0';
slouken@1330
   124
        } else if ( radix == 16 && isupperhex(*text) ) {
slouken@1330
   125
            v = 10 + (*text - 'A');
slouken@1330
   126
        } else if ( radix == 16 && islowerhex(*text) ) {
slouken@1330
   127
            v = 10 + (*text - 'a');
slouken@1330
   128
        } else {
slouken@1330
   129
            break;
slouken@1330
   130
        }
slouken@1330
   131
        value *= radix;
slouken@1330
   132
        value += v;
slouken@1330
   133
        ++text;
slouken@1330
   134
    }
slouken@1330
   135
    if ( valuep ) {
slouken@1330
   136
        if ( negative && value ) {
slouken@1330
   137
            *valuep = -value;
slouken@1330
   138
        } else {
slouken@1330
   139
            *valuep = value;
slouken@1330
   140
        }
slouken@1330
   141
    }
slouken@1330
   142
    return (text - textstart);
slouken@1330
   143
}
slouken@1330
   144
#endif
slouken@1330
   145
slouken@1330
   146
#ifndef HAVE_SSCANF
slouken@1330
   147
static size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
slouken@1330
   148
{
slouken@1330
   149
    const char *textstart = text;
slouken@1330
   150
    Uint64 value = 0;
slouken@1330
   151
slouken@1330
   152
    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
slouken@1330
   153
        text += 2;
slouken@1330
   154
    }
slouken@1330
   155
    for ( ; ; ) {
slouken@1330
   156
        int v;
slouken@1330
   157
        if ( isdigit(*text) ) {
slouken@1330
   158
            v = *text - '0';
slouken@1330
   159
        } else if ( radix == 16 && isupperhex(*text) ) {
slouken@1330
   160
            v = 10 + (*text - 'A');
slouken@1330
   161
        } else if ( radix == 16 && islowerhex(*text) ) {
slouken@1330
   162
            v = 10 + (*text - 'a');
slouken@1330
   163
        } else {
slouken@1330
   164
            break;
slouken@1330
   165
        }
slouken@1330
   166
        value *= radix;
slouken@1330
   167
        value += v;
slouken@1330
   168
        ++text;
slouken@1330
   169
    }
slouken@1330
   170
    if ( valuep ) {
slouken@1330
   171
        *valuep = value;
slouken@1330
   172
    }
slouken@1330
   173
    return (text - textstart);
slouken@1330
   174
}
slouken@1330
   175
#endif
slouken@1330
   176
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   177
slouken@1330
   178
#ifndef HAVE_SSCANF
slouken@1330
   179
static size_t SDL_ScanFloat(const char *text, double *valuep)
slouken@1330
   180
{
slouken@1330
   181
    const char *textstart = text;
slouken@1330
   182
    unsigned long lvalue = 0;
slouken@1330
   183
    double value = 0.0;
slouken@1330
   184
    SDL_bool negative = SDL_FALSE;
slouken@1330
   185
slouken@1330
   186
    if ( *text == '-' ) {
slouken@1330
   187
        negative = SDL_TRUE;
slouken@1330
   188
        ++text;
slouken@1330
   189
    }
slouken@1330
   190
    text += SDL_ScanUnsignedLong(text, 10, &lvalue);
slouken@1330
   191
    value += lvalue;
slouken@1330
   192
    if ( *text == '.' ) {
slouken@1330
   193
        int mult = 10;
slouken@1330
   194
        ++text;
slouken@1330
   195
        while ( isdigit(*text) ) {
slouken@1330
   196
            lvalue = *text - '0';
slouken@1330
   197
            value += (double)lvalue / mult;
slouken@1330
   198
            mult *= 10;
slouken@1330
   199
            ++text;
slouken@1330
   200
        }
slouken@1330
   201
    }
slouken@1330
   202
    if ( valuep ) {
slouken@1330
   203
        if ( negative && value ) {
slouken@1330
   204
            *valuep = -value;
slouken@1330
   205
        } else {
slouken@1330
   206
            *valuep = value;
slouken@1330
   207
        }
slouken@1330
   208
    }
slouken@1330
   209
    return (text - textstart);
slouken@1330
   210
}
slouken@1330
   211
#endif
slouken@1330
   212
slouken@1330
   213
#ifndef SDL_memset
slouken@1330
   214
void *SDL_memset(void *dst, int c, size_t len)
slouken@1330
   215
{
slouken@1330
   216
    size_t left = (len % 4);
slouken@1330
   217
    if ( len >= 4 ) {
slouken@1330
   218
        Uint32 value = 0;
slouken@1330
   219
        Uint32 *dstp = (Uint32 *)dst;
slouken@1330
   220
        int i;
slouken@1330
   221
        for (i = 0; i < 4; ++i) {
slouken@1330
   222
            value <<= 8;
slouken@1330
   223
            value |= c;
slouken@1330
   224
        }
slouken@1330
   225
        len /= 4;
slouken@1330
   226
        while ( len-- ) {
slouken@1330
   227
            *dstp++ = value;
slouken@1330
   228
        }
slouken@1330
   229
    }
slouken@1330
   230
    if ( left > 0 ) {
slouken@1330
   231
        Uint8 value = (Uint8)c;
slouken@1330
   232
        Uint8 *dstp = (Uint8 *)dst;
slouken@1330
   233
	switch(left) {
slouken@1330
   234
	case 3:
slouken@1330
   235
            *dstp++ = value;
slouken@1330
   236
	case 2:
slouken@1330
   237
            *dstp++ = value;
slouken@1330
   238
	case 1:
slouken@1330
   239
            *dstp++ = value;
slouken@1330
   240
        }
slouken@1330
   241
    }
slouken@1330
   242
    return dst;
slouken@1330
   243
}
slouken@1330
   244
#endif
slouken@1330
   245
slouken@1330
   246
#ifndef SDL_memcpy
slouken@1330
   247
void *SDL_memcpy(void *dst, const void *src, size_t len)
slouken@1330
   248
{
slouken@1330
   249
    char *srcp = (char *)src;
slouken@1330
   250
    char *dstp = (char *)dst;
slouken@1330
   251
    while ( len-- ) {
slouken@1330
   252
        *dstp++ = *srcp++;
slouken@1330
   253
    }
slouken@1330
   254
    return dst;
slouken@1330
   255
}
slouken@1330
   256
#endif
slouken@1330
   257
slouken@1330
   258
#ifndef SDL_revcpy
slouken@1330
   259
void *SDL_revcpy(void *dst, const void *src, size_t len)
slouken@1330
   260
{
slouken@1330
   261
    char *srcp = (char *)src;
slouken@1330
   262
    char *dstp = (char *)dst;
slouken@1330
   263
    srcp += len;
slouken@1330
   264
    dstp += len;
slouken@1330
   265
    while ( len-- ) {
slouken@1330
   266
        *dstp-- = *srcp--;
slouken@1330
   267
    }
slouken@1330
   268
    return dst;
slouken@1330
   269
}
slouken@1330
   270
#endif
slouken@1330
   271
slouken@1330
   272
#ifndef SDL_memcmp
slouken@1330
   273
int SDL_memcmp(const void *s1, const void *s2, size_t len)
slouken@1330
   274
{
slouken@1330
   275
    char *s1p = (char *)s1;
slouken@1330
   276
    char *s2p = (char *)s2;
slouken@1330
   277
    while ( len-- ) {
slouken@1330
   278
        if ( *s1p != *s2p ) {
slouken@1330
   279
            return (*s1p - *s2p);
slouken@1330
   280
    }
slouken@1330
   281
    ++s1p;
slouken@1330
   282
    ++s2p;
slouken@1330
   283
    }
slouken@1330
   284
    return 0;
slouken@1330
   285
}
slouken@1330
   286
#endif
slouken@1330
   287
slouken@1330
   288
#ifndef HAVE_STRLEN
slouken@1330
   289
size_t SDL_strlen(const char *string)
slouken@1330
   290
{
slouken@1330
   291
    size_t len = 0;
slouken@1330
   292
    while ( *string++ ) {
slouken@1330
   293
        ++len;
slouken@1330
   294
    }
slouken@1330
   295
    return len;
slouken@1330
   296
}
slouken@1330
   297
#endif
slouken@1330
   298
slouken@1330
   299
#ifndef HAVE_STRCPY
slouken@1330
   300
char *SDL_strcpy(char *dst, const char *src)
slouken@1330
   301
{
slouken@1330
   302
    char *dstp = dst;
slouken@1330
   303
    while ( *src ) {
slouken@1330
   304
        *dstp++ = *src++;
slouken@1330
   305
    }
slouken@1330
   306
    *dstp = '\0';
slouken@1330
   307
slouken@1330
   308
    return dst;
slouken@1330
   309
}
slouken@1330
   310
#endif
slouken@1330
   311
slouken@1330
   312
#ifndef HAVE_STRNCPY
slouken@1330
   313
char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
slouken@1330
   314
{
slouken@1330
   315
    char *dstp = dst;
slouken@1330
   316
    while ( maxlen-- && *src ) {
slouken@1330
   317
        *dstp++ = *src++;
slouken@1330
   318
    }
slouken@1330
   319
    *dstp = '\0';
slouken@1330
   320
slouken@1330
   321
    return dst;
slouken@1330
   322
}
slouken@1330
   323
#endif
slouken@1330
   324
slouken@1330
   325
#ifndef HAVE__STRREV
slouken@1330
   326
char *SDL_strrev(char *string)
slouken@1330
   327
{
slouken@1330
   328
    size_t len = SDL_strlen(string);
slouken@1330
   329
    char *a = &string[0];
slouken@1330
   330
    char *b = &string[len-1];
slouken@1330
   331
    len /= 2;
slouken@1330
   332
    while ( len-- ) {
slouken@1330
   333
        char c = *a;
slouken@1330
   334
        *a++ = *b;
slouken@1330
   335
        *b-- = c;
slouken@1330
   336
    }
slouken@1330
   337
    return string;
slouken@1330
   338
}
slouken@1330
   339
#endif
slouken@1330
   340
slouken@1330
   341
#ifndef HAVE__STRUPR
slouken@1330
   342
char *SDL_strupr(char *string)
slouken@1330
   343
{
slouken@1330
   344
    char *bufp = string;
slouken@1330
   345
    while ( *bufp ) {
slouken@1330
   346
        *bufp++ = toupper(*bufp);
slouken@1330
   347
    }
slouken@1330
   348
    return string;
slouken@1330
   349
}
slouken@1330
   350
#endif
slouken@1330
   351
slouken@1330
   352
#ifndef HAVE__STRLWR
slouken@1330
   353
char *SDL_strlwr(char *string)
slouken@1330
   354
{
slouken@1330
   355
    char *bufp = string;
slouken@1330
   356
    while ( *bufp ) {
slouken@1330
   357
        *bufp++ = tolower(*bufp);
slouken@1330
   358
    }
slouken@1330
   359
    return string;
slouken@1330
   360
}
slouken@1330
   361
#endif
slouken@1330
   362
slouken@1330
   363
#ifndef HAVE_STRCHR
slouken@1330
   364
char *SDL_strchr(const char *string, int c)
slouken@1330
   365
{
slouken@1330
   366
    while ( *string ) {
slouken@1330
   367
        if ( *string == c ) {
slouken@1330
   368
            return (char *)string;
slouken@1330
   369
        }
slouken@1330
   370
    }
slouken@1330
   371
    return NULL;
slouken@1330
   372
}
slouken@1330
   373
#endif
slouken@1330
   374
slouken@1330
   375
#ifndef HAVE_STRRCHR
slouken@1330
   376
char *SDL_strrchr(const char *string, int c)
slouken@1330
   377
{
slouken@1336
   378
    const char *bufp = string + SDL_strlen(string) - 1;
slouken@1330
   379
    while ( bufp >= string ) {
slouken@1330
   380
        if ( *bufp == c ) {
slouken@1330
   381
            return (char *)bufp;
slouken@1330
   382
        }
slouken@1330
   383
    }
slouken@1330
   384
    return NULL;
slouken@1330
   385
}
slouken@1330
   386
#endif
slouken@1330
   387
slouken@1330
   388
#ifndef HAVE_STRSTR
slouken@1330
   389
char *SDL_strstr(const char *haystack, const char *needle)
slouken@1330
   390
{
slouken@1336
   391
    size_t length = SDL_strlen(needle);
slouken@1330
   392
    while ( *haystack ) {
slouken@1336
   393
        if ( SDL_strncmp(haystack, needle, length) == 0 ) {
slouken@1330
   394
            return (char *)haystack;
slouken@1330
   395
        }
slouken@1330
   396
    }
slouken@1330
   397
    return NULL;
slouken@1330
   398
}
slouken@1330
   399
#endif
slouken@1330
   400
slouken@1330
   401
#if !defined(HAVE__LTOA)  || !defined(HAVE__I64TOA) || \
slouken@1330
   402
    !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
slouken@1330
   403
static const char ntoa_table[] = {
slouken@1330
   404
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
slouken@1330
   405
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
slouken@1330
   406
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
slouken@1330
   407
    'U', 'V', 'W', 'X', 'Y', 'Z'
slouken@1330
   408
};
slouken@1330
   409
#endif /* ntoa() conversion table */
slouken@1330
   410
slouken@1330
   411
#ifndef HAVE__LTOA
slouken@1330
   412
char *SDL_ltoa(long value, char *string, int radix)
slouken@1330
   413
{
slouken@1330
   414
    char *bufp = string;
slouken@1330
   415
slouken@1330
   416
    if ( value < 0 ) {
slouken@1330
   417
        *bufp++ = '-';
slouken@1330
   418
        value = -value;
slouken@1330
   419
    }
slouken@1330
   420
    if ( value ) {
slouken@1330
   421
        while ( value > 0 ) {
slouken@1330
   422
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   423
            value /= radix;
slouken@1330
   424
        }
slouken@1330
   425
    } else {
slouken@1330
   426
        *bufp++ = '0';
slouken@1330
   427
    }
slouken@1330
   428
    *bufp = '\0';
slouken@1330
   429
slouken@1330
   430
    /* The numbers went into the string backwards. :) */
slouken@1330
   431
    if ( *string == '-' ) {
slouken@1336
   432
        SDL_strrev(string+1);
slouken@1330
   433
    } else {
slouken@1336
   434
        SDL_strrev(string);
slouken@1330
   435
    }
slouken@1330
   436
slouken@1330
   437
    return string;
slouken@1330
   438
}
slouken@1330
   439
#endif
slouken@1330
   440
slouken@1330
   441
#ifndef HAVE__ULTOA
slouken@1330
   442
char *SDL_ultoa(unsigned long value, char *string, int radix)
slouken@1330
   443
{
slouken@1330
   444
    char *bufp = string;
slouken@1330
   445
slouken@1330
   446
    if ( value ) {
slouken@1330
   447
        while ( value > 0 ) {
slouken@1330
   448
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   449
            value /= radix;
slouken@1330
   450
        }
slouken@1330
   451
    } else {
slouken@1330
   452
        *bufp++ = '0';
slouken@1330
   453
    }
slouken@1330
   454
    *bufp = '\0';
slouken@1330
   455
slouken@1330
   456
    /* The numbers went into the string backwards. :) */
slouken@1336
   457
    SDL_strrev(string);
slouken@1330
   458
slouken@1330
   459
    return string;
slouken@1330
   460
}
slouken@1330
   461
#endif
slouken@1330
   462
slouken@1330
   463
#ifndef HAVE_STRTOL
slouken@1330
   464
long SDL_strtol(const char *string, char **endp, int base)
slouken@1330
   465
{
slouken@1330
   466
    size_t len;
slouken@1330
   467
    long value;
slouken@1330
   468
slouken@1330
   469
    len = SDL_ScanLong(string, base ? base : 10, &value);
slouken@1330
   470
    if ( endp ) {
slouken@1330
   471
        *endp = (char *)string + len;
slouken@1330
   472
    }
slouken@1330
   473
    return value;
slouken@1330
   474
}
slouken@1330
   475
#endif
slouken@1330
   476
slouken@1330
   477
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   478
slouken@1330
   479
#ifndef HAVE__I64TOA
slouken@1330
   480
char *SDL_lltoa(Sint64 value, char *string, int radix)
slouken@1330
   481
{
slouken@1330
   482
    char *bufp = string;
slouken@1330
   483
slouken@1330
   484
    if ( value < 0 ) {
slouken@1330
   485
        *bufp++ = '-';
slouken@1330
   486
        value = -value;
slouken@1330
   487
    }
slouken@1330
   488
    if ( value ) {
slouken@1330
   489
        while ( value > 0 ) {
slouken@1330
   490
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   491
            value /= radix;
slouken@1330
   492
        }
slouken@1330
   493
    } else {
slouken@1330
   494
        *bufp++ = '0';
slouken@1330
   495
    }
slouken@1330
   496
    *bufp = '\0';
slouken@1330
   497
slouken@1330
   498
    /* The numbers went into the string backwards. :) */
slouken@1330
   499
    if ( *string == '-' ) {
slouken@1336
   500
        SDL_strrev(string+1);
slouken@1330
   501
    } else {
slouken@1336
   502
        SDL_strrev(string);
slouken@1330
   503
    }
slouken@1330
   504
slouken@1330
   505
    return string;
slouken@1330
   506
}
slouken@1330
   507
#endif
slouken@1330
   508
slouken@1330
   509
#ifndef HAVE__UI64TOA
slouken@1330
   510
char *SDL_ulltoa(Uint64 value, char *string, int radix)
slouken@1330
   511
{
slouken@1330
   512
    char *bufp = string;
slouken@1330
   513
slouken@1330
   514
    if ( value ) {
slouken@1330
   515
        while ( value > 0 ) {
slouken@1330
   516
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   517
            value /= radix;
slouken@1330
   518
        }
slouken@1330
   519
    } else {
slouken@1330
   520
        *bufp++ = '0';
slouken@1330
   521
    }
slouken@1330
   522
    *bufp = '\0';
slouken@1330
   523
slouken@1330
   524
    /* The numbers went into the string backwards. :) */
slouken@1336
   525
    SDL_strrev(string);
slouken@1330
   526
slouken@1330
   527
    return string;
slouken@1330
   528
}
slouken@1330
   529
#endif
slouken@1330
   530
slouken@1330
   531
#ifndef HAVE_STRTOLL
slouken@1330
   532
Sint64 SDL_strtoll(const char *string, char **endp, int base)
slouken@1330
   533
{
slouken@1330
   534
    size_t len;
slouken@1330
   535
    Sint64 value;
slouken@1330
   536
slouken@1330
   537
    len = SDL_ScanLongLong(string, base ? base : 10, &value);
slouken@1330
   538
    if ( endp ) {
slouken@1330
   539
        *endp = (char *)string + len;
slouken@1330
   540
    }
slouken@1330
   541
    return value;
slouken@1330
   542
}
slouken@1330
   543
#endif
slouken@1330
   544
slouken@1330
   545
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   546
slouken@1330
   547
#ifndef HAVE_STRCMP
slouken@1330
   548
int SDL_strcmp(const char *str1, const char *str2)
slouken@1330
   549
{
slouken@1330
   550
    while (*str1 && *str2) {
slouken@1330
   551
        if ( *str1 != *str2 )
slouken@1330
   552
            break;
slouken@1330
   553
        ++str1;
slouken@1330
   554
        ++str2;
slouken@1330
   555
    }
slouken@1330
   556
    return (int)((unsigned char)*str1 - (unsigned char)*str2);
slouken@1330
   557
}
slouken@1330
   558
#endif
slouken@1330
   559
slouken@1330
   560
#ifndef HAVE_STRNCMP
slouken@1330
   561
int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
slouken@1330
   562
{
slouken@1330
   563
    while ( *str1 && *str2 && maxlen ) {
slouken@1330
   564
        if ( *str1 != *str2 )
slouken@1330
   565
            break;
slouken@1330
   566
        ++str1;
slouken@1330
   567
        ++str2;
slouken@1330
   568
        --maxlen;
slouken@1330
   569
    }
slouken@1330
   570
    if ( ! maxlen ) {
slouken@1330
   571
        return 0;
slouken@1330
   572
    }
slouken@1330
   573
    return (int)((unsigned char)*str1 - (unsigned char)*str2);
slouken@1330
   574
}
slouken@1330
   575
#endif
slouken@1330
   576
slouken@1330
   577
#ifndef HAVE_STRCASECMP
slouken@1330
   578
int SDL_strcasecmp(const char *str1, const char *str2)
slouken@1330
   579
{
slouken@1330
   580
    char a = 0;
slouken@1330
   581
    char b = 0;
slouken@1330
   582
    while (*str1 && *str2) {
slouken@1330
   583
        a = tolower(*str1);
slouken@1330
   584
        b = tolower(*str2);
slouken@1330
   585
        if ( a != b )
slouken@1330
   586
            break;
slouken@1330
   587
        ++str1;
slouken@1330
   588
        ++str2;
slouken@1330
   589
    }
slouken@1330
   590
    return (int)((unsigned char)a - (unsigned char)b);
slouken@1330
   591
}
slouken@1330
   592
#endif
slouken@1330
   593
slouken@1330
   594
#ifndef HAVE_SSCANF
slouken@1330
   595
int SDL_sscanf(const char *text, const char *fmt, ...)
slouken@1330
   596
{
slouken@1330
   597
    va_list ap;
slouken@1330
   598
    int retval = 0;
slouken@1330
   599
slouken@1330
   600
    va_start(ap, fmt);
slouken@1330
   601
    while ( *fmt ) {
slouken@1330
   602
        if ( *fmt == ' ' ) {
slouken@1330
   603
            while ( isspace(*text) ) {
slouken@1330
   604
                ++text;
slouken@1330
   605
            }
slouken@1330
   606
            ++fmt;
slouken@1330
   607
            continue;
slouken@1330
   608
        }
slouken@1330
   609
        if ( *fmt == '%' ) {
slouken@1330
   610
            SDL_bool done = SDL_FALSE;
slouken@1330
   611
            long count = 0;
slouken@1330
   612
            int radix = 10;
slouken@1330
   613
            enum {
slouken@1330
   614
                DO_SHORT,
slouken@1330
   615
                DO_INT,
slouken@1330
   616
                DO_LONG,
slouken@1330
   617
                DO_LONGLONG
slouken@1330
   618
            } inttype = DO_INT;
slouken@1330
   619
            SDL_bool suppress = SDL_FALSE;
slouken@1330
   620
slouken@1330
   621
            ++fmt;
slouken@1330
   622
            if ( *fmt == '%' ) {
slouken@1330
   623
                if ( *text == '%' ) {
slouken@1330
   624
                    ++text;
slouken@1330
   625
                    ++fmt;
slouken@1330
   626
                    continue;
slouken@1330
   627
                }
slouken@1330
   628
                break;
slouken@1330
   629
            }
slouken@1330
   630
            if ( *fmt == '*' ) {
slouken@1330
   631
                suppress = SDL_TRUE;
slouken@1330
   632
                ++fmt;
slouken@1330
   633
            }
slouken@1330
   634
            fmt += SDL_ScanLong(fmt, 10, &count);
slouken@1330
   635
slouken@1330
   636
            if ( *fmt == 'c' ) {
slouken@1330
   637
                if ( ! count ) {
slouken@1330
   638
                    count = 1;
slouken@1330
   639
                }
slouken@1330
   640
                if ( suppress ) {
slouken@1330
   641
                    while ( count-- ) {
slouken@1330
   642
                        ++text;
slouken@1330
   643
                    }
slouken@1330
   644
                } else {
slouken@1330
   645
                    char *valuep = va_arg(ap, char*);
slouken@1330
   646
                    while ( count-- ) {
slouken@1330
   647
                        *valuep++ = *text++;
slouken@1330
   648
                    }
slouken@1330
   649
                    ++retval;
slouken@1330
   650
                }
slouken@1330
   651
                continue;
slouken@1330
   652
            }
slouken@1330
   653
slouken@1330
   654
            while ( isspace(*text) ) {
slouken@1330
   655
                ++text;
slouken@1330
   656
            }
slouken@1330
   657
slouken@1330
   658
            /* FIXME: implement more of the format specifiers */
slouken@1330
   659
            while (!done) {
slouken@1330
   660
                switch(*fmt) {
slouken@1330
   661
                    case '*':
slouken@1330
   662
                        suppress = SDL_TRUE;
slouken@1330
   663
                        break;
slouken@1330
   664
                    case 'h':
slouken@1330
   665
                        if ( inttype > DO_SHORT ) {
slouken@1330
   666
                            ++inttype;
slouken@1330
   667
                        }
slouken@1330
   668
                        break;
slouken@1330
   669
                    case 'l':
slouken@1330
   670
                        if ( inttype < DO_LONGLONG ) {
slouken@1330
   671
                            ++inttype;
slouken@1330
   672
                        }
slouken@1330
   673
                        break;
slouken@1330
   674
                    case 'I':
slouken@1330
   675
                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
slouken@1330
   676
                            fmt += 2;
slouken@1330
   677
                            inttype = DO_LONGLONG;
slouken@1330
   678
                        }
slouken@1330
   679
                        break;
slouken@1330
   680
                    case 'i':
slouken@1330
   681
                        {
slouken@1330
   682
                            int index = 0;
slouken@1330
   683
                            if ( text[index] == '-' ) {
slouken@1330
   684
                                ++index;
slouken@1330
   685
                            }
slouken@1330
   686
                            if ( text[index] == '0' ) {
slouken@1330
   687
                                if ( tolower(text[index+1]) == 'x' ) {
slouken@1330
   688
                                    radix = 16;
slouken@1330
   689
                                } else {
slouken@1330
   690
                                    radix = 8;
slouken@1330
   691
                                }
slouken@1330
   692
                            }
slouken@1330
   693
                        }
slouken@1330
   694
                        /* Fall through to %d handling */
slouken@1330
   695
                    case 'd':
slouken@1330
   696
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   697
                        if ( inttype == DO_LONGLONG ) {
slouken@1330
   698
                            Sint64 value;
slouken@1330
   699
                            text += SDL_ScanLongLong(text, radix, &value);
slouken@1330
   700
                            if ( ! suppress ) {
slouken@1330
   701
                                Sint64 *valuep = va_arg(ap, Sint64*);
slouken@1330
   702
                                *valuep = value;
slouken@1330
   703
                                ++retval;
slouken@1330
   704
                            }
slouken@1330
   705
                        }
slouken@1330
   706
                        else
slouken@1330
   707
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   708
                        {
slouken@1330
   709
                            long value;
slouken@1330
   710
                            text += SDL_ScanLong(text, radix, &value);
slouken@1330
   711
                            if ( ! suppress ) {
slouken@1330
   712
                                switch (inttype) {
slouken@1330
   713
                                    case DO_SHORT:
slouken@1330
   714
                                        { short* valuep = va_arg(ap, short*);
slouken@1330
   715
                                            *valuep = (short)value;
slouken@1330
   716
                                        }
slouken@1330
   717
                                        break;
slouken@1330
   718
                                    case DO_INT:
slouken@1330
   719
                                        { int* valuep = va_arg(ap, int*);
slouken@1330
   720
                                            *valuep = (int)value;
slouken@1330
   721
                                        }
slouken@1330
   722
                                        break;
slouken@1330
   723
                                    case DO_LONG:
slouken@1330
   724
                                        { long* valuep = va_arg(ap, long*);
slouken@1330
   725
                                            *valuep = value;
slouken@1330
   726
                                        }
slouken@1330
   727
                                        break;
slouken@1330
   728
                                    case DO_LONGLONG:
slouken@1330
   729
                                        /* Handled above */
slouken@1330
   730
                                        break;
slouken@1330
   731
                                }
slouken@1330
   732
                                ++retval;
slouken@1330
   733
                            }
slouken@1330
   734
                        }
slouken@1330
   735
                        done = SDL_TRUE;
slouken@1330
   736
                        break;
slouken@1330
   737
                    case 'o':
slouken@1330
   738
                        if ( radix == 10 ) {
slouken@1330
   739
                            radix = 8;
slouken@1330
   740
                        }
slouken@1330
   741
                        /* Fall through to unsigned handling */
slouken@1330
   742
                    case 'x':
slouken@1330
   743
                    case 'X':
slouken@1330
   744
                        if ( radix == 10 ) {
slouken@1330
   745
                            radix = 16;
slouken@1330
   746
                        }
slouken@1330
   747
                        /* Fall through to unsigned handling */
slouken@1330
   748
                    case 'u':
slouken@1330
   749
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   750
                        if ( inttype == DO_LONGLONG ) {
slouken@1330
   751
                            Uint64 value;
slouken@1330
   752
                            text += SDL_ScanUnsignedLongLong(text, radix, &value);
slouken@1330
   753
                            if ( ! suppress ) {
slouken@1330
   754
                                Uint64 *valuep = va_arg(ap, Uint64*);
slouken@1330
   755
                                *valuep = value;
slouken@1330
   756
                                ++retval;
slouken@1330
   757
                            }
slouken@1330
   758
                        }
slouken@1330
   759
                        else
slouken@1330
   760
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   761
                        {
slouken@1330
   762
                            unsigned long value;
slouken@1330
   763
                            text += SDL_ScanUnsignedLong(text, radix, &value);
slouken@1330
   764
                            if ( ! suppress ) {
slouken@1330
   765
                                switch (inttype) {
slouken@1330
   766
                                    case DO_SHORT:
slouken@1330
   767
                                        { short* valuep = va_arg(ap, short*);
slouken@1330
   768
                                            *valuep = (short)value;
slouken@1330
   769
                                        }
slouken@1330
   770
                                        break;
slouken@1330
   771
                                    case DO_INT:
slouken@1330
   772
                                        { int* valuep = va_arg(ap, int*);
slouken@1330
   773
                                            *valuep = (int)value;
slouken@1330
   774
                                        }
slouken@1330
   775
                                        break;
slouken@1330
   776
                                    case DO_LONG:
slouken@1330
   777
                                        { long* valuep = va_arg(ap, long*);
slouken@1330
   778
                                            *valuep = value;
slouken@1330
   779
                                        }
slouken@1330
   780
                                        break;
slouken@1330
   781
                                    case DO_LONGLONG:
slouken@1330
   782
                                        /* Handled above */
slouken@1330
   783
                                        break;
slouken@1330
   784
                                }
slouken@1330
   785
                                ++retval;
slouken@1330
   786
                            }
slouken@1330
   787
                        }
slouken@1330
   788
                        done = SDL_TRUE;
slouken@1330
   789
                        break;
slouken@1330
   790
                    case 'p':
slouken@1330
   791
                        {
slouken@1330
   792
                            unsigned long value;
slouken@1330
   793
                            text += SDL_ScanUnsignedLong(text, 16, &value);
slouken@1330
   794
                            if ( ! suppress ) {
slouken@1330
   795
                                void** valuep = va_arg(ap, void**);
slouken@1330
   796
                                *valuep = (void*)value;
slouken@1330
   797
                                ++retval;
slouken@1330
   798
                            }
slouken@1330
   799
                        }
slouken@1330
   800
                        done = SDL_TRUE;
slouken@1330
   801
                        break;
slouken@1330
   802
                    case 'f':
slouken@1330
   803
                        {
slouken@1330
   804
                            double value;
slouken@1330
   805
                            text += SDL_ScanFloat(text, &value);
slouken@1330
   806
                            if ( ! suppress ) {
slouken@1330
   807
                                float* valuep = va_arg(ap, float*);
slouken@1330
   808
                                *valuep = (float)value;
slouken@1330
   809
                                ++retval;
slouken@1330
   810
                            }
slouken@1330
   811
                        }
slouken@1330
   812
                        done = SDL_TRUE;
slouken@1330
   813
                        break;
slouken@1330
   814
                    case 's':
slouken@1330
   815
                        if ( suppress ) {
slouken@1330
   816
                            while ( !isspace(*text) ) {
slouken@1330
   817
                                ++text;
slouken@1330
   818
                                if ( count ) {
slouken@1330
   819
                                    if ( --count == 0 ) {
slouken@1330
   820
                                        break;
slouken@1330
   821
                                    }
slouken@1330
   822
                                }
slouken@1330
   823
                            }
slouken@1330
   824
                        } else {
slouken@1330
   825
                            char *valuep = va_arg(ap, char*);
slouken@1330
   826
                            while ( !isspace(*text) ) {
slouken@1330
   827
                                *valuep++ = *text++;
slouken@1330
   828
                                if ( count ) {
slouken@1330
   829
                                    if ( --count == 0 ) {
slouken@1330
   830
                                        break;
slouken@1330
   831
                                    }
slouken@1330
   832
                                }
slouken@1330
   833
                            }
slouken@1330
   834
                            *valuep = '\0';
slouken@1330
   835
                            ++retval;
slouken@1330
   836
                        }
slouken@1330
   837
                        done = SDL_TRUE;
slouken@1330
   838
                        break;
slouken@1330
   839
                    default:
slouken@1330
   840
                        done = SDL_TRUE;
slouken@1330
   841
                        break;
slouken@1330
   842
                }
slouken@1330
   843
                ++fmt;
slouken@1330
   844
            }
slouken@1330
   845
            continue;
slouken@1330
   846
        }
slouken@1330
   847
        if ( *text == *fmt ) {
slouken@1330
   848
            ++text;
slouken@1330
   849
            ++fmt;
slouken@1330
   850
            continue;
slouken@1330
   851
        }
slouken@1330
   852
        /* Text didn't match format specifier */
slouken@1330
   853
        break;
slouken@1330
   854
    }
slouken@1330
   855
    va_end(ap);
slouken@1330
   856
slouken@1330
   857
    return retval;
slouken@1330
   858
}
slouken@1330
   859
#endif
slouken@1330
   860
slouken@1330
   861
#ifndef HAVE_SNPRINTF
slouken@1330
   862
int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
slouken@1330
   863
{
slouken@1330
   864
    va_list ap;
slouken@1330
   865
    int retval;
slouken@1330
   866
slouken@1330
   867
    va_start(ap, fmt);
slouken@1330
   868
    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
slouken@1330
   869
    va_end(ap);
slouken@1330
   870
slouken@1330
   871
    return retval;
slouken@1330
   872
}
slouken@1330
   873
#endif
slouken@1330
   874
slouken@1330
   875
#ifndef HAVE_VSNPRINTF
slouken@1330
   876
static size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
slouken@1330
   877
{
slouken@1330
   878
    char num[130];
slouken@1330
   879
    size_t size;
slouken@1330
   880
slouken@1336
   881
    SDL_ltoa(value, num, radix);
slouken@1330
   882
    size = SDL_strlen(num);
slouken@1330
   883
    if ( size > maxlen ) {
slouken@1330
   884
        size = maxlen;
slouken@1330
   885
    }
slouken@1336
   886
    SDL_strncpy(text, num, size);
slouken@1330
   887
slouken@1330
   888
    return size;
slouken@1330
   889
}
slouken@1330
   890
static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen)
slouken@1330
   891
{
slouken@1330
   892
    char num[130];
slouken@1330
   893
    size_t size;
slouken@1330
   894
slouken@1336
   895
    SDL_ultoa(value, num, radix);
slouken@1330
   896
    size = SDL_strlen(num);
slouken@1330
   897
    if ( size > maxlen ) {
slouken@1330
   898
        size = maxlen;
slouken@1330
   899
    }
slouken@1336
   900
    SDL_strncpy(text, num, size);
slouken@1330
   901
slouken@1330
   902
    return size;
slouken@1330
   903
}
slouken@1330
   904
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   905
static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
slouken@1330
   906
{
slouken@1330
   907
    char num[130];
slouken@1330
   908
    size_t size;
slouken@1330
   909
slouken@1336
   910
    SDL_lltoa(value, num, radix);
slouken@1330
   911
    size = SDL_strlen(num);
slouken@1330
   912
    if ( size > maxlen ) {
slouken@1330
   913
        size = maxlen;
slouken@1330
   914
    }
slouken@1336
   915
    SDL_strncpy(text, num, size);
slouken@1330
   916
slouken@1330
   917
    return size;
slouken@1330
   918
}
slouken@1330
   919
static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
slouken@1330
   920
{
slouken@1330
   921
    char num[130];
slouken@1330
   922
    size_t size;
slouken@1330
   923
slouken@1336
   924
    SDL_ulltoa(value, num, radix);
slouken@1330
   925
    size = SDL_strlen(num);
slouken@1330
   926
    if ( size > maxlen ) {
slouken@1330
   927
        size = maxlen;
slouken@1330
   928
    }
slouken@1336
   929
    SDL_strncpy(text, num, size);
slouken@1330
   930
slouken@1330
   931
    return size;
slouken@1330
   932
}
slouken@1330
   933
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   934
static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen)
slouken@1330
   935
{
slouken@1330
   936
    char *textstart = text;
slouken@1330
   937
    if ( arg ) {
slouken@1330
   938
        /* This isn't especially accurate, but hey, it's easy. :) */
slouken@1330
   939
        const double precision = 0.00000001;
slouken@1330
   940
        size_t len;
slouken@1330
   941
        unsigned long value;
slouken@1330
   942
slouken@1330
   943
        if ( arg < 0 ) {
slouken@1330
   944
            *text++ = '-';
slouken@1330
   945
            --maxlen;
slouken@1330
   946
            arg = -arg;
slouken@1330
   947
        }
slouken@1330
   948
        value = (unsigned long)arg;
slouken@1330
   949
        len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
slouken@1330
   950
        text += len;
slouken@1330
   951
        maxlen -= len;
slouken@1330
   952
        arg -= value;
slouken@1330
   953
        if ( arg > precision && maxlen ) {
slouken@1330
   954
            int mult = 10;
slouken@1330
   955
            *text++ = '.';
slouken@1330
   956
            while ( (arg > precision) && maxlen ) {
slouken@1330
   957
                value = (unsigned long)(arg * mult);
slouken@1330
   958
                len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
slouken@1330
   959
                text += len;
slouken@1330
   960
                maxlen -= len;
slouken@1330
   961
                arg -= (double)value / mult;
slouken@1330
   962
                mult *= 10;
slouken@1330
   963
            }
slouken@1330
   964
        }
slouken@1330
   965
    } else {
slouken@1330
   966
        *text++ = '0';
slouken@1330
   967
    }
slouken@1330
   968
    return (text - textstart);
slouken@1330
   969
}
slouken@1330
   970
static size_t SDL_PrintString(char *text, const char *string, size_t maxlen)
slouken@1330
   971
{
slouken@1330
   972
    char *textstart = text;
slouken@1330
   973
    while ( *string && maxlen-- ) {
slouken@1330
   974
        *text++ = *string++;
slouken@1330
   975
    }
slouken@1330
   976
    return (text - textstart);
slouken@1330
   977
}
slouken@1330
   978
int  SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@1330
   979
{
slouken@1330
   980
    char *textstart = text;
slouken@1330
   981
    if ( maxlen <= 0 ) {
slouken@1330
   982
        return 0;
slouken@1330
   983
    }
slouken@1330
   984
    --maxlen; /* For the trailing '\0' */
slouken@1330
   985
    while ( *fmt && maxlen ) {
slouken@1330
   986
        if ( *fmt == '%' ) {
slouken@1330
   987
            SDL_bool done = SDL_FALSE;
slouken@1330
   988
            size_t len = 0;
slouken@1330
   989
            SDL_bool do_lowercase = SDL_FALSE;
slouken@1330
   990
            int radix = 10;
slouken@1330
   991
            enum {
slouken@1330
   992
                DO_INT,
slouken@1330
   993
                DO_LONG,
slouken@1330
   994
                DO_LONGLONG
slouken@1330
   995
            } inttype = DO_INT;
slouken@1330
   996
slouken@1330
   997
            ++fmt;
slouken@1330
   998
            /* FIXME: implement more of the format specifiers */
slouken@1330
   999
            while (!done) {
slouken@1330
  1000
                switch(*fmt) {
slouken@1330
  1001
                    case '%':
slouken@1330
  1002
                        *text = '%';
slouken@1330
  1003
                        len = 1;
slouken@1330
  1004
                        done = SDL_TRUE;
slouken@1330
  1005
                        break;
slouken@1330
  1006
                    case 'c':
slouken@1330
  1007
                        /* char is promoted to int when passed through (...) */
slouken@1330
  1008
                        *text = (char)va_arg(ap, int);
slouken@1330
  1009
                        len = 1;
slouken@1330
  1010
                        done = SDL_TRUE;
slouken@1330
  1011
                        break;
slouken@1330
  1012
                    case 'h':
slouken@1330
  1013
                        /* short is promoted to int when passed through (...) */
slouken@1330
  1014
                        break;
slouken@1330
  1015
                    case 'l':
slouken@1330
  1016
                        if ( inttype < DO_LONGLONG ) {
slouken@1330
  1017
                            ++inttype;
slouken@1330
  1018
                        }
slouken@1330
  1019
                        break;
slouken@1330
  1020
                    case 'I':
slouken@1330
  1021
                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
slouken@1330
  1022
                            fmt += 2;
slouken@1330
  1023
                            inttype = DO_LONGLONG;
slouken@1330
  1024
                        }
slouken@1330
  1025
                        break;
slouken@1330
  1026
                    case 'i':
slouken@1330
  1027
                    case 'd':
slouken@1330
  1028
                        switch (inttype) {
slouken@1330
  1029
                            case DO_INT:
slouken@1330
  1030
                                len = SDL_PrintLong(text, (long)va_arg(ap, int), radix, maxlen);
slouken@1330
  1031
                                break;
slouken@1330
  1032
                            case DO_LONG:
slouken@1330
  1033
                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
slouken@1330
  1034
                                break;
slouken@1330
  1035
                            case DO_LONGLONG:
slouken@1330
  1036
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
  1037
                                len = SDL_PrintLongLong(text, va_arg(ap, Sint64), radix, maxlen);
slouken@1330
  1038
#else
slouken@1330
  1039
                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
slouken@1330
  1040
#endif
slouken@1330
  1041
                                break;
slouken@1330
  1042
                        }
slouken@1330
  1043
                        done = SDL_TRUE;
slouken@1330
  1044
                        break;
slouken@1330
  1045
                    case 'p':
slouken@1330
  1046
                    case 'x':
slouken@1330
  1047
                        do_lowercase = SDL_TRUE;
slouken@1330
  1048
                        /* Fall through to 'X' handling */
slouken@1330
  1049
                    case 'X':
slouken@1330
  1050
                        if ( radix == 10 ) {
slouken@1330
  1051
                            radix = 16;
slouken@1330
  1052
                        }
slouken@1330
  1053
                        if ( *fmt == 'p' ) {
slouken@1330
  1054
                            inttype = DO_LONG;
slouken@1330
  1055
                        }
slouken@1330
  1056
                        /* Fall through to unsigned handling */
slouken@1330
  1057
                    case 'o':
slouken@1330
  1058
                        if ( radix == 10 ) {
slouken@1330
  1059
                            radix = 8;
slouken@1330
  1060
                        }
slouken@1330
  1061
                        /* Fall through to unsigned handling */
slouken@1330
  1062
                    case 'u':
slouken@1330
  1063
                        switch (inttype) {
slouken@1330
  1064
                            case DO_INT:
slouken@1330
  1065
                                len = SDL_PrintUnsignedLong(text, (unsigned long)va_arg(ap, unsigned int), radix, maxlen);
slouken@1330
  1066
                                break;
slouken@1330
  1067
                            case DO_LONG:
slouken@1330
  1068
                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
slouken@1330
  1069
                                break;
slouken@1330
  1070
                            case DO_LONGLONG:
slouken@1330
  1071
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
  1072
                                len = SDL_PrintUnsignedLongLong(text, va_arg(ap, Uint64), radix, maxlen);
slouken@1330
  1073
#else
slouken@1330
  1074
                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
slouken@1330
  1075
#endif
slouken@1330
  1076
                                break;
slouken@1330
  1077
                        }
slouken@1330
  1078
                        if ( do_lowercase ) {
slouken@1336
  1079
                            SDL_strlwr(text);
slouken@1330
  1080
                        }
slouken@1330
  1081
                        done = SDL_TRUE;
slouken@1330
  1082
                        break;
slouken@1330
  1083
                    case 'f':
slouken@1330
  1084
                        len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
slouken@1330
  1085
                        done = SDL_TRUE;
slouken@1330
  1086
                        break;
slouken@1330
  1087
                    case 's':
slouken@1330
  1088
                        len = SDL_PrintString(text, va_arg(ap, char*), maxlen);
slouken@1330
  1089
                        done = SDL_TRUE;
slouken@1330
  1090
                        break;
slouken@1330
  1091
                    default:
slouken@1330
  1092
                        done = SDL_TRUE;
slouken@1330
  1093
                        break;
slouken@1330
  1094
                }
slouken@1330
  1095
                ++fmt;
slouken@1330
  1096
            }
slouken@1330
  1097
            text += len;
slouken@1330
  1098
            maxlen -= len;
slouken@1330
  1099
        } else {
slouken@1330
  1100
            *text++ = *fmt++;
slouken@1330
  1101
            --maxlen;
slouken@1330
  1102
        }
slouken@1330
  1103
    }
slouken@1330
  1104
    *text = '\0';
slouken@1330
  1105
slouken@1330
  1106
    return (text - textstart);
slouken@1330
  1107
}
slouken@1330
  1108
#endif