src/stdlib/SDL_string.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Mar 2014 12:29:38 -0700
changeset 8688 1543a7183f88
parent 8149 681eb46b8ac4
child 8779 d21a3ff2b0d2
permissions -rw-r--r--
Fixed XInput haptic support on Windows 8
It turns out the XBox 360 controller driver never reports force feedback capability, so we'll try to set 0 state and see if that succeeds.
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@1330
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1330
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1330
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1330
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@1330
    22
slouken@1330
    23
/* This file contains portable string manipulation functions for SDL */
slouken@1330
    24
slouken@1354
    25
#include "SDL_stdinc.h"
slouken@1330
    26
slouken@1330
    27
slouken@1354
    28
#define SDL_isupperhex(X)   (((X) >= 'A') && ((X) <= 'F'))
slouken@1354
    29
#define SDL_islowerhex(X)   (((X) >= 'a') && ((X) <= 'f'))
slouken@1330
    30
dewyatt@4754
    31
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
dewyatt@4754
    32
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
dewyatt@4754
    33
slouken@6044
    34
static int UTF8_TrailingBytes(unsigned char c)
dewyatt@4754
    35
{
slouken@6029
    36
    if (c >= 0xC0 && c <= 0xDF)
dewyatt@4754
    37
        return 1;
dewyatt@4754
    38
    else if (c >= 0xE0 && c <= 0xEF)
dewyatt@4754
    39
        return 2;
dewyatt@4754
    40
    else if (c >= 0xF0 && c <= 0xF4)
dewyatt@4754
    41
        return 3;
dewyatt@4754
    42
    else
dewyatt@4754
    43
        return 0;
dewyatt@4754
    44
}
dewyatt@4754
    45
icculus@8089
    46
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL)
slouken@1895
    47
static size_t
slouken@1895
    48
SDL_ScanLong(const char *text, int radix, long *valuep)
slouken@1330
    49
{
slouken@1330
    50
    const char *textstart = text;
slouken@1330
    51
    long value = 0;
slouken@1330
    52
    SDL_bool negative = SDL_FALSE;
slouken@1330
    53
slouken@1895
    54
    if (*text == '-') {
slouken@1330
    55
        negative = SDL_TRUE;
slouken@1330
    56
        ++text;
slouken@1330
    57
    }
slouken@1895
    58
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
    59
        text += 2;
slouken@1330
    60
    }
slouken@1895
    61
    for (;;) {
slouken@1330
    62
        int v;
icculus@2092
    63
        if (SDL_isdigit((unsigned char) *text)) {
slouken@1330
    64
            v = *text - '0';
slouken@1895
    65
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
    66
            v = 10 + (*text - 'A');
slouken@1895
    67
        } else if (radix == 16 && SDL_islowerhex(*text)) {
slouken@1330
    68
            v = 10 + (*text - 'a');
slouken@1330
    69
        } else {
slouken@1330
    70
            break;
slouken@1330
    71
        }
slouken@1330
    72
        value *= radix;
slouken@1330
    73
        value += v;
slouken@1330
    74
        ++text;
slouken@1330
    75
    }
slouken@1895
    76
    if (valuep) {
slouken@1895
    77
        if (negative && value) {
slouken@1330
    78
            *valuep = -value;
slouken@1330
    79
        } else {
slouken@1330
    80
            *valuep = value;
slouken@1330
    81
        }
slouken@1330
    82
    }
slouken@1330
    83
    return (text - textstart);
slouken@1330
    84
}
slouken@1330
    85
#endif
slouken@1330
    86
icculus@8089
    87
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
slouken@1895
    88
static size_t
slouken@1895
    89
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
slouken@1330
    90
{
slouken@1330
    91
    const char *textstart = text;
slouken@1330
    92
    unsigned long value = 0;
slouken@1330
    93
slouken@1895
    94
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
    95
        text += 2;
slouken@1330
    96
    }
slouken@1895
    97
    for (;;) {
slouken@1330
    98
        int v;
icculus@2092
    99
        if (SDL_isdigit((unsigned char) *text)) {
slouken@1330
   100
            v = *text - '0';
slouken@1895
   101
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
   102
            v = 10 + (*text - 'A');
slouken@1895
   103
        } else if (radix == 16 && SDL_islowerhex(*text)) {
slouken@1330
   104
            v = 10 + (*text - 'a');
slouken@1330
   105
        } else {
slouken@1330
   106
            break;
slouken@1330
   107
        }
slouken@1330
   108
        value *= radix;
slouken@1330
   109
        value += v;
slouken@1330
   110
        ++text;
slouken@1330
   111
    }
slouken@1895
   112
    if (valuep) {
slouken@1330
   113
        *valuep = value;
slouken@1330
   114
    }
slouken@1330
   115
    return (text - textstart);
slouken@1330
   116
}
slouken@1330
   117
#endif
slouken@1330
   118
icculus@8089
   119
#ifndef HAVE_VSSCANF
slouken@1895
   120
static size_t
slouken@1895
   121
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
slouken@1456
   122
{
slouken@1456
   123
    const char *textstart = text;
slouken@1456
   124
    uintptr_t value = 0;
slouken@1456
   125
slouken@1895
   126
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1456
   127
        text += 2;
slouken@1456
   128
    }
slouken@1895
   129
    for (;;) {
slouken@1456
   130
        int v;
icculus@2092
   131
        if (SDL_isdigit((unsigned char) *text)) {
slouken@1456
   132
            v = *text - '0';
slouken@1895
   133
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1456
   134
            v = 10 + (*text - 'A');
slouken@1895
   135
        } else if (radix == 16 && SDL_islowerhex(*text)) {
slouken@1456
   136
            v = 10 + (*text - 'a');
slouken@1456
   137
        } else {
slouken@1456
   138
            break;
slouken@1456
   139
        }
slouken@1456
   140
        value *= radix;
slouken@1456
   141
        value += v;
slouken@1456
   142
        ++text;
slouken@1456
   143
    }
slouken@1895
   144
    if (valuep) {
slouken@1456
   145
        *valuep = value;
slouken@1456
   146
    }
slouken@1456
   147
    return (text - textstart);
slouken@1456
   148
}
slouken@1456
   149
#endif
slouken@1456
   150
icculus@8089
   151
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL)
slouken@1895
   152
static size_t
slouken@1895
   153
SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
slouken@1330
   154
{
slouken@1330
   155
    const char *textstart = text;
slouken@1330
   156
    Sint64 value = 0;
slouken@1330
   157
    SDL_bool negative = SDL_FALSE;
slouken@1330
   158
slouken@1895
   159
    if (*text == '-') {
slouken@1330
   160
        negative = SDL_TRUE;
slouken@1330
   161
        ++text;
slouken@1330
   162
    }
slouken@1895
   163
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
   164
        text += 2;
slouken@1330
   165
    }
slouken@1895
   166
    for (;;) {
slouken@1330
   167
        int v;
icculus@2092
   168
        if (SDL_isdigit((unsigned char) *text)) {
slouken@1330
   169
            v = *text - '0';
slouken@1895
   170
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
   171
            v = 10 + (*text - 'A');
slouken@1895
   172
        } else if (radix == 16 && SDL_islowerhex(*text)) {
slouken@1330
   173
            v = 10 + (*text - 'a');
slouken@1330
   174
        } else {
slouken@1330
   175
            break;
slouken@1330
   176
        }
slouken@1330
   177
        value *= radix;
slouken@1330
   178
        value += v;
slouken@1330
   179
        ++text;
slouken@1330
   180
    }
slouken@1895
   181
    if (valuep) {
slouken@1895
   182
        if (negative && value) {
slouken@1330
   183
            *valuep = -value;
slouken@1330
   184
        } else {
slouken@1330
   185
            *valuep = value;
slouken@1330
   186
        }
slouken@1330
   187
    }
slouken@1330
   188
    return (text - textstart);
slouken@1330
   189
}
slouken@1330
   190
#endif
slouken@1330
   191
icculus@8089
   192
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
slouken@1895
   193
static size_t
slouken@1895
   194
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
slouken@1330
   195
{
slouken@1330
   196
    const char *textstart = text;
slouken@1330
   197
    Uint64 value = 0;
slouken@1330
   198
slouken@1895
   199
    if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
slouken@1330
   200
        text += 2;
slouken@1330
   201
    }
slouken@1895
   202
    for (;;) {
slouken@1330
   203
        int v;
icculus@2092
   204
        if (SDL_isdigit((unsigned char) *text)) {
slouken@1330
   205
            v = *text - '0';
slouken@1895
   206
        } else if (radix == 16 && SDL_isupperhex(*text)) {
slouken@1330
   207
            v = 10 + (*text - 'A');
slouken@1895
   208
        } else if (radix == 16 && SDL_islowerhex(*text)) {
slouken@1330
   209
            v = 10 + (*text - 'a');
slouken@1330
   210
        } else {
slouken@1330
   211
            break;
slouken@1330
   212
        }
slouken@1330
   213
        value *= radix;
slouken@1330
   214
        value += v;
slouken@1330
   215
        ++text;
slouken@1330
   216
    }
slouken@1895
   217
    if (valuep) {
slouken@1330
   218
        *valuep = value;
slouken@1330
   219
    }
slouken@1330
   220
    return (text - textstart);
slouken@1330
   221
}
slouken@1330
   222
#endif
slouken@1330
   223
icculus@8089
   224
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
slouken@1895
   225
static size_t
slouken@1895
   226
