src/stdlib/SDL_string.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Aug 2017 01:42:18 -0700
changeset 11386 d5c2d689bf6d
parent 11265 9152aa462a82
child 11451 b96f291a772e
permissions -rw-r--r--
Fixed build when Wayland is dynamically loaded
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
*/
slouken@10604
    21
#if defined(__clang_analyzer__)
icculus@9306
    22
#define SDL_DISABLE_ANALYZE_MACROS 1
icculus@9306
    23
#endif
icculus@9306
    24
icculus@8093
    25
#include "../SDL_internal.h"
slouken@1330
    26
slouken@1330
    27
/* This file contains portable string manipulation functions for SDL */
slouken@1330
    28
slouken@1354
    29
#include "SDL_stdinc.h"
slouken@1330
    30
slouken@10609
    31
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL)  || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL) || !defined(HAVE_STRTOD)
slouken@1354
    32
#define SDL_isupperhex(X)   (((X) >= 'A') && ((X) <= 'F'))
slouken@1354
    33
#define SDL_islowerhex(X)   (((X) >= 'a') && ((X) <= 'f'))
slouken@10609
    34
#endif
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@11227
    81
    if (valuep && text > textstart) {
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@11227
   117
    if (valuep && text > textstart) {
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@11227
   149
    if (valuep && text > textstart) {
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@11227
   186
    if (valuep && text > textstart) {
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@11227
   222
    if (valuep && text > textstart) {
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@11227
   254
    if (valuep && text > textstart) {
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@11265
   464
int
slouken@11265
   465
SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
slouken@11265
   466
{
slouken@11265
   467
#if defined(HAVE_WCSCMP)
slouken@11265
   468
    return wcscmp(str1, str2);
slouken@11265
   469
#else
slouken@11265
   470
    while (*str1 && *str2) {
slouken@11265
   471
        if (*str1 != *str2)
slouken@11265
   472
            break;
slouken@11265
   473
        ++str1;
slouken@11265
   474
        ++str2;
slouken@11265
   475
    }
slouken@11265
   476
    return (int)(*str1 - *str2);
slouken@11265
   477
#endif /* HAVE_WCSCMP */
slouken@11265
   478
}
slouken@11265
   479
slouken@1895
   480
size_t
slouken@8820
   481
SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
slouken@1330
   482
{
slouken@7351
   483
#if defined(HAVE_STRLCPY)
slouken@7351
   484
    return strlcpy(dst, src, maxlen);
slouken@7351
   485
#else
slouken@1379
   486
    size_t srclen = SDL_strlen(src);
slouken@1895
   487
    if (maxlen > 0) {
slouken@1895
   488
        size_t len = SDL_min(srclen, maxlen - 1);
slouken@1379
   489
        SDL_memcpy(dst, src, len);
slouken@1379
   490
        dst[len] = '\0';
slouken@1330
   491
    }
slouken@1379
   492
    return srclen;
slouken@7351
   493
#endif /* HAVE_STRLCPY */
slouken@1330
   494
}
slouken@1330
   495
icculus@11052
   496
size_t
icculus@11052
   497
SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
dewyatt@4754
   498
{
dewyatt@4754
   499
    size_t src_bytes = SDL_strlen(src);
dewyatt@4754
   500
    size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
slouken@5076
   501
    size_t i = 0;
dewyatt@4754
   502
    char trailing_bytes = 0;
dewyatt@4754
   503
    if (bytes)
dewyatt@4754
   504
    {
dewyatt@4754
   505
        unsigned char c = (unsigned char)src[bytes - 1];
dewyatt@4754
   506
        if (UTF8_IsLeadByte(c))
dewyatt@4754
   507
            --bytes;
dewyatt@4754
   508
        else if (UTF8_IsTrailingByte(c))
dewyatt@4754
   509
        {
dewyatt@4754
   510
            for (i = bytes - 1; i != 0; --i)
dewyatt@4754
   511
            {
dewyatt@4754
   512
                c = (unsigned char)src[i];
dewyatt@4754
   513
                trailing_bytes = UTF8_TrailingBytes(c);
dewyatt@4754
   514
                if (trailing_bytes)
dewyatt@4754
   515
                {
dewyatt@4754
   516
                    if (bytes - i != trailing_bytes + 1)
dewyatt@4754
   517
                        bytes = i;
dewyatt@4754
   518
dewyatt@4754
   519
                    break;
dewyatt@4754
   520
                }
dewyatt@4754
   521
            }
dewyatt@4754
   522
        }
dewyatt@4754
   523
        SDL_memcpy(dst, src, bytes);
dewyatt@4754
   524
    }
dewyatt@4754
   525
    dst[bytes] = '\0';
dewyatt@4754
   526
    return bytes;
dewyatt@4754
   527
}
dewyatt@4754
   528
slouken@1895
   529
size_t
icculus@11049
   530
SDL_utf8strlen(const char *str)
icculus@11049
   531
{
icculus@11049
   532
    size_t retval = 0;
icculus@11049
   533
    const char *p = str;
icculus@11049
   534
    char ch;
icculus@11049
   535
icculus@11049
   536
    while ((ch = *(p++))) {
icculus@11049
   537
        /* if top two bits are 1 and 0, it's a continuation byte. */
icculus@11049
   538
        if ((ch & 0xc0) != 0x80) {
icculus@11049
   539
            retval++;
icculus@11049
   540
        }
icculus@11049
   541
    }
icculus@11049
   542
    
icculus@11049
   543
    return retval;
icculus@11049
   544
}
icculus@11049
   545
icculus@11049
   546
size_t
slouken@8820
   547
SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
slouken@1330
   548
{
slouken@7351
   549
#if defined(HAVE_STRLCAT)
slouken@7351
   550
    return strlcat(dst, src, maxlen);
slouken@7351
   551
#else
slouken@1379
   552
    size_t dstlen = SDL_strlen(dst);
slouken@1379
   553
    size_t srclen = SDL_strlen(src);
slouken@1895
   554
    if (dstlen < maxlen) {
slouken@1895
   555
        SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
slouken@1330
   556
    }
slouken@1895
   557
    return dstlen + srclen;
slouken@7351
   558
#endif /* HAVE_STRLCAT */
slouken@1330
   559
}
slouken@1330
   560
slouken@1895
   561
char *
slouken@1895
   562
SDL_strdup(const char *string)
slouken@1341
   563
{
slouken@7351
   564
#if defined(HAVE_STRDUP)
slouken@7352
   565
    return strdup(string);
slouken@7351
   566
#else
slouken@1895
   567
    size_t len = SDL_strlen(string) + 1;
slouken@1379
   568
    char *newstr = SDL_malloc(len);
slouken@1895
   569
    if (newstr) {
slouken@1379
   570
        SDL_strlcpy(newstr, string, len);
slouken@1341
   571
    }
slouken@1341
   572
    return newstr;
slouken@7351
   573
#endif /* HAVE_STRDUP */
slouken@1341
   574
}
slouken@1341
   575
slouken@1895
   576
char *
slouken@1895
   577
SDL_strrev(char *string)
slouken@1330
   578
{
slouken@7351
   579
#if defined(HAVE__STRREV)
slouken@7353
   580
    return _strrev(string);
slouken@7351
   581
#else
slouken@1330
   582
    size_t len = SDL_strlen(string);
slouken@1330
   583
    char *a = &string[0];
slouken@1895
   584
    char *b = &string[len - 1];
slouken@1330
   585
    len /= 2;
slouken@1895
   586
    while (len--) {
slouken@1330
   587
        char c = *a;
slouken@1330
   588
        *a++ = *b;
slouken@1330
   589
        *b-- = c;
slouken@1330
   590
    }
slouken@1330
   591
    return string;
slouken@7351
   592
#endif /* HAVE__STRREV */
slouken@1330
   593
}
slouken@1330
   594
slouken@1895
   595
char *
slouken@1895
   596
SDL_strupr(char *string)
slouken@1330
   597
{
slouken@7351
   598
#if defined(HAVE__STRUPR)
slouken@7353
   599
    return _strupr(string);
slouken@7351
   600
#else
slouken@1330
   601
    char *bufp = string;
slouken@1895
   602
    while (*bufp) {
icculus@2092
   603
        *bufp = SDL_toupper((unsigned char) *bufp);
slouken@1895
   604
        ++bufp;
slouken@1330
   605
    }
slouken@1330
   606
    return string;
slouken@7351
   607
#endif /* HAVE__STRUPR */
slouken@1330
   608
}
slouken@1330
   609
slouken@1895
   610
char *
slouken@1895
   611
SDL_strlwr(char *string)
slouken@1330
   612
{
slouken@7351
   613
#if defined(HAVE__STRLWR)
slouken@7353
   614
    return _strlwr(string);
slouken@7351
   615
#else
slouken@1330
   616
    char *bufp = string;
slouken@1895
   617
    while (*bufp) {
icculus@2092
   618
        *bufp = SDL_tolower((unsigned char) *bufp);
slouken@1895
   619
        ++bufp;
slouken@1330
   620
    }
slouken@1330
   621
    return string;
slouken@7351
   622
#endif /* HAVE__STRLWR */
slouken@1330
   623
}
slouken@1330
   624
slouken@1895
   625
char *
slouken@1895
   626
SDL_strchr(const char *string, int c)
slouken@1330
   627
{
slouken@7351
   628
#ifdef HAVE_STRCHR
slouken@7352
   629
    return SDL_const_cast(char*,strchr(string, c));
slouken@7351
   630
#elif defined(HAVE_INDEX)
slouken@7352
   631
    return SDL_const_cast(char*,index(string, c));
slouken@7351
   632
#else
slouken@1895
   633
    while (*string) {
slouken@1895
   634
        if (*string == c) {
slouken@1895
   635
            return (char *) string;
slouken@1330
   636
        }
slouken@1895
   637
        ++string;
slouken@1330
   638
    }
slouken@1330
   639
    return NULL;
slouken@7351
   640
#endif /* HAVE_STRCHR */
slouken@1330
   641
}
slouken@1330
   642
slouken@1895
   643
char *
slouken@1895
   644
SDL_strrchr(const char *string, int c)
slouken@1330
   645
{
slouken@7351
   646
#ifdef HAVE_STRRCHR
slouken@7352
   647
    return SDL_const_cast(char*,strrchr(string, c));
slouken@7351
   648
#elif defined(HAVE_RINDEX)
slouken@7352
   649
    return SDL_const_cast(char*,rindex(string, c));
slouken@7351
   650
#else
slouken@1336
   651
    const char *bufp = string + SDL_strlen(string) - 1;
slouken@1895
   652
    while (bufp >= string) {
slouken@1895
   653
        if (*bufp == c) {
slouken@1895
   654
            return (char *) bufp;
slouken@1330
   655
        }
slouken@1895
   656
        --bufp;
slouken@1330
   657
    }
slouken@1330
   658
    return NULL;
slouken@7351
   659
#endif /* HAVE_STRRCHR */
slouken@1330
   660
}
slouken@1330
   661
slouken@1895
   662
char *
slouken@1895
   663
SDL_strstr(const char *haystack, const char *needle)
slouken@1330
   664
{
slouken@7351
   665
#if defined(HAVE_STRSTR)
slouken@7351
   666
    return SDL_const_cast(char*,strstr(haystack, needle));
slouken@7351
   667
#else
slouken@1336
   668
    size_t length = SDL_strlen(needle);
slouken@1895
   669
    while (*haystack) {
slouken@1895
   670
        if (SDL_strncmp(haystack, needle, length) == 0) {
slouken@1895
   671
            return (char *) haystack;
slouken@1330
   672
        }
slouken@1895
   673
        ++haystack;
slouken@1330
   674
    }
slouken@1330
   675
    return NULL;
slouken@7351
   676
#endif /* HAVE_STRSTR */
slouken@1330
   677
}
slouken@1330
   678
slouken@7353
   679
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
slouken@7353
   680
    !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
slouken@1330
   681
static const char ntoa_table[] = {
slouken@1330
   682
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
slouken@1330
   683
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
slouken@1330
   684
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
slouken@1330
   685
    'U', 'V', 'W', 'X', 'Y', 'Z'
slouken@1330
   686
};
slouken@1330
   687
#endif /* ntoa() conversion table */
slouken@1330
   688
slouken@7351
   689
char *
slouken@7353
   690
SDL_itoa(int value, char *string, int radix)
slouken@7351
   691
{
slouken@7351
   692
#ifdef HAVE_ITOA
slouken@7353
   693
    return itoa(value, string, radix);
icculus@7003
   694
#else
slouken@7353
   695
    return SDL_ltoa((long)value, string, radix);
slouken@7351
   696
#endif /* HAVE_ITOA */
slouken@7351
   697
}
slouken@7351
   698
slouken@7351
   699
char *
slouken@7353
   700
SDL_uitoa(unsigned int value, char *string, int radix)
slouken@7351
   701
{
slouken@7351
   702
#ifdef HAVE__UITOA
slouken@7353
   703
    return _uitoa(value, string, radix);
slouken@7351
   704
#else
slouken@7353
   705
    return SDL_ultoa((unsigned long)value, string, radix);
slouken@7351
   706
#endif /* HAVE__UITOA */
slouken@7351
   707
}
slouken@7351
   708
slouken@1895
   709
char *
slouken@1895
   710
SDL_ltoa(long value, char *string, int radix)
slouken@1330
   711
{
slouken@7351
   712
#if defined(HAVE__LTOA)
slouken@7353
   713
    return _ltoa(value, string, radix);
slouken@7351
   714
#else
slouken@7351
   715
    char *bufp = string;
slouken@7351
   716
slouken@7351
   717
    if (value < 0) {
slouken@7351
   718
        *bufp++ = '-';
slouken@8059
   719
        SDL_ultoa(-value, bufp, radix);
slouken@7351
   720
    } else {
slouken@8059
   721
        SDL_ultoa(value, bufp, radix);
slouken@7351
   722
    }
slouken@7351
   723
slouken@7351
   724
    return string;
slouken@7351
   725
#endif /* HAVE__LTOA */
slouken@7351
   726
}
slouken@7351
   727
slouken@7351
   728
char *
slouken@7351
   729
SDL_ultoa(unsigned long value, char *string, int radix)
slouken@7351
   730
{
slouken@7351
   731
#if defined(HAVE__ULTOA)
slouken@7353
   732
    return _ultoa(value, string, radix);
slouken@7351
   733
#else
slouken@7351
   734
    char *bufp = string;
slouken@7351
   735
slouken@7351
   736
    if (value) {
slouken@7351
   737
        while (value > 0) {
slouken@7351
   738
            *bufp++ = ntoa_table[value % radix];
slouken@7351
   739
            value /= radix;
slouken@7351
   740
        }
slouken@7351
   741
    } else {
slouken@7351
   742
        *bufp++ = '0';
slouken@7351
   743
    }
slouken@7351
   744
    *bufp = '\0';
slouken@7351
   745
slouken@7351
   746
    /* The numbers went into the string backwards. :) */
slouken@7351
   747
    SDL_strrev(string);
slouken@7351
   748
slouken@7351
   749
    return string;
slouken@7351
   750
#endif /* HAVE__ULTOA */
slouken@7351
   751
}
slouken@7351
   752
slouken@7351
   753
char *
slouken@7351
   754
SDL_lltoa(Sint64 value, char *string, int radix)
slouken@7351
   755
{
slouken@7351
   756
#if defined(HAVE__I64TOA)
slouken@7353
   757
    return _i64toa(value, string, radix);
slouken@7351
   758
#else
slouken@1330
   759
    char *bufp = string;
slouken@1330
   760
slouken@1895
   761
    if (value < 0) {
slouken@1330
   762
        *bufp++ = '-';
slouken@8059
   763
        SDL_ulltoa(-value, bufp, radix);
slouken@1330
   764
    } else {
slouken@8059
   765
        SDL_ulltoa(value, bufp, radix);
slouken@1330
   766
    }
slouken@1330
   767
slouken@1330
   768
    return string;
slouken@7351
   769
#endif /* HAVE__I64TOA */
slouken@1330
   770
}
slouken@1330
   771
slouken@1895
   772
char *
slouken@1895
   773
SDL_ulltoa(Uint64 value, char *string, int radix)
slouken@1330
   774
{
slouken@7351
   775
#if defined(HAVE__UI64TOA)
slouken@7353
   776
    return _ui64toa(value, string, radix);
slouken@7351
   777
#else
slouken@1330
   778
    char *bufp = string;
slouken@1330
   779
slouken@1895
   780
    if (value) {
slouken@1895
   781
        while (value > 0) {
slouken@1330
   782
            *bufp++ = ntoa_table[value % radix];
slouken@1330
   783
            value /= radix;
slouken@1330
   784
        }
slouken@1330
   785
    } else {
slouken@1330
   786
        *bufp++ = '0';
slouken@1330
   787
    }
slouken@1330
   788
    *bufp = '\0';
slouken@1330
   789
slouken@1330
   790
    /* The numbers went into the string backwards. :) */
slouken@1336
   791
    SDL_strrev(string);
slouken@1330
   792
slouken@1330
   793
    return string;
slouken@7351
   794
#endif /* HAVE__UI64TOA */
slouken@1330
   795
}
slouken@7351
   796
slouken@7353
   797
int SDL_atoi(const char *string)
slouken@7351
   798
{
slouken@7351
   799
#ifdef HAVE_ATOI
slouken@7353
   800
    return atoi(string);
slouken@7351
   801
#else
slouken@7353
   802
    return SDL_strtol(string, NULL, 0);
slouken@7351
   803
#endif /* HAVE_ATOI */
slouken@7351
   804
}
slouken@7351
   805
slouken@7353
   806
double SDL_atof(const char *string)
slouken@7351
   807
{
slouken@7351
   808
#ifdef HAVE_ATOF
slouken@7353
   809
    return (double) atof(string);
slouken@7351
   810
#else
slouken@7353
   811
    return SDL_strtod(string, NULL);
slouken@7351
   812
#endif /* HAVE_ATOF */
slouken@7351
   813
}
slouken@7351
   814
slouken@7351
   815
long
slouken@7351
   816
SDL_strtol(const char *string, char **endp, int base)
slouken@7351
   817
{
slouken@7351
   818
#if defined(HAVE_STRTOL)
slouken@7352
   819
    return strtol(string, endp, base);
slouken@7351
   820
#else
slouken@7351
   821
    size_t len;
slouken@11235
   822
    long value = 0;
slouken@1330
   823
slouken@7351
   824
    if (!base) {
slouken@7351
   825
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@7351
   826
            base = 16;
slouken@7351
   827
        } else {
slouken@7351
   828
            base = 10;
slouken@7351
   829
        }
slouken@7351
   830
    }
slouken@7351
   831
slouken@7351
   832
    len = SDL_ScanLong(string, base, &value);
slouken@7351
   833
    if (endp) {
slouken@7351
   834
        *endp = (char *) string + len;
slouken@7351
   835
    }
slouken@7351
   836
    return value;
slouken@7351
   837
#endif /* HAVE_STRTOL */
slouken@7351
   838
}
slouken@7351
   839
slouken@7351
   840
unsigned long
slouken@7351
   841
SDL_strtoul(const char *string, char **endp, int base)
slouken@7351
   842
{
slouken@7351
   843
#if defined(HAVE_STRTOUL)
slouken@7352
   844
    return strtoul(string, endp, base);
icculus@7003
   845
#else
slouken@7351
   846
    size_t len;
slouken@11235
   847
    unsigned long value = 0;
slouken@7351
   848
slouken@7351
   849
    if (!base) {
slouken@7351
   850
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@7351
   851
            base = 16;
slouken@7351
   852
        } else {
slouken@7351
   853
            base = 10;
slouken@7351
   854
        }
slouken@7351
   855
    }
slouken@7351
   856
slouken@7351
   857
    len = SDL_ScanUnsignedLong(string, base, &value);
slouken@7351
   858
    if (endp) {
slouken@7351
   859
        *endp = (char *) string + len;
slouken@7351
   860
    }
slouken@7351
   861
    return value;
slouken@7351
   862
#endif /* HAVE_STRTOUL */
slouken@7351
   863
}
slouken@7351
   864
slouken@1895
   865
Sint64
slouken@1895
   866
SDL_strtoll(const char *string, char **endp, int base)
slouken@1330
   867
{
slouken@7351
   868
#if defined(HAVE_STRTOLL)
slouken@7352
   869
    return strtoll(string, endp, base);
slouken@7351
   870
#else
slouken@1330
   871
    size_t len;
slouken@11235
   872
    Sint64 value = 0;
slouken@1330
   873
slouken@1895
   874
    if (!base) {
slouken@1895
   875
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   876
            base = 16;
slouken@1867
   877
        } else {
slouken@1867
   878
            base = 10;
slouken@1867
   879
        }
slouken@1867
   880
    }
slouken@1867
   881
slouken@1867
   882
    len = SDL_ScanLongLong(string, base, &value);
slouken@1895
   883
    if (endp) {
slouken@1895
   884
        *endp = (char *) string + len;
slouken@1330
   885
    }
slouken@1330
   886
    return value;
slouken@7351
   887
#endif /* HAVE_STRTOLL */
slouken@1330
   888
}
slouken@1330
   889
slouken@1895
   890
Uint64
slouken@1895
   891
SDL_strtoull(const char *string, char **endp, int base)
slouken@1456
   892
{
slouken@7351
   893
#if defined(HAVE_STRTOULL)
slouken@7352
   894
    return strtoull(string, endp, base);
slouken@7351
   895
#else
slouken@1456
   896
    size_t len;
slouken@11235
   897
    Uint64 value = 0;
slouken@1456
   898
slouken@1895
   899
    if (!base) {
slouken@1895
   900
        if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
slouken@1867
   901
            base = 16;
slouken@1867
   902
        } else {
slouken@1867
   903
            base = 10;
slouken@1867
   904
        }
slouken@1867
   905
    }
slouken@1867
   906
slouken@1867
   907
    len = SDL_ScanUnsignedLongLong(string, base, &value);
slouken@1895
   908
    if (endp) {
slouken@1895
   909
        *endp = (char *) string + len;
slouken@1456
   910
    }
slouken@1456
   911
    return value;
slouken@7351
   912
#endif /* HAVE_STRTOULL */
slouken@1456
   913
}
slouken@1456
   914
slouken@1895
   915
double
slouken@1895
   916
SDL_strtod(const char *string, char **endp)
slouken@1341
   917
{
slouken@7351
   918
#if defined(HAVE_STRTOD)
slouken@7352
   919
    return strtod(string, endp);
slouken@7351
   920
#else
slouken@1341
   921
    size_t len;
slouken@11235
   922
    double value = 0.0;
slouken@1341
   923
slouken@1341
   924
    len = SDL_ScanFloat(string, &value);
slouken@1895
   925
    if (endp) {
slouken@1895
   926
        *endp = (char *) string + len;
slouken@1341
   927
    }
slouken@1341
   928
    return value;
slouken@7351
   929
#endif /* HAVE_STRTOD */
slouken@1341
   930
}
slouken@1341
   931
slouken@1895
   932
int
slouken@1895
   933
SDL_strcmp(const char *str1, const char *str2)
slouken@1330
   934
{
slouken@7351
   935
#if defined(HAVE_STRCMP)
slouken@7351
   936
    return strcmp(str1, str2);
slouken@7351
   937
#else
slouken@1330
   938
    while (*str1 && *str2) {
slouken@1895
   939
        if (*str1 != *str2)
slouken@1330
   940
            break;
slouken@1330
   941
        ++str1;
slouken@1330
   942
        ++str2;
slouken@1330
   943
    }
slouken@11265
   944
    return (int)((unsigned char) *str1 - (unsigned char) *str2);
slouken@7351
   945
#endif /* HAVE_STRCMP */
slouken@1330
   946
}
slouken@1330
   947
slouken@1895
   948
int
slouken@1895
   949
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
slouken@1330
   950
{
slouken@7351
   951
#if defined(HAVE_STRNCMP)
slouken@7351
   952
    return strncmp(str1, str2, maxlen);
slouken@7351
   953
#else
slouken@1895
   954
    while (*str1 && *str2 && maxlen) {
slouken@1895
   955
        if (*str1 != *str2)
slouken@1330
   956
            break;
slouken@1330
   957
        ++str1;
slouken@1330
   958
        ++str2;
slouken@1330
   959
        --maxlen;
slouken@1330
   960
    }
slouken@1895
   961
    if (!maxlen) {
slouken@1330
   962
        return 0;
slouken@1330
   963
    }
slouken@1895
   964
    return (int) ((unsigned char) *str1 - (unsigned char) *str2);
slouken@7351
   965
#endif /* HAVE_STRNCMP */
slouken@1330
   966
}
slouken@1330
   967
slouken@1895
   968
int
slouken@1895
   969
SDL_strcasecmp(const char *str1, const char *str2)
slouken@1330
   970
{
slouken@7351
   971
#ifdef HAVE_STRCASECMP
slouken@7351
   972
    return strcasecmp(str1, str2);
slouken@7351
   973
#elif defined(HAVE__STRICMP)
slouken@7351
   974
    return _stricmp(str1, str2);
slouken@7351
   975
#else
slouken@1330
   976
    char a = 0;
slouken@1330
   977
    char b = 0;
slouken@1895
   978
    while (*str1 && *str2) {
slouken@7351
   979
        a = SDL_toupper((unsigned char) *str1);
slouken@7351
   980
        b = SDL_toupper((unsigned char) *str2);
slouken@1895
   981
        if (a != b)
slouken@1330
   982
            break;
slouken@1330
   983
        ++str1;
slouken@1330
   984
        ++str2;
slouken@1330
   985
    }
slouken@7351
   986
    a = SDL_toupper(*str1);
slouken@7351
   987
    b = SDL_toupper(*str2);
slouken@1895
   988
    return (int) ((unsigned char) a - (unsigned char) b);
slouken@7351
   989
#endif /* HAVE_STRCASECMP */
slouken@1330
   990
}
slouken@1330
   991
slouken@1895
   992
int
slouken@1895
   993
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
slouken@1501
   994
{
slouken@7351
   995
#ifdef HAVE_STRNCASECMP
slouken@7352
   996
    return strncasecmp(str1, str2, maxlen);
slouken@7351
   997
#elif defined(HAVE__STRNICMP)
slouken@7352
   998
    return _strnicmp(str1, str2, maxlen);
slouken@7351
   999
#else
slouken@1501
  1000
    char a = 0;
slouken@1501
  1001
    char b = 0;
slouken@1895
  1002
    while (*str1 && *str2 && maxlen) {
icculus@2092
  1003
        a = SDL_tolower((unsigned char) *str1);
icculus@2092
  1004
        b = SDL_tolower((unsigned char) *str2);
slouken@1895
  1005
        if (a != b)
slouken@1501
  1006
            break;
slouken@1501
  1007
        ++str1;
slouken@1501
  1008
        ++str2;
slouken@1501
  1009
        --maxlen;
slouken@1501
  1010
    }
slouken@6766
  1011
    if (maxlen == 0) {
slouken@6766
  1012
        return 0;
slouken@6766
  1013
    } else {
slouken@6766
  1014
        a = SDL_tolower((unsigned char) *str1);
slouken@6766
  1015
        b = SDL_tolower((unsigned char) *str2);
slouken@6766
  1016
        return (int) ((unsigned char) a - (unsigned char) b);
slouken@6766
  1017
    }
slouken@7351
  1018
#endif /* HAVE_STRNCASECMP */
slouken@1501
  1019
}
slouken@1501
  1020
icculus@7003
  1021
int
slouken@8820
  1022
SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
icculus@7003
  1023
{
icculus@7003
  1024
    int rc;
icculus@7003
  1025
    va_list ap;
icculus@7003
  1026
    va_start(ap, fmt);
icculus@8089
  1027
    rc = SDL_vsscanf(text, fmt, ap);
icculus@7003
  1028
    va_end(ap);
icculus@7003
  1029
    return rc;
icculus@7003
  1030
}
icculus@8089
  1031
icculus@8089
  1032
#ifdef HAVE_VSSCANF
icculus@8089
  1033
int
icculus@8089
  1034
SDL_vsscanf(const char *text, const char *fmt, va_list ap)
icculus@8089
  1035
{
icculus@8089
  1036
    return vsscanf(text, fmt, ap);
icculus@8089
  1037
}
icculus@7003
  1038
#else
slouken@1895
  1039
int
icculus@8092
  1040
SDL_vsscanf(const char *text, const char *fmt, va_list ap)
slouken@1330
  1041
{
slouken@1330
  1042
    int retval = 0;
slouken@1330
  1043
slouken@11227
  1044
    if (!text || !*text) {
slouken@11227
  1045
        return -1;
slouken@11227
  1046
    }
slouken@11227
  1047
slouken@1895
  1048
    while (*fmt) {
slouken@1895
  1049
        if (*fmt == ' ') {
icculus@2092
  1050
            while (SDL_isspace((unsigned char) *text)) {
slouken@1330
  1051
                ++text;
slouken@1330
  1052
            }
slouken@1330
  1053
            ++fmt;
slouken@1330
  1054
            continue;
slouken@1330
  1055
        }
slouken@1895
  1056
        if (*fmt == '%') {
slouken@1330
  1057
            SDL_bool done = SDL_FALSE;
slouken@1330
  1058
            long count = 0;
slouken@1330
  1059
            int radix = 10;
slouken@1895
  1060
            enum
slouken@1895
  1061
            {
slouken@1330
  1062
                DO_SHORT,
slouken@1330
  1063
                DO_INT,
slouken@1330
  1064
                DO_LONG,
slouken@1330
  1065
                DO_LONGLONG
slouken@1330
  1066
            } inttype = DO_INT;
slouken@11227
  1067
            size_t advance;
slouken@1330
  1068
            SDL_bool suppress = SDL_FALSE;
slouken@1330
  1069
slouken@1330
  1070
            ++fmt;
slouken@1895
  1071
            if (*fmt == '%') {
slouken@1895
  1072
                if (*text == '%') {
slouken@1330
  1073
                    ++text;
slouken@1330
  1074
                    ++fmt;
slouken@1330
  1075
                    continue;
slouken@1330
  1076
                }
slouken@1330
  1077
                break;
slouken@1330
  1078
            }
slouken@1895
  1079
            if (*fmt == '*') {
slouken@1330
  1080
                suppress = SDL_TRUE;
slouken@1330
  1081
                ++fmt;
slouken@1330
  1082
            }
slouken@1330
  1083
            fmt += SDL_ScanLong(fmt, 10, &count);
slouken@1330
  1084
slouken@1895
  1085
            if (*fmt == 'c') {
slouken@1895
  1086
                if (!count) {
slouken@1330
  1087
                    count = 1;
slouken@1330
  1088
                }
slouken@1895
  1089
                if (suppress) {
slouken@1895
  1090
                    while (count--) {
slouken@1330
  1091
                        ++text;
slouken@1330
  1092
                    }
slouken@1330
  1093
                } else {
slouken@1895
  1094
                    char *valuep = va_arg(ap, char *);
slouken@1895
  1095
                    while (count--) {
slouken@1330
  1096
                        *valuep++ = *text++;
slouken@1330
  1097
                    }
slouken@1330
  1098
                    ++retval;
slouken@1330
  1099
                }
slouken@1330
  1100
                continue;
slouken@1330
  1101
            }
slouken@1330
  1102
icculus@2092
  1103
            while (SDL_isspace((unsigned char) *text)) {
slouken@1330
  1104
                ++text;
slouken@1330
  1105
            }
slouken@1330
  1106
slouken@1330
  1107
            /* FIXME: implement more of the format specifiers */
slouken@1330
  1108
            while (!done) {
slouken@1895
  1109
                switch (*fmt) {
slouken@1895
  1110
                case '*':
slouken@1895
  1111
                    suppress = SDL_TRUE;
slouken@1895
  1112
                    break;
slouken@1895
  1113
                case 'h':
slouken@1895
  1114
                    if (inttype > DO_SHORT) {
slouken@1895
  1115
                        ++inttype;
slouken@1895
  1116
                    }
slouken@1895
  1117
                    break;
slouken@1895
  1118
                case 'l':
slouken@1895
  1119
                    if (inttype < DO_LONGLONG) {
slouken@1895
  1120
                        ++inttype;
slouken@1895
  1121
                    }
slouken@1895
  1122
                    break;
slouken@1895
  1123
                case 'I':
slouken@1895
  1124
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
  1125
                        fmt += 2;
slouken@1895
  1126
                        inttype = DO_LONGLONG;
slouken@1895
  1127
                    }
slouken@1895
  1128
                    break;
slouken@1895
  1129
                case 'i':
slouken@1895
  1130
                    {
slouken@1895
  1131
                        int index = 0;
slouken@1895
  1132
                        if (text[index] == '-') {
slouken@1895
  1133
                            ++index;
slouken@1895
  1134
                        }
slouken@1895
  1135
                        if (text[index] == '0') {
slouken@7216
  1136
                            if (SDL_tolower((unsigned char) text[index + 1]) == 'x') {
slouken@1895
  1137
                                radix = 16;
slouken@1895
  1138
                            } else {
slouken@1895
  1139
                                radix = 8;
slouken@1895
  1140
                            }
slouken@1330
  1141
                        }
slouken@1895
  1142
                    }
slouken@1895
  1143
                    /* Fall through to %d handling */
slouken@1895
  1144
                case 'd':
slouken@1895
  1145
                    if (inttype == DO_LONGLONG) {
slouken@1895
  1146
                        Sint64 value;
slouken@11235
  1147
                        advance = SDL_ScanLongLong(text, radix, &value);
slouken@11235
  1148
                        text += advance;
slouken@11235
  1149
                        if (advance && !suppress) {
slouken@1895
  1150
                            Sint64 *valuep = va_arg(ap, Sint64 *);
slouken@1895
  1151
                            *valuep = value;
slouken@1895
  1152
                            ++retval;
slouken@1895
  1153
                        }
slouken@5512
  1154
                    } else {
slouken@1895
  1155
                        long value;
slouken@11227
  1156
                        advance = SDL_ScanLong(text, radix, &value);
slouken@11227
  1157
                        text += advance;
slouken@11227
  1158
                        if (advance && !suppress) {
slouken@1895
  1159
                            switch (inttype) {
slouken@1895
  1160
                            case DO_SHORT:
slouken@1895
  1161
                                {
slouken@1895
  1162
                                    short *valuep = va_arg(ap, short *);
slouken@1895
  1163
                                    *valuep = (short) value;
slouken@1895
  1164
                                }
slouken@1895
  1165
                                break;
slouken@1895
  1166
                            case DO_INT:
slouken@1895
  1167
                                {
slouken@1895
  1168
                                    int *valuep = va_arg(ap, int *);
slouken@1895
  1169
                                    *valuep = (int) value;
slouken@1895
  1170
                                }
slouken@1895
  1171
                                break;
slouken@1895
  1172
                            case DO_LONG:
slouken@1895
  1173
                                {
slouken@1895
  1174
                                    long *valuep = va_arg(ap, long *);
slouken@1895
  1175
                                    *valuep = value;
slouken@1895
  1176
                                }
slouken@1895
  1177
                                break;
slouken@1895
  1178
                            case DO_LONGLONG:
slouken@1895
  1179
                                /* Handled above */
slouken@1895
  1180
                                break;
slouken@1895
  1181
                            }
slouken@1895
  1182
                            ++retval;
slouken@1330
  1183
                        }
slouken@1895
  1184
                    }
slouken@1895
  1185
                    done = SDL_TRUE;
slouken@1895
  1186
                    break;
slouken@1895
  1187
                case 'o':
slouken@1895
  1188
                    if (radix == 10) {
slouken@1895
  1189
                        radix = 8;
slouken@1895
  1190
                    }
slouken@1895
  1191
                    /* Fall through to unsigned handling */
slouken@1895
  1192
                case 'x':
slouken@1895
  1193
                case 'X':
slouken@1895
  1194
                    if (radix == 10) {
slouken@1895
  1195
                        radix = 16;
slouken@1895
  1196
                    }
slouken@1895
  1197
                    /* Fall through to unsigned handling */
slouken@1895
  1198
                case 'u':
slouken@1895
  1199
                    if (inttype == DO_LONGLONG) {
slouken@1895
  1200
                        Uint64 value;
slouken@11227
  1201
                        advance = SDL_ScanUnsignedLongLong(text, radix, &value);
slouken@11227
  1202
                        text += advance;
slouken@11227
  1203
                        if (advance && !suppress) {
slouken@1895
  1204
                            Uint64 *valuep = va_arg(ap, Uint64 *);
slouken@1895
  1205
                            *valuep = value;
slouken@1895
  1206
                            ++retval;
slouken@1330
  1207
                        }
slouken@5512
  1208
                    } else {
slouken@1895
  1209
                        unsigned long value;
slouken@11227
  1210
                        advance = SDL_ScanUnsignedLong(text, radix, &value);
slouken@11227
  1211
                        text += advance;
slouken@11227
  1212
                        if (advance && !suppress) {
slouken@1895
  1213
                            switch (inttype) {
slouken@1895
  1214
                            case DO_SHORT:
slouken@1895
  1215
                                {
slouken@1895
  1216
                                    short *valuep = va_arg(ap, short *);
slouken@1895
  1217
                                    *valuep = (short) value;
slouken@1895
  1218
                                }
slouken@1895
  1219
                                break;
slouken@1895
  1220
                            case DO_INT:
slouken@1895
  1221
                                {
slouken@1895
  1222
                                    int *valuep = va_arg(ap, int *);
slouken@1895
  1223
                                    *valuep = (int) value;
slouken@1895
  1224
                                }
slouken@1895
  1225
                                break;
slouken@1895
  1226
                            case DO_LONG:
slouken@1895
  1227
                                {
slouken@1895
  1228
                                    long *valuep = va_arg(ap, long *);
slouken@1895
  1229
                                    *valuep = value;
slouken@1895
  1230
                                }
slouken@1895
  1231
                                break;
slouken@1895
  1232
                            case DO_LONGLONG:
slouken@1895
  1233
                                /* Handled above */
slouken@1895
  1234
                                break;
slouken@1330
  1235
                            }
slouken@1895
  1236
                            ++retval;
slouken@1895
  1237
                        }
slouken@1895
  1238
                    }
slouken@1895
  1239
                    done = SDL_TRUE;
slouken@1895
  1240
                    break;
slouken@1895
  1241
                case 'p':
slouken@1895
  1242
                    {
slouken@1895
  1243
                        uintptr_t value;
slouken@11227
  1244
                        advance = SDL_ScanUintPtrT(text, 16, &value);
slouken@11227
  1245
                        text += advance;
slouken@11227
  1246
                        if (advance && !suppress) {
slouken@1895
  1247
                            void **valuep = va_arg(ap, void **);
slouken@1895
  1248
                            *valuep = (void *) value;
slouken@1895
  1249
                            ++retval;
slouken@1895
  1250
                        }
slouken@1895
  1251
                    }
slouken@1895
  1252
                    done = SDL_TRUE;
slouken@1895
  1253
                    break;
slouken@1895
  1254
                case 'f':
slouken@1895
  1255
                    {
slouken@1895
  1256
                        double value;
slouken@11227
  1257
                        advance = SDL_ScanFloat(text, &value);
slouken@11227
  1258
                        text += advance;
slouken@11227
  1259
                        if (advance && !suppress) {
slouken@1895
  1260
                            float *valuep = va_arg(ap, float *);
slouken@1895
  1261
                            *valuep = (float) value;
slouken@1895
  1262
                            ++retval;
slouken@1895
  1263
                        }
slouken@1895
  1264
                    }
slouken@1895
  1265
                    done = SDL_TRUE;
slouken@1895
  1266
                    break;
slouken@1895
  1267
                case 's':
slouken@1895
  1268
                    if (suppress) {
icculus@2092
  1269
                        while (!SDL_isspace((unsigned char) *text)) {
slouken@1895
  1270
                            ++text;
slouken@1895
  1271
                            if (count) {
slouken@1895
  1272
                                if (--count == 0) {
slouken@1895
  1273
                                    break;
slouken@1330
  1274
                                }
slouken@1330
  1275
                            }
slouken@1330
  1276
                        }
slouken@1895
  1277
                    } else {
slouken@1895
  1278
                        char *valuep = va_arg(ap, char *);
icculus@2092
  1279
                        while (!SDL_isspace((unsigned char) *text)) {
slouken@1895
  1280
                            *valuep++ = *text++;
slouken@1895
  1281
                            if (count) {
slouken@1895
  1282
                                if (--count == 0) {
slouken@1895
  1283
                                    break;
slouken@1330
  1284
                                }
slouken@1330
  1285
                            }
slouken@1330
  1286
                        }
slouken@1895
  1287
                        *valuep = '\0';
slouken@1895
  1288
                        ++retval;
slouken@1895
  1289
                    }
slouken@1895
  1290
                    done = SDL_TRUE;
slouken@1895
  1291
                    break;
slouken@1895
  1292
                default:
slouken@1895
  1293
                    done = SDL_TRUE;
slouken@1895
  1294
                    break;
slouken@1330
  1295
                }
slouken@1330
  1296
                ++fmt;
slouken@1330
  1297
            }
slouken@1330
  1298
            continue;
slouken@1330
  1299
        }
slouken@1895
  1300
        if (*text == *fmt) {
slouken@1330
  1301
            ++text;
slouken@1330
  1302
            ++fmt;
slouken@1330
  1303
            continue;
slouken@1330
  1304
        }
slouken@1330
  1305
        /* Text didn't match format specifier */
slouken@1330
  1306
        break;
slouken@1330
  1307
    }
slouken@1330
  1308
slouken@1330
  1309
    return retval;
slouken@1330
  1310
}
icculus@8089
  1311
#endif /* HAVE_VSSCANF */
slouken@1330
  1312
slouken@1895
  1313
int
slouken@8820
  1314
SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
slouken@1330
  1315
{
slouken@1330
  1316
    va_list ap;
slouken@1330
  1317
    int retval;
slouken@1330
  1318
slouken@1330
  1319
    va_start(ap, fmt);
slouken@1330
  1320
    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
slouken@1330
  1321
    va_end(ap);
slouken@1330
  1322
slouken@1330
  1323
    return retval;
slouken@1330
  1324
}
slouken@1330
  1325
slouken@7351
  1326
#ifdef HAVE_VSNPRINTF
slouken@8820
  1327
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@7351
  1328
{
slouken@8888
  1329
    if (!fmt) {
slouken@8888
  1330
        fmt = "";
slouken@8888
  1331
    }
slouken@7351
  1332
    return vsnprintf(text, maxlen, fmt, ap);
slouken@7351
  1333
}
icculus@7003
  1334
#else
slouken@7216
  1335
 /* FIXME: implement more of the format specifiers */
slouken@7728
  1336
typedef enum
slouken@7728
  1337
{
slouken@7728
  1338
    SDL_CASE_NOCHANGE,
slouken@7728
  1339
    SDL_CASE_LOWER,
slouken@7728
  1340
    SDL_CASE_UPPER
slouken@7728
  1341
} SDL_letter_case;
slouken@7728
  1342
slouken@7216
  1343
typedef struct
slouken@1330
  1344
{
slouken@7216
  1345
    SDL_bool left_justify;
slouken@7216
  1346
    SDL_bool force_sign;
slouken@7216
  1347
    SDL_bool force_type;
slouken@7216
  1348
    SDL_bool pad_zeroes;
slouken@7728
  1349
    SDL_letter_case force_case;
slouken@7216
  1350
    int width;
slouken@7216
  1351
    int radix;
slouken@7216
  1352
    int precision;
slouken@7216
  1353
} SDL_FormatInfo;
slouken@1330
  1354
slouken@7216
  1355
static size_t
slouken@7216
  1356
SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
slouken@7216
  1357
{
slouken@7503
  1358
    size_t length = 0;
icculus@10460
  1359
    size_t slen;
slouken@7503
  1360
icculus@10887
  1361
    if (string == NULL) {
icculus@10887
  1362
        string = "(null)";
icculus@10887
  1363
    }
icculus@10887
  1364
slouken@7503
  1365
    if (info && info->width && (size_t)info->width > SDL_strlen(string)) {
slouken@7503
  1366
        char fill = info->pad_zeroes ? '0' : ' ';
slouken@7503
  1367
        size_t width = info->width - SDL_strlen(string);
slouken@7503
  1368
        while (width-- > 0 && maxlen > 0) {
slouken@7503
  1369
            *text++ = fill;
slouken@7503
  1370
            ++length;
slouken@7503
  1371
            --maxlen;
slouken@7503
  1372
        }
slouken@7503
  1373
    }
slouken@7503
  1374
icculus@10460
  1375
    slen = SDL_strlcpy(text, string, maxlen);
icculus@10460
  1376
    length += SDL_min(slen, maxlen);
slouken@7503
  1377
slouken@7728
  1378
    if (info) {
slouken@7728
  1379
        if (info->force_case == SDL_CASE_LOWER) {
slouken@7728
  1380
            SDL_strlwr(text);
slouken@7728
  1381
        } else if (info->force_case == SDL_CASE_UPPER) {
slouken@7728
  1382
            SDL_strupr(text);
slouken@7728
  1383
        }
slouken@7503
  1384
    }
slouken@7503
  1385
    return length;
slouken@1330
  1386
}
slouken@1895
  1387
slouken@1895
  1388
static size_t
slouken@7216
  1389
SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
slouken@7216
  1390
{
slouken@7216
  1391
    char num[130];
slouken@7216
  1392
slouken@7216
  1393
    SDL_ltoa(value, num, info ? info->radix : 10);
slouken@7216
  1394
    return SDL_PrintString(text, maxlen, info, num);
slouken@7216
  1395
}
slouken@7216
  1396
slouken@7216
  1397
static size_t
slouken@7216
  1398
SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
slouken@1330
  1399
{
slouken@1330
  1400
    char num[130];
slouken@7216
  1401
slouken@7216
  1402
    SDL_ultoa(value, num, info ? info->radix : 10);
slouken@7216
  1403
    return SDL_PrintString(text, maxlen, info, num);
slouken@7216
  1404
}
slouken@7216
  1405
slouken@7216
  1406
static size_t
slouken@7216
  1407
SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
slouken@7216
  1408
{
slouken@7216
  1409
    char num[130];
slouken@1330
  1410
slouken@7216
  1411
    SDL_lltoa(value, num, info ? info->radix : 10);
slouken@7216
  1412
    return SDL_PrintString(text, maxlen, info, num);
slouken@7216
  1413
}
slouken@1330
  1414
slouken@7216
  1415
static size_t
slouken@7216
  1416
SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
slouken@7216
  1417
{
slouken@7216
  1418
    char num[130];
slouken@7216
  1419
slouken@7216
  1420
    SDL_ulltoa(value, num, info ? info->radix : 10);
slouken@7216
  1421
    return SDL_PrintString(text, maxlen, info, num);
slouken@1330
  1422
}
slouken@1895
  1423
slouken@1895
  1424
static size_t
slouken@7216
  1425
SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
slouken@1330
  1426
{
icculus@7482
  1427
    int width;
slouken@7217
  1428
    size_t len;
slouken@7216
  1429
    size_t left = maxlen;
slouken@7216
  1430
    char *textstart = text;
slouken@1330
  1431
slouken@1895
  1432
    if (arg) {
slouken@1330
  1433
        /* This isn't especially accurate, but hey, it's easy. :) */
slouken@1330
  1434
        unsigned long value;
slouken@1330
  1435
slouken@1895
  1436
        if (arg < 0) {
slouken@7216
  1437
            if (left > 1) {
slouken@7216
  1438
                *text = '-';
slouken@7216
  1439
                --left;
slouken@7216
  1440
            }
slouken@7216
  1441
            ++text;
slouken@1330
  1442
            arg = -arg;
slouken@7216
  1443
        } else if (info->force_sign) {
slouken@7216
  1444
            if (left > 1) {
slouken@7216
  1445
                *text = '+';
slouken@7216
  1446
                --left;
slouken@7216
  1447
            }
slouken@7216
  1448
            ++text;
slouken@1330
  1449
        }
slouken@1895
  1450
        value = (unsigned long) arg;
slouken@7216
  1451
        len = SDL_PrintUnsignedLong(text, left, NULL, value);
slouken@7216
  1452
        if (len >= left) {
icculus@10460
  1453
            text += (left > 1) ? left - 1 : 0;
slouken@7216
  1454
            left = SDL_min(left, 1);
slouken@7216
  1455
        } else {
icculus@10460
  1456
            text += len;
slouken@7216
  1457
            left -= len;
slouken@7216
  1458
        }
slouken@1330
  1459
        arg -= value;
slouken@7216
  1460
        if (info->precision < 0) {
slouken@7216
  1461
            info->precision = 6;
slouken@7216
  1462
        }
slouken@7216
  1463
        if (info->force_type || info->precision > 0) {
slouken@1330
  1464
            int mult = 10;
slouken@7216
  1465
            if (left > 1) {
slouken@7216
  1466
                *text = '.';
slouken@7216
  1467
                --left;
slouken@7216
  1468
            }
slouken@7216
  1469
            ++text;
slouken@7216
  1470
            while (info->precision-- > 0) {
slouken@1895
  1471
                value = (unsigned long) (arg * mult);
slouken@7216
  1472
                len = SDL_PrintUnsignedLong(text, left, NULL, value);
slouken@7216
  1473
                if (len >= left) {
icculus@10460
  1474
                    text += (left > 1) ? left - 1 : 0;
slouken@7216
  1475
                    left = SDL_min(left, 1);
slouken@7216
  1476
                } else {
icculus@10460
  1477
                    text += len;
slouken@7216
  1478
                    left -= len;
slouken@7216
  1479
                }
slouken@1895
  1480
                arg -= (double) value / mult;
slouken@1330
  1481
                mult *= 10;
slouken@1330
  1482
            }
slouken@1330
  1483
        }
slouken@1330
  1484
    } else {
slouken@7216
  1485
        if (left > 1) {
slouken@7216
  1486
            *text = '0';
slouken@7216
  1487
            --left;
slouken@7216
  1488
        }
slouken@7216
  1489
        ++text;
slouken@7216
  1490
        if (info->force_type) {
slouken@7216
  1491
            if (left > 1) {
slouken@7216
  1492
                *text = '.';
slouken@7216
  1493
                --left;
slouken@7216
  1494
            }
slouken@7216
  1495
            ++text;
slouken@7216
  1496
        }
slouken@1330
  1497
    }
slouken@1895
  1498
slouken@7216
  1499
    width = info->width - (int)(text - textstart);
slouken@7216
  1500
    if (width > 0) {
slouken@7216
  1501
        char fill = info->pad_zeroes ? '0' : ' ';
slouken@7216
  1502
        char *end = text+left-1;
slouken@7216
  1503
        len = (text - textstart);
slouken@7216
  1504
        for (len = (text - textstart); len--; ) {
slouken@7216
  1505
            if ((textstart+len+width) < end) {
slouken@7216
  1506
                *(textstart+len+width) = *(textstart+len);
slouken@7216
  1507
            }
slouken@7216
  1508
        }
slouken@7216
  1509
        len = (size_t)width;
slouken@7216
  1510
        if (len >= left) {
icculus@10460
  1511
            text += (left > 1) ? left - 1 : 0;
slouken@7216
  1512
            left = SDL_min(left, 1);
slouken@7216
  1513
        } else {
icculus@10460
  1514
            text += len;
slouken@7216
  1515
            left -= len;
slouken@7216
  1516
        }
slouken@7216
  1517
        while (len--) {
slouken@7216
  1518
            if (textstart+len < end) {
slouken@7216
  1519
                textstart[len] = fill;
slouken@7216
  1520
            }
slouken@7216
  1521
        }
slouken@1330
  1522
    }
slouken@7216
  1523
slouken@1330
  1524
    return (text - textstart);
slouken@1330
  1525
}
slouken@1895
  1526
slouken@1895
  1527
int
slouken@8820
  1528
SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
slouken@1330
  1529
{
slouken@7216
  1530
    size_t left = maxlen;
slouken@1330
  1531
    char *textstart = text;
slouken@7216
  1532
slouken@8839
  1533
    if (!fmt) {
slouken@8839
  1534
        fmt = "";
slouken@8839
  1535
    }
slouken@10733
  1536
    while (*fmt && left > 1) {
slouken@1895
  1537
        if (*fmt == '%') {
slouken@1330
  1538
            SDL_bool done = SDL_FALSE;
slouken@1330
  1539
            size_t len = 0;
slouken@7216
  1540
            SDL_bool check_flag;
slouken@7216
  1541
            SDL_FormatInfo info;
slouken@1895
  1542
            enum
slouken@1895
  1543
            {
slouken@1330
  1544
                DO_INT,
slouken@1330
  1545
                DO_LONG,
slouken@1330
  1546
                DO_LONGLONG
slouken@1330
  1547
            } inttype = DO_INT;
slouken@1330
  1548
slouken@7216
  1549
            SDL_zero(info);
slouken@7216
  1550
            info.radix = 10;
slouken@7216
  1551
            info.precision = -1;
slouken@7216
  1552
slouken@7216
  1553
            check_flag = SDL_TRUE;
slouken@7216
  1554
            while (check_flag) {
slouken@1818
  1555
                ++fmt;
slouken@7216
  1556
                switch (*fmt) {
slouken@7216
  1557
                case '-':
slouken@7216
  1558
                    info.left_justify = SDL_TRUE;
slouken@7216
  1559
                    break;
slouken@7216
  1560
                case '+':
slouken@7216
  1561
                    info.force_sign = SDL_TRUE;
slouken@7216
  1562
                    break;
slouken@7216
  1563
                case '#':
slouken@7216
  1564
                    info.force_type = SDL_TRUE;
slouken@7216
  1565
                    break;
slouken@7216
  1566
                case '0':
slouken@7216
  1567
                    info.pad_zeroes = SDL_TRUE;
slouken@7216
  1568
                    break;
slouken@7216
  1569
                default:
slouken@7216
  1570
                    check_flag = SDL_FALSE;
slouken@7216
  1571
                    break;
slouken@7216
  1572
                }
slouken@1818
  1573
            }
slouken@7216
  1574
slouken@7216
  1575
            if (*fmt >= '0' && *fmt <= '9') {
slouken@7216
  1576
                info.width = SDL_strtol(fmt, (char **)&fmt, 0);
slouken@7216
  1577
            }
slouken@7216
  1578
slouken@7216
  1579
            if (*fmt == '.') {
slouken@7216
  1580
                ++fmt;
slouken@7216
  1581
                if (*fmt >= '0' && *fmt <= '9') {
slouken@7216
  1582
                    info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
slouken@7216
  1583
                } else {
slouken@7216
  1584
                    info.precision = 0;
slouken@7216
  1585
                }
slouken@7216
  1586
            }
slouken@7216
  1587
slouken@1330
  1588
            while (!done) {
slouken@1895
  1589
                switch (*fmt) {
slouken@1895
  1590
                case '%':
slouken@7216
  1591
                    if (left > 1) {
slouken@7216
  1592
                        *text = '%';
slouken@7216
  1593
                    }
slouken@1895
  1594
                    len = 1;
slouken@1895
  1595
                    done = SDL_TRUE;
slouken@1895
  1596
                    break;
slouken@1895
  1597
                case 'c':
slouken@1895
  1598
                    /* char is promoted to int when passed through (...) */
slouken@7216
  1599
                    if (left > 1) {
slouken@7216
  1600
                        *text = (char) va_arg(ap, int);
slouken@7216
  1601
                    }
slouken@1895
  1602
                    len = 1;
slouken@1895
  1603
                    done = SDL_TRUE;
slouken@1895
  1604
                    break;
slouken@1895
  1605
                case 'h':
slouken@1895
  1606
                    /* short is promoted to int when passed through (...) */
slouken@1895
  1607
                    break;
slouken@1895
  1608
                case 'l':
slouken@1895
  1609
                    if (inttype < DO_LONGLONG) {
slouken@1895
  1610
                        ++inttype;
slouken@1895
  1611
                    }
slouken@1895
  1612
                    break;
slouken@1895
  1613
                case 'I':
slouken@1895
  1614
                    if (SDL_strncmp(fmt, "I64", 3) == 0) {
slouken@1895
  1615
                        fmt += 2;
slouken@1895
  1616
                        inttype = DO_LONGLONG;
slouken@1895
  1617
                    }
slouken@1895
  1618
                    break;
slouken@1895
  1619
                case 'i':
slouken@1895
  1620
                case 'd':
slouken@1895
  1621
                    switch (inttype) {
slouken@1895
  1622
                    case DO_INT:
slouken@7216
  1623
                        len = SDL_PrintLong(text, left, &info,
slouken@7216
  1624
                                            (long) va_arg(ap, int));
slouken@1330
  1625
                        break;
slouken@1895
  1626
                    case DO_LONG:
slouken@7216
  1627
                        len = SDL_PrintLong(text, left, &info,
slouken@7216
  1628
                                            va_arg(ap, long));
slouken@1330
  1629
                        break;
slouken@1895
  1630
                    case DO_LONGLONG:
slouken@7216
  1631
                        len = SDL_PrintLongLong(text, left, &info,
slouken@7216
  1632
                                                va_arg(ap, Sint64));
slouken@1330
  1633
                        break;
slouken@1895
  1634
                    }
slouken@1895
  1635
                    done = SDL_TRUE;
slouken@1895
  1636
                    break;
slouken@1895
  1637
                case 'p':
slouken@1895
  1638
                case 'x':
slouken@7728
  1639
                    info.force_case = SDL_CASE_LOWER;
slouken@1895
  1640
                    /* Fall through to 'X' handling */
slouken@1895
  1641
                case 'X':
slouken@7728
  1642
                    if (info.force_case == SDL_CASE_NOCHANGE) {
slouken@7728
  1643
                        info.force_case = SDL_CASE_UPPER;
slouken@7728
  1644
                    }
slouken@7216
  1645
                    if (info.radix == 10) {
slouken@7216
  1646
                        info.radix = 16;
slouken@1895
  1647
                    }
slouken@1895
  1648
                    if (*fmt == 'p') {
slouken@1895
  1649
                        inttype = DO_LONG;
slouken@1895
  1650
                    }
slouken@1895
  1651
                    /* Fall through to unsigned handling */
slouken@1895
  1652
                case 'o':
slouken@7216
  1653
                    if (info.radix == 10) {
slouken@7216
  1654
                        info.radix = 8;
slouken@1895
  1655
                    }
slouken@1895
  1656
                    /* Fall through to unsigned handling */
slouken@1895
  1657
                case 'u':
slouken@7503
  1658
                    info.pad_zeroes = SDL_TRUE;
slouken@1895
  1659
                    switch (inttype) {
slouken@1895
  1660
                    case DO_INT:
slouken@7216
  1661
                        len = SDL_PrintUnsignedLong(text, left, &info,
slouken@7216
  1662
                                                    (unsigned long)
slouken@7216
  1663
                                                    va_arg(ap, unsigned int));
slouken@1895
  1664
                        break;
slouken@1895
  1665
                    case DO_LONG:
slouken@7216
  1666
                        len = SDL_PrintUnsignedLong(text, left, &info,
slouken@7216
  1667
                                                    va_arg(ap, unsigned long));
slouken@1895
  1668
                        break;
slouken@1895
  1669
                    case DO_LONGLONG:
slouken@7216
  1670
                        len = SDL_PrintUnsignedLongLong(text, left, &info,
slouken@7216
  1671
                                                        va_arg(ap, Uint64));
slouken@1330
  1672
                        break;
slouken@1895
  1673
                    }
slouken@1895
  1674
                    done = SDL_TRUE;
slouken@1895
  1675
                    break;
slouken@1895
  1676
                case 'f':
slouken@7216
  1677
                    len = SDL_PrintFloat(text, left, &info, va_arg(ap, double));
slouken@1895
  1678
                    done = SDL_TRUE;
slouken@1895
  1679
                    break;
slouken@10818
  1680
                case 'S':
slouken@10818
  1681
                    {
slouken@10818
  1682
                        /* In practice this is used on Windows for WCHAR strings */
slouken@10818
  1683
                        wchar_t *wide_arg = va_arg(ap, wchar_t *);
slouken@10818
  1684
                        char *arg = SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(wide_arg), (SDL_wcslen(wide_arg)+1)*sizeof(*wide_arg));
slouken@10818
  1685
                        len = SDL_PrintString(text, left, &info, arg);
slouken@10818
  1686
                        SDL_free(arg);
slouken@10818
  1687
                        done = SDL_TRUE;
slouken@10818
  1688
                    }
slouken@10818
  1689
                    break;
slouken@1895
  1690
                case 's':
slouken@7216
  1691
                    len = SDL_PrintString(text, left, &info, va_arg(ap, char *));
slouken@1895
  1692
                    done = SDL_TRUE;
slouken@1895
  1693
                    break;
slouken@1895
  1694
                default:
slouken@1895
  1695
                    done = SDL_TRUE;
slouken@1895
  1696
                    break;
slouken@1330
  1697
                }
slouken@1330
  1698
                ++fmt;
slouken@1330
  1699
            }
slouken@7216
  1700
            if (len >= left) {
icculus@10460
  1701
                text += (left > 1) ? left - 1 : 0;
slouken@7216
  1702
                left = SDL_min(left, 1);
slouken@7216
  1703
            } else {
icculus@10460
  1704
                text += len;
slouken@7216
  1705
                left -= len;
slouken@7216
  1706
            }
slouken@1330
  1707
        } else {
slouken@10733
  1708
            *text++ = *fmt++;
slouken@10733
  1709
            --left;
slouken@1330
  1710
        }
slouken@1330
  1711
    }
slouken@7216
  1712
    if (left > 0) {
slouken@7216
  1713
        *text = '\0';
slouken@7216
  1714
    }
slouken@3253
  1715
    return (int)(text - textstart);
slouken@1330
  1716
}
slouken@7351
  1717
#endif /* HAVE_VSNPRINTF */
icculus@7003
  1718
slouken@1895
  1719
/* vi: set ts=4 sw=4 expandtab: */