19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This file contains portable string manipulation functions for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_stdinc.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_isupperhex(X)   (((X) >= 'A') && ((X) <= 'F'))
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_islowerhex(X)   (((X) >= 'a') && ((X) <= 'f'))
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanLong(const char *text, int radix, long *valuep)
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    long value = 0;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_bool negative = SDL_FALSE;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *text == '-' ) {
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        negative = SDL_TRUE;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += 2;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( ; ; ) {
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int v;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_isdigit((unsigned char) *text) ) {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = *text - '0';
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_isupperhex(*text) ) {
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'A');
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_islowerhex(*text) ) {
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'a');
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value *= radix;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value += v;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( negative && value ) {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = -value;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = value;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned long value = 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += 2;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( ; ; ) {
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int v;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_isdigit((unsigned char) *text) ) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = *text - '0';
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_isupperhex(*text) ) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'A');
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_islowerhex(*text) ) {
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'a');
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value *= radix;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value += v;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *valuep = value;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_SSCANF
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    uintptr_t value = 0;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += 2;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( ; ; ) {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int v;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_isdigit((unsigned char) *text) ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = *text - '0';
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_isupperhex(*text) ) {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'A');
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_islowerhex(*text) ) {
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'a');
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value *= radix;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value += v;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *valuep = value;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanLongLong(const char *text, int radix, Sint64 *valuep)
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sint64 value = 0;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_bool negative = SDL_FALSE;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *text == '-' ) {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        negative = SDL_TRUE;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += 2;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( ; ; ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int v;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_isdigit((unsigned char) *text) ) {
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = *text - '0';
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_isupperhex(*text) ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'A');
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_islowerhex(*text) ) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'a');
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value *= radix;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value += v;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( negative && value ) {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = -value;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = value;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 *valuep)
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint64 value = 0;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += 2;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( ; ; ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int v;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_isdigit((unsigned char) *text) ) {
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = *text - '0';
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_isupperhex(*text) ) {
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'A');
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else if ( radix == 16 && SDL_islowerhex(*text) ) {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            v = 10 + (*text - 'a');
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value *= radix;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value += v;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *valuep = value;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_HAS_64BIT_TYPE */
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_ScanFloat(const char *text, double *valuep)
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *textstart = text;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned long lvalue = 0;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    double value = 0.0;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_bool negative = SDL_FALSE;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *text == '-' ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        negative = SDL_TRUE;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    text += SDL_ScanUnsignedLong(text, 10, &lvalue);
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    value += lvalue;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *text == '.' ) {
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int mult = 10;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++text;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( SDL_isdigit((unsigned char) *text) ) {
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            lvalue = *text - '0';
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value += (double)lvalue / mult;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            mult *= 10;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++text;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( valuep ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( negative && value ) {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = -value;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *valuep = value;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef SDL_memset
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid *SDL_memset(void *dst, int c, size_t len)
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t left = (len % 4);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( len >= 4 ) {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint32 value = 0;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint32 *dstp = (Uint32 *)dst;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for (i = 0; i < 4; ++i) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value <<= 8;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value |= c;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        len /= 4;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( len-- ) {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *dstp++ = value;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( left > 0 ) {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint8 value = (Uint8)c;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint8 *dstp = (Uint8 *)dst;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch(left) {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 3:
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *dstp++ = value;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 2:
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *dstp++ = value;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 1:
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *dstp++ = value;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return dst;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef SDL_memcpy
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid *SDL_memcpy(void *dst, const void *src, size_t len)
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *srcp = (char *)src;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *dstp = (char *)dst;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( len-- ) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dstp++ = *srcp++;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return dst;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef SDL_revcpy
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid *SDL_revcpy(void *dst, const void *src, size_t len)
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *srcp = (char *)src;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *dstp = (char *)dst;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    srcp += len-1;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dstp += len-1;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( len-- ) {
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *dstp-- = *srcp--;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return dst;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef SDL_memcmp
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_memcmp(const void *s1, const void *s2, size_t len)
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *s1p = (char *)s1;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *s2p = (char *)s2;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( len-- ) {
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *s1p != *s2p ) {
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (*s1p - *s2p);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ++s1p;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ++s2p;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRLEN
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsize_t SDL_strlen(const char *string)
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len = 0;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *string++ ) {
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++len;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return len;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRLCPY
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsize_t SDL_strlcpy(char *dst, const char *src, size_t maxlen)
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t srclen = SDL_strlen(src);
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( maxlen > 0 ) {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size_t len = SDL_min(srclen, maxlen-1);
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memcpy(dst, src, len);
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        dst[len] = '\0';
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return srclen;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRLCAT
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallsize_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t dstlen = SDL_strlen(dst);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t srclen = SDL_strlen(src);
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( dstlen < maxlen ) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strlcpy(dst+dstlen, src, maxlen-dstlen);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return dstlen+srclen;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRDUP
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strdup(const char *string)
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len = SDL_strlen(string)+1;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *newstr = SDL_malloc(len);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( newstr ) {
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strlcpy(newstr, string, len);
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return newstr;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__STRREV
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strrev(char *string)
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len = SDL_strlen(string);
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *a = &string[0];
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *b = &string[len-1];
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len /= 2;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( len-- ) {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        char c = *a;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *a++ = *b;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *b-- = c;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__STRUPR
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strupr(char *string)
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *bufp ) {
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp = SDL_toupper((unsigned char) *bufp);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++bufp;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__STRLWR
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strlwr(char *string)
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *bufp ) {
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp = SDL_tolower((unsigned char) *bufp);
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++bufp;
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRCHR
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strchr(const char *string, int c)
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *string ) {
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *string == c ) {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (char *)string;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++string;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRRCHR
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strrchr(const char *string, int c)
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *bufp = string + SDL_strlen(string) - 1;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( bufp >= string ) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *bufp == c ) {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (char *)bufp;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	--bufp;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRSTR
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_strstr(const char *haystack, const char *needle)
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t length = SDL_strlen(needle);
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *haystack ) {
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_strncmp(haystack, needle, length) == 0 ) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (char *)haystack;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++haystack;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE__LTOA)  || !defined(HAVE__I64TOA) || \
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char ntoa_table[] = {
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    'U', 'V', 'W', 'X', 'Y', 'Z'
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* ntoa() conversion table */
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__LTOA
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_ltoa(long value, char *string, int radix)
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value < 0 ) {
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '-';
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value = -value;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value ) {
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( value > 0 ) {
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *bufp++ = ntoa_table[value % radix];
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value /= radix;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '0';
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *bufp = '\0';
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* The numbers went into the string backwards. :) */
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *string == '-' ) {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strrev(string+1);
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strrev(string);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__ULTOA
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_ultoa(unsigned long value, char *string, int radix)
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value ) {
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( value > 0 ) {
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *bufp++ = ntoa_table[value % radix];
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value /= radix;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '0';
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *bufp = '\0';
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* The numbers went into the string backwards. :) */
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strrev(string);
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRTOL
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halllong SDL_strtol(const char *string, char **endp, int base)
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    long value;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !base ) {
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 16;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 10;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_ScanLong(string, base, &value);
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( endp ) {
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *endp = (char *)string + len;
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return value;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRTOUL
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallunsigned long SDL_strtoul(const char *string, char **endp, int base)
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    unsigned long value;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !base ) {
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 16;
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 10;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_ScanUnsignedLong(string, base, &value);
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( endp ) {
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *endp = (char *)string + len;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return value;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__I64TOA
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_lltoa(Sint64 value, char *string, int radix)
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value < 0 ) {
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '-';
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value = -value;
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value ) {
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( value > 0 ) {
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *bufp++ = ntoa_table[value % radix];
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value /= radix;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '0';
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *bufp = '\0';
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* The numbers went into the string backwards. :) */
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( *string == '-' ) {
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strrev(string+1);
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_strrev(string);
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE__UI64TOA
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallchar *SDL_ulltoa(Uint64 value, char *string, int radix)
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *bufp = string;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( value ) {
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while ( value > 0 ) {
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *bufp++ = ntoa_table[value % radix];
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value /= radix;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *bufp++ = '0';
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *bufp = '\0';
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* The numbers went into the string backwards. :) */
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strrev(string);
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return string;
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRTOLL
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSint64 SDL_strtoll(const char *string, char **endp, int base)
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len;
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sint64 value;
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !base ) {
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 16;
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 10;
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_ScanLongLong(string, base, &value);
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( endp ) {
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *endp = (char *)string + len;
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return value;
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRTOULL
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint64 SDL_strtoull(const char *string, char **endp, int base)
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint64 value;
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !base ) {
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( (SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0) ) {
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 16;
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            base = 10;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_ScanUnsignedLongLong(string, base, &value);
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( endp ) {
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *endp = (char *)string + len;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return value;
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_HAS_64BIT_TYPE */
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRTOD
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldouble SDL_strtod(const char *string, char **endp)
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t len;
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    double value;
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    len = SDL_ScanFloat(string, &value);
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( endp ) {
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *endp = (char *)string + len;
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return value;
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRCMP
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_strcmp(const char *str1, const char *str2)
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (*str1 && *str2) {
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *str1 != *str2 )
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str1;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str2;
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (int)((unsigned char)*str1 - (unsigned char)*str2);
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_STRNCMP
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *str1 && *str2 && maxlen ) {
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *str1 != *str2 )
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str1;
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str2;
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        --maxlen;
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ! maxlen ) {
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (int)((unsigned char)*str1 - (unsigned char)*str2);
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_strcasecmp(const char *str1, const char *str2)
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char a = 0;
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char b = 0;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *str1 && *str2 ) {
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        a = SDL_tolower((unsigned char) *str1);
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        b = SDL_tolower((unsigned char) *str2);
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( a != b )
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str1;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str2;
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (int)((unsigned char)a - (unsigned char)b);
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char a = 0;
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char b = 0;
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *str1 && *str2 && maxlen ) {
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        a = SDL_tolower((unsigned char) *str1);
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        b = SDL_tolower((unsigned char) *str2);
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( a != b )
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str1;
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ++str2;
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        --maxlen;
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (int)((unsigned char)a - (unsigned char)b);
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_SSCANF
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_sscanf(const char *text, const char *fmt, ...)
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_list ap;
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int retval = 0;
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_start(ap, fmt);
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *fmt ) {
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *fmt == ' ' ) {
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while ( SDL_isspace((unsigned char) *text) ) {
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++text;
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++fmt;
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            continue;
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *fmt == '%' ) {
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_bool done = SDL_FALSE;
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            long count = 0;
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int radix = 10;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            enum {
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_SHORT,
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_INT,
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_LONG,
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_LONGLONG
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } inttype = DO_INT;
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_bool suppress = SDL_FALSE;
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++fmt;
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( *fmt == '%' ) {
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( *text == '%' ) {
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    ++text;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    ++fmt;
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    continue;
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( *fmt == '*' ) {
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                suppress = SDL_TRUE;
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++fmt;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fmt += SDL_ScanLong(fmt, 10, &count);
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( *fmt == 'c' ) {
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( ! count ) {
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    count = 1;
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( suppress ) {
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    while ( count-- ) {
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        ++text;
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                } else {
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    char *valuep = va_arg(ap, char*);
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    while ( count-- ) {
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        *valuep++ = *text++;
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    ++retval;
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                continue;
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while ( SDL_isspace((unsigned char) *text) ) {
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++text;
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* FIXME: implement more of the format specifiers */
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while (!done) {
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                switch(*fmt) {
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case '*':
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        suppress = SDL_TRUE;
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'h':
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( inttype > DO_SHORT ) {
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            ++inttype;
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'l':
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( inttype < DO_LONGLONG ) {
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            ++inttype;
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'I':
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            fmt += 2;
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            inttype = DO_LONGLONG;
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'i':
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            int index = 0;
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( text[index] == '-' ) {
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++index;
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( text[index] == '0' ) {
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                if ( SDL_tolower((unsigned char) text[index+1]) == 'x' ) {
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    radix = 16;
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                } else {
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    radix = 8;
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to %d handling */
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'd':
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( inttype == DO_LONGLONG ) {
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            Sint64 value;
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanLongLong(text, radix, &value);
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                Sint64 *valuep = va_arg(ap, Sint64*);
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                *valuep = value;
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        else
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_HAS_64BIT_TYPE */
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            long value;
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanLong(text, radix, &value);
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                switch (inttype) {
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_SHORT:
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { short* valuep = va_arg(ap, short*);
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = (short)value;
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_INT:
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { int* valuep = va_arg(ap, int*);
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = (int)value;
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_LONG:
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { long* valuep = va_arg(ap, long*);
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = value;
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_LONGLONG:
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        /* Handled above */
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'o':
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( radix == 10 ) {
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            radix = 8;
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to unsigned handling */
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'x':
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'X':
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( radix == 10 ) {
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            radix = 16;
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to unsigned handling */
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'u':
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( inttype == DO_LONGLONG ) {
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            Uint64 value;
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanUnsignedLongLong(text, radix, &value);
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                Uint64 *valuep = va_arg(ap, Uint64*);
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                *valuep = value;
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        else
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_HAS_64BIT_TYPE */
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            unsigned long value;
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanUnsignedLong(text, radix, &value);
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                switch (inttype) {
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_SHORT:
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { short* valuep = va_arg(ap, short*);
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = (short)value;
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_INT:
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { int* valuep = va_arg(ap, int*);
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = (int)value;
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_LONG:
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        { long* valuep = va_arg(ap, long*);
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            *valuep = value;
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        }
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    case DO_LONGLONG:
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        /* Handled above */
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'p':
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            uintptr_t value;
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanUintPtrT(text, 16, &value);
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                void** valuep = va_arg(ap, void**);
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                *valuep = (void*)value;
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'f':
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            double value;
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            text += SDL_ScanFloat(text, &value);
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            if ( ! suppress ) {
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                float* valuep = va_arg(ap, float*);
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                *valuep = (float)value;
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++retval;
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 's':
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( suppress ) {
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            while ( !SDL_isspace((unsigned char) *text) ) {
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                ++text;
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                if ( count ) {
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    if ( --count == 0 ) {
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    }
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        } else {
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            char *valuep = va_arg(ap, char*);
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            while ( !SDL_isspace((unsigned char) *text) ) {
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                *valuep++ = *text++;
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                if ( count ) {
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    if ( --count == 0 ) {
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        break;
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                    }
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            }
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            *valuep = '\0';
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            ++retval;
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    default:
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++fmt;
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            continue;
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *text == *fmt ) {
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++text;
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++fmt;
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            continue;
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Text didn't match format specifier */
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_end(ap);
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return retval;
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_SNPRINTF
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_list ap;
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int retval;
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_start(ap, fmt);
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    retval = SDL_vsnprintf(text, maxlen, fmt, ap);
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    va_end(ap);
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return retval;
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef HAVE_VSNPRINTF
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char num[130];
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t size;
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_ltoa(value, num, radix);
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size = SDL_strlen(num);
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( size >= maxlen ) {
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size = maxlen-1;
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(text, num, size+1);
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return size;
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen)
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char num[130];
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t size;
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_ultoa(value, num, radix);
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size = SDL_strlen(num);
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( size >= maxlen ) {
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size = maxlen-1;
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(text, num, size+1);
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return size;
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char num[130];
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t size;
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_lltoa(value, num, radix);
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size = SDL_strlen(num);
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( size >= maxlen ) {
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size = maxlen-1;
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(text, num, size+1);
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return size;
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char num[130];
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size_t size;
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_ulltoa(value, num, radix);
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    size = SDL_strlen(num);
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( size >= maxlen ) {
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size = maxlen-1;
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_strlcpy(text, num, size+1);
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return size;
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_HAS_64BIT_TYPE */
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintFloat(char *text, double arg, size_t maxlen)
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *textstart = text;
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( arg ) {
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* This isn't especially accurate, but hey, it's easy. :) */
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        const double precision = 0.00000001;
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        size_t len;
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        unsigned long value;
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( arg < 0 ) {
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *text++ = '-';
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            --maxlen;
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            arg = -arg;
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        value = (unsigned long)arg;
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        text += len;
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        maxlen -= len;
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        arg -= value;
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( arg > precision && maxlen ) {
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int mult = 10;
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *text++ = '.';
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while ( (arg > precision) && maxlen ) {
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                value = (unsigned long)(arg * mult);
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                text += len;
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                maxlen -= len;
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                arg -= (double)value / mult;
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                mult *= 10;
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *text++ = '0';
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic size_t SDL_PrintString(char *text, const char *string, size_t maxlen)
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *textstart = text;
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *string && maxlen-- ) {
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *text++ = *string++;
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *textstart = text;
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( maxlen <= 0 ) {
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    --maxlen; /* For the trailing '\0' */
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( *fmt && maxlen ) {
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( *fmt == '%' ) {
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_bool done = SDL_FALSE;
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            size_t len = 0;
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_bool do_lowercase = SDL_FALSE;
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int radix = 10;
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            enum {
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_INT,
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_LONG,
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DO_LONGLONG
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } inttype = DO_INT;
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++fmt;
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* FIXME: implement more of the format specifiers */
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) {
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++fmt;
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            while (!done) {
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                switch(*fmt) {
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case '%':
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        *text = '%';
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        len = 1;
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'c':
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* char is promoted to int when passed through (...) */
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        *text = (char)va_arg(ap, int);
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        len = 1;
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'h':
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* short is promoted to int when passed through (...) */
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'l':
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( inttype < DO_LONGLONG ) {
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            ++inttype;
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'I':
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( SDL_strncmp(fmt, "I64", 3) == 0 ) {
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            fmt += 2;
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            inttype = DO_LONGLONG;
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'i':
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'd':
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        switch (inttype) {
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_INT:
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintLong(text, (long)va_arg(ap, int), radix, maxlen);
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
11729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_LONG:
11739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
11749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
11759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_LONGLONG:
11769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
11779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintLongLong(text, va_arg(ap, Sint64), radix, maxlen);
11789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
11799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintLong(text, va_arg(ap, long), radix, maxlen);
11809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
11829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
11839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
11849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
11859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'p':
11869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'x':
11879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        do_lowercase = SDL_TRUE;
11889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to 'X' handling */
11899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'X':
11909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( radix == 10 ) {
11919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            radix = 16;
11929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
11939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( *fmt == 'p' ) {
11949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            inttype = DO_LONG;
11959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
11969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to unsigned handling */
11979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'o':
11989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( radix == 10 ) {
11999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            radix = 8;
12009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
12019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* Fall through to unsigned handling */
12029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'u':
12039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        switch (inttype) {
12049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_INT:
12059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintUnsignedLong(text, (unsigned long)va_arg(ap, unsigned int), radix, maxlen);
12069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
12079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_LONG:
12089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
12099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
12109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            case DO_LONGLONG:
12119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_HAS_64BIT_TYPE
12129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintUnsignedLongLong(text, va_arg(ap, Uint64), radix, maxlen);
12139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
12149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                len = SDL_PrintUnsignedLong(text, va_arg(ap, unsigned long), radix, maxlen);
12159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
12169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                break;
12179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
12189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( do_lowercase ) {
12199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            SDL_strlwr(text);
12209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
12219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
12229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
12239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 'f':
12249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
12259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
12269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
12279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case 's':
12289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        len = SDL_PrintString(text, va_arg(ap, char*), maxlen);
12299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
12309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
12319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    default:
12329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        done = SDL_TRUE;
12339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
12349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
12359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++fmt;
12369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
12379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            text += len;
12389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            maxlen -= len;
12399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
12409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *text++ = *fmt++;
12419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            --maxlen;
12429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
12439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
12449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *text = '\0';
12459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
12469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (text - textstart);
12479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
12489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1249