SDL_ScanFloat(const char *text, double *valuep)
slouken@1330
   227
{
slouken@1330
   228
    const char *textstart = text;
slouken@1330
   229
    unsigned long lvalue = 0;
slouken@1330
   230
    double value = 0.0;
slouken@1330
   231
    SDL_bool negative = SDL_FALSE;
slouken@1330
   232
slouken@1895
   233
    if (*text == '-') {
slouken@1330
   234
        negative = SDL_TRUE;
slouken@1330
   235
        ++text;
slouken@1330
   236
    }
slouken@1330
   237
    text += SDL_ScanUnsignedLong(text, 10, &lvalue);
slouken@1330
   238
    value += lvalue;
slouken@1895
   239
    if (*text == '.') {
slouken@1330
   240
        int mult = 10;
slouken@1330
   241
        ++text;
icculus@2092
   242
        while (SDL_isdigit((unsigned char) *text)) {
slouken@1330
   243
            lvalue = *text - '0';
slouken@1895
   244
            value += (double) lvalue / mult;
slouken@1330
   245
            mult *= 10;
slouken@1330
   246
            ++text;
slouken@1330
   247
        }
slouken@1330
   248
    }
slouken@1895
   249
    if (valuep) {
slouken@1895
   250
        if (negative && value) {
slouken@1330
   251
            *valuep = -value;
slouken@1330
   252
        } else {
slouken@1330
   253
            *valuep = value;
slouken@1330
   254
        }
slouken@1330
   255
    }
slouken@1330
   256
    return (text - textstart);
slouken@1330
   257
}
slouken@1330
   258
#endif
slouken@1330
   259
slouken@1895
   260
void *
slouken@1895
   261
SDL_memset(void *dst, int c, size_t len)
slouken@1330
   262
{
slouken@7351
   263
#if defined(HAVE_MEMSET)
slouken@7351
   264
    return memset(dst, c, len);
slouken@7351
   265
#else
slouken@1330
   266
    size_t left = (len % 4);
slouken@5325
   267
    Uint32 *dstp4;
slouken@5325
   268
    Uint8 *dstp1;
slouken@5325
   269
    Uint32 value4 = (c | (c << 8) | (c << 16) | (c << 24));
slouken@5325
   270
    Uint8 value1 = (Uint8) c;
slouken@5325
   271
slouken@5325
   272
    dstp4 = (Uint32 *) dst;
slouken@5325
   273
    len /= 4;
slouken@5325
   274
    while (len--) {
slouken@5325
   275
        *dstp4++ = value4;
slouken@1330
   276
    }
slouken@5325
   277
slouken@5325
   278
    dstp1 = (Uint8 *) dstp4;
slouken@5325
   279
    switch (left) {
slouken@5325
   280
    case 3:
slouken@5325
   281
        *dstp1++ = value1;
slouken@5325
   282
    case 2:
slouken@5325
   283
        *dstp1++ = value1;
slouken@5325
   284
    case 1:
slouken@5325
   285
        *dstp1++ = value1;
slouken@1330
   286
    }
slouken@5325
   287
slouken@1330
   288
    return dst;
slouken@7351
   289
#endif /* HAVE_MEMSET */
slouken@1330
   290
}
slouken@1330
   291
slouken@1895
   292
void *
slouken@1895
   293
SDL_memcpy(void *dst, const void *src, size_t len)
slouken@1330
   294
{
slouken@7236
   295
#ifdef __GNUC__
slouken@7236
   296
    /* Presumably this is well tuned for speed.
slouken@7236
   297
       On my machine this is twice as fast as the C code below.
slouken@7236
   298
     */
slouken@7236
   299
    return __builtin_memcpy(dst, src, len);
slouken@7351
   300
#elif defined(HAVE_MEMCPY)
slouken@7351
   301
    return memcpy(dst, src, len);
slouken@7351
   302
#elif defined(HAVE_BCOPY)
slouken@7351
   303
    bcopy(src, dst, len);
slouken@7351
   304
    return dst;
slouken@7236
   305
#else
slouken@7236
   306
    /* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
slouken@7236
   307
       using Uint32* pointers, so we need to make sure the pointers are
slouken@7236
   308
       aligned before we loop using them.
slouken@7236
   309
     */
slouken@7236
   310
    if (((intptr_t)src & 0x3) || ((intptr_t)dst & 0x3)) {
slouken@7236
   311
        /* Do an unaligned byte copy */
slouken@7236
   312
        Uint8 *srcp1 = (Uint8 *)src;
slouken@7236
   313
        Uint8 *dstp1 = (Uint8 *)dst;
slouken@5325
   314
slouken@7236
   315
        while (len--) {
slouken@7236
   316
            *dstp1++ = *srcp1++;
slouken@7236
   317
        }
slouken@7236
   318
    } else {
slouken@7236
   319
        size_t left = (len % 4);
slouken@7236
   320
        Uint32 *srcp4, *dstp4;
slouken@7236
   321
        Uint8 *srcp1, *dstp1;
slouken@7236
   322
slouken@7236
   323
        srcp4 = (Uint32 *) src;
slouken@7236
   324
        dstp4 = (Uint32 *) dst;
slouken@7236
   325
        len /= 4;
slouken@7236
   326
        while (len--) {
slouken@7236
   327
            *dstp4++ = *srcp4++;
slouken@7236
   328
        }
slouken@7236
   329
slouken@7236
   330
        srcp1 = (Uint8 *) srcp4;
slouken@7236
   331
        dstp1 = (Uint8 *) dstp4;
slouken@7236
   332
        switch (left) {
slouken@7236
   333
        case 3:
slouken@7236
   334
            *dstp1++ = *srcp1++;
slouken@7236
   335
        case 2:
slouken@7236
   336
            *dstp1++ = *srcp1++;
slouken@7236
   337
        case 1:
slouken@7236
   338
            *dstp1++ = *srcp1++;
slouken@7236
   339
        }
slouken@1330
   340
    }
slouken@1330
   341
    return dst;
slouken@7236
   342
#endif /* __GNUC__ */
slouken@1330
   343
}
slouken@1330
   344
slouken@1895
   345
void *
slouken@5325
   346
SDL_memmove(void *dst, const void *src, size_t len)
slouken@1330
   347
{
slouken@7351
   348
#if defined(HAVE_MEMMOVE)
slouken@7351
   349
    return memmove(dst, src, len);
slouken@7351
   350
#else
slouken@1895
   351
    char *srcp = (char *) src;
slouken@1895
   352
    char *dstp = (char *) dst;
slouken@5325
   353
slouken@5325
   354
    if (src < dst) {
slouken@5325
   355
        srcp += len - 1;
slouken@5325
   356
        dstp += len - 1;
slouken@5325
   357
        while (len--) {
slouken@5325
   358
            *dstp-- = *srcp--;
slouken@5325
   359
        }
slouken@5325
   360
    } else {
slouken@5325
   361
        while (len--) {
slouken@5325
   362
            *dstp++ = *srcp++;
slouken@5325
   363
        }
slouken@1330
   364
    }
slouken@1330
   365
    return dst;
slouken@7351
   366
#endif /* HAVE_MEMMOVE */
slouken@1330
   367
}
slouken@1330
   368
slouken@1895
   369
int
slouken@1895
   370
SDL_memcmp(const void *s1, const void *s2, size_t len)
slouken@1330
   371
{
slouken@7351
   372
#if defined(HAVE_MEMCMP)
slouken@7351
   373
    return memcmp(s1, s2, len);
slouken@7351
   374
#else
slouken@1895
   375
    char *s1p = (char *) s1;
slouken@1895
   376
    char *s2p = (char *) s2;
slouken@1895
   377
    while (len--) {
slouken@1895
   378
        if (*s1p != *s2p) {
slouken@1330
   379
            return (*s1p - *s2p);
slouken@1895
   380
        }
slouken@1895
   381
        ++s1p;
slouken@1895
   382
        ++s2p;
slouken@1330
   383
    }
slouken@1330
   384
    return 0;
slouken@7351
   385
#endif /* HAVE_MEMCMP */
slouken@1330
   386
}
slouken@1330
   387
slouken@1895
   388
size_t
slouken@1895
   389
SDL_strlen(const char *string)
slouken@1330
   390
{
slouken@7351
   391
#if defined(HAVE_STRLEN)
slouken@7352
   392
    return strlen(string);
slouken@7351
   393
#else
slouken@1330
   394
    size_t len = 0;
slouken@1895
   395
    while (*string++) {
slouken@1330
   396
        ++len;
slouken@1330
   397
    }
slouken@1330
   398
    return len;
slouken@7351
   399
#endif /* HAVE_STRLEN */
slouken@1330
   400
}
slouken@1330
   401
slouken@1901
   402
size_t
slouken@1903
   403
SDL_wcslen(const wchar_t * string)
slouken@1901
   404
{
slouken@7351
   405
#if defined(HAVE_WCSLEN)
slouken@7352
   406
    return wcslen(string);
slouken@7351
   407
#else
slouken@1901
   408
    size_t len = 0;
slouken@1901
   409
    while (*string++) {
slouken@1901
   410
        ++len;
slouken@1901
   411
    }
slouken@1901
   412
    return len;
slouken@7351
   413
#endif /* HAVE_WCSLEN */
slouken@1901
   414
}
slouken@1901
   415
dewyatt@4758
   416
size_t
dewyatt@4758
   417
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
dewyatt@4758
   418
{
slouken@7351
   419
#if defined(HAVE_WCSLCPY)
slouken@7351
   420
    return wcslcpy(dst, src, maxlen);
slouken@7351
   421
#else
dewyatt@4758
   422
    size_t srclen = SDL_wcslen(src);
dewyatt@4758
   423
    if (maxlen > 0) {
dewyatt@4758
   424
        size_t len = SDL_min(srclen, maxlen - 1);
dewyatt@4758
   425
        SDL_memcpy(dst, src, len * sizeof(wchar_t));
dewyatt@4758
   426
        dst[len] = '\0';
dewyatt@4758
   427
    }
dewyatt@4758
   428
    return srclen;
slouken@7351
   429
#endif /* HAVE_WCSLCPY */
dewyatt@4758
   430
}
dewyatt@4758
   431
