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