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