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