2006-02-06 08:28:51 +00:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2013-02-15 08:47:44 -08:00
|
|
|
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
|
2006-02-06 08:28:51 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
2006-02-06 08:28:51 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
2006-02-06 08:28:51 +00:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2006-02-06 08:28:51 +00:00
|
|
|
*/
|
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
|
|
|
|
2010-07-13 15:05:45 -04:00
|
|
|
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
|
|
|
|
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
|
|
|
|
|
2011-10-31 05:56:58 -04:00
|
|
|
static int UTF8_TrailingBytes(unsigned char c)
|
2010-07-13 15:05:45 -04:00
|
|
|
{
|
2011-10-24 21:34:54 -04:00
|
|
|
if (c >= 0xC0 && c <= 0xDF)
|
2010-07-13 15:05:45 -04:00
|
|
|
return 1;
|
|
|
|
else if (c >= 0xE0 && c <= 0xEF)
|
|
|
|
return 2;
|
|
|
|
else if (c >= 0xF0 && c <= 0xF4)
|
|
|
|
return 3;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2007-02-15 11:14:24 +00:00
|
|
|
if (SDL_isdigit((unsigned char) *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;
|
2007-02-15 11:14:24 +00:00
|
|
|
if (SDL_isdigit((unsigned char) *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;
|
2007-02-15 11:14:24 +00:00
|
|
|
if (SDL_isdigit((unsigned char) *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
|
|
|
#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;
|
2007-02-15 11:14:24 +00:00
|
|
|
if (SDL_isdigit((unsigned char) *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;
|
2007-02-15 11:14:24 +00:00
|
|
|
if (SDL_isdigit((unsigned char) *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-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;
|
2007-02-15 11:14:24 +00:00
|
|
|
while (SDL_isdigit((unsigned char) *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);
|
2011-02-16 15:25:10 -08:00
|
|
|
Uint32 *dstp4;
|
|
|
|
Uint8 *dstp1;
|
|
|
|
Uint32 value4 = (c | (c << 8) | (c << 16) | (c << 24));
|
|
|
|
Uint8 value1 = (Uint8) c;
|
|
|
|
|
|
|
|
dstp4 = (Uint32 *) dst;
|
|
|
|
len /= 4;
|
|
|
|
while (len--) {
|
|
|
|
*dstp4++ = value4;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2011-02-16 15:25:10 -08:00
|
|
|
|
|
|
|
dstp1 = (Uint8 *) dstp4;
|
|
|
|
switch (left) {
|
|
|
|
case 3:
|
|
|
|
*dstp1++ = value1;
|
|
|
|
case 2:
|
|
|
|
*dstp1++ = value1;
|
|
|
|
case 1:
|
|
|
|
*dstp1++ = value1;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2011-02-16 15:25:10 -08:00
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
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
|
|
|
{
|
2011-02-16 15:25:10 -08:00
|
|
|
size_t left = (len % 4);
|
|
|
|
Uint32 *srcp4, *dstp4;
|
|
|
|
Uint8 *srcp1, *dstp1;
|
|
|
|
|
|
|
|
srcp4 = (Uint32 *) src;
|
|
|
|
dstp4 = (Uint32 *) dst;
|
|
|
|
len /= 4;
|
2006-07-10 21:04:37 +00:00
|
|
|
while (len--) {
|
2011-02-16 15:25:10 -08:00
|
|
|
*dstp4++ = *srcp4++;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcp1 = (Uint8 *) srcp4;
|
|
|
|
dstp1 = (Uint8 *) dstp4;
|
|
|
|
switch (left) {
|
|
|
|
case 3:
|
|
|
|
*dstp1++ = *srcp1++;
|
|
|
|
case 2:
|
|
|
|
*dstp1++ = *srcp1++;
|
|
|
|
case 1:
|
|
|
|
*dstp1++ = *srcp1++;
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
2011-02-16 15:25:10 -08:00
|
|
|
|
2006-02-06 08:28:51 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-16 15:25:10 -08:00
|
|
|
#ifndef SDL_memmove
|
2006-07-10 21:04:37 +00:00
|
|
|
void *
|
2011-02-16 15:25:10 -08:00
|
|
|
SDL_memmove(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;
|
2011-02-16 15:25:10 -08:00
|
|
|
|
|
|
|
if (src < dst) {
|
|
|
|
srcp += len - 1;
|
|
|
|
dstp += len - 1;
|
|
|
|
while (len--) {
|
|
|
|
*dstp-- = *srcp--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (len--) {
|
|
|
|
*dstp++ = *srcp++;
|
|
|
|
}
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2010-08-03 16:52:10 -04:00
|
|
|
#ifndef HAVE_WCSLCPY
|
|
|
|
size_t
|
|
|
|
SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t srclen = SDL_wcslen(src);
|
|
|
|
if (maxlen > 0) {
|
|
|
|
size_t len = SDL_min(srclen, maxlen - 1);
|
|
|
|
SDL_memcpy(dst, src, len * sizeof(wchar_t));
|
|
|
|
dst[len] = '\0';
|
|
|
|
}
|
|
|
|
return srclen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_WCSLCAT
|
|
|
|
size_t
|
|
|
|
SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen)
|
|
|
|
{
|
|
|
|
size_t dstlen = SDL_wcslen(dst);
|
|
|
|
size_t srclen = SDL_wcslen(src);
|
|
|
|
if (dstlen < maxlen) {
|
|
|
|
SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
|
|
|
|
}
|
|
|
|
return dstlen + srclen;
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
|
2010-07-13 15:05:45 -04:00
|
|
|
size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes)
|
|
|
|
{
|
|
|
|
size_t src_bytes = SDL_strlen(src);
|
|
|
|
size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
|
2011-01-22 15:58:21 -08:00
|
|
|
size_t i = 0;
|
2010-07-13 15:05:45 -04:00
|
|
|
char trailing_bytes = 0;
|
|
|
|
if (bytes)
|
|
|
|
{
|
|
|
|
unsigned char c = (unsigned char)src[bytes - 1];
|
|
|
|
if (UTF8_IsLeadByte(c))
|
|
|
|
--bytes;
|
|
|
|
else if (UTF8_IsTrailingByte(c))
|
|
|
|
{
|
|
|
|
for (i = bytes - 1; i != 0; --i)
|
|
|
|
{
|
|
|
|
c = (unsigned char)src[i];
|
|
|
|
trailing_bytes = UTF8_TrailingBytes(c);
|
|
|
|
if (trailing_bytes)
|
|
|
|
{
|
|
|
|
if (bytes - i != trailing_bytes + 1)
|
|
|
|
bytes = i;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_memcpy(dst, src, bytes);
|
|
|
|
}
|
|
|
|
dst[bytes] = '\0';
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
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) {
|
2007-02-15 11:14:24 +00:00
|
|
|
*bufp = SDL_toupper((unsigned char) *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) {
|
2007-02-15 11:14:24 +00:00
|
|
|
*bufp = SDL_tolower((unsigned char) *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
|
|
|
#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-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) {
|
2007-02-15 11:14:24 +00:00
|
|
|
a = SDL_tolower((unsigned char) *str1);
|
|
|
|
b = SDL_tolower((unsigned char) *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-19 05:03:21 +00:00
|
|
|
a = SDL_tolower(*str1);
|
|
|
|
b = SDL_tolower(*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) {
|
2007-02-15 11:14:24 +00:00
|
|
|
a = SDL_tolower((unsigned char) *str1);
|
|
|
|
b = SDL_tolower((unsigned char) *str2);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (a != b)
|
2006-03-13 01:08:00 +00:00
|
|
|
break;
|
|
|
|
++str1;
|
|
|
|
++str2;
|
|
|
|
--maxlen;
|
|
|
|
}
|
2012-12-22 16:52:33 -08:00
|
|
|
if (maxlen == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
a = SDL_tolower((unsigned char) *str1);
|
|
|
|
b = SDL_tolower((unsigned char) *str2);
|
|
|
|
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 == ' ') {
|
2007-02-15 11:14:24 +00:00
|
|
|
while (SDL_isspace((unsigned char) *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;
|
|
|
|
}
|
|
|
|
|
2007-02-15 11:14:24 +00:00
|
|
|
while (SDL_isspace((unsigned char) *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') {
|
2007-06-14 13:21:29 +00:00
|
|
|
if (SDL_tolower((unsigned char) text[index + 1])
|
|
|
|
== 'x') {
|
2006-07-10 21:04:37 +00:00
|
|
|
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':
|
|
|
|
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
|
|
|
}
|
2011-03-25 13:47:49 -07:00
|
|
|
} else {
|
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':
|
|
|
|
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
|
|
|
}
|
2011-03-25 13:47:49 -07:00
|
|
|
} else {
|
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) {
|
2007-02-15 11:14:24 +00:00
|
|
|
while (!SDL_isspace((unsigned char) *text)) {
|
2006-07-10 21:04:37 +00:00
|
|
|
++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 *);
|
2007-02-15 11:14:24 +00:00
|
|
|
while (!SDL_isspace((unsigned char) *text)) {
|
2006-07-10 21:04:37 +00:00
|
|
|
*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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-03-25 13:47:49 -07:00
|
|
|
|
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:
|
|
|
|
len =
|
|
|
|
SDL_PrintLongLong(text,
|
|
|
|
va_arg(ap, Sint64),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
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:
|
|
|
|
len =
|
|
|
|
SDL_PrintUnsignedLongLong(text,
|
|
|
|
va_arg(ap,
|
|
|
|
Uint64),
|
|
|
|
radix, maxlen);
|
2006-02-06 08:28:51 +00:00
|
|
|
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';
|
|
|
|
|
2009-09-05 23:37:35 +00:00
|
|
|
return (int)(text - textstart);
|
2006-02-06 08:28:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|