src/stdlib/SDL_string.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 08 Sep 2014 01:36:22 -0400
changeset 9126 08f3b56969b1
parent 8888 75c7095a5e8e
child 9306 817656bd36ec
permissions -rw-r--r--
Deal with various .bmp file format variants in SDL_LoadBMP_RW().

This helps when modern versions of The Gimp (and lots of other things)
produces a 32-bit bitmap with an alpha channel, or anything with "BI_BITFIELDS"
format, since that data is now embedded in the bitmap info header instead of
directly following it and we would accidentally skip over embedded versions of
it.

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