dewyatt@4758
   432
size_t
dewyatt@4758
   433
SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen)
dewyatt@4758
   434
{
slouken@7351
   435
#if defined(HAVE_WCSLCAT)
slouken@7351
   436
    return wcslcat(dst, src, maxlen);
slouken@7351
   437
#else
dewyatt@4758
   438
    size_t dstlen = SDL_wcslen(dst);
dewyatt@4758
   439
    size_t srclen = SDL_wcslen(src);
dewyatt@4758
   440
    if (dstlen < maxlen) {
dewyatt@4758
   441
        SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
dewyatt@4758
   442
    }
dewyatt@4758
   443
    return dstlen + srclen;
slouken@7351
   444
#endif /* HAVE_WCSLCAT */
dewyatt@4758
   445
}
dewyatt@4758
   446
slouken@1895
   447
size_t
slouken@1895
   448
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
slouken@1330
   449
{
slouken@7351
   450
#if defined(HAVE_STRLCPY)
slouken@7351
   451
    return strlcpy(dst, src, maxlen);
slouken@7351
   452
#else
slouken@1379
   453
    size_t srclen = SDL_strlen(src);
slouken@1895
   454
    if (maxlen > 0) {
slouken@1895
   455
        size_t len = SDL_min(srclen, maxlen - 1);
slouken@1379
   456
        SDL_memcpy(dst, src, len);
slouken@1379
   457
        dst[len] = '\0';
slouken@1330
   458
    }
slouken@1379
   459
    return srclen;
slouken@7351
   460
#endif /* HAVE_STRLCPY */
slouken@1330
   461
}
slouken@1330
   462
dewyatt@4754
   463
size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes)
dewyatt@4754
   464
{
dewyatt@4754
   465
    size_t src_bytes = SDL_strlen(src);
dewyatt@4754
   466
    size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
slouken@5076
   467
    size_t i = 0;
dewyatt@4754
   468
    char trailing_bytes = 0;
dewyatt@4754
   469
    if (bytes)
dewyatt@4754
   470
    {
dewyatt@4754
   471
        unsigned char c = (unsigned char)src[bytes - 1];
dewyatt@4754
   472
        if (UTF8_IsLeadByte(c))
dewyatt@4754
   473
            --bytes;
dewyatt@4754
   474
        else if (UTF8_IsTrailingByte(c))
dewyatt@4754
   475
        {
dewyatt@4754
   476
            for (i = bytes - 1; i != 0; --i)
dewyatt@4754
   477
            {
dewyatt@4754
   478
                c = (unsigned char)src[i];
dewyatt@4754
   479
                trailing_bytes = UTF8_TrailingBytes(c);
dewyatt@4754
   480
                if (trailing_bytes)
dewyatt@4754
   481
                {
dewyatt@4754
   482
                    if (bytes - i != trailing_bytes + 1)
dewyatt@4754
   483
                        bytes = i;
dewyatt@4754
   484
dewyatt@4754
   485
                    break;
dewyatt@4754
   486
                }
dewyatt@4754
   487
            }
dewyatt@4754
   488
        }
dewyatt@4754
   489
        SDL_memcpy(dst, src, bytes);
dewyatt@4754
   490
    }
dewyatt@4754
   491
    dst[bytes] = '\0';
dewyatt@4754
   492
    return bytes;
dewyatt@4754
   493
}
dewyatt@4754
   494
slouken@1895
   495
size_t
slouken@1895
   496
SDL_strlcat(char *dst, const char *src, size_t maxlen)
slouken@1330
   497
{
slouken@7351
   498
#if defined(HAVE_STRLCAT)
slouken@7351
   499
    return strlcat(dst, src, maxlen);
slouken@7351
   500
#else
slouken@1379
   501
    size_t dstlen = SDL_strlen(dst);
slouken@1379
   502
    size_t srclen = SDL_strlen(src);
slouken@1895
   503
    if (dstlen < maxlen) {
slouken@1895
   504
        SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
slouken@1330
   505
    }
slouken@1895
   506
    return dstlen + srclen;
slouken@7351
   507
#endif /* HAVE_STRLCAT */
slouken@1330
   508
}
slouken@1330
   509
slouken@1895
   510
char *
slouken@1895
   511
SDL_strdup(const char *string)
slouken@1341
   512
{
slouken@7351
   513
#if defined(HAVE_STRDUP)
slouken@7352
   514
    return strdup(string);
slouken@7351
   515
#else
slouken@1895
   516
    size_t len = SDL_strlen(string) + 1;
slouken@1379
   517
    char *newstr = SDL_malloc(len);
slouken@1895
   518
    if (newstr) {
slouken@1379
   519
        SDL_strlcpy(newstr, string, len);
slouken@1341
   520
    }
slouken@1341
   521
    return newstr;
slouken@7351
   522
#endif /* HAVE_STRDUP */
slouken@1341
   523
}
slouken@1341
   524
slouken@1895
   525
char *
slouken@1895
   526
SDL_strrev(char *string)
slouken@1330
   527
{
slouken@7351
   528
#if defined(HAVE__STRREV)
slouken@7353
   529
    return _strrev(string);
slouken@7351
   530
#else
slouken@1330
   531
    size_t len = SDL_strlen(string);
slouken@1330
   532
    char *a = &string[0];
slouken@1895
   533
    char *b = &string[len - 1];
slouken@1330
   534
    len /= 2;
slouken@1895
   535
    while (len--) {
slouken@1330
   536
        char c = *a;
slouken@1330
   537
        *a++ = *b;
slouken@1330
   538
        *b-- = c;
slouken@1330
   539
    }
slouken@1330
   540
    return string;
slouken@7351
   541
#endif /* HAVE__STRREV */
slouken@1330
   542
}
slouken@1330
   543
slouken@1895
   544
char *
slouken@1895
   545
SDL_strupr(char *string)
slouken@1330
   546
{
slouken@7351
   547
#if defined(HAVE__STRUPR)
slouken@7353
   548
    return _strupr(string);
slouken@7351
   549
#else
slouken@1330
   550
    char *bufp = string;
slouken@1895
   551
    while (*bufp) {
icculus@2092
   552
        *bufp = SDL_toupper((unsigned char) *bufp);
slouken@1895
   553
        ++bufp;
slouken@1330
   554
    }
slouken@1330
   555
    return string;
slouken@7351
   556
#endif /* HAVE__STRUPR */
slouken@1330
   557
}
slouken@1330
   558
slouken@1895
   559
char *
slouken@1895
   560
SDL_strlwr(char *string)
slouken@1330
   561
{
slouken@7351
   562
#if defined(HAVE__STRLWR)
slouken@7353
   563
    return _strlwr(string);
slouken@7351
   564
#else
slouken@1330
   565
    char *bufp = string;
slouken@1895
   566
    while (*bufp) {
icculus@2092
   567
        *bufp = SDL_tolower((unsigned char) *bufp);
slouken@1895
   568
        ++bufp;
slouken@1330
   569
    }
slouken@1330
   570
    return string;
slouken@7351
   571
#endif /* HAVE__STRLWR */
slouken@1330
   572
}
slouken@1330
   573
slouken@1895
   574
char *
slouken@1895
   575
SDL_strchr(const char *string, int c)
slouken@1330
   576
{
slouken@7351
   577
#ifdef HAVE_STRCHR
slouken@7352
   578
    return SDL_const_cast(char*,strchr(string, c));
slouken@7351
   579
#elif defined(HAVE_INDEX)
slouken@7352
   580
    return SDL_const_cast(char*,index(string, c));
slouken@7351
   581
#else
slouken@1895
   582
    while (*string) {
slouken@1895
   583
        if (*string == c) {
slouken@1895
   584
            return (char *) string;
slouken@1330
   585
        }
slouken@1895
   586
        ++string;
slouken@1330
   587
    }
slouken@1330
   588
    return NULL;
slouken@7351
   589
#endif /* HAVE_STRCHR */
slouken@1330
   590
}
slouken@1330
   591
slouken@1895
   592
char *
slouken@1895
   593
SDL_strrchr(const char *string, int c)
slouken@1330
   594
{
slouken@7351
   595
#ifdef HAVE_STRRCHR
slouken@7352
   596
    return SDL_const_cast(char*,strrchr(string, c));
slouken@7351
   597
#elif defined(HAVE_RINDEX)
slouken@7352
   598
    return SDL_const_cast(char*,rindex(string, c));
slouken@7351
   599
#else
slouken@1336
   600
    const char *bufp = string + SDL_strlen(string) - 1;
slouken@1895
   601
    while (bufp >= string) {
slouken@1895
   602
        if (*bufp == c) {
slouken@1895
   603
            return (char *) bufp;
slouken@1330
   604
        }
slouken@1895
   605
        --bufp;
slouken@1330
   606
    }
slouken@1330
   607
    return NULL;
slouken@7351
   608
#endif /* HAVE_STRRCHR */
slouken@1330
   609
}
slouken@1330
   610
slouken@1895
   611
char *
slouken@1895
   612
SDL_strstr(const char *haystack, const char *needle)
slouken@1330
   613
{
slouken@7351
   614
#if defined(HAVE_STRSTR)
slouken@7351
   615
    return SDL_const_cast(char*,strstr(haystack, needle));
slouken@7351
   616
#else
slouken@1336
   617
    size_t length = SDL_strlen(needle);
slouken@1895
   618
    while (*haystack) {
slouken@1895
   619
        if (SDL_strncmp(haystack, needle, length) == 0) {
slouken@1895
   620
            return (char *) haystack;
slouken@1330
   621
        }
slouken@1895
   622
        ++haystack;
slouken@1330
   623
    }
slouken@1330
   624
    return NULL;
slouken@7351
   625
#endif /* HAVE_STRSTR */
slouken@1330
   626
}
slouken@1330
   627
