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