2006-02-06 08:28:51 +00:00
|
|
|
/*
|
|
|
|
SDL - Simple DirectMedia Layer
|
|
|
|
Copyright (C) 1997-2006 Sam Lantinga
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Sam Lantinga
|
|
|
|
slouken@libsdl.org
|
|
|
|
*/
|
2006-02-21 08:46:50 +00:00
|
|
|
#include "SDL_config.h"
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
/* This file contains portable string manipulation functions for SDL */
|
|
|
|
|
2006-02-09 09:38:05 +00:00
|
|
|
#include "SDL_stdinc.h"
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
|
2006-02-09 09:38:05 +00:00
|
|
|
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
|
|
|
|
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL)
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanLong(const char *text, int radix, long *valuep)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
long value = 0;
|
|
|
|
SDL_bool negative = SDL_FALSE;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*text == '-') {
|
2006-02-06 08:28:51 +00:00
|
|
|
negative = SDL_TRUE;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
text += 2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
for (;;) {
|
2006-02-06 08:28:51 +00:00
|
|
|
int v;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_isdigit(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'A');
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'a');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value *= radix;
|
|
|
|
value += v;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
|
|
|
if (negative && value) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep = -value;
|
|
|
|
} else {
|
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-01 09:43:47 +00:00
|
|
|
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanUnsignedLong(const char *text, int radix, unsigned long *valuep)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
unsigned long value = 0;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
text += 2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
for (;;) {
|
2006-02-06 08:28:51 +00:00
|
|
|
int v;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_isdigit(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'A');
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'a');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value *= radix;
|
|
|
|
value += v;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-01 09:43:47 +00:00
|
|
|
#ifndef HAVE_SSCANF
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanUintPtrT(const char *text, int radix, uintptr_t * valuep)
|
2006-03-01 09:43:47 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
uintptr_t value = 0;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
2006-03-01 09:43:47 +00:00
|
|
|
text += 2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
for (;;) {
|
2006-03-01 09:43:47 +00:00
|
|
|
int v;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_isdigit(*text)) {
|
2006-03-01 09:43:47 +00:00
|
|
|
v = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
2006-03-01 09:43:47 +00:00
|
|
|
v = 10 + (*text - 'A');
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
2006-03-01 09:43:47 +00:00
|
|
|
v = 10 + (*text - 'a');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value *= radix;
|
|
|
|
value += v;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
2006-03-01 09:43:47 +00:00
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
|
|
|
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOLL)
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanLongLong(const char *text, int radix, Sint64 * valuep)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
Sint64 value = 0;
|
|
|
|
SDL_bool negative = SDL_FALSE;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*text == '-') {
|
2006-02-06 08:28:51 +00:00
|
|
|
negative = SDL_TRUE;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
text += 2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
for (;;) {
|
2006-02-06 08:28:51 +00:00
|
|
|
int v;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_isdigit(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'A');
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'a');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value *= radix;
|
|
|
|
value += v;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
|
|
|
if (negative && value) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep = -value;
|
|
|
|
} else {
|
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-01 09:43:47 +00:00
|
|
|
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOULL)
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanUnsignedLongLong(const char *text, int radix, Uint64 * valuep)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
Uint64 value = 0;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
text += 2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
for (;;) {
|
2006-02-06 08:28:51 +00:00
|
|
|
int v;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_isdigit(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_isupperhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'A');
|
2006-07-10 21:04:37 +00:00
|
|
|
} else if (radix == 16 && SDL_islowerhex(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
v = 10 + (*text - 'a');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value *= radix;
|
|
|
|
value += v;
|
|
|
|
++text;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* SDL_HAS_64BIT_TYPE */
|
|
|
|
|
2006-02-07 12:11:33 +00:00
|
|
|
#if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOD)
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_ScanFloat(const char *text, double *valuep)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
const char *textstart = text;
|
|
|
|
unsigned long lvalue = 0;
|
|
|
|
double value = 0.0;
|
|
|
|
SDL_bool negative = SDL_FALSE;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*text == '-') {
|
2006-02-06 08:28:51 +00:00
|
|
|
negative = SDL_TRUE;
|
|
|
|
++text;
|
|
|
|
}
|
|
|
|
text += SDL_ScanUnsignedLong(text, 10, &lvalue);
|
|
|
|
value += lvalue;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*text == '.') {
|
2006-02-06 08:28:51 +00:00
|
|
|
int mult = 10;
|
|
|
|
++text;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (SDL_isdigit(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
lvalue = *text - '0';
|
2006-07-10 21:04:37 +00:00
|
|
|
value += (double) lvalue / mult;
|
2006-02-06 08:28:51 +00:00
|
|
|
mult *= 10;
|
|
|
|
++text;
|
|
|
|
}
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (valuep) {
|
|
|
|
if (negative && value) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep = -value;
|
|
|
|
} else {
|
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SDL_memset
|
2006-07-10 21:04:37 +00:00
|
|
|
void *
|
|
|
|
SDL_memset(void *dst, int c, size_t len)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
size_t left = (len % 4);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (len >= 4) {
|
2006-02-06 08:28:51 +00:00
|
|
|
Uint32 value = 0;
|
2006-07-10 21:04:37 +00:00
|
|
|
Uint32 *dstp = (Uint32 *) dst;
|
2006-02-06 08:28:51 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
value <<= 8;
|
|
|
|
value |= c;
|
|
|
|
}
|
|
|
|
len /= 4;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (len--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp++ = value;
|
|
|
|
}
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (left > 0) {
|
|
|
|
Uint8 value = (Uint8) c;
|
|
|
|
Uint8 *dstp = (Uint8 *) dst;
|
|
|
|
switch (left) {
|
|
|
|
case 3:
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp++ = value;
|
2006-07-10 21:04:37 +00:00
|
|
|
case 2:
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp++ = value;
|
2006-07-10 21:04:37 +00:00
|
|
|
case 1:
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp++ = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SDL_memcpy
|
2006-07-10 21:04:37 +00:00
|
|
|
void *
|
|
|
|
SDL_memcpy(void *dst, const void *src, size_t len)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char *srcp = (char *) src;
|
|
|
|
char *dstp = (char *) dst;
|
|
|
|
while (len--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp++ = *srcp++;
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SDL_revcpy
|
2006-07-10 21:04:37 +00:00
|
|
|
void *
|
|
|
|
SDL_revcpy(void *dst, const void *src, size_t len)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char *srcp = (char *) src;
|
|
|
|
char *dstp = (char *) dst;
|
2006-02-06 08:28:51 +00:00
|
|
|
srcp += len;
|
|
|
|
dstp += len;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (len--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*dstp-- = *srcp--;
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SDL_memcmp
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_memcmp(const void *s1, const void *s2, size_t len)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char *s1p = (char *) s1;
|
|
|
|
char *s2p = (char *) s2;
|
|
|
|
while (len--) {
|
|
|
|
if (*s1p != *s2p) {
|
2006-02-06 08:28:51 +00:00
|
|
|
return (*s1p - *s2p);
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
++s1p;
|
|
|
|
++s2p;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRLEN
|
2006-07-10 21:04:37 +00:00
|
|
|
size_t
|
|
|
|
SDL_strlen(const char *string)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
size_t len = 0;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*string++) {
|
2006-02-06 08:28:51 +00:00
|
|
|
++len;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-13 08:13:02 +00:00
|
|
|
#ifndef HAVE_WCSLEN
|
|
|
|
size_t
|
2006-07-14 06:40:53 +00:00
|
|
|
SDL_wcslen(const wchar_t * string)
|
2006-07-13 08:13:02 +00:00
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
while (*string++) {
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-19 23:46:34 +00:00
|
|
|
#ifndef HAVE_STRLCPY
|
2006-07-10 21:04:37 +00:00
|
|
|
size_t
|
|
|
|
SDL_strlcpy(char *dst, const char *src, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-02-19 23:46:34 +00:00
|
|
|
size_t srclen = SDL_strlen(src);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (maxlen > 0) {
|
|
|
|
size_t len = SDL_min(srclen, maxlen - 1);
|
2006-02-19 23:46:34 +00:00
|
|
|
SDL_memcpy(dst, src, len);
|
|
|
|
dst[len] = '\0';
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-02-19 23:46:34 +00:00
|
|
|
return srclen;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-19 23:46:34 +00:00
|
|
|
#ifndef HAVE_STRLCAT
|
2006-07-10 21:04:37 +00:00
|
|
|
size_t
|
|
|
|
SDL_strlcat(char *dst, const char *src, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-02-19 23:46:34 +00:00
|
|
|
size_t dstlen = SDL_strlen(dst);
|
|
|
|
size_t srclen = SDL_strlen(src);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (dstlen < maxlen) {
|
|
|
|
SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return dstlen + srclen;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-07 12:11:33 +00:00
|
|
|
#ifndef HAVE_STRDUP
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strdup(const char *string)
|
2006-02-07 12:11:33 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
size_t len = SDL_strlen(string) + 1;
|
2006-02-19 23:46:34 +00:00
|
|
|
char *newstr = SDL_malloc(len);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (newstr) {
|
2006-02-19 23:46:34 +00:00
|
|
|
SDL_strlcpy(newstr, string, len);
|
2006-02-07 12:11:33 +00:00
|
|
|
}
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifndef HAVE__STRREV
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strrev(char *string)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
size_t len = SDL_strlen(string);
|
|
|
|
char *a = &string[0];
|
2006-07-10 21:04:37 +00:00
|
|
|
char *b = &string[len - 1];
|
2006-02-06 08:28:51 +00:00
|
|
|
len /= 2;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (len--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
char c = *a;
|
|
|
|
*a++ = *b;
|
|
|
|
*b-- = c;
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE__STRUPR
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strupr(char *string)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*bufp) {
|
2006-02-10 06:48:43 +00:00
|
|
|
*bufp = SDL_toupper(*bufp);
|
2006-07-10 21:04:37 +00:00
|
|
|
++bufp;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE__STRLWR
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strlwr(char *string)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*bufp) {
|
2006-02-10 06:48:43 +00:00
|
|
|
*bufp = SDL_tolower(*bufp);
|
2006-07-10 21:04:37 +00:00
|
|
|
++bufp;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRCHR
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strchr(const char *string, int c)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*string) {
|
|
|
|
if (*string == c) {
|
|
|
|
return (char *) string;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
++string;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRRCHR
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strrchr(const char *string, int c)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-02-07 06:59:48 +00:00
|
|
|
const char *bufp = string + SDL_strlen(string) - 1;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (bufp >= string) {
|
|
|
|
if (*bufp == c) {
|
|
|
|
return (char *) bufp;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
--bufp;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRSTR
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_strstr(const char *haystack, const char *needle)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-02-07 06:59:48 +00:00
|
|
|
size_t length = SDL_strlen(needle);
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*haystack) {
|
|
|
|
if (SDL_strncmp(haystack, needle, length) == 0) {
|
|
|
|
return (char *) haystack;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
++haystack;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
|
|
|
|
!defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
|
|
|
|
static const char ntoa_table[] = {
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
|
|
|
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
|
|
|
'U', 'V', 'W', 'X', 'Y', 'Z'
|
|
|
|
};
|
|
|
|
#endif /* ntoa() conversion table */
|
|
|
|
|
|
|
|
#ifndef HAVE__LTOA
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_ltoa(long value, char *string, int radix)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value < 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = '-';
|
|
|
|
value = -value;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value) {
|
|
|
|
while (value > 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
value /= radix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
/* The numbers went into the string backwards. :) */
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*string == '-') {
|
|
|
|
SDL_strrev(string + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
} else {
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_strrev(string);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE__ULTOA
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_ultoa(unsigned long value, char *string, int radix)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value) {
|
|
|
|
while (value > 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
value /= radix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
/* The numbers went into the string backwards. :) */
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_strrev(string);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRTOL
|
2006-07-10 21:04:37 +00:00
|
|
|
long
|
|
|
|
SDL_strtol(const char *string, char **endp, int base)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
long value;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!base) {
|
|
|
|
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
2006-06-20 05:55:23 +00:00
|
|
|
base = 16;
|
|
|
|
} else {
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = SDL_ScanLong(string, base, &value);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (endp) {
|
|
|
|
*endp = (char *) string + len;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-01 09:43:47 +00:00
|
|
|
#ifndef HAVE_STRTOUL
|
2006-07-10 21:04:37 +00:00
|
|
|
unsigned long
|
|
|
|
SDL_strtoul(const char *string, char **endp, int base)
|
2006-03-01 09:43:47 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
unsigned long value;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!base) {
|
|
|
|
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
2006-06-20 05:55:23 +00:00
|
|
|
base = 16;
|
|
|
|
} else {
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = SDL_ScanUnsignedLong(string, base, &value);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (endp) {
|
|
|
|
*endp = (char *) string + len;
|
2006-03-01 09:43:47 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
|
|
|
|
|
|
|
#ifndef HAVE__I64TOA
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_lltoa(Sint64 value, char *string, int radix)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value < 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = '-';
|
|
|
|
value = -value;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value) {
|
|
|
|
while (value > 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
value /= radix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
/* The numbers went into the string backwards. :) */
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*string == '-') {
|
|
|
|
SDL_strrev(string + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
} else {
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_strrev(string);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE__UI64TOA
|
2006-07-10 21:04:37 +00:00
|
|
|
char *
|
|
|
|
SDL_ulltoa(Uint64 value, char *string, int radix)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *bufp = string;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (value) {
|
|
|
|
while (value > 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*bufp++ = ntoa_table[value % radix];
|
|
|
|
value /= radix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
|
|
|
|
/* The numbers went into the string backwards. :) */
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_strrev(string);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRTOLL
|
2006-07-10 21:04:37 +00:00
|
|
|
Sint64
|
|
|
|
SDL_strtoll(const char *string, char **endp, int base)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
Sint64 value;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!base) {
|
|
|
|
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
2006-06-20 05:55:23 +00:00
|
|
|
base = 16;
|
|
|
|
} else {
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = SDL_ScanLongLong(string, base, &value);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (endp) {
|
|
|
|
*endp = (char *) string + len;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-01 09:43:47 +00:00
|
|
|
#ifndef HAVE_STRTOULL
|
2006-07-10 21:04:37 +00:00
|
|
|
Uint64
|
|
|
|
SDL_strtoull(const char *string, char **endp, int base)
|
2006-03-01 09:43:47 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
Uint64 value;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!base) {
|
|
|
|
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
|
2006-06-20 05:55:23 +00:00
|
|
|
base = 16;
|
|
|
|
} else {
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = SDL_ScanUnsignedLongLong(string, base, &value);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (endp) {
|
|
|
|
*endp = (char *) string + len;
|
2006-03-01 09:43:47 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#endif /* SDL_HAS_64BIT_TYPE */
|
|
|
|
|
2006-02-07 12:11:33 +00:00
|
|
|
#ifndef HAVE_STRTOD
|
2006-07-10 21:04:37 +00:00
|
|
|
double
|
|
|
|
SDL_strtod(const char *string, char **endp)
|
2006-02-07 12:11:33 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
double value;
|
|
|
|
|
|
|
|
len = SDL_ScanFloat(string, &value);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (endp) {
|
|
|
|
*endp = (char *) string + len;
|
2006-02-07 12:11:33 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifndef HAVE_STRCMP
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_strcmp(const char *str1, const char *str2)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
while (*str1 && *str2) {
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*str1 != *str2)
|
2006-02-06 08:28:51 +00:00
|
|
|
break;
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_STRNCMP
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*str1 && *str2 && maxlen) {
|
|
|
|
if (*str1 != *str2)
|
2006-02-06 08:28:51 +00:00
|
|
|
break;
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
--maxlen;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!maxlen) {
|
2006-02-06 08:28:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-13 02:06:33 +00:00
|
|
|
#if !defined(HAVE_STRCASECMP) && !defined(HAVE__STRICMP)
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_strcasecmp(const char *str1, const char *str2)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char a = 0;
|
|
|
|
char b = 0;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*str1 && *str2) {
|
2006-02-10 06:48:43 +00:00
|
|
|
a = SDL_tolower(*str1);
|
|
|
|
b = SDL_tolower(*str2);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (a != b)
|
2006-02-06 08:28:51 +00:00
|
|
|
break;
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return (int) ((unsigned char) a - (unsigned char) b);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-13 02:12:39 +00:00
|
|
|
#if !defined(HAVE_STRNCASECMP) && !defined(HAVE__STRNICMP)
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
|
2006-03-13 01:08:00 +00:00
|
|
|
{
|
|
|
|
char a = 0;
|
|
|
|
char b = 0;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*str1 && *str2 && maxlen) {
|
2006-03-13 01:08:00 +00:00
|
|
|
a = SDL_tolower(*str1);
|
|
|
|
b = SDL_tolower(*str2);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (a != b)
|
2006-03-13 01:08:00 +00:00
|
|
|
break;
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
--maxlen;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return (int) ((unsigned char) a - (unsigned char) b);
|
2006-03-13 01:08:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifndef HAVE_SSCANF
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_sscanf(const char *text, const char *fmt, ...)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt == ' ') {
|
|
|
|
while (SDL_isspace(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
++text;
|
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*fmt == '%') {
|
2006-02-06 08:28:51 +00:00
|
|
|
SDL_bool done = SDL_FALSE;
|
|
|
|
long count = 0;
|
|
|
|
int radix = 10;
|
2006-07-10 21:04:37 +00:00
|
|
|
enum
|
|
|
|
{
|
2006-02-06 08:28:51 +00:00
|
|
|
DO_SHORT,
|
|
|
|
DO_INT,
|
|
|
|
DO_LONG,
|
|
|
|
DO_LONGLONG
|
|
|
|
} inttype = DO_INT;
|
|
|
|
SDL_bool suppress = SDL_FALSE;
|
|
|
|
|
|
|
|
++fmt;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*fmt == '%') {
|
|
|
|
if (*text == '%') {
|
2006-02-06 08:28:51 +00:00
|
|
|
++text;
|
|
|
|
++fmt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*fmt == '*') {
|
2006-02-06 08:28:51 +00:00
|
|
|
suppress = SDL_TRUE;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
fmt += SDL_ScanLong(fmt, 10, &count);
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*fmt == 'c') {
|
|
|
|
if (!count) {
|
2006-02-06 08:28:51 +00:00
|
|
|
count = 1;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (suppress) {
|
|
|
|
while (count--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
++text;
|
|
|
|
}
|
|
|
|
} else {
|
2006-07-10 21:04:37 +00:00
|
|
|
char *valuep = va_arg(ap, char *);
|
|
|
|
while (count--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*valuep++ = *text++;
|
|
|
|
}
|
|
|
|
++retval;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
while (SDL_isspace(*text)) {
|
2006-02-06 08:28:51 +00:00
|
|
|
++text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: implement more of the format specifiers */
|
|
|
|
while (!done) {
|
2006-07-10 21:04:37 +00:00
|
|
|
switch (*fmt) {
|
|
|
|
case '*':
|
|
|
|
suppress = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
if (inttype > DO_SHORT) {
|
|
|
|
++inttype;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (inttype < DO_LONGLONG) {
|
|
|
|
++inttype;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
if (SDL_strncmp(fmt, "I64", 3) == 0) {
|
|
|
|
fmt += 2;
|
|
|
|
inttype = DO_LONGLONG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
if (text[index] == '-') {
|
|
|
|
++index;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (text[index] == '0') {
|
|
|
|
if (SDL_tolower(text[index + 1]) == 'x') {
|
|
|
|
radix = 16;
|
|
|
|
} else {
|
|
|
|
radix = 8;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
/* Fall through to %d handling */
|
|
|
|
case 'd':
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
2006-07-10 21:04:37 +00:00
|
|
|
if (inttype == DO_LONGLONG) {
|
|
|
|
Sint64 value;
|
|
|
|
text += SDL_ScanLongLong(text, radix, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
Sint64 *valuep = va_arg(ap, Sint64 *);
|
|
|
|
*valuep = value;
|
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
} else
|
2006-02-06 08:28:51 +00:00
|
|
|
#endif /* SDL_HAS_64BIT_TYPE */
|
2006-07-10 21:04:37 +00:00
|
|
|
{
|
|
|
|
long value;
|
|
|
|
text += SDL_ScanLong(text, radix, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
switch (inttype) {
|
|
|
|
case DO_SHORT:
|
|
|
|
{
|
|
|
|
short *valuep = va_arg(ap, short *);
|
|
|
|
*valuep = (short) value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DO_INT:
|
|
|
|
{
|
|
|
|
int *valuep = va_arg(ap, int *);
|
|
|
|
*valuep = (int) value;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
break;
|
|
|
|
case DO_LONG:
|
|
|
|
{
|
|
|
|
long *valuep = va_arg(ap, long *);
|
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DO_LONGLONG:
|
|
|
|
/* Handled above */
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if (radix == 10) {
|
|
|
|
radix = 8;
|
|
|
|
}
|
|
|
|
/* Fall through to unsigned handling */
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
if (radix == 10) {
|
|
|
|
radix = 16;
|
|
|
|
}
|
|
|
|
/* Fall through to unsigned handling */
|
|
|
|
case 'u':
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
2006-07-10 21:04:37 +00:00
|
|
|
if (inttype == DO_LONGLONG) {
|
|
|
|
Uint64 value;
|
|
|
|
text += SDL_ScanUnsignedLongLong(text, radix, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
Uint64 *valuep = va_arg(ap, Uint64 *);
|
|
|
|
*valuep = value;
|
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
} else
|
2006-02-06 08:28:51 +00:00
|
|
|
#endif /* SDL_HAS_64BIT_TYPE */
|
2006-07-10 21:04:37 +00:00
|
|
|
{
|
|
|
|
unsigned long value;
|
|
|
|
text += SDL_ScanUnsignedLong(text, radix, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
switch (inttype) {
|
|
|
|
case DO_SHORT:
|
|
|
|
{
|
|
|
|
short *valuep = va_arg(ap, short *);
|
|
|
|
*valuep = (short) value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DO_INT:
|
|
|
|
{
|
|
|
|
int *valuep = va_arg(ap, int *);
|
|
|
|
*valuep = (int) value;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
break;
|
|
|
|
case DO_LONG:
|
|
|
|
{
|
|
|
|
long *valuep = va_arg(ap, long *);
|
|
|
|
*valuep = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DO_LONGLONG:
|
|
|
|
/* Handled above */
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
{
|
|
|
|
uintptr_t value;
|
|
|
|
text += SDL_ScanUintPtrT(text, 16, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
void **valuep = va_arg(ap, void **);
|
|
|
|
*valuep = (void *) value;
|
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
double value;
|
|
|
|
text += SDL_ScanFloat(text, &value);
|
|
|
|
if (!suppress) {
|
|
|
|
float *valuep = va_arg(ap, float *);
|
|
|
|
*valuep = (float) value;
|
|
|
|
++retval;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (suppress) {
|
|
|
|
while (!SDL_isspace(*text)) {
|
|
|
|
++text;
|
|
|
|
if (count) {
|
|
|
|
if (--count == 0) {
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *valuep = va_arg(ap, char *);
|
|
|
|
while (!SDL_isspace(*text)) {
|
|
|
|
*valuep++ = *text++;
|
|
|
|
if (count) {
|
|
|
|
if (--count == 0) {
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
*valuep = '\0';
|
|
|
|
++retval;
|
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
if (*text == *fmt) {
|
2006-02-06 08:28:51 +00:00
|
|
|
++text;
|
|
|
|
++fmt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Text didn't match format specifier */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_SNPRINTF
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
retval = SDL_vsnprintf(text, maxlen, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_VSNPRINTF
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_PrintLong(char *text, long value, int radix, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char num[130];
|
|
|
|
size_t size;
|
|
|
|
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_ltoa(value, num, radix);
|
2006-02-06 08:28:51 +00:00
|
|
|
size = SDL_strlen(num);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (size >= maxlen) {
|
|
|
|
size = maxlen - 1;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_strlcpy(text, num, size + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
SDL_PrintUnsignedLong(char *text, unsigned long value, int radix,
|
|
|
|
size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char num[130];
|
|
|
|
size_t size;
|
|
|
|
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_ultoa(value, num, radix);
|
2006-02-06 08:28:51 +00:00
|
|
|
size = SDL_strlen(num);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (size >= maxlen) {
|
|
|
|
size = maxlen - 1;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_strlcpy(text, num, size + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char num[130];
|
|
|
|
size_t size;
|
|
|
|
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_lltoa(value, num, radix);
|
2006-02-06 08:28:51 +00:00
|
|
|
size = SDL_strlen(num);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (size >= maxlen) {
|
|
|
|
size = maxlen - 1;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_strlcpy(text, num, size + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char num[130];
|
|
|
|
size_t size;
|
|
|
|
|
2006-02-07 06:59:48 +00:00
|
|
|
SDL_ulltoa(value, num, radix);
|
2006-02-06 08:28:51 +00:00
|
|
|
size = SDL_strlen(num);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (size >= maxlen) {
|
|
|
|
size = maxlen - 1;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_strlcpy(text, num, size + 1);
|
2006-02-06 08:28:51 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
#endif /* SDL_HAS_64BIT_TYPE */
|
2006-07-10 21:04:37 +00:00
|
|
|
static size_t
|
|
|
|
SDL_PrintFloat(char *text, double arg, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *textstart = text;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (arg) {
|
2006-02-06 08:28:51 +00:00
|
|
|
/* This isn't especially accurate, but hey, it's easy. :) */
|
|
|
|
const double precision = 0.00000001;
|
|
|
|
size_t len;
|
|
|
|
unsigned long value;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (arg < 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*text++ = '-';
|
|
|
|
--maxlen;
|
|
|
|
arg = -arg;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
value = (unsigned long) arg;
|
2006-02-06 08:28:51 +00:00
|
|
|
len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
|
|
|
|
text += len;
|
|
|
|
maxlen -= len;
|
|
|
|
arg -= value;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (arg > precision && maxlen) {
|
2006-02-06 08:28:51 +00:00
|
|
|
int mult = 10;
|
|
|
|
*text++ = '.';
|
2006-07-10 21:04:37 +00:00
|
|
|
while ((arg > precision) && maxlen) {
|
|
|
|
value = (unsigned long) (arg * mult);
|
2006-02-06 08:28:51 +00:00
|
|
|
len = SDL_PrintUnsignedLong(text, value, 10, maxlen);
|
|
|
|
text += len;
|
|
|
|
maxlen -= len;
|
2006-07-10 21:04:37 +00:00
|
|
|
arg -= (double) value / mult;
|
2006-02-06 08:28:51 +00:00
|
|
|
mult *= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*text++ = '0';
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
static size_t
|
|
|
|
SDL_PrintString(char *text, const char *string, size_t maxlen)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *textstart = text;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*string && maxlen--) {
|
2006-02-06 08:28:51 +00:00
|
|
|
*text++ = *string++;
|
|
|
|
}
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
SDL_vsnprintf(char *text, size_t maxlen, const char *fmt, va_list ap)
|
2006-02-06 08:28:51 +00:00
|
|
|
{
|
|
|
|
char *textstart = text;
|
2006-07-10 21:04:37 +00:00
|
|
|
if (maxlen <= 0) {
|
2006-02-06 08:28:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
--maxlen; /* For the trailing '\0' */
|
|
|
|
while (*fmt && maxlen) {
|
|
|
|
if (*fmt == '%') {
|
2006-02-06 08:28:51 +00:00
|
|
|
SDL_bool done = SDL_FALSE;
|
|
|
|
size_t len = 0;
|
|
|
|
SDL_bool do_lowercase = SDL_FALSE;
|
|
|
|
int radix = 10;
|
2006-07-10 21:04:37 +00:00
|
|
|
enum
|
|
|
|
{
|
2006-02-06 08:28:51 +00:00
|
|
|
DO_INT,
|
|
|
|
DO_LONG,
|
|
|
|
DO_LONGLONG
|
|
|
|
} inttype = DO_INT;
|
|
|
|
|
|
|
|
++fmt;
|
|
|
|
/* FIXME: implement more of the format specifiers */
|
2006-07-10 21:04:37 +00:00
|
|
|
while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
|
2006-05-11 21:03:23 +00:00
|
|
|
++fmt;
|
|
|
|
}
|
2006-02-06 08:28:51 +00:00
|
|
|
while (!done) {
|
2006-07-10 21:04:37 +00:00
|
|
|
switch (*fmt) {
|
|
|
|
case '%':
|
|
|
|
*text = '%';
|
|
|
|
len = 1;
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
/* char is promoted to int when passed through (...) */
|
|
|
|
*text = (char) va_arg(ap, int);
|
|
|
|
len = 1;
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
/* short is promoted to int when passed through (...) */
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (inttype < DO_LONGLONG) {
|
|
|
|
++inttype;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
if (SDL_strncmp(fmt, "I64", 3) == 0) {
|
|
|
|
fmt += 2;
|
|
|
|
inttype = DO_LONGLONG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'd':
|
|
|
|
switch (inttype) {
|
|
|
|
case DO_INT:
|
|
|
|
len =
|
|
|
|
SDL_PrintLong(text,
|
|
|
|
(long) va_arg(ap, int),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
break;
|
2006-07-10 21:04:37 +00:00
|
|
|
case DO_LONG:
|
|
|
|
len =
|
|
|
|
SDL_PrintLong(text, va_arg(ap, long),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
break;
|
2006-07-10 21:04:37 +00:00
|
|
|
case DO_LONGLONG:
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
2006-07-10 21:04:37 +00:00
|
|
|
len =
|
|
|
|
SDL_PrintLongLong(text,
|
|
|
|
va_arg(ap, Sint64),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
#else
|
2006-07-10 21:04:37 +00:00
|
|
|
len =
|
|
|
|
SDL_PrintLong(text, va_arg(ap, long),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
#endif
|
|
|
|
break;
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
case 'x':
|
|
|
|
do_lowercase = SDL_TRUE;
|
|
|
|
/* Fall through to 'X' handling */
|
|
|
|
case 'X':
|
|
|
|
if (radix == 10) {
|
|
|
|
radix = 16;
|
|
|
|
}
|
|
|
|
if (*fmt == 'p') {
|
|
|
|
inttype = DO_LONG;
|
|
|
|
}
|
|
|
|
/* Fall through to unsigned handling */
|
|
|
|
case 'o':
|
|
|
|
if (radix == 10) {
|
|
|
|
radix = 8;
|
|
|
|
}
|
|
|
|
/* Fall through to unsigned handling */
|
|
|
|
case 'u':
|
|
|
|
switch (inttype) {
|
|
|
|
case DO_INT:
|
|
|
|
len = SDL_PrintUnsignedLong(text, (unsigned long)
|
|
|
|
va_arg(ap,
|
|
|
|
unsigned
|
|
|
|
int),
|
|
|
|
radix, maxlen);
|
|
|
|
break;
|
|
|
|
case DO_LONG:
|
|
|
|
len =
|
|
|
|
SDL_PrintUnsignedLong(text,
|
|
|
|
va_arg(ap,
|
|
|
|
unsigned
|
|
|
|
long),
|
|
|
|
radix, maxlen);
|
|
|
|
break;
|
|
|
|
case DO_LONGLONG:
|
2006-02-06 08:28:51 +00:00
|
|
|
#ifdef SDL_HAS_64BIT_TYPE
|
2006-07-10 21:04:37 +00:00
|
|
|
len =
|
|
|
|
SDL_PrintUnsignedLongLong(text,
|
|
|
|
va_arg(ap,
|
|
|
|
Uint64),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
#else
|
2006-07-10 21:04:37 +00:00
|
|
|
len =
|
|
|
|
SDL_PrintUnsignedLong(text,
|
|
|
|
va_arg(ap,
|
|
|
|
unsigned
|
|
|
|
long),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
#endif
|
|
|
|
break;
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
if (do_lowercase) {
|
|
|
|
SDL_strlwr(text);
|
|
|
|
}
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
len = SDL_PrintFloat(text, va_arg(ap, double), maxlen);
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
len = SDL_PrintString(text, va_arg(ap, char *), maxlen);
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
text += len;
|
|
|
|
maxlen -= len;
|
|
|
|
} else {
|
|
|
|
*text++ = *fmt++;
|
|
|
|
--maxlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*text = '\0';
|
|
|
|
|
|
|
|
return (text - textstart);
|
|
|
|
}
|
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|