slouken@7353
   628
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
slouken@7353
   629
    !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
slouken@1330
   630
static const char ntoa_table[] = {
slouken@1330
   631
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
slouken@1330
   632
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
slouken@1330
   633
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
slouken@1330
   634
    'U', 'V', 'W', 'X', 'Y', 'Z'
slouken@1330
   635
};
slouken@1330
   636
#endif /* ntoa() conversion table */
slouken@1330
   637
slouken@7351
   638
char *
slouken@7353
   639
SDL_itoa(int value, char *string, int radix)
slouken@7351
   640
{
slouken@7351
   641
#ifdef HAVE_ITOA
slouken@7353
   642
    return itoa(value, string, radix);
icculus@7003
   643
#else
slouken@7353
   644
    return SDL_ltoa((long)value, string, radix);
slouken@7351
   645
#endif /* HAVE_ITOA */
slouken@7351
   646
}
slouken@7351
   647
slouken@7351
   648
char *
slouken@7353
   649
SDL_uitoa(unsigned int value, char *string, int radix)
slouken@7351
   650
{
slouken@7351
   651
#ifdef HAVE__UITOA
slouken@7353
   652
    return _uitoa(value, string, radix);
slouken@7351
   653
#else
slouken@7353
   654
    return SDL_ultoa((unsigned long)value, string, radix);
slouken@7351
   655
#endif /* HAVE__UITOA */
slouken@7351
   656
}
slouken@7351
   657
slouken@1895
   658
char *
slouken@1895
   659
SDL_ltoa(long value, char *string, int radix)
slouken@1330
   660
{
slouken@7351
   661
#if defined(HAVE__LTOA)
slouken@7353
   662
    return _ltoa(value, string, radix);
slouken@7351
   663
#else
slouken@1330
   664
    char *bufp = string;
slouken@1330
   665
slouken@1895
   666
    if (value < 0) {
slouken@1330
   667
        *bufp++ = '-';
slouken@8059
   668
        SDL_ultoa(-value, bufp, radix);
slouken@1330
   669
    } else {
slouken@8059
   670
        SDL_ultoa(value, bufp, radix);
slouken@1330
   671
    }
slouken@1330
   672
slouken@1330
   673
    return string;
slouken@7351
   674
#endif /* HAVE__LTOA */
slouken@1330
   675
}
slouken@1330
   676
slouken@1895
   677
char *
slouken@1895
   678
SDL_ultoa(unsigned long value, char *string, int radix)
slouken@1330
   679
{
slouken@7351
   680
#if defined(HAVE__ULTOA)
slouken@7353
   681
    return _ultoa(value, string, radix);
slouken@7351
   682
#else
slouken@1330
   683
    char *bufp = string;
slouken@1330
   684
slouken@1895
   685
    if (value) {
slouken@1895
   686
        while (value > 0) {
slouken@1330
   687
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   688
            value /= radix;
slouken@1330
   689
        }
slouken@1330
   690
    } else {
slouken@1330
   691
        *bufp++ = '0';
slouken@1330
   692
    }
slouken@1330
   693
    *bufp = '\0';
slouken@1330
   694
slouken@1330
   695
    /* The numbers went into the string backwards. :) */
slouken@1336
   696
    SDL_strrev(string);
slouken@1330
   697
slouken@1330
   698
    return string;
slouken@7351
   699
#endif /* HAVE__ULTOA */
slouken@1330
   700
}
slouken@1330
   701
slouken@1895
   702
char *
slouken@1895
   703
SDL_lltoa(Sint64 value, char *string, int radix)
slouken@1330
   704
{
slouken@7351
   705
#if defined(HAVE__I64TOA)
slouken@7353
   706
    return _i64toa(value, string, radix);
slouken@7351
   707
#else
slouken@1330
   708
    char *bufp = string;
slouken@1330
   709
slouken@1895
   710
    if (value < 0) {
slouken@1330
   711
        *bufp++ = '-';
slouken@8059
   712
        SDL_ulltoa(-value, bufp, radix);
slouken@1330
   713
    } else {
slouken@8059
   714
        SDL_ulltoa(value, bufp, radix);
slouken@1330
   715
    }
slouken@1330
   716
slouken@1330
   717
    return string;
slouken@7351
   718
#endif /* HAVE__I64TOA */
slouken@1330
   719
}
slouken@1330
   720
slouken@1895
   721
char *
slouken@1895
   722
SDL_ulltoa(Uint64 value, char *string, int radix)
slouken@1330
   723
{
slouken@7351
   724
#if defined(HAVE__UI64TOA)
slouken@7353
   725
    return _ui64toa(value, string, radix);
slouken@7351
   726
#else
slouken@1330
   727
    char *bufp = string;
slouken@1330
   728
slouken@1895
   729
    if (value) {
slouken@1895
   730
        while (value > 0) {
slouken@1330
   731
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   732
            value /= radix;
slouken@1330
   733
        }
slouken@1330
   734
    } else {
slouken@1330
   735
        *bufp++ = '0';
slouken@1330
   736
    }
slouken@1330
   737
    *bufp = '\0';
slouken@1330
   738
slouken@1330
   739
    /* The numbers went into the string backwards. :) */
slouken@1336
   740
    SDL_strrev(string);
slouken@1330
   741
slouken@1330
   742
    return string;
slouken@7351
   743
#endif /* HAVE__UI64TOA */
slouken@1330
   744
}
slouken@1330
   745
slouken@7353
   746
int SDL_atoi(const char *string)
slouken@7351
   747
{
slouken@7351
   748
#ifdef HAVE_ATOI
slouken@7353
   749
    return atoi(string);
icculus@7003
   750
#else
slouken@7353
   751
    return SDL_strtol(string, NULL, 0);
slouken@7351
   752
#endif /* HAVE_ATOI */
slouken@7351
   753
}
slouken@7351
   754
slouken@7353
   755
double SDL_atof(const char *string)
slouken@7351
   756
{
slouken@7351
   757
#ifdef HAVE_ATOF
slouken@7353
   758
    return (double) atof(string);
slouken@7351
   759
#else
slouken@7353
   760
    return SDL_strtod(string, NULL);
slouken@7351
   761
#endif /* HAVE_ATOF */
slouken@7351
   762
}
slouken@7351
   763
slouken@7351
   764
long
slouken@7351
   765
SDL_strtol(const char *string, char **endp, int base)
slouken@7351
   766
{
slouken@7351
   767
#if defined(HAVE_STRTOL)
slouken@7352
   768
    return strtol(string, endp, base);
slouken@7351
   769
#else
slouken@7351
   770
    size_t len;
slouken@7351
   771
    long value;
slouken@7351
   772
slouken@7351
   773
    if (!base) {
slouken@7351
   774
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@7351
   775
            base = 16;
slouken@7351
   776
        } else {
slouken@7351
   777
            base = 10;
slouken@7351
   778
        }
slouken@7351
   779
    }
slouken@7351
   780
slouken@7351
   781
    len = SDL_ScanLong(string, base, &value);
slouken@7351
   782
    if (endp) {
slouken@7351
   783
        *endp = (char *) string + len;
slouken@7351
   784
    }
slouken@7351
   785
    return value;
slouken@7351
   786
#endif /* HAVE_STRTOL */
slouken@7351
   787
}
slouken@7351
   788
slouken@7351
   789
unsigned long
slouken@7351
   790
SDL_strtoul(const char *string, char **endp, int base)
slouken@7351
   791
{
slouken@7351
   792
#if defined(HAVE_STRTOUL)
slouken@7352
   793
    return strtoul(string, endp, base);
slouken@7351
   794
#else
slouken@7351
   795
    size_t len;
slouken@7351
   796
    unsigned long value;
slouken@7351
   797
slouken@7351
   798
    if (!base) {
slouken@7351
   799
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@7351
   800
            base = 16;
slouken@7351
   801
        } else {
slouken@7351
   802
            base = 10;
slouken@7351
   803
        }
slouken@7351
   804
    }
slouken@7351
   805
slouken@7351
   806
    len = SDL_ScanUnsignedLong(string, base, &value);
slouken@7351
   807
    if (endp) {
slouken@7351
   808
        *endp = (char *) string + len;
slouken@7351
   809
    }
slouken@7351
   810
    return value;
slouken@7351
   811
#endif /* HAVE_STRTOUL */
slouken@7351
   812
}
slouken@7351
   813
slouken@1895
   814
Sint64
slouken@1895
   815
SDL_strtoll(const char *string, char **endp, int base)
slouken@1330
   816
{
slouken@7351
   817
#if defined(HAVE_STRTOLL)
slouken@7352
   818
    return strtoll(string, endp, base);
slouken@7351
   819
#else
slouken@1330
   820
    size_t len;
slouken@1330
   821
    Sint64 value;
slouken@1330
   822
slouken@1895
   823
    if (!base) {
slouken@1895
   824
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   825
            base = 16;
slouken@1867
   826
        } else {
slouken@1867
   827
            base = 10;
slouken@1867
   828
        }
slouken@1867
   829
    }
slouken@1867
   830
slouken@1867
   831
    len = SDL_ScanLongLong(string, base, &value);
slouken@1895
   832
    if (endp) {
slouken@1895
   833
        *endp = (char *) string + len;
slouken@1330
   834
    }
slouken@1330
   835
    return value;
slouken@7351
   836
#endif /* HAVE_STRTOLL */
slouken@1330
   837
}
slouken@1330
   838
slouken@1895
   839
Uint64
slouken@1895
   840
SDL_strtoull(const char *string, char **endp, int base)
slouken@1456
   841
{
slouken@7351
   842
#if defined(HAVE_STRTOULL)
slouken@7352
   843
    return strtoull(string, endp, base);
slouken@7351
   844
#else
slouken@1456
   845
    size_t len;
slouken@1456
   846
    Uint64 value;
slouken@1456
   847
slouken@1895
   848
    if (!base) {
slouken@1895
   849
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   850
            base = 16;
slouken@1867
   851
        } else {
slouken@1867
   852
            base = 10;
slouken@1867
   853
        }
slouken@1867
   854
    }
