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