src/stdlib/SDL_string.c
author Sam Lantinga
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1867 887c3600826b
child 1901 f1828a500391
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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@1895
    33
static size_t
slouken@1895
    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@1895
    40
    if (*text == '-') {
slouken@1330
    41
        negative = SDL_TRUE;
slouken@1330
    42
        ++text;
slouken@1330
    43
    }
slouken@1895
    44
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
    45
        text += 2;
slouken@1330
    46
    }
slouken@1895
    47
    for (;;) {
slouken@1330
    48
        int v;
slouken@1895
    49
        if (SDL_isdigit(*text)) {
slouken@1330
    50
            v = *text - '0';
slouken@1895
    51
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
    52
            v = 10 + (*text - 'A');
slouken@1895
    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@1895
    62
    if (valuep) {
slouken@1895
    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@1895
    74
static size_t
slouken@1895
    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@1895
    80
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
    81
        text += 2;
slouken@1330
    82
    }
slouken@1895
    83
    for (;;) {
slouken@1330
    84
        int v;
slouken@1895
    85
        if (SDL_isdigit(*text)) {
slouken@1330
    86
            v = *text - '0';
slouken@1895
    87
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
    88
            v = 10 + (*text - 'A');
slouken@1895
    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@1895
    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@1895
   106
static size_t
slouken@1895
   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@1895
   112
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1456
   113
        text += 2;
slouken@1456
   114
    }
slouken@1895
   115
    for (;;) {
slouken@1456
   116
        int v;
slouken@1895
   117
        if (SDL_isdigit(*text)) {
slouken@1456
   118
            v = *text - '0';
slouken@1895
   119
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1456
   120
            v = 10 + (*text - 'A');
slouken@1895
   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@1895
   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@1895
   139
static size_t
slouken@1895
   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@1895
   146
    if (*text == '-') {
slouken@1330
   147
        negative = SDL_TRUE;
slouken@1330
   148
        ++text;
slouken@1330
   149
    }
slouken@1895
   150
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
   151
        text += 2;
slouken@1330
   152
    }
slouken@1895
   153
    for (;;) {
slouken@1330
   154
        int v;
slouken@1895
   155
        if (SDL_isdigit(*text)) {
slouken@1330
   156
            v = *text - '0';
slouken@1895
   157
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
   158
            v = 10 + (*text - 'A');
slouken@1895
   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@1895
   168
    if (valuep) {
slouken@1895
   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@1895
   180
static size_t
slouken@1895
   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@1895
   186
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
   187
        text += 2;
slouken@1330
   188
    }
slouken@1895
   189
    for (;;) {
slouken@1330
   190
        int v;
slouken@1895
   191
        if (SDL_isdigit(*text)) {
slouken@1330
   192
            v = *text - '0';
slouken@1895
   193
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
   194
            v = 10 + (*text - 'A');
slouken@1895
   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@1895
   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@1895
   213
static size_t
slouken@1895
   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@1895
   221
    if (*text == '-') {
slouken@1330
   222
        negative = SDL_TRUE;
slouken@1330
   223
        ++text;
slouken@1330
   224
    }
slouken@1330
   225
    text += SDL_ScanUnsignedLong(text, 10, &lvalue);
slouken@1330
   226
    value += lvalue;
slouken@1895
   227
    if (*text == '.') {
slouken@1330
   228
        int mult = 10;
slouken@1330
   229
        ++text;
slouken@1895
   230
        while (SDL_isdigit(*text)) {
slouken@1330
   231
            lvalue = *text - '0';
slouken@1895
   232
            value += (double) lvalue / mult;
slouken@1330
   233
            mult *= 10;
slouken@1330
   234
            ++text;
slouken@1330
   235
        }
slouken@1330
   236
    }
slouken@1895
   237
    if (valuep) {
slouken@1895
   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@1895
   249
void *
slouken@1895
   250
SDL_memset(void *dst, int c, size_t len)
slouken@1330
   251
{
slouken@1330
   252
    size_t left = (len % 4);
slouken@1895
   253
    if (len >= 4) {
slouken@1330
   254
        Uint32 value = 0;
slouken@1895
   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@1895
   262
        while (len--) {
slouken@1330
   263
            *dstp++ = value;
slouken@1330
   264
        }
slouken@1330
   265
    }
slouken@1895
   266
    if (left > 0) {
slouken@1895
   267
        Uint8 value = (Uint8) c;
slouken@1895
   268
        Uint8 *dstp = (Uint8 *) dst;
slouken@1895
   269
        switch (left) {
slouken@1895
   270
        case 3:
slouken@1330
   271
            *dstp++ = value;
slouken@1895
   272
        case 2:
slouken@1330
   273
            *dstp++ = value;
slouken@1895
   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@1895
   283
void *
slouken@1895
   284
SDL_memcpy(void *dst, const void *src, size_t len)
slouken@1330
   285
{
slouken@1895
   286
    char *srcp = (char *) src;
slouken@1895
   287
    char *dstp = (char *) dst;
slouken@1895
   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@1895
   296
void *
slouken@1895
   297
SDL_revcpy(void *dst, const void *src, size_t len)
slouken@1330
   298
{
slouken@1895
   299
    char *srcp = (char *) src;
slouken@1895
   300
    char *dstp = (char *) dst;
slouken@1330
   301
    srcp += len;
slouken@1330
   302
    dstp += len;
slouken@1895
   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@1895
   311
int
slouken@1895
   312
SDL_memcmp(const void *s1, const void *s2, size_t len)
slouken@1330
   313
{
slouken@1895
   314
    char *s1p = (char *) s1;
slouken@1895
   315
    char *s2p = (char *) s2;
slouken@1895
   316
    while (len--) {
slouken@1895
   317
        if (*s1p != *s2p) {
slouken@1330
   318
            return (*s1p - *s2p);
slouken@1895
   319
        }
slouken@1895
   320
        ++s1p;
slouken@1895
   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@1895
   328
size_t
slouken@1895
   329
SDL_strlen(const char *string)
slouken@1330
   330
{
slouken@1330
   331
    size_t len = 0;
slouken@1895
   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@1895
   340
size_t
slouken@1895
   341
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
slouken@1330
   342
{
slouken@1379
   343
    size_t srclen = SDL_strlen(src);
slouken@1895
   344
    if (maxlen > 0) {
slouken@1895
   345
        size_t len = SDL_min(srclen, maxlen - 1);
slouken@1379
   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@1895
   354
size_t
slouken@1895
   355
SDL_strlcat(char *dst, const char *src, size_t maxlen)
slouken@1330
   356
{
slouken@1379
   357
    size_t dstlen = SDL_strlen(dst);
slouken@1379
   358
    size_t srclen = SDL_strlen(src);
slouken@1895
   359
    if (dstlen < maxlen) {
slouken@1895
   360
        SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
slouken@1330
   361
    }
slouken@1895
   362
    return dstlen + srclen;
slouken@1330
   363
}
slouken@1330
   364
#endif
slouken@1330
   365
slouken@1341
   366
#ifndef HAVE_STRDUP
slouken@1895
   367
char *
slouken@1895
   368
SDL_strdup(const char *string)
slouken@1341
   369
{
slouken@1895
   370
    size_t len = SDL_strlen(string) + 1;
slouken@1379
   371
    char *newstr = SDL_malloc(len);
slouken@1895
   372
    if (newstr) {
slouken@1379
   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@1895
   380
char *
slouken@1895
   381
SDL_strrev(char *string)
slouken@1330
   382
{
slouken@1330
   383
    size_t len = SDL_strlen(string);
slouken@1330
   384
    char *a = &string[0];
slouken@1895
   385
    char *b = &string[len - 1];
slouken@1330
   386
    len /= 2;
slouken@1895
   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@1895
   397
char *
slouken@1895
   398
SDL_strupr(char *string)
slouken@1330
   399
{
slouken@1330
   400
    char *bufp = string;
slouken@1895
   401
    while (*bufp) {
slouken@1358
   402
        *bufp = SDL_toupper(*bufp);
slouken@1895
   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@1895
   410
char *
slouken@1895
   411
SDL_strlwr(char *string)
slouken@1330
   412
{
slouken@1330
   413
    char *bufp = string;
slouken@1895
   414
    while (*bufp) {
slouken@1358
   415
        *bufp = SDL_tolower(*bufp);
slouken@1895
   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@1895
   423
char *
slouken@1895
   424
SDL_strchr(const char *string, int c)
slouken@1330
   425
{
slouken@1895
   426
    while (*string) {
slouken@1895
   427
        if (*string == c) {
slouken@1895
   428
            return (char *) string;
slouken@1330
   429
        }
slouken@1895
   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@1895
   437
char *
slouken@1895
   438
SDL_strrchr(const char *string, int c)
slouken@1330
   439
{
slouken@1336
   440
    const char *bufp = string + SDL_strlen(string) - 1;
slouken@1895
   441
    while (bufp >= string) {
slouken@1895
   442
        if (*bufp == c) {
slouken@1895
   443
            return (char *) bufp;
slouken@1330
   444
        }
slouken@1895
   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@1895
   452
char *
slouken@1895
   453
SDL_strstr(const char *haystack, const char *needle)
slouken@1330
   454
{
slouken@1336
   455
    size_t length = SDL_strlen(needle);
slouken@1895
   456
    while (*haystack) {
slouken@1895
   457
        if (SDL_strncmp(haystack, needle, length) == 0) {
slouken@1895
   458
            return (char *) haystack;
slouken@1330
   459
        }
slouken@1895
   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@1895
   477
char *
slouken@1895
   478
SDL_ltoa(long value, char *string, int radix)
slouken@1330
   479
{
slouken@1330
   480
    char *bufp = string;
slouken@1330
   481
slouken@1895
   482
    if (value < 0) {
slouken@1330
   483
        *bufp++ = '-';
slouken@1330
   484
        value = -value;
slouken@1330
   485
    }
slouken@1895
   486
    if (value) {
slouken@1895
   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@1895
   497
    if (*string == '-') {
slouken@1895
   498
        SDL_strrev(string + 1);
slouken@1330
   499
    } else {
slouken@1336
   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@1895
   508
char *
slouken@1895
   509
SDL_ultoa(unsigned long value, char *string, int radix)
slouken@1330
   510
{
slouken@1330
   511
    char *bufp = string;
slouken@1330
   512
slouken@1895
   513
    if (value) {
slouken@1895
   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@1336
   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@1895
   531
long
slouken@1895
   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@1895
   537
    if (!base) {
slouken@1895
   538
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   539
            base = 16;
slouken@1867
   540
        } else {
slouken@1867
   541
            base = 10;
slouken@1867
   542
        }
slouken@1867
   543
    }
slouken@1867
   544
slouken@1867
   545
    len = SDL_ScanLong(string, base, &value);
slouken@1895
   546
    if (endp) {
slouken@1895
   547
        *endp = (char *) string + len;
slouken@1330
   548
    }
slouken@1330
   549
    return value;
slouken@1330
   550
}
slouken@1330
   551
#endif
slouken@1330
   552
slouken@1456
   553
#ifndef HAVE_STRTOUL
slouken@1895
   554
unsigned long
slouken@1895
   555
SDL_strtoul(const char *string, char **endp, int base)
slouken@1456
   556
{
slouken@1456
   557
    size_t len;
slouken@1456
   558
    unsigned long value;
slouken@1456
   559
slouken@1895
   560
    if (!base) {
slouken@1895
   561
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   562
            base = 16;
slouken@1867
   563
        } else {
slouken@1867
   564
            base = 10;
slouken@1867
   565
        }
slouken@1867
   566
    }
slouken@1867
   567
slouken@1867
   568
    len = SDL_ScanUnsignedLong(string, base, &value);
slouken@1895
   569
    if (endp) {
slouken@1895
   570
        *endp = (char *) string + len;
slouken@1456
   571
    }
slouken@1456
   572
    return value;
slouken@1456
   573
}
slouken@1456
   574
#endif
slouken@1456
   575
slouken@1330
   576
#ifdef SDL_HAS_64BIT_TYPE
slouken@1330
   577
slouken@1330
   578
#ifndef HAVE__I64TOA
slouken@1895
   579
char *
slouken@1895
   580
SDL_lltoa(Sint64 value, char *string, int radix)
slouken@1330
   581
{
slouken@1330
   582
    char *bufp = string;
slouken@1330
   583
slouken@1895
   584
    if (value < 0) {
slouken@1330
   585
        *bufp++ = '-';
slouken@1330
   586
        value = -value;
slouken@1330
   587
    }
slouken@1895
   588
    if (value) {
slouken@1895
   589
        while (value > 0) {
slouken@1330
   590
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   591
            value /= radix;
slouken@1330
   592
        }
slouken@1330
   593
    } else {
slouken@1330
   594
        *bufp++ = '0';
slouken@1330
   595
    }
slouken@1330
   596
    *bufp = '\0';
slouken@1330
   597
slouken@1330
   598
    /* The numbers went into the string backwards. :) */
slouken@1895
   599
    if (*string == '-') {
slouken@1895
   600
        SDL_strrev(string + 1);
slouken@1330
   601
    } else {
slouken@1336
   602
        SDL_strrev(string);
slouken@1330
   603
    }
slouken@1330
   604
slouken@1330
   605
    return string;
slouken@1330
   606
}
slouken@1330
   607
#endif
slouken@1330
   608
slouken@1330
   609
#ifndef HAVE__UI64TOA
slouken@1895
   610
char *
slouken@1895
   611
SDL_ulltoa(Uint64 value, char *string, int radix)
slouken@1330
   612
{
slouken@1330
   613
    char *bufp = string;
slouken@1330
   614
slouken@1895
   615
    if (value) {
slouken@1895
   616
        while (value > 0) {
slouken@1330
   617
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   618
            value /= radix;
slouken@1330
   619
        }
slouken@1330
   620
    } else {
slouken@1330
   621
        *bufp++ = '0';
slouken@1330
   622
    }
slouken@1330
   623
    *bufp = '\0';
slouken@1330
   624
slouken@1330
   625
    /* The numbers went into the string backwards. :) */
slouken@1336
   626
    SDL_strrev(string);
slouken@1330
   627
slouken@1330
   628
    return string;
slouken@1330
   629
}
slouken@1330
   630
#endif
slouken@1330
   631
slouken@1330
   632
#ifndef HAVE_STRTOLL
slouken@1895
   633
Sint64
slouken@1895
   634
SDL_strtoll(const char *string, char **endp, int base)
slouken@1330
   635
{
slouken@1330
   636
    size_t len;
slouken@1330
   637
    Sint64 value;
slouken@1330
   638
slouken@1895
   639
    if (!base) {
slouken@1895
   640
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   641
            base = 16;
slouken@1867
   642
        } else {
slouken@1867
   643
            base = 10;
slouken@1867
   644
        }
slouken@1867
   645
    }
slouken@1867
   646
slouken@1867
   647
    len = SDL_ScanLongLong(string, base, &value);
slouken@1895
   648
    if (endp) {
slouken@1895
   649
        *endp = (char *) string + len;
slouken@1330
   650
    }
slouken@1330
   651
    return value;
slouken@1330
   652
}
slouken@1330
   653
#endif
slouken@1330
   654
slouken@1456
   655
#ifndef HAVE_STRTOULL
slouken@1895
   656
Uint64
slouken@1895
   657
SDL_strtoull(const char *string, char **endp, int base)
slouken@1456
   658
{
slouken@1456
   659
    size_t len;
slouken@1456
   660
    Uint64 value;
slouken@1456
   661
slouken@1895
   662
    if (!base) {
slouken@1895
   663
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   664
            base = 16;
slouken@1867
   665
        } else {
slouken@1867
   666
            base = 10;
slouken@1867
   667
        }
slouken@1867
   668
    }
slouken@1867
   669
slouken@1867
   670
    len = SDL_ScanUnsignedLongLong(string, base, &value);
slouken@1895
   671
    if (endp) {
slouken@1895
   672
        *endp = (char *) string + len;
slouken@1456
   673
    }
slouken@1456
   674
    return value;
slouken@1456
   675
}
slouken@1456
   676
#endif
slouken@1456
   677
slouken@1330
   678
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1330
   679
slouken@1341
   680
#ifndef HAVE_STRTOD
slouken@1895
   681
double
slouken@1895
   682
SDL_strtod(const char *string, char **endp)
slouken@1341
   683
{
slouken@1341
   684
    size_t len;
slouken@1341
   685
    double value;
slouken@1341
   686
slouken@1341
   687
    len = SDL_ScanFloat(string, &value);
slouken@1895
   688
    if (endp) {
slouken@1895
   689
        *endp = (char *) string + len;
slouken@1341
   690
    }
slouken@1341
   691
    return value;
slouken@1341
   692
}
slouken@1341
   693
#endif
slouken@1341
   694
slouken@1330
   695
#ifndef HAVE_STRCMP
slouken@1895
   696
int
slouken@1895
   697
SDL_strcmp(const char *str1, const char *str2)
slouken@1330
   698
{
slouken@1330
   699
    while (*str1 && *str2) {
slouken@1895
   700
        if (*str1 != *str2)
slouken@1330
   701
            break;
slouken@1330
   702
        ++str1;
slouken@1330
   703
        ++str2;
slouken@1330
   704
    }
slouken@1895
   705
    return (int) ((unsigned char) *str1 - (unsigned char) *str2);
slouken@1330
   706
}
slouken@1330
   707
#endif
slouken@1330
   708
slouken@1330
   709
#ifndef HAVE_STRNCMP
slouken@1895
   710
int
slouken@1895
   711
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
slouken@1330
   712
{
slouken@1895
   713
    while (*str1 && *str2 && maxlen) {
slouken@1895
   714
        if (*str1 != *str2)
slouken@1330
   715
            break;
slouken@1330
   716
        ++str1;
slouken@1330
   717
        ++str2;
slouken@1330
   718
        --maxlen;
slouken@1330
   719
    }
slouken@1895
   720
    if (!maxlen) {
slouken@1330
   721
        return 0;
slouken@1330
   722
    }
slouken@1895
   723
    return (int) ((unsigned char) *str1 - (unsigned char) *str2);
slouken@1330
   724
}
slouken@1330
   725
#endif
slouken@1330
   726
slouken@1510
   727
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
slouken@1895
   728
int
slouken@1895
   729
SDL_strcasecmp(const char *str1, const char *str2)
slouken@1330
   730
{
slouken@1330
   731
    char a = 0;
slouken@1330
   732
    char b = 0;
slouken@1895
   733
    while (*str1 && *str2) {
slouken@1358
   734
        a = SDL_tolower(*str1);
slouken@1358
   735
        b = SDL_tolower(*str2);
slouken@1895
   736
        if (a != b)
slouken@1330
   737
            break;
slouken@1330
   738
        ++str1;
slouken@1330
   739
        ++str2;
slouken@1330
   740
    }
slouken@1895
   741
    return (int) ((unsigned char) a - (unsigned char) b);
slouken@1330
   742
}
slouken@1330
   743
#endif
slouken@1330
   744
slouken@1512
   745
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
slouken@1895
   746
int
slouken@1895
   747
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
slouken@1501
   748
{
slouken@1501
   749
    char a = 0;
slouken@1501
   750
    char b = 0;
slouken@1895
   751
    while (*str1 && *str2 && maxlen) {
slouken@1501
   752
        a = SDL_tolower(*str1);
slouken@1501
   753
        b = SDL_tolower(*str2);
slouken@1895
   754
        if (a != b)
slouken@1501
   755
            break;
slouken@1501
   756
        ++str1;
slouken@1501
   757
        ++str2;
slouken@1501
   758
        --maxlen;
slouken@1501
   759
    }
slouken@1895
   760
    return (int) ((unsigned char) a - (unsigned char) b);
slouken@1501
   761
}
slouken@1501
   762
#endif
slouken@1501
   763
slouken@1330
   764
#ifndef HAVE_SSCANF
slouken@1895
   765
int
slouken@1895
   766
SDL_sscanf(const char *text, const char *fmt, ...)
slouken@1330
   767
{
slouken@1330
   768
    va_list ap;
slouken@1330
   769
    int retval = 0;
slouken@1330
   770
slouken@1330
   771
    va_start(ap, fmt);
slouken@1895
   772
    while (*fmt) {
slouken@1895
   773
        if (*fmt == ' ') {
slouken@1895
   774
            while (SDL_isspace(*text)) {
slouken@1330
   775
                ++text;
slouken@1330
   776
            }
slouken@1330
   777
            ++fmt;
slouken@1330
   778
            continue;
slouken@1330
   779
        }
slouken@1895
   780
        if (*fmt == '%') {
slouken@1330
   781
            SDL_bool done = SDL_FALSE;
slouken@1330
   782
            long count = 0;
slouken@1330
   783
            int radix = 10;
slouken@1895
   784
            enum
slouken@1895
   785
            {
slouken@1330
   786
                DO_SHORT,
slouken@1330
   787
                DO_INT,
slouken@1330
   788
                DO_LONG,
slouken@1330
   789
                DO_LONGLONG
slouken@1330
   790
            } inttype = DO_INT;
slouken@1330
   791
            SDL_bool suppress = SDL_FALSE;
slouken@1330
   792
slouken@1330
   793
            ++fmt;
slouken@1895
   794
            if (*fmt == '%') {
slouken@1895
   795
                if (*text == '%') {
slouken@1330
   796
                    ++text;
slouken@1330
   797
                    ++fmt;
slouken@1330
   798
                    continue;
slouken@1330
   799
                }
slouken@1330
   800
                break;
slouken@1330
   801
            }
slouken@1895
   802
            if (*fmt == '*') {
slouken@1330
   803
                suppress = SDL_TRUE;
slouken@1330
   804
                ++fmt;
slouken@1330
   805
            }
slouken@1330
   806
            fmt += SDL_ScanLong(fmt, 10, &count);
slouken@1330
   807
slouken@1895
   808
            if (*fmt == 'c') {
slouken@1895
   809
                if (!count) {
slouken@1330
   810
                    count = 1;
slouken@1330
   811
                }
slouken@1895
   812
                if (suppress) {
slouken@1895
   813
                    while (count--) {
slouken@1330
   814
                        ++text;
slouken@1330
   815
                    }
slouken@1330
   816
                } else {
slouken@1895
   817
                    char *valuep = va_arg(ap, char *);
slouken@1895
   818
                    while (count--) {
slouken@1330
   819
                        *valuep++ = *text++;
slouken@1330
   820
                    }
slouken@1330
   821
                    ++retval;
slouken@1330
   822
                }
slouken@1330
   823
                continue;
slouken@1330
   824
            }
slouken@1330
   825
slouken@1895
   826
            while (SDL_isspace(*text)) {
slouken@1330
   827
                ++text;
slouken@1330
   828
            }
slouken@1330
   829
slouken@1330
   830
            /* FIXME: implement more of the format specifiers */
slouken@1330
   831
            while (!done) {
slouken@1895
   832
                switch (*fmt) {
slouken@1895
   833
                case '*':
slouken@1895
   834
                    suppress = SDL_TRUE;
slouken@1895
   835
                    break;
slouken@1895
   836
                case 'h':
slouken@1895
   837
                    if (inttype > DO_SHORT) {
slouken@1895
   838
                        ++inttype;
slouken@1895
   839
                    }
slouken@1895
   840
                    break;
slouken@1895
   841
                case 'l':
slouken@1895
   842
                    if (inttype < DO_LONGLONG) {
slouken@1895
   843
                        ++inttype;
slouken@1895
   844
                    }
slouken@1895
   845
                    break;
slouken@1895
   846
                case 'I':
slouken@1895
   847
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
   848
                        fmt += 2;
slouken@1895
   849
                        inttype = DO_LONGLONG;
slouken@1895
   850
                    }
slouken@1895
   851
                    break;
slouken@1895
   852
                case 'i':
slouken@1895
   853
                    {
slouken@1895
   854
                        int index = 0;
slouken@1895
   855
                        if (text[index] == '-') {
slouken@1895
   856
                            ++index;
slouken@1895
   857
                        }
slouken@1895
   858
                        if (text[index] == '0') {
slouken@1895
   859
                            if (SDL_tolower(text[index + 1]) == 'x') {
slouken@1895
   860
                                radix = 16;
slouken@1895
   861
                            } else {
slouken@1895
   862
                                radix = 8;
slouken@1895
   863
                            }
slouken@1330
   864
                        }
slouken@1895
   865
                    }
slouken@1895
   866
                    /* Fall through to %d handling */
slouken@1895
   867
                case 'd':
slouken@1895
   868
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
   869
                    if (inttype == DO_LONGLONG) {
slouken@1895
   870
                        Sint64 value;
slouken@1895
   871
                        text += SDL_ScanLongLong(text, radix, &value);
slouken@1895
   872
                        if (!suppress) {
slouken@1895
   873
                            Sint64 *valuep = va_arg(ap, Sint64 *);
slouken@1895
   874
                            *valuep = value;
slouken@1895
   875
                            ++retval;
slouken@1895
   876
                        }
slouken@1895
   877
                    } else
slouken@1895
   878
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1895
   879
                    {
slouken@1895
   880
                        long value;
slouken@1895
   881
                        text += SDL_ScanLong(text, radix, &value);
slouken@1895
   882
                        if (!suppress) {
slouken@1895
   883
                            switch (inttype) {
slouken@1895
   884
                            case DO_SHORT:
slouken@1895
   885
                                {
slouken@1895
   886
                                    short *valuep = va_arg(ap, short *);
slouken@1895
   887
                                    *valuep = (short) value;
slouken@1895
   888
                                }
slouken@1895
   889
                                break;
slouken@1895
   890
                            case DO_INT:
slouken@1895
   891
                                {
slouken@1895
   892
                                    int *valuep = va_arg(ap, int *);
slouken@1895
   893
                                    *valuep = (int) value;
slouken@1895
   894
                                }
slouken@1895
   895
                                break;
slouken@1895
   896
                            case DO_LONG:
slouken@1895
   897
                                {
slouken@1895
   898
                                    long *valuep = va_arg(ap, long *);
slouken@1895
   899
                                    *valuep = value;
slouken@1895
   900
                                }
slouken@1895
   901
                                break;
slouken@1895
   902
                            case DO_LONGLONG:
slouken@1895
   903
                                /* Handled above */
slouken@1895
   904
                                break;
slouken@1895
   905
                            }
slouken@1895
   906
                            ++retval;
slouken@1330
   907
                        }
slouken@1895
   908
                    }
slouken@1895
   909
                    done = SDL_TRUE;
slouken@1895
   910
                    break;
slouken@1895
   911
                case 'o':
slouken@1895
   912
                    if (radix == 10) {
slouken@1895
   913
                        radix = 8;
slouken@1895
   914
                    }
slouken@1895
   915
                    /* Fall through to unsigned handling */
slouken@1895
   916
                case 'x':
slouken@1895
   917
                case 'X':
slouken@1895
   918
                    if (radix == 10) {
slouken@1895
   919
                        radix = 16;
slouken@1895
   920
                    }
slouken@1895
   921
                    /* Fall through to unsigned handling */
slouken@1895
   922
                case 'u':
slouken@1895
   923
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
   924
                    if (inttype == DO_LONGLONG) {
slouken@1895
   925
                        Uint64 value;
slouken@1895
   926
                        text += SDL_ScanUnsignedLongLong(text, radix, &value);
slouken@1895
   927
                        if (!suppress) {
slouken@1895
   928
                            Uint64 *valuep = va_arg(ap, Uint64 *);
slouken@1895
   929
                            *valuep = value;
slouken@1895
   930
                            ++retval;
slouken@1330
   931
                        }
slouken@1895
   932
                    } else
slouken@1895
   933
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1895
   934
                    {
slouken@1895
   935
                        unsigned long value;
slouken@1895
   936
                        text += SDL_ScanUnsignedLong(text, radix, &value);
slouken@1895
   937
                        if (!suppress) {
slouken@1895
   938
                            switch (inttype) {
slouken@1895
   939
                            case DO_SHORT:
slouken@1895
   940
                                {
slouken@1895
   941
                                    short *valuep = va_arg(ap, short *);
slouken@1895
   942
                                    *valuep = (short) value;
slouken@1895
   943
                                }
slouken@1895
   944
                                break;
slouken@1895
   945
                            case DO_INT:
slouken@1895
   946
                                {
slouken@1895
   947
                                    int *valuep = va_arg(ap, int *);
slouken@1895
   948
                                    *valuep = (int) value;
slouken@1895
   949
                                }
slouken@1895
   950
                                break;
slouken@1895
   951
                            case DO_LONG:
slouken@1895
   952
                                {
slouken@1895
   953
                                    long *valuep = va_arg(ap, long *);
slouken@1895
   954
                                    *valuep = value;
slouken@1895
   955
                                }
slouken@1895
   956
                                break;
slouken@1895
   957
                            case DO_LONGLONG:
slouken@1895
   958
                                /* Handled above */
slouken@1895
   959
                                break;
slouken@1330
   960
                            }
slouken@1895
   961
                            ++retval;
slouken@1895
   962
                        }
slouken@1895
   963
                    }
slouken@1895
   964
                    done = SDL_TRUE;
slouken@1895
   965
                    break;
slouken@1895
   966
                case 'p':
slouken@1895
   967
                    {
slouken@1895
   968
                        uintptr_t value;
slouken@1895
   969
                        text += SDL_ScanUintPtrT(text, 16, &value);
slouken@1895
   970
                        if (!suppress) {
slouken@1895
   971
                            void **valuep = va_arg(ap, void **);
slouken@1895
   972
                            *valuep = (void *) value;
slouken@1895
   973
                            ++retval;
slouken@1895
   974
                        }
slouken@1895
   975
                    }
slouken@1895
   976
                    done = SDL_TRUE;
slouken@1895
   977
                    break;
slouken@1895
   978
                case 'f':
slouken@1895
   979
                    {
slouken@1895
   980
                        double value;
slouken@1895
   981
                        text += SDL_ScanFloat(text, &value);
slouken@1895
   982
                        if (!suppress) {
slouken@1895
   983
                            float *valuep = va_arg(ap, float *);
slouken@1895
   984
                            *valuep = (float) value;
slouken@1895
   985
                            ++retval;
slouken@1895
   986
                        }
slouken@1895
   987
                    }
slouken@1895
   988
                    done = SDL_TRUE;
slouken@1895
   989
                    break;
slouken@1895
   990
                case 's':
slouken@1895
   991
                    if (suppress) {
slouken@1895
   992
                        while (!SDL_isspace(*text)) {
slouken@1895
   993
                            ++text;
slouken@1895
   994
                            if (count) {
slouken@1895
   995
                                if (--count == 0) {
slouken@1895
   996
                                    break;
slouken@1330
   997
                                }
slouken@1330
   998
                            }
slouken@1330
   999
                        }
slouken@1895
  1000
                    } else {
slouken@1895
  1001
                        char *valuep = va_arg(ap, char *);
slouken@1895
  1002
                        while (!SDL_isspace(*text)) {
slouken@1895
  1003
                            *valuep++ = *text++;
slouken@1895
  1004
                            if (count) {
slouken@1895
  1005
                                if (--count == 0) {
slouken@1895
  1006
                                    break;
slouken@1330
  1007
                                }
slouken@1330
  1008
                            }
slouken@1330
  1009
                        }
slouken@1895
  1010
                        *valuep = '\0';
slouken@1895
  1011
                        ++retval;
slouken@1895
  1012
                    }
slouken@1895
  1013
                    done = SDL_TRUE;
slouken@1895
  1014
                    break;
slouken@1895
  1015
                default:
slouken@1895
  1016
                    done = SDL_TRUE;
slouken@1895
  1017
                    break;
slouken@1330
  1018
                }
slouken@1330
  1019
                ++fmt;
slouken@1330
  1020
            }
slouken@1330
  1021
            continue;
slouken@1330
  1022
        }
slouken@1895
  1023
        if (*text == *fmt) {
slouken@1330
  1024
            ++text;
slouken@1330
  1025
            ++fmt;
slouken@1330
  1026
            continue;
slouken@1330
  1027
        }
slouken@1330
  1028
        /* Text didn't match format specifier */
slouken@1330
  1029
        break;
slouken@1330
  1030
    }
slouken@1330
  1031
    va_end(ap);
slouken@1330
  1032
slouken@1330
  1033
    return retval;
slouken@1330
  1034
}
slouken@1330
  1035
#endif
slouken@1330
  1036
slouken@1330
  1037
#ifndef HAVE_SNPRINTF
slouken@1895
  1038
int
slouken@1895
  1039
SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
slouken@1330
  1040
{
slouken@1330
  1041
    va_list ap;
slouken@1330
  1042
    int retval;
slouken@1330
  1043
slouken@1330
  1044
    va_start(ap, fmt);
slouken@1330
  1045
    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
slouken@1330
  1046
    va_end(ap);
slouken@1330
  1047
slouken@1330
  1048
    return retval;
slouken@1330
  1049
}
slouken@1330
  1050
#endif
slouken@1330
  1051
slouken@1330
  1052
#ifndef HAVE_VSNPRINTF
slouken@1895
  1053
static size_t
slouken@1895
  1054
SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
slouken@1330
  1055
{
slouken@1330
  1056
    char num[130];
slouken@1330
  1057
    size_t size;
slouken@1330
  1058
slouken@1336
  1059
    SDL_ltoa(value, num, radix);
slouken@1330
  1060
    size = SDL_strlen(num);
slouken@1895
  1061
    if (size >= maxlen) {
slouken@1895
  1062
        size = maxlen - 1;
slouken@1330
  1063
    }
slouken@1895
  1064
    SDL_strlcpy(text, num, size + 1);
slouken@1330
  1065
slouken@1330
  1066
    return size;
slouken@1330
  1067
}
slouken@1895
  1068
slouken@1895
  1069
static size_t
slouken@1895
  1070
SDL_PrintUnsignedLong(char *text, unsigned long value, int radix,
slouken@1895
  1071
                      size_t maxlen)
slouken@1330
  1072
{
slouken@1330
  1073
    char num[130];
slouken@1330
  1074
    size_t size;
slouken@1330
  1075
slouken@1336
  1076
    SDL_ultoa(value, num, radix);
slouken@1330
  1077
    size = SDL_strlen(num);
slouken@1895
  1078
    if (size >= maxlen) {
slouken@1895
  1079
        size = maxlen - 1;
slouken@1330
  1080
    }
slouken@1895
  1081
    SDL_strlcpy(text, num, size + 1);
slouken@1330
  1082
slouken@1330
  1083
    return size;
slouken@1330
  1084
}
slouken@1895
  1085
slouken@1330
  1086
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
  1087
static size_t
slouken@1895
  1088
SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
slouken@1330
  1089
{
slouken@1330
  1090
    char num[130];
slouken@1330
  1091
    size_t size;
slouken@1330
  1092
slouken@1336
  1093
    SDL_lltoa(value, num, radix);
slouken@1330
  1094
    size = SDL_strlen(num);
slouken@1895
  1095
    if (size >= maxlen) {
slouken@1895
  1096
        size = maxlen - 1;
slouken@1330
  1097
    }
slouken@1895
  1098
    SDL_strlcpy(text, num, size + 1);
slouken@1330
  1099
slouken@1330
  1100
    return size;
slouken@1330
  1101
}
slouken@1895
  1102
slouken@1895
  1103
static size_t
slouken@1895
  1104
SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
slouken@1330
  1105
{
slouken@1330
  1106
    char num[130];
slouken@1330
  1107
    size_t size;
slouken@1330
  1108
slouken@1336
  1109
    SDL_ulltoa(value, num, radix);
slouken@1330
  1110
    size = SDL_strlen(num);
slouken@1895
  1111
    if (size >= maxlen) {
slouken@1895
  1112
        size = maxlen - 1;
slouken@1330
  1113
    }
slouken@1895
  1114
    SDL_strlcpy(text, num, size + 1);
slouken@1330
  1115
slouken@1330
  1116
    return size;
slouken@1330
  1117
}
slouken@1330
  1118
#endif /* SDL_HAS_64BIT_TYPE */
slouken@1895
  1119
static size_t
slouken@1895
  1120
SDL_PrintFloat(char *text, double arg, size_t maxlen)
slouken@1330
  1121
{
slouken@1330
  1122
    char *textstart = text;
slouken@1895
  1123
    if (arg) {
slouken@1330
  1124
        /* This isn't especially accurate, but hey, it's easy. :) */
slouken@1330
  1125
        const double precision = 0.00000001;
slouken@1330
  1126
        size_t len;
slouken@1330
  1127
        unsigned long value;
slouken@1330
  1128
slouken@1895
  1129
        if (arg < 0) {
slouken@1330
  1130
            *text++ = '-';
slouken@1330
  1131
            --maxlen;
slouken@1330
  1132
            arg = -arg;
slouken@1330
  1133
        }
slouken@1895
  1134
        value = (unsigned long) arg;
slouken@1330
  1135
        len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
slouken@1330
  1136
        text += len;
slouken@1330
  1137
        maxlen -= len;
slouken@1330
  1138
        arg -= value;
slouken@1895
  1139
        if (arg > precision && maxlen) {
slouken@1330
  1140
            int mult = 10;
slouken@1330
  1141
            *text++ = '.';
slouken@1895
  1142
            while ((arg > precision) && maxlen) {
slouken@1895
  1143
                value = (unsigned long) (arg * mult);
slouken@1330
  1144
                len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
slouken@1330
  1145
                text += len;
slouken@1330
  1146
                maxlen -= len;
slouken@1895
  1147
                arg -= (double) value / mult;
slouken@1330
  1148
                mult *= 10;
slouken@1330
  1149
            }
slouken@1330
  1150
        }
slouken@1330
  1151
    } else {
slouken@1330
  1152
        *text++ = '0';
slouken@1330
  1153
    }
slouken@1330
  1154
    return (text - textstart);
slouken@1330
  1155
}
slouken@1895
  1156
slouken@1895
  1157
static size_t
slouken@1895
  1158
SDL_PrintString(char *text, const char *string, size_t maxlen)
slouken@1330
  1159
{
slouken@1330
  1160
    char *textstart = text;
slouken@1895
  1161
    while (*string && maxlen--) {
slouken@1330
  1162
        *text++ = *string++;
slouken@1330
  1163
    }
slouken@1330
  1164
    return (text - textstart);
slouken@1330
  1165
}
slouken@1895
  1166
slouken@1895
  1167
int
slouken@1895
  1168
SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@1330
  1169
{
slouken@1330
  1170
    char *textstart = text;
slouken@1895
  1171
    if (maxlen <= 0) {
slouken@1330
  1172
        return 0;
slouken@1330
  1173
    }
slouken@1895
  1174
    --maxlen;                   /* For the trailing '\0' */
slouken@1895
  1175
    while (*fmt && maxlen) {
slouken@1895
  1176
        if (*fmt == '%') {
slouken@1330
  1177
            SDL_bool done = SDL_FALSE;
slouken@1330
  1178
            size_t len = 0;
slouken@1330
  1179
            SDL_bool do_lowercase = SDL_FALSE;
slouken@1330
  1180
            int radix = 10;
slouken@1895
  1181
            enum
slouken@1895
  1182
            {
slouken@1330
  1183
                DO_INT,
slouken@1330
  1184
                DO_LONG,
slouken@1330
  1185
                DO_LONGLONG
slouken@1330
  1186
            } inttype = DO_INT;
slouken@1330
  1187
slouken@1330
  1188
            ++fmt;
slouken@1330
  1189
            /* FIXME: implement more of the format specifiers */
slouken@1895
  1190
            while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
slouken@1818
  1191
                ++fmt;
slouken@1818
  1192
            }
slouken@1330
  1193
            while (!done) {
slouken@1895
  1194
                switch (*fmt) {
slouken@1895
  1195
                case '%':
slouken@1895
  1196
                    *text = '%';
slouken@1895
  1197
                    len = 1;
slouken@1895
  1198
                    done = SDL_TRUE;
slouken@1895
  1199
                    break;
slouken@1895
  1200
                case 'c':
slouken@1895
  1201
                    /* char is promoted to int when passed through (...) */
slouken@1895
  1202
                    *text = (char) va_arg(ap, int);
slouken@1895
  1203
                    len = 1;
slouken@1895
  1204
                    done = SDL_TRUE;
slouken@1895
  1205
                    break;
slouken@1895
  1206
                case 'h':
slouken@1895
  1207
                    /* short is promoted to int when passed through (...) */
slouken@1895
  1208
                    break;
slouken@1895
  1209
                case 'l':
slouken@1895
  1210
                    if (inttype < DO_LONGLONG) {
slouken@1895
  1211
                        ++inttype;
slouken@1895
  1212
                    }
slouken@1895
  1213
                    break;
slouken@1895
  1214
                case 'I':
slouken@1895
  1215
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
  1216
                        fmt += 2;
slouken@1895
  1217
                        inttype = DO_LONGLONG;
slouken@1895
  1218
                    }
slouken@1895
  1219
                    break;
slouken@1895
  1220
                case 'i':
slouken@1895
  1221
                case 'd':
slouken@1895
  1222
                    switch (inttype) {
slouken@1895
  1223
                    case DO_INT:
slouken@1895
  1224
                        len =
slouken@1895
  1225
                            SDL_PrintLong(text,
slouken@1895
  1226
                                          (long) va_arg(ap, int),
slouken@1895
  1227
                                          radix, maxlen);
slouken@1330
  1228
                        break;
slouken@1895
  1229
                    case DO_LONG:
slouken@1895
  1230
                        len =
slouken@1895
  1231
                            SDL_PrintLong(text, va_arg(ap, long),
slouken@1895
  1232
                                          radix, maxlen);
slouken@1330
  1233
                        break;
slouken@1895
  1234
                    case DO_LONGLONG:
slouken@1330
  1235
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
  1236
                        len =
slouken@1895
  1237
                            SDL_PrintLongLong(text,
slouken@1895
  1238
                                              va_arg(ap, Sint64),
slouken@1895
  1239
                                              radix, maxlen);
slouken@1330
  1240
#else
slouken@1895
  1241
                        len =
slouken@1895
  1242
                            SDL_PrintLong(text, va_arg(ap, long),
slouken@1895
  1243
                                          radix, maxlen);
slouken@1330
  1244
#endif
slouken@1330
  1245
                        break;
slouken@1895
  1246
                    }
slouken@1895
  1247
                    done = SDL_TRUE;
slouken@1895
  1248
                    break;
slouken@1895
  1249
                case 'p':
slouken@1895
  1250
                case 'x':
slouken@1895
  1251
                    do_lowercase = SDL_TRUE;
slouken@1895
  1252
                    /* Fall through to 'X' handling */
slouken@1895
  1253
                case 'X':
slouken@1895
  1254
                    if (radix == 10) {
slouken@1895
  1255
                        radix = 16;
slouken@1895
  1256
                    }
slouken@1895
  1257
                    if (*fmt == 'p') {
slouken@1895
  1258
                        inttype = DO_LONG;
slouken@1895
  1259
                    }
slouken@1895
  1260
                    /* Fall through to unsigned handling */
slouken@1895
  1261
                case 'o':
slouken@1895
  1262
                    if (radix == 10) {
slouken@1895
  1263
                        radix = 8;
slouken@1895
  1264
                    }
slouken@1895
  1265
                    /* Fall through to unsigned handling */
slouken@1895
  1266
                case 'u':
slouken@1895
  1267
                    switch (inttype) {
slouken@1895
  1268
                    case DO_INT:
slouken@1895
  1269
                        len = SDL_PrintUnsignedLong(text, (unsigned long)
slouken@1895
  1270
                                                    va_arg(ap,
slouken@1895
  1271
                                                           unsigned
slouken@1895
  1272
                                                           int),
slouken@1895
  1273
                                                    radix, maxlen);
slouken@1895
  1274
                        break;
slouken@1895
  1275
                    case DO_LONG:
slouken@1895
  1276
                        len =
slouken@1895
  1277
                            SDL_PrintUnsignedLong(text,
slouken@1895
  1278
                                                  va_arg(ap,
slouken@1895
  1279
                                                         unsigned
slouken@1895
  1280
                                                         long),
slouken@1895
  1281
                                                  radix, maxlen);
slouken@1895
  1282
                        break;
slouken@1895
  1283
                    case DO_LONGLONG:
slouken@1330
  1284
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
  1285
                        len =
slouken@1895
  1286
                            SDL_PrintUnsignedLongLong(text,
slouken@1895
  1287
                                                      va_arg(ap,
slouken@1895
  1288
                                                             Uint64),
slouken@1895
  1289
                                                      radix, maxlen);
slouken@1330
  1290
#else
slouken@1895
  1291
                        len =
slouken@1895
  1292
                            SDL_PrintUnsignedLong(text,
slouken@1895
  1293
                                                  va_arg(ap,
slouken@1895
  1294
                                                         unsigned
slouken@1895
  1295
                                                         long),
slouken@1895
  1296
                                                  radix, maxlen);
slouken@1330
  1297
#endif
slouken@1330
  1298
                        break;
slouken@1895
  1299
                    }
slouken@1895
  1300
                    if (do_lowercase) {
slouken@1895
  1301
                        SDL_strlwr(text);
slouken@1895
  1302
                    }
slouken@1895
  1303
                    done = SDL_TRUE;
slouken@1895
  1304
                    break;
slouken@1895
  1305
                case 'f':
slouken@1895
  1306
                    len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
slouken@1895
  1307
                    done = SDL_TRUE;
slouken@1895
  1308
                    break;
slouken@1895
  1309
                case 's':
slouken@1895
  1310
                    len = SDL_PrintString(text, va_arg(ap, char *), maxlen);
slouken@1895
  1311
                    done = SDL_TRUE;
slouken@1895
  1312
                    break;
slouken@1895
  1313
                default:
slouken@1895
  1314
                    done = SDL_TRUE;
slouken@1895
  1315
                    break;
slouken@1330
  1316
                }
slouken@1330
  1317
                ++fmt;
slouken@1330
  1318
            }
slouken@1330
  1319
            text += len;
slouken@1330
  1320
            maxlen -= len;
slouken@1330
  1321
        } else {
slouken@1330
  1322
            *text++ = *fmt++;
slouken@1330
  1323
            --maxlen;
slouken@1330
  1324
        }
slouken@1330
  1325
    }
slouken@1330
  1326
    *text = '\0';
slouken@1330
  1327
slouken@1330
  1328
    return (text - textstart);
slouken@1330
  1329
}
slouken@1330
  1330
#endif
slouken@1895
  1331
/* vi: set ts=4 sw=4 expandtab: */