slouken@1867
   855
slouken@1867
   856
    len = SDL_ScanUnsignedLongLong(string, base, &value);
slouken@1895
   857
    if (endp) {
slouken@1895
   858
        *endp = (char *) string + len;
slouken@1456
   859
    }
slouken@1456
   860
    return value;
slouken@7351
   861
#endif /* HAVE_STRTOULL */
slouken@1456
   862
}
slouken@1456
   863
slouken@1895
   864
double
slouken@1895
   865
SDL_strtod(const char *string, char **endp)
slouken@1341
   866
{
slouken@7351
   867
#if defined(HAVE_STRTOD)
slouken@7352
   868
    return strtod(string, endp);
slouken@7351
   869
#else
slouken@1341
   870
    size_t len;
slouken@1341
   871
    double value;
slouken@1341
   872
slouken@1341
   873
    len = SDL_ScanFloat(string, &value);
slouken@1895
   874
    if (endp) {
slouken@1895
   875
        *endp = (char *) string + len;
slouken@1341
   876
    }
slouken@1341
   877
    return value;
slouken@7351
   878
#endif /* HAVE_STRTOD */
slouken@1341
   879
}
slouken@1341
   880
slouken@1895
   881
int
slouken@1895
   882
SDL_strcmp(const char *str1, const char *str2)
slouken@1330
   883
{
slouken@7351
   884
#if defined(HAVE_STRCMP)
slouken@7351
   885
    return strcmp(str1, str2);
slouken@7351
   886
#else
slouken@1330
   887
    while (*str1 && *str2) {
slouken@1895
   888
        if (*str1 != *str2)
slouken@1330
   889
            break;
slouken@1330
   890
        ++str1;
slouken@1330
   891
        ++str2;
slouken@1330
   892
    }
slouken@1895
   893
    return (int) ((unsigned char) *str1 - (unsigned char) *str2);
slouken@7351
   894
#endif /* HAVE_STRCMP */
slouken@1330
   895
}
slouken@1330
   896
slouken@1895
   897
int
slouken@1895
   898
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
slouken@1330
   899
{
slouken@7351
   900
#if defined(HAVE_STRNCMP)
slouken@7351
   901
    return strncmp(str1, str2, maxlen);
slouken@7351
   902
#else
slouken@1895
   903
    while (*str1 && *str2 && maxlen) {
slouken@1895
   904
        if (*str1 != *str2)
slouken@1330
   905
            break;
slouken@1330
   906
        ++str1;
slouken@1330
   907
        ++str2;
slouken@1330
   908
        --maxlen;
slouken@1330
   909
    }
slouken@1895
   910
    if (!maxlen) {
slouken@1330
   911
        return 0;
slouken@1330
   912
    }
slouken@1895
   913
    return (int) ((unsigned char) *str1 - (unsigned char) *str2);
slouken@7351
   914
#endif /* HAVE_STRNCMP */
slouken@1330
   915
}
slouken@1330
   916
slouken@1895
   917
int
slouken@1895
   918
SDL_strcasecmp(const char *str1, const char *str2)
slouken@1330
   919
{
slouken@7351
   920
#ifdef HAVE_STRCASECMP
slouken@7351
   921
    return strcasecmp(str1, str2);
slouken@7351
   922
#elif defined(HAVE__STRICMP)
slouken@7351
   923
    return _stricmp(str1, str2);
slouken@7351
   924
#else
slouken@1330
   925
    char a = 0;
slouken@1330
   926
    char b = 0;
slouken@1895
   927
    while (*str1 && *str2) {
slouken@7351
   928
        a = SDL_toupper((unsigned char) *str1);
slouken@7351
   929
        b = SDL_toupper((unsigned char) *str2);
slouken@1895
   930
        if (a != b)
slouken@1330
   931
            break;
slouken@1330
   932
        ++str1;
slouken@1330
   933
        ++str2;
slouken@1330
   934
    }
slouken@7351
   935
    a = SDL_toupper(*str1);
slouken@7351
   936
    b = SDL_toupper(*str2);
slouken@1895
   937
    return (int) ((unsigned char) a - (unsigned char) b);
slouken@7351
   938
#endif /* HAVE_STRCASECMP */
slouken@1330
   939
}
slouken@1330
   940
slouken@1895
   941
int
slouken@1895
   942
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
slouken@1501
   943
{
slouken@7351
   944
#ifdef HAVE_STRNCASECMP
slouken@7352
   945
    return strncasecmp(str1, str2, maxlen);
slouken@7351
   946
#elif defined(HAVE__STRNICMP)
slouken@7352
   947
    return _strnicmp(str1, str2, maxlen);
slouken@7351
   948
#else
slouken@1501
   949
    char a = 0;
slouken@1501
   950
    char b = 0;
slouken@1895
   951
    while (*str1 && *str2 && maxlen) {
icculus@2092
   952
        a = SDL_tolower((unsigned char) *str1);
icculus@2092
   953
        b = SDL_tolower((unsigned char) *str2);
slouken@1895
   954
        if (a != b)
slouken@1501
   955
            break;
slouken@1501
   956
        ++str1;
slouken@1501
   957
        ++str2;
slouken@1501
   958
        --maxlen;
slouken@1501
   959
    }
slouken@6766
   960
    if (maxlen == 0) {
slouken@6766
   961
        return 0;
slouken@6766
   962
    } else {
slouken@6766
   963
        a = SDL_tolower((unsigned char) *str1);
slouken@6766
   964
        b = SDL_tolower((unsigned char) *str2);
slouken@6766
   965
        return (int) ((unsigned char) a - (unsigned char) b);
slouken@6766
   966
    }
slouken@7351
   967
#endif /* HAVE_STRNCASECMP */
slouken@1501
   968
}
slouken@1501
   969
icculus@7003
   970
int
icculus@7003
   971
SDL_sscanf(const char *text, const char *fmt, ...)
icculus@7003
   972
{
icculus@7003
   973
    int rc;
icculus@7003
   974
    va_list ap;
icculus@7003
   975
    va_start(ap, fmt);
icculus@8089
   976
    rc = SDL_vsscanf(text, fmt, ap);
icculus@7003
   977
    va_end(ap);
icculus@7003
   978
    return rc;
icculus@7003
   979
}
icculus@8089
   980
icculus@8089
   981
#ifdef HAVE_VSSCANF
icculus@8089
   982
int
icculus@8089
   983
SDL_vsscanf(const char *text, const char *fmt, va_list ap)
icculus@8089
   984
{
icculus@8089
   985
    return vsscanf(text, fmt, ap);
icculus@8089
   986
}
icculus@7003
   987
#else
slouken@1895
   988
int
icculus@8092
   989
SDL_vsscanf(const char *text, const char *fmt, va_list ap)
slouken@1330
   990
{
slouken@1330
   991
    int retval = 0;
slouken@1330
   992
slouken@1895
   993
    while (*fmt) {
slouken@1895
   994
        if (*fmt == ' ') {
icculus@2092
   995
            while (SDL_isspace((unsigned char) *text)) {
slouken@1330
   996
                ++text;
slouken@1330
   997
            }
slouken@1330
   998
            ++fmt;
slouken@1330
   999
            continue;
slouken@1330
  1000
        }
slouken@1895
  1001
        if (*fmt == '%') {
slouken@1330
  1002
            SDL_bool done = SDL_FALSE;
slouken@1330
  1003
            long count = 0;
slouken@1330
  1004
            int radix = 10;
slouken@1895
  1005
            enum
slouken@1895
  1006
            {
slouken@1330
  1007
                DO_SHORT,
slouken@1330
  1008
                DO_INT,
slouken@1330
  1009
                DO_LONG,
slouken@1330
  1010
                DO_LONGLONG
slouken@1330
  1011
            } inttype = DO_INT;
slouken@1330
  1012
            SDL_bool suppress = SDL_FALSE;
slouken@1330
  1013
slouken@1330
  1014
            ++fmt;
slouken@1895
  1015
            if (*fmt == '%') {
slouken@1895
  1016
                if (*text == '%') {
slouken@1330
  1017
                    ++text;
slouken@1330
  1018
                    ++fmt;
slouken@1330
  1019
                    continue;
slouken@1330
  1020
                }
slouken@1330
  1021
                break;
slouken@1330
  1022
            }
slouken@1895
  1023
            if (*fmt == '*') {
slouken@1330
  1024
                suppress = SDL_TRUE;
slouken@1330
  1025
                ++fmt;
slouken@1330
  1026
            }
slouken@1330
  1027
            fmt += SDL_ScanLong(fmt, 10, &count);
slouken@1330
  1028
slouken@1895
  1029
            if (*fmt == 'c') {
slouken@1895
  1030
                if (!count) {
slouken@1330
  1031
                    count = 1;
slouken@1330
  1032
                }
slouken@1895
  1033
                if (suppress) {
slouken@1895
  1034
                    while (count--) {
slouken@1330
  1035
                        ++text;
slouken@1330
  1036
                    }
slouken@1330
  1037
                } else {
slouken@1895
  1038
                    char *valuep = va_arg(ap, char *);
slouken@1895
  1039
                    while (count--) {
slouken@1330
  1040
                        *valuep++ = *text++;
slouken@1330
  1041
                    }
slouken@1330
  1042
                    ++retval;
slouken@1330
  1043
                }
slouken@1330
  1044
                continue;
slouken@1330
  1045
            }
slouken@1330
  1046
icculus@2092
  1047
            while (SDL_isspace((unsigned char) *text)) {
slouken@1330
  1048
                ++text;
slouken@1330
  1049
            }
slouken@1330
  1050
slouken@1330
  1051
            /* FIXME: implement more of the format specifiers */
slouken@1330
  1052
            while (!done) {
slouken@1895
  1053
                switch (*fmt) {
slouken@1895
  1054
                case '*':
slouken@1895
  1055
                    suppress = SDL_TRUE;
slouken@1895
  1056
                    break;
slouken@1895
  1057
                case 'h':
slouken@1895
  1058
                    if (inttype > DO_SHORT) {
slouken@1895
  1059
                        ++inttype;
slouken@1895
  1060
                    }
slouken@1895
  1061
                    break;
slouken@1895
  1062
                case 'l':
slouken@1895
  1063
                    if (inttype < DO_LONGLONG) {
slouken@1895
  1064
                        ++inttype;
slouken@1895
  1065
                    }
slouken@1895
  1066
                    break;
slouken@1895
  1067
                case 'I':
slouken@1895
  1068
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
  1069
                        fmt += 2;
slouken@1895
  1070
                        inttype = DO_LONGLONG;
slouken@1895
  1071
                    }
slouken@1895
  1072
                    break;
slouken@1895
  1073
                case 'i':
slouken@1895
  1074
                    {
slouken@1895
  1075
                        int index = 0;
slouken@1895
  1076
                        if (text[index] == '-') {
slouken@1895
  1077
                            ++index;
slouken@1330
  1078
                        }
slouken@1895
  1079
                        if (text[index] == '0') {
slouken@7216
  1080
                            if (SDL_tolower((unsigned char) text[index + 1]) == 'x') {
slouken@1895
  1081
                                radix = 16;
slouken@1895
  1082
                            } else {
slouken@1895
  1083
                                radix = 8;
slouken@1895
  1084
                            }
slouken@1330
  1085
                        }
slouken@1895
  1086
                    }
slouken@1895
  1087
                    /* Fall through to %d handling */
slouken@1895
  1088
                case 'd':
slouken@1895
  1089
                    if (inttype == DO_LONGLONG) {
slouken@1895
  1090
                        Sint64 value;
slouken@1895
  1091
                        text += SDL_ScanLongLong(text, radix, &value);
slouken@1895
  1092
                        if (!suppress) {
slouken@1895
  1093
                            Sint64 *valuep = va_arg(ap, Sint64 *);
slouken@1895
  1094
                            *valuep = value;
slouken@1895
  1095
                            ++retval;
slouken@1330
  1096
                        }
slouken@5512
  1097
                    } else {
slouken@1895
  1098
                        long value;
slouken@1895
  1099
                        text += SDL_ScanLong(text, radix, &value);
slouken@1895
  1100
                        if (!suppress) {
slouken@1895
  1101
                            switch (inttype) {
slouken@1895
  1102
                            case DO_SHORT:
slouken@1895
  1103
                                {
slouken@1895
  1104
                                    short *valuep = va_arg(ap, short *);
slouken@1895
  1105
                                    *valuep = (short) value;
slouken@1895
  1106
                                }
slouken@1895
  1107
                                break;
slouken@1895
  1108
                            case DO_INT:
slouken@1895
  1109
                                {
slouken@1895
  1110
                                    int *valuep = va_arg(ap, int *);
slouken@1895
  1111
                                    *valuep = (int) value;
slouken@1895
  1112
                                }
slouken@1895
  1113
                                break;
slouken@1895
  1114
                            case DO_LONG:
slouken@1895
  1115
                                {
slouken@1895
  1116
                                    long *valuep = va_arg(ap, long *);
slouken@1895
  1117
                                    *valuep = value;
slouken@1895
  1118
                                }
slouken@1895
  1119
                                break;
slouken@1895
  1120
                            case DO_LONGLONG:
slouken@1895
  1121
                                /* Handled above */
slouken@1895
  1122
                                break;
slouken@1330
  1123
                            }
slouken@1895
  1124
                            ++retval;
slouken@1895
  1125
                        }
slouken@1895
  1126
                    }
slouken@1895
  1127
                    done = SDL_TRUE;
slouken@1895
  1128
                    break;
slouken@1895
  1129
                case 'o':
slouken@1895
  1130
                    if (radix == 10) {
slouken@1895
  1131
                        radix = 8;
slouken@1895
  1132
                    }
slouken@1895
  1133
                    /* Fall through to unsigned handling */
slouken@1895
  1134
                case 'x':
slouken@1895
  1135
                case 'X':
slouken@1895
  1136
                    if (radix == 10) {
slouken@1895
  1137
                        radix = 16;
slouken@1895
  1138
                    }
slouken@1895
  1139
                    /* Fall through to unsigned handling */
slouken@1895
  1140
                case 'u':
slouken@1895
  1141
                    if (inttype == DO_LONGLONG) {
slouken@1895
  1142
                        Uint64 value;
slouken@1895
  1143
                        text += SDL_ScanUnsignedLongLong(text, radix, &value);
slouken@1895
  1144
                        if (!suppress) {
slouken@1895
  1145
                            Uint64 *valuep = va_arg(ap, Uint64 *);
slouken@1895
  1146
                            *valuep = value;
slouken@1895
  1147
                            ++retval;
slouken@1895
  1148
                        }
slouken@5512
  1149
                    } else {
slouken@1895
  1150
                        unsigned long value;
slouken@1895
  1151
                        text += SDL_ScanUnsignedLong(text, radix, &value);
slouken@1895
  1152
                        if (!suppress) {
slouken@1895
  1153
                            switch (inttype) {
slouken@1895
  1154
                            case DO_SHORT:
slouken@1895
  1155
                                {
slouken@1895
  1156
                                    short *valuep = va_arg(ap, short *);
slouken@1895
  1157
                                    *valuep = (short) value;
slouken@1895
  1158
                                }
slouken@1895
  1159
                                break;
slouken@1895
  1160
                            case DO_INT:
slouken@1895
  1161
                                {
slouken@1895
  1162
                                    int *valuep = va_arg(ap, int *);
slouken@1895
  1163
                                    *valuep = (int) value;
slouken@1895
  1164
                                }
slouken@1895
  1165
                                break;
slouken@1895
  1166
                            case DO_LONG:
slouken@1895
  1167
                                {
slouken@1895
  1168
                                    long *valuep = va_arg(ap, long *);
slouken@1895
  1169
                                    *valuep = value;
slouken@1895
  1170
                                }
slouken@1895
  1171
                                break;
slouken@1895
  1172
                            case DO_LONGLONG:
slouken@1895
  1173
                                /* Handled above */
slouken@1895
  1174
                                break;
slouken@1895
  1175
                            }
slouken@1895
  1176
                            ++retval;
slouken@1895
  1177
                        }
slouken@1895
  1178
                    }
slouken@1895
  1179
                    done = SDL_TRUE;
slouken@1895
  1180
                    break;
slouken@1895
  1181
                case 'p':
slouken@1895
  1182
                    {
slouken@1895
  1183
                        uintptr_t value;
slouken@1895
  1184
                        text += SDL_ScanUintPtrT(text, 16, &value);
slouken@1895
  1185
                        if (!suppress) {
slouken@1895
  1186
                            void **valuep = va_arg(ap, void **);
slouken@1895
  1187
                            *valuep = (void *) value;
slouken@1895
  1188
                            ++retval;
slouken@1895
  1189
                        }
slouken@1895
  1190
                    }
slouken@1895
  1191
                    done = SDL_TRUE;
slouken@1895
  1192
                    break;
slouken@1895
  1193
                case 'f':
slouken@1895
  1194
                    {
slouken@1895
  1195
                        double value;
slouken@1895
  1196
                        text += SDL_ScanFloat(text, &value);
slouken@1895
  1197
                        if (!suppress) {
slouken@1895
  1198
                            float *valuep = va_arg(ap, float *);
slouken@1895
  1199
                            *valuep = (float) value;
slouken@1895
  1200
                            ++retval;
slouken@1895
  1201
                        }
slouken@1895
  1202
                    }
slouken@1895
  1203
                    done = SDL_TRUE;
slouken@1895
  1204
                    break;
slouken@1895
  1205
                case 's':
slouken@1895
  1206
                    if (suppress) {
icculus@2092
  1207
                        while (!SDL_isspace((unsigned char) *text)) {
slouken@1895
  1208
                            ++text;
slouken@1895
  1209
                            if (count) {
slouken@1895
  1210
                                if (--count == 0) {
slouken@1895
  1211
                                    break;
slouken@1330
  1212
                                }
slouken@1330
  1213
                            }
slouken@1330
  1214
                        }
slouken@1895
  1215
                    } else {
slouken@1895
  1216
                        char *valuep = va_arg(ap, char *);
icculus@2092
  1217
                        while (!SDL_isspace((unsigned char) *text)) {
slouken@1895
  1218
                            *valuep++ = *text++;
slouken@1895
  1219
                            if (count) {
slouken@1895
  1220
                                if (--count == 0) {
slouken@1895
  1221
                                    break;
slouken@1895
  1222
                                }
slouken@1330
  1223
                            }
slouken@1330
  1224
                        }
slouken@1895
  1225
                        *valuep = '\0';
slouken@1895
  1226
                        ++retval;
slouken@1895
  1227
                    }
slouken@1895
  1228
                    done = SDL_TRUE;
slouken@1895
  1229
                    break;
slouken@1895
  1230
                default:
slouken@1895
  1231
                    done = SDL_TRUE;
slouken@1895
  1232
                    break;
slouken@1330
  1233
                }
slouken@1330
  1234
                ++fmt;
slouken@1330
  1235
            }
slouken@1330
  1236
            continue;
slouken@1330
  1237
        }
slouken@1895
  1238
        if (*text == *fmt) {
slouken@1330
  1239
            ++text;
slouken@1330
  1240
            ++fmt;
slouken@1330
  1241
            continue;
slouken@1330
  1242
        }
slouken@1330
  1243
        /* Text didn't match format specifier */
slouken@1330
  1244
        break;
slouken@1330
  1245
    }
slouken@1330
  1246
slouken@1330
  1247
    return retval;
slouken@1330
  1248
}
icculus@8089
  1249
#endif /* HAVE_VSSCANF */
slouken@1330
  1250
slouken@1895
  1251
int
slouken@1895
  1252
SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
slouken@1330
  1253
{
slouken@1330
  1254
    va_list ap;
slouken@1330
  1255
    int retval;
slouken@1330
  1256
slouken@1330
  1257
    va_start(ap, fmt);
slouken@1330
  1258
    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
slouken@1330
  1259
    va_end(ap);
slouken@1330
  1260
slouken@1330
  1261
    return retval;
slouken@1330
  1262
}
slouken@1330
  1263
slouken@7351
  1264
#ifdef HAVE_VSNPRINTF
slouken@7351
  1265
int SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@7351
  1266
{
slouken@7351
  1267
    return vsnprintf(text, maxlen, fmt, ap);
slouken@7351
  1268
}
icculus@7003
  1269
#else
slouken@7216
  1270
 /* FIXME: implement more of the format specifiers */
slouken@7728
  1271
typedef enum
slouken@7728
  1272
{
slouken@7728
  1273
    SDL_CASE_NOCHANGE,
slouken@7728
  1274
    SDL_CASE_LOWER,
slouken@7728
  1275
    SDL_CASE_UPPER
slouken@7728
  1276
} SDL_letter_case;
slouken@7728
  1277
slouken@7216
  1278
typedef struct
slouken@7216
  1279
{
slouken@7216
  1280
    SDL_bool left_justify;
slouken@7216
  1281
    SDL_bool force_sign;
slouken@7216
  1282
    SDL_bool force_type;
slouken@7216
  1283
    SDL_bool pad_zeroes;
slouken@7728
  1284
    SDL_letter_case force_case;
slouken@7216
  1285
    int width;
slouken@7216
  1286
    int radix;
slouken@7216
  1287
    int precision;
slouken@7216
  1288
} SDL_FormatInfo;
slouken@7216
  1289
slouken@1895
  1290
static size_t
slouken@7216
  1291
SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
slouken@1330
  1292
{
slouken@7503
  1293
    size_t length = 0;
slouken@7503
  1294
slouken@7503
  1295
    if (info && info->width && (size_t)info->width > SDL_strlen(string)) {
slouken@7503
  1296
        char fill = info->pad_zeroes ? '0' : ' ';
slouken@7503
  1297
        size_t width = info->width - SDL_strlen(string);
slouken@7503
  1298
        while (width-- > 0 && maxlen > 0) {
slouken@7503
  1299
            *text++ = fill;
slouken@7503
  1300
            ++length;
slouken@7503
  1301
            --maxlen;
slouken@7503
  1302
        }
slouken@7503
  1303
    }
slouken@7503
  1304
slouken@7503
  1305
    length += SDL_strlcpy(text, string, maxlen);
slouken@7503
  1306
slouken@7728
  1307
    if (info) {
slouken@7728
  1308
        if (info->force_case == SDL_CASE_LOWER) {
slouken@7728
  1309
            SDL_strlwr(text);
slouken@7728
  1310
        } else if (info->force_case == SDL_CASE_UPPER) {
slouken@7728
  1311
            SDL_strupr(text);
slouken@7728
  1312
        }
slouken@7503
  1313
    }
slouken@7503
  1314
    return length;
slouken@1330
  1315
}
slouken@1895
  1316
slouken@1895
  1317
static size_t
slouken@7216
  1318
SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
slouken@1330
  1319
{
slouken@1330
  1320
    char num[130];
slouken@1330
  1321
slouken@7216
  1322
    SDL_ltoa(value, num, info ? info->radix : 10);
slouken@7216
  1323
    return SDL_PrintString(text, maxlen, info, num);
slouken@1330
  1324
}
slouken@1895
  1325
slouken@1895
  1326
static size_t
slouken@7216
  1327
SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
slouken@1330
  1328
{
slouken@1330
  1329
    char num[130];
slouken@1330
  1330
slouken@7216
  1331
    SDL_ultoa(value, num, info ? info->radix : 10);
slouken@7216
  1332
    return SDL_PrintString(text, maxlen, info, num);
slouken@1330
  1333
}
slouken@1895
  1334
slouken@1895
  1335
static size_t
slouken@7216
  1336
SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
slouken@1330
  1337
{
slouken@1330
  1338
    char num[130];
slouken@1330
  1339
slouken@7216
  1340
    SDL_lltoa(value, num, info ? info->radix : 10);
slouken@7216
  1341
    return SDL_PrintString(text, maxlen, info, num);
slouken@1330
  1342
}
slouken@5512
  1343
slouken@1895
  1344
static size_t
slouken@7216
  1345
SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
slouken@1330
  1346
{
slouken@7216
  1347
    char num[130];
slouken@7216
  1348
slouken@7216
  1349
    SDL_ulltoa(value, num, info ? info->radix : 10);
slouken@7216
  1350
    return SDL_PrintString(text, maxlen, info, num);
slouken@7216
  1351
}
slouken@7216
  1352
slouken@7216
  1353
static size_t
slouken@7216
  1354
SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
slouken@7216
  1355
{
icculus@7482
  1356
    int width;
slouken@7217
  1357
    size_t len;
slouken@7216
  1358
    size_t left = maxlen;
slouken@1330
  1359
    char *textstart = text;
slouken@7216
  1360
slouken@1895
  1361
    if (arg) {
slouken@1330
  1362
        /* This isn't especially accurate, but hey, it's easy. :) */
slouken@1330
  1363
        unsigned long value;
slouken@1330
  1364
slouken@1895
  1365
        if (arg < 0) {
slouken@7216
  1366
            if (left > 1) {
slouken@7216
  1367
                *text = '-';
slouken@7216
  1368
                --left;
slouken@7216
  1369
            }
slouken@7216
  1370
            ++text;
slouken@1330
  1371
            arg = -arg;
slouken@7216
  1372
        } else if (info->force_sign) {
slouken@7216
  1373
            if (left > 1) {
slouken@7216
  1374
                *text = '+';
slouken@7216
  1375
                --left;
slouken@7216
  1376
            }
slouken@7216
  1377
            ++text;
slouken@1330
  1378
        }
slouken@1895
  1379
        value = (unsigned long) arg;
slouken@7216
  1380
        len = SDL_PrintUnsignedLong(text, left, NULL, value);
slouken@1330
  1381
        text += len;
slouken@7216
  1382
        if (len >= left) {
slouken@7216
  1383
            left = SDL_min(left, 1);
slouken@7216
  1384
        } else {
slouken@7216
  1385
            left -= len;
slouken@7216
  1386
        }
slouken@1330
  1387
        arg -= value;
slouken@7216
  1388
        if (info->precision < 0) {
slouken@7216
  1389
            info->precision = 6;
slouken@7216
  1390
        }
slouken@7216
  1391
        if (info->force_type || info->precision > 0) {
slouken@1330
  1392
            int mult = 10;
slouken@7216
  1393
            if (left > 1) {
slouken@7216
  1394
                *text = '.';
slouken@7216
  1395
                --left;
slouken@7216
  1396
            }
slouken@7216
  1397
            ++text;
slouken@7216
  1398
            while (info->precision-- > 0) {
slouken@1895
  1399
                value = (unsigned long) (arg * mult);
slouken@7216
  1400
                len = SDL_PrintUnsignedLong(text, left, NULL, value);
slouken@1330
  1401
                text += len;
slouken@7216
  1402
                if (len >= left) {
slouken@7216
  1403
                    left = SDL_min(left, 1);
slouken@7216
  1404
                } else {
slouken@7216
  1405
                    left -= len;
slouken@7216
  1406
                }
slouken@1895
  1407
                arg -= (double) value / mult;
slouken@1330
  1408
                mult *= 10;
slouken@1330
  1409
            }
slouken@1330
  1410
        }
slouken@1330
  1411
    } else {
slouken@7216
  1412
        if (left > 1) {
slouken@7216
  1413
            *text = '0';
slouken@7216
  1414
            --left;
slouken@7216
  1415
        }
slouken@7216
  1416
        ++text;
slouken@7216
  1417
        if (info->force_type) {
slouken@7216
  1418
            if (left > 1) {
slouken@7216
  1419
                *text = '.';
slouken@7216
  1420
                --left;
slouken@7216
  1421
            }
slouken@7216
  1422
            ++text;
slouken@7216
  1423
        }
slouken@1330
  1424
    }
slouken@1895
  1425
slouken@7216
  1426
    width = info->width - (int)(text - textstart);
slouken@7216
  1427
    if (width > 0) {
slouken@7216
  1428
        char fill = info->pad_zeroes ? '0' : ' ';
slouken@7216
  1429
        char *end = text+left-1;
slouken@7216
  1430
        len = (text - textstart);
slouken@7216
  1431
        for (len = (text - textstart); len--; ) {
slouken@7216
  1432
            if ((textstart+len+width) < end) {
slouken@7216
  1433
                *(textstart+len+width) = *(textstart+len);
slouken@7216
  1434
            }
slouken@7216
  1435
        }
slouken@7216
  1436
        len = (size_t)width;
slouken@7216
  1437
        text += len;
slouken@7216
  1438
        if (len >= left) {
slouken@7216
  1439
            left = SDL_min(left, 1);
slouken@7216
  1440
        } else {
slouken@7216
  1441
            left -= len;
slouken@7216
  1442
        }
slouken@7216
  1443
        while (len--) {
slouken@7216
  1444
            if (textstart+len < end) {
slouken@7216
  1445
                textstart[len] = fill;
slouken@7216
  1446
            }
slouken@7216
  1447
        }
slouken@1330
  1448
    }
slouken@7216
  1449
slouken@1330
  1450
    return (text - textstart);
slouken@1330
  1451
}
slouken@1895
  1452
slouken@1895
  1453
int
slouken@1895
  1454
SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@1330
  1455
{
slouken@7216
  1456
    size_t left = maxlen;
slouken@1330
  1457
    char *textstart = text;
slouken@7216
  1458
slouken@7216
  1459
    while (*fmt) {
slouken@1895
  1460
        if (*fmt == '%') {
slouken@1330
  1461
            SDL_bool done = SDL_FALSE;
slouken@1330
  1462
            size_t len = 0;
slouken@7216
  1463
            SDL_bool check_flag;
slouken@7216
  1464
            SDL_FormatInfo info;
slouken@1895
  1465
            enum
slouken@1895
  1466
            {
slouken@1330
  1467
                DO_INT,
slouken@1330
  1468
                DO_LONG,
slouken@1330
  1469
                DO_LONGLONG
slouken@1330
  1470
            } inttype = DO_INT;
slouken@1330
  1471
slouken@7216
  1472
            SDL_zero(info);
slouken@7216
  1473
            info.radix = 10;
slouken@7216
  1474
            info.precision = -1;
slouken@7216
  1475
slouken@7216
  1476
            check_flag = SDL_TRUE;
slouken@7216
  1477
            while (check_flag) {
slouken@1818
  1478
                ++fmt;
slouken@7216
  1479
                switch (*fmt) {
slouken@7216
  1480
                case '-':
slouken@7216
  1481
                    info.left_justify = SDL_TRUE;
slouken@7216
  1482
                    break;
slouken@7216
  1483
                case '+':
slouken@7216
  1484
                    info.force_sign = SDL_TRUE;
slouken@7216
  1485
                    break;
slouken@7216
  1486
                case '#':
slouken@7216
  1487
                    info.force_type = SDL_TRUE;
slouken@7216
  1488
                    break;
slouken@7216
  1489
                case '0':
slouken@7216
  1490
                    info.pad_zeroes = SDL_TRUE;
slouken@7216
  1491
                    break;
slouken@7216
  1492
                default:
slouken@7216
  1493
                    check_flag = SDL_FALSE;
slouken@7216
  1494
                    break;
slouken@7216
  1495
                }
slouken@1818
  1496
            }
slouken@7216
  1497
slouken@7216
  1498
            if (*fmt >= '0' && *fmt <= '9') {
slouken@7216
  1499
                info.width = SDL_strtol(fmt, (char **)&fmt, 0);
slouken@7216
  1500
            }
slouken@7216
  1501
slouken@7216
  1502
            if (*fmt == '.') {
slouken@7216
  1503
                ++fmt;
slouken@7216
  1504
                if (*fmt >= '0' && *fmt <= '9') {
slouken@7216
  1505
                    info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
slouken@7216
  1506
                } else {
slouken@7216
  1507
                    info.precision = 0;
slouken@7216
  1508
                }
slouken@7216
  1509
            }
slouken@7216
  1510
slouken@1330
  1511
            while (!done) {
slouken@1895
  1512
                switch (*fmt) {
slouken@1895
  1513
                case '%':
slouken@7216
  1514
                    if (left > 1) {
slouken@7216
  1515
                        *text = '%';
slouken@7216
  1516
                    }
slouken@1895
  1517
                    len = 1;
slouken@1895
  1518
                    done = SDL_TRUE;
slouken@1895
  1519
                    break;
slouken@1895
  1520
                case 'c':
slouken@1895
  1521
                    /* char is promoted to int when passed through (...) */
slouken@7216
  1522
                    if (left > 1) {
slouken@7216
  1523
                        *text = (char) va_arg(ap, int);
slouken@7216
  1524
                    }
slouken@1895
  1525
                    len = 1;
slouken@1895
  1526
                    done = SDL_TRUE;
slouken@1895
  1527
                    break;
slouken@1895
  1528
                case 'h':
slouken@1895
  1529
                    /* short is promoted to int when passed through (...) */
slouken@1895
  1530
                    break;
slouken@1895
  1531
                case 'l':
slouken@1895
  1532
                    if (inttype < DO_LONGLONG) {
slouken@1895
  1533
                        ++inttype;
slouken@1895
  1534
                    }
slouken@1895
  1535
                    break;
slouken@1895
  1536
                case 'I':
slouken@1895
  1537
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
  1538
                        fmt += 2;
slouken@1895
  1539
                        inttype = DO_LONGLONG;
slouken@1895
  1540
                    }
slouken@1895
  1541
                    break;
slouken@1895
  1542
                case 'i':
slouken@1895
  1543
                case 'd':
slouken@1895
  1544
                    switch (inttype) {
slouken@1895
  1545
                    case DO_INT:
slouken@7216
  1546
                        len = SDL_PrintLong(text, left, &info,
slouken@7216
  1547
                                            (long) va_arg(ap, int));
slouken@1330
  1548
                        break;
slouken@1895
  1549
                    case DO_LONG:
slouken@7216
  1550
                        len = SDL_PrintLong(text, left, &info,
slouken@7216
  1551
                                            va_arg(ap, long));
slouken@1330
  1552
                        break;
slouken@1895
  1553
                    case DO_LONGLONG:
slouken@7216
  1554
                        len = SDL_PrintLongLong(text, left, &info,
slouken@7216
  1555
                                                va_arg(ap, Sint64));
slouken@1330
  1556
                        break;
slouken@1895
  1557
                    }
slouken@1895
  1558
                    done = SDL_TRUE;
slouken@1895
  1559
                    break;
slouken@1895
  1560
                case 'p':
slouken@1895
  1561
                case 'x':
slouken@7728
  1562
                    info.force_case = SDL_CASE_LOWER;
slouken@1895
  1563
                    /* Fall through to 'X' handling */
slouken@1895
  1564
                case 'X':
slouken@7728
  1565
                    if (info.force_case == SDL_CASE_NOCHANGE) {
slouken@7728
  1566
                        info.force_case = SDL_CASE_UPPER;
slouken@7728
  1567
                    }
slouken@7216
  1568
                    if (info.radix == 10) {
slouken@7216
  1569
                        info.radix = 16;
slouken@1895
  1570
                    }
slouken@1895
  1571
                    if (*fmt == 'p') {
slouken@1895
  1572
                        inttype = DO_LONG;
slouken@1895
  1573
                    }
slouken@1895
  1574
                    /* Fall through to unsigned handling */
slouken@1895
  1575
                case 'o':
slouken@7216
  1576
                    if (info.radix == 10) {
slouken@7216
  1577
                        info.radix = 8;
slouken@1895
  1578
                    }
slouken@1895
  1579
                    /* Fall through to unsigned handling */
slouken@1895
  1580
                case 'u':
slouken@7503
  1581
                    info.pad_zeroes = SDL_TRUE;
slouken@1895
  1582
                    switch (inttype) {
slouken@1895
  1583
                    case DO_INT:
slouken@7216
  1584
                        len = SDL_PrintUnsignedLong(text, left, &info,
slouken@7216
  1585
                                                    (unsigned long)
slouken@7216
  1586
                                                    va_arg(ap, unsigned int));
slouken@1330
  1587
                        break;
slouken@1895
  1588
                    case DO_LONG:
slouken@7216
  1589
                        len = SDL_PrintUnsignedLong(text, left, &info,
slouken@7216
  1590
                                                    va_arg(ap, unsigned long));
slouken@1330
  1591
                        break;
slouken@1895
  1592
                    case DO_LONGLONG:
slouken@7216
  1593
                        len = SDL_PrintUnsignedLongLong(text, left, &info,
slouken@7216
  1594
                                                        va_arg(ap, Uint64));
slouken@1330
  1595
                        break;
slouken@1895
  1596
                    }
slouken@1895
  1597
                    done = SDL_TRUE;
slouken@1895
  1598
                    break;
slouken@1895
  1599
                case 'f':
slouken@7216
  1600
                    len = SDL_PrintFloat(text, left, &info, va_arg(ap, double));
slouken@1895
  1601
                    done = SDL_TRUE;
slouken@1895
  1602
                    break;
slouken@1895
  1603
                case 's':
slouken@7216
  1604
                    len = SDL_PrintString(text, left, &info, va_arg(ap, char *));
slouken@1895
  1605
                    done = SDL_TRUE;
slouken@1895
  1606
                    break;
slouken@1895
  1607
                default:
slouken@1895
  1608
                    done = SDL_TRUE;
slouken@1895
  1609
                    break;
slouken@1330
  1610
                }
slouken@1330
  1611
                ++fmt;
slouken@1330
  1612
            }
slouken@1330
  1613
            text += len;
slouken@7216
  1614
            if (len >= left) {
slouken@7216
  1615
                left = SDL_min(left, 1);
slouken@7216
  1616
            } else {
slouken@7216
  1617
                left -= len;
slouken@7216
  1618
            }
slouken@1330
  1619
        } else {
slouken@7216
  1620
            if (left > 1) {
slouken@7216
  1621
                *text = *fmt;
slouken@7216
  1622
                --left;
slouken@7216
  1623
            }
slouken@7216
  1624
            ++fmt;
slouken@7216
  1625
            ++text;
slouken@1330
  1626
        }
slouken@1330
  1627
    }
slouken@7216
  1628
    if (left > 0) {
slouken@7216
  1629
        *text = '\0';
slouken@7216
  1630
    }
slouken@3253
  1631
    return (int)(text - textstart);
slouken@1330
  1632
}
slouken@7351
  1633
#endif /* HAVE_VSNPRINTF */
icculus@7003
  1634
slouken@1895
  1635
/* vi: set ts=4 sw=4 expandtab: */