This commit is contained in:
AzaezelX 2022-03-23 01:43:08 -05:00
parent ee4253c982
commit 2614274639
1225 changed files with 148950 additions and 51674 deletions

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -48,7 +48,7 @@ int
SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
@ -59,7 +59,7 @@ int
SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
@ -82,7 +82,7 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char *new_variable;
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
@ -115,7 +115,7 @@ SDL_setenv(const char *name, const char *value, int overwrite)
char *new_variable;
/* Input validation */
if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return (-1);
}
@ -181,7 +181,7 @@ SDL_getenv(const char *name)
#endif
/* Input validation */
if (!name || !*name) {
if (!name || *name == '\0') {
return NULL;
}
@ -194,7 +194,7 @@ SDL_getenv(const char *name)
size_t bufferlen;
/* Input validation */
if (!name || SDL_strlen(name)==0) {
if (!name || *name == '\0') {
return NULL;
}
@ -222,7 +222,7 @@ SDL_getenv(const char *name)
char *value;
/* Input validation */
if (!name || SDL_strlen(name)==0) {
if (!name || *name == '\0') {
return NULL;
}

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -31,19 +31,11 @@
#include "SDL_endian.h"
#if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
#include <iconv.h>
/* Depending on which standard the iconv() was implemented with,
iconv() may or may not use const char ** for the inbuf param.
If we get this wrong, it's just a warning, so no big deal.
*/
#if defined(_XGP6) || defined(__APPLE__) || defined(__RISCOS__) || defined(__FREEBSD__) || \
defined(__EMSCRIPTEN__) || \
(defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) || \
(defined(_NEWLIB_VERSION)))
#define ICONV_INBUF_NONCONST
#ifdef __FreeBSD__
/* Define LIBICONV_PLUG to use iconv from the base instead of ports and avoid linker errors. */
#define LIBICONV_PLUG 1
#endif
#include <iconv.h>
#include <errno.h>
SDL_COMPILE_TIME_ASSERT(iconv_t, sizeof (iconv_t) <= sizeof (SDL_iconv_t));
@ -51,13 +43,13 @@ SDL_COMPILE_TIME_ASSERT(iconv_t, sizeof (iconv_t) <= sizeof (SDL_iconv_t));
SDL_iconv_t
SDL_iconv_open(const char *tocode, const char *fromcode)
{
return (SDL_iconv_t) ((size_t) iconv_open(tocode, fromcode));
return (SDL_iconv_t) ((uintptr_t) iconv_open(tocode, fromcode));
}
int
SDL_iconv_close(SDL_iconv_t cd)
{
return iconv_close((iconv_t) ((size_t) cd));
return iconv_close((iconv_t) ((uintptr_t) cd));
}
size_t
@ -65,12 +57,9 @@ SDL_iconv(SDL_iconv_t cd,
const char **inbuf, size_t * inbytesleft,
char **outbuf, size_t * outbytesleft)
{
size_t retCode;
#ifdef ICONV_INBUF_NONCONST
retCode = iconv((iconv_t) ((size_t) cd), (char **) inbuf, inbytesleft, outbuf, outbytesleft);
#else
retCode = iconv((iconv_t) ((size_t) cd), inbuf, inbytesleft, outbuf, outbytesleft);
#endif
/* iconv's second parameter may or may not be `const char const *` depending on the
C runtime's whims. Casting to void * seems to make everyone happy, though. */
const size_t retCode = iconv((iconv_t) ((uintptr_t) cd), (void *) inbuf, inbytesleft, outbuf, outbytesleft);
if (retCode == (size_t) - 1) {
switch (errno) {
case E2BIG:
@ -142,6 +131,11 @@ static struct
{ "US-ASCII", ENCODING_ASCII },
{ "8859-1", ENCODING_LATIN1 },
{ "ISO-8859-1", ENCODING_LATIN1 },
#if defined(__WIN32__) || defined(__OS2__)
{ "WCHAR_T", ENCODING_UTF16LE },
#else
{ "WCHAR_T", ENCODING_UCS4NATIVE },
#endif
{ "UTF8", ENCODING_UTF8 },
{ "UTF-8", ENCODING_UTF8 },
{ "UTF16", ENCODING_UTF16 },
@ -365,33 +359,7 @@ SDL_iconv(SDL_iconv_t cd,
Uint8 *p = (Uint8 *) src;
size_t left = 0;
SDL_bool overlong = SDL_FALSE;
if (p[0] >= 0xFC) {
if ((p[0] & 0xFE) != 0xFC) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if (p[0] == 0xFC && srclen > 1 && (p[1] & 0xFC) == 0x80) {
overlong = SDL_TRUE;
}
ch = (Uint32) (p[0] & 0x01);
left = 5;
}
} else if (p[0] >= 0xF8) {
if ((p[0] & 0xFC) != 0xF8) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if (p[0] == 0xF8 && srclen > 1 && (p[1] & 0xF8) == 0x80) {
overlong = SDL_TRUE;
}
ch = (Uint32) (p[0] & 0x03);
left = 4;
}
} else if (p[0] >= 0xF0) {
if (p[0] >= 0xF0) {
if ((p[0] & 0xF8) != 0xF0) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
@ -666,7 +634,7 @@ SDL_iconv(SDL_iconv_t cd,
p[2] = 0x80 | (Uint8) (ch & 0x3F);
dst += 3;
dstlen -= 3;
} else if (ch <= 0x1FFFFF) {
} else {
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
@ -676,29 +644,6 @@ SDL_iconv(SDL_iconv_t cd,
p[3] = 0x80 | (Uint8) (ch & 0x3F);
dst += 4;
dstlen -= 4;
} else if (ch <= 0x3FFFFFF) {
if (dstlen < 5) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xF8 | (Uint8) ((ch >> 24) & 0x03);
p[1] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[4] = 0x80 | (Uint8) (ch & 0x3F);
dst += 5;
dstlen -= 5;
} else {
if (dstlen < 6) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xFC | (Uint8) ((ch >> 30) & 0x01);
p[1] = 0x80 | (Uint8) ((ch >> 24) & 0x3F);
p[2] = 0x80 | (Uint8) ((ch >> 18) & 0x3F);
p[3] = 0x80 | (Uint8) ((ch >> 12) & 0x3F);
p[4] = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
p[5] = 0x80 | (Uint8) (ch & 0x3F);
dst += 6;
dstlen -= 6;
}
}
break;
@ -798,7 +743,7 @@ SDL_iconv(SDL_iconv_t cd,
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
/* fallthrough */
SDL_FALLTHROUGH;
case ENCODING_UCS4BE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
@ -820,7 +765,7 @@ SDL_iconv(SDL_iconv_t cd,
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
/* fallthrough */
SDL_FALLTHROUGH;
case ENCODING_UCS4LE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
@ -907,6 +852,7 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
stringsize *= 2;
string = (char *) SDL_realloc(string, stringsize);
if (!string) {
SDL_free(oldstring);
SDL_iconv_close(cd);
return NULL;
}
@ -927,8 +873,7 @@ SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf,
break;
}
/* Avoid infinite loops when nothing gets converted */
if (oldinbytesleft == inbytesleft)
{
if (oldinbytesleft == inbytesleft) {
break;
}
}

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -485,6 +485,7 @@ DEFAULT_MMAP_THRESHOLD default: 256K
#define WIN32 1
#endif /* _WIN32 */
#endif /* WIN32 */
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
@ -2326,7 +2327,7 @@ static size_t traverse_and_check(mstate m);
#define treebin_at(M,i) (&((M)->treebins[i]))
/* assign tree index for size S to variable I */
#if defined(__GNUC__) && defined(i386)
#if defined(__GNUC__) && defined(__i386__)
#define compute_tree_index(S, I)\
{\
size_t X = S >> TREEBIN_SHIFT;\
@ -2391,7 +2392,7 @@ static size_t traverse_and_check(mstate m);
/* index corresponding to given bit */
#if defined(__GNUC__) && defined(i386)
#if defined(__GNUC__) && defined(__i386__)
#define compute_bit2idx(X, I)\
{\
unsigned int J;\

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -52,22 +52,22 @@ SDL_atanf(float x)
}
double
SDL_atan2(double x, double y)
SDL_atan2(double y, double x)
{
#if defined(HAVE_ATAN2)
return atan2(x, y);
return atan2(y, x);
#else
return SDL_uclibc_atan2(x, y);
return SDL_uclibc_atan2(y, x);
#endif
}
float
SDL_atan2f(float x, float y)
SDL_atan2f(float y, float x)
{
#if defined(HAVE_ATAN2F)
return atan2f(x, y);
return atan2f(y, x);
#else
return (float)SDL_atan2((double)x, (double)y);
return (float)SDL_atan2((double)y, (double)x);
#endif
}
@ -148,7 +148,7 @@ SDL_ceilf(float x)
#if defined(HAVE_CEILF)
return ceilf(x);
#else
return (float)SDL_ceil((float)x);
return (float)SDL_ceil((double)x);
#endif
}
@ -364,6 +364,50 @@ SDL_powf(float x, float y)
#endif
}
double
SDL_round(double arg)
{
#if defined HAVE_ROUND
return round(arg);
#else
if (arg >= 0.0) {
return SDL_floor(arg + 0.5);
} else {
return SDL_ceil(arg - 0.5);
}
#endif
}
float
SDL_roundf(float arg)
{
#if defined HAVE_ROUNDF
return roundf(arg);
#else
return (float)SDL_round((double)arg);
#endif
}
long
SDL_lround(double arg)
{
#if defined HAVE_LROUND
return lround(arg);
#else
return (long)SDL_round(arg);
#endif
}
long
SDL_lroundf(float arg)
{
#if defined HAVE_LROUNDF
return lroundf(arg);
#else
return (long)SDL_round((double)arg);
#endif
}
double
SDL_scalbn(double x, int n)
{
@ -400,7 +444,7 @@ SDL_sin(double x)
#endif
}
float
float
SDL_sinf(float x)
{
#if defined(HAVE_SINF)
@ -455,26 +499,45 @@ int SDL_abs(int x)
#if defined(HAVE_ABS)
return abs(x);
#else
return ((x) < 0 ? -(x) : (x));
return (x < 0) ? -x : x;
#endif
}
#if defined(HAVE_CTYPE_H)
int SDL_isalpha(int x) { return isalpha(x); }
int SDL_isalnum(int x) { return isalnum(x); }
int SDL_isdigit(int x) { return isdigit(x); }
int SDL_isxdigit(int x) { return isxdigit(x); }
int SDL_ispunct(int x) { return ispunct(x); }
int SDL_isspace(int x) { return isspace(x); }
int SDL_isupper(int x) { return isupper(x); }
int SDL_islower(int x) { return islower(x); }
int SDL_isprint(int x) { return isprint(x); }
int SDL_isgraph(int x) { return isgraph(x); }
int SDL_iscntrl(int x) { return iscntrl(x); }
int SDL_toupper(int x) { return toupper(x); }
int SDL_tolower(int x) { return tolower(x); }
#else
int SDL_isalpha(int x) { return (SDL_isupper(x)) || (SDL_islower(x)); }
int SDL_isalnum(int x) { return (SDL_isalpha(x)) || (SDL_isdigit(x)); }
int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
int SDL_isxdigit(int x) { return (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f')) || (SDL_isdigit(x)); }
int SDL_ispunct(int x) { return (SDL_isgraph(x)) && (!SDL_isalnum(x)); }
int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
int SDL_isupper(int x) { return ((x) >= 'A') && ((x) <= 'Z'); }
int SDL_islower(int x) { return ((x) >= 'a') && ((x) <= 'z'); }
int SDL_isprint(int x) { return ((x) >= ' ') && ((x) < '\x7f'); }
int SDL_isgraph(int x) { return (SDL_isprint(x)) && ((x) != ' '); }
int SDL_iscntrl(int x) { return (((x) >= '\0') && ((x) <= '\x1f')) || ((x) == '\x7f'); }
int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A'+((x)-'a')) : (x); }
int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a'+((x)-'A')) : (x); }
#endif
#if defined(HAVE_CTYPE_H) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
int SDL_isblank(int x) { return isblank(x); }
#else
int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); }
#endif
#ifndef HAVE_LIBC
/* These are some C runtime intrinsics that need to be defined */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,15 @@
#include "SDL_stdinc.h"
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL) || !defined(HAVE_STRTOD)
#if defined(_MSC_VER) && _MSC_VER <= 1800
/* Visual Studio 2013 tries to link with _vacopy in the C runtime. Newer versions do an inline assignment */
#undef va_copy
#define va_copy(dst, src) dst = src
#elif defined(__GNUC__) && (__GNUC__ < 3)
#define va_copy(to, from) __va_copy(to, from)
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
#define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
#define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
#endif
@ -36,7 +44,7 @@
#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
static int UTF8_TrailingBytes(unsigned char c)
static unsigned UTF8_TrailingBytes(unsigned char c)
{
if (c >= 0xC0 && c <= 0xDF)
return 1;
@ -281,7 +289,7 @@ SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
* execute a 32-bit set. Set first bytes manually if needed until it is
* aligned. */
value1 = (Uint8)c;
while ((intptr_t)dstp1 & 0x3) {
while ((uintptr_t)dstp1 & 0x3) {
if (len--) {
*dstp1++ = value1;
} else {
@ -289,7 +297,7 @@ SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
}
}
value4 = (c | (c << 8) | (c << 16) | (c << 24));
value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24));
dstp4 = (Uint32 *) dstp1;
left = (len % 4);
len /= 4;
@ -329,7 +337,7 @@ SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src,
using Uint32* pointers, so we need to make sure the pointers are
aligned before we loop using them.
*/
if (((intptr_t)src & 0x3) || ((intptr_t)dst & 0x3)) {
if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {
/* Do an unaligned byte copy */
Uint8 *srcp1 = (Uint8 *)src;
Uint8 *dstp1 = (Uint8 *)dst;
@ -632,20 +640,17 @@ SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_
size_t src_bytes = SDL_strlen(src);
size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
size_t i = 0;
char trailing_bytes = 0;
if (bytes)
{
unsigned char trailing_bytes = 0;
if (bytes) {
unsigned char c = (unsigned char)src[bytes - 1];
if (UTF8_IsLeadByte(c))
if (UTF8_IsLeadByte(c)) {
--bytes;
else if (UTF8_IsTrailingByte(c))
{
for (i = bytes - 1; i != 0; --i)
{
} 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 (trailing_bytes) {
if (bytes - i != trailing_bytes + 1)
bytes = i;
@ -656,6 +661,7 @@ SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_
SDL_memcpy(dst, src, bytes);
}
dst[bytes] = '\0';
return bytes;
}
@ -664,7 +670,7 @@ SDL_utf8strlen(const char *str)
{
size_t retval = 0;
const char *p = str;
char ch;
unsigned char ch;
while ((ch = *(p++)) != 0) {
/* if top two bits are 1 and 0, it's a continuation byte. */
@ -928,7 +934,7 @@ int SDL_atoi(const char *string)
#ifdef HAVE_ATOI
return atoi(string);
#else
return SDL_strtol(string, NULL, 0);
return SDL_strtol(string, NULL, 10);
#endif /* HAVE_ATOI */
}
@ -1064,13 +1070,16 @@ SDL_strcmp(const char *str1, const char *str2)
#if defined(HAVE_STRCMP)
return strcmp(str1, str2);
#else
while (*str1 && *str2) {
if (*str1 != *str2)
int result;
while(1) {
result = (int)((unsigned char) *str1 - (unsigned char) *str2);
if (result != 0 || (*str1 == '\0'/* && *str2 == '\0'*/))
break;
++str1;
++str2;
}
return (int)((unsigned char) *str1 - (unsigned char) *str2);
return result;
#endif /* HAVE_STRCMP */
}
@ -1080,17 +1089,20 @@ SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
#if defined(HAVE_STRNCMP)
return strncmp(str1, str2, maxlen);
#else
while (*str1 && *str2 && maxlen) {
if (*str1 != *str2)
int result;
while (maxlen) {
result = (int) (unsigned char) *str1 - (unsigned char) *str2;
if (result != 0 || *str1 == '\0'/* && *str2 == '\0'*/)
break;
++str1;
++str2;
--maxlen;
}
if (!maxlen) {
return 0;
result = 0;
}
return (int) ((unsigned char) *str1 - (unsigned char) *str2);
return result;
#endif /* HAVE_STRNCMP */
}
@ -1102,19 +1114,18 @@ SDL_strcasecmp(const char *str1, const char *str2)
#elif defined(HAVE__STRICMP)
return _stricmp(str1, str2);
#else
char a = 0;
char b = 0;
while (*str1 && *str2) {
int a, b, result;
while (1) {
a = SDL_toupper((unsigned char) *str1);
b = SDL_toupper((unsigned char) *str2);
if (a != b)
result = a - b;
if (result != 0 || a == 0 /*&& b == 0*/)
break;
++str1;
++str2;
}
a = SDL_toupper(*str1);
b = SDL_toupper(*str2);
return (int) ((unsigned char) a - (unsigned char) b);
return result;
#endif /* HAVE_STRCASECMP */
}
@ -1126,24 +1137,21 @@ SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
#elif defined(HAVE__STRNICMP)
return _strnicmp(str1, str2, maxlen);
#else
char a = 0;
char b = 0;
while (*str1 && *str2 && maxlen) {
int a, b, result;
while (maxlen) {
a = SDL_tolower((unsigned char) *str1);
b = SDL_tolower((unsigned char) *str2);
if (a != b)
result = a - b;
if (result != 0 || a == 0 /*&& b == 0*/)
break;
++str1;
++str2;
--maxlen;
}
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);
}
if (maxlen == 0)
result = 0;
return result;
#endif /* HAVE_STRNCASECMP */
}
@ -1269,7 +1277,7 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
}
}
/* Fall through to %d handling */
SDL_FALLTHROUGH;
case 'd':
if (inttype == DO_LONGLONG) {
Sint64 value;
@ -1317,13 +1325,13 @@ SDL_vsscanf(const char *text, const char *fmt, va_list ap)
if (radix == 10) {
radix = 8;
}
/* Fall through to unsigned handling */
SDL_FALLTHROUGH;
case 'x':
case 'X':
if (radix == 10) {
radix = 16;
}
/* Fall through to unsigned handling */
SDL_FALLTHROUGH;
case 'u':
if (inttype == DO_LONGLONG) {
Uint64 value = 0;
@ -1472,6 +1480,8 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *f
return vsnprintf(text, maxlen, fmt, ap);
}
#else
#define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
/* FIXME: implement more of the format specifiers */
typedef enum
{
@ -1514,25 +1524,27 @@ SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *str
filllen = SDL_min(width, maxlen);
SDL_memset(text, fill, filllen);
text += filllen;
length += filllen;
maxlen -= filllen;
length += width;
}
slen = SDL_strlcpy(text, string, maxlen);
length += SDL_min(slen, maxlen);
SDL_strlcpy(text, string, maxlen);
length += sz;
if (info) {
if (info->precision >= 0 && (size_t)info->precision < sz) {
slen = (size_t)info->precision;
if (slen < maxlen) {
text[slen] = 0;
length -= (sz - slen);
text[slen] = '\0';
}
length -= (sz - slen);
}
if (info->force_case == SDL_CASE_LOWER) {
SDL_strlwr(text);
} else if (info->force_case == SDL_CASE_UPPER) {
SDL_strupr(text);
if (maxlen > 1) {
if (info->force_case == SDL_CASE_LOWER) {
SDL_strlwr(text);
} else if (info->force_case == SDL_CASE_UPPER) {
SDL_strupr(text);
}
}
}
return length;
@ -1585,7 +1597,7 @@ SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
}
SDL_ltoa(value, p, info ? info->radix : 10);
SDL_IntPrecisionAdjust(num, maxlen, info);
SDL_IntPrecisionAdjust(num, sizeof(num), info);
return SDL_PrintString(text, maxlen, info, num);
}
@ -1595,7 +1607,7 @@ SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned
char num[130];
SDL_ultoa(value, num, info ? info->radix : 10);
SDL_IntPrecisionAdjust(num, maxlen, info);
SDL_IntPrecisionAdjust(num, sizeof(num), info);
return SDL_PrintString(text, maxlen, info, num);
}
@ -1609,7 +1621,7 @@ SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
}
SDL_lltoa(value, p, info ? info->radix : 10);
SDL_IntPrecisionAdjust(num, maxlen, info);
SDL_IntPrecisionAdjust(num, sizeof(num), info);
return SDL_PrintString(text, maxlen, info, num);
}
@ -1619,126 +1631,79 @@ SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint6
char num[130];
SDL_ulltoa(value, num, info ? info->radix : 10);
SDL_IntPrecisionAdjust(num, maxlen, info);
SDL_IntPrecisionAdjust(num, sizeof(num), info);
return SDL_PrintString(text, maxlen, info, num);
}
static size_t
SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
{
int width;
size_t len;
size_t left = maxlen;
char *textstart = text;
size_t length = 0;
if (arg) {
/* This isn't especially accurate, but hey, it's easy. :) */
unsigned long value;
/* This isn't especially accurate, but hey, it's easy. :) */
unsigned long value;
if (arg < 0) {
if (left > 1) {
*text = '-';
--left;
}
++text;
arg = -arg;
} else if (info->force_sign) {
if (left > 1) {
*text = '+';
--left;
}
++text;
if (arg < 0) {
if (length < maxlen) {
text[length] = '-';
}
value = (unsigned long) arg;
len = SDL_PrintUnsignedLong(text, left, NULL, value);
if (len >= left) {
text += (left > 1) ? left - 1 : 0;
left = SDL_min(left, 1);
} else {
text += len;
left -= len;
++length;
arg = -arg;
} else if (info->force_sign) {
if (length < maxlen) {
text[length] = '+';
}
arg -= value;
if (info->precision < 0) {
info->precision = 6;
++length;
}
value = (unsigned long) arg;
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
arg -= value;
if (info->precision < 0) {
info->precision = 6;
}
if (info->force_type || info->precision > 0) {
int mult = 10;
if (length < maxlen) {
text[length] = '.';
}
if (info->force_type || info->precision > 0) {
int mult = 10;
if (left > 1) {
*text = '.';
--left;
}
++text;
while (info->precision-- > 0) {
value = (unsigned long) (arg * mult);
len = SDL_PrintUnsignedLong(text, left, NULL, value);
if (len >= left) {
text += (left > 1) ? left - 1 : 0;
left = SDL_min(left, 1);
} else {
text += len;
left -= len;
}
arg -= (double) value / mult;
mult *= 10;
}
}
} else {
if (left > 1) {
*text = '0';
--left;
}
++text;
if (info->force_type) {
if (left > 1) {
*text = '.';
--left;
}
++text;
++length;
while (info->precision-- > 0) {
value = (unsigned long) (arg * mult);
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
arg -= (double) value / mult;
mult *= 10;
}
}
width = info->width - (int)(text - textstart);
if (width > 0) {
if (info->width > 0 && (size_t)info->width > length) {
const char fill = info->pad_zeroes ? '0' : ' ';
char *end = text+left-1;
len = (text - textstart);
for (len = (text - textstart); len--; ) {
if ((textstart+len+width) < end) {
*(textstart+len+width) = *(textstart+len);
}
}
len = (size_t)width;
if (len >= left) {
text += (left > 1) ? left - 1 : 0;
left = SDL_min(left, 1);
} else {
text += len;
left -= len;
}
size_t width = info->width - length;
size_t filllen, movelen;
if (end != textstart) {
const size_t filllen = SDL_min(len, ((size_t) (end - textstart)) - 1);
SDL_memset(textstart, fill, filllen);
}
filllen = SDL_min(width, maxlen);
movelen = SDL_min(length, (maxlen - filllen));
SDL_memmove(&text[filllen], text, movelen);
SDL_memset(text, fill, filllen);
length += width;
}
return (text - textstart);
return length;
}
int
SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
{
size_t left = maxlen;
char *textstart = text;
size_t length = 0;
if (!text) {
maxlen = 0;
}
if (!fmt) {
fmt = "";
}
while (*fmt && left > 1) {
while (*fmt) {
if (*fmt == '%') {
SDL_bool done = SDL_FALSE;
size_t len = 0;
SDL_bool check_flag;
SDL_FormatInfo info;
enum
@ -1800,18 +1765,18 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
while (!done) {
switch (*fmt) {
case '%':
if (left > 1) {
*text = '%';
if (length < maxlen) {
text[length] = '%';
}
len = 1;
++length;
done = SDL_TRUE;
break;
case 'c':
/* char is promoted to int when passed through (...) */
if (left > 1) {
*text = (char) va_arg(ap, int);
if (length < maxlen) {
text[length] = (char) va_arg(ap, int);
}
len = 1;
++length;
done = SDL_TRUE;
break;
case 'h':
@ -1835,15 +1800,15 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
}
switch (inttype) {
case DO_INT:
len = SDL_PrintLong(text, left, &info,
length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
(long) va_arg(ap, int));
break;
case DO_LONG:
len = SDL_PrintLong(text, left, &info,
length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
va_arg(ap, long));
break;
case DO_LONGLONG:
len = SDL_PrintLongLong(text, left, &info,
length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
va_arg(ap, Sint64));
break;
}
@ -1852,7 +1817,7 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
case 'p':
case 'x':
info.force_case = SDL_CASE_LOWER;
/* Fall through to 'X' handling */
SDL_FALLTHROUGH;
case 'X':
if (info.force_case == SDL_CASE_NOCHANGE) {
info.force_case = SDL_CASE_UPPER;
@ -1863,12 +1828,12 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
if (*fmt == 'p') {
inttype = DO_LONG;
}
/* Fall through to unsigned handling */
SDL_FALLTHROUGH;
case 'o':
if (info.radix == 10) {
info.radix = 8;
}
/* Fall through to unsigned handling */
SDL_FALLTHROUGH;
case 'u':
info.force_sign = SDL_FALSE;
if (info.precision >= 0) {
@ -1876,23 +1841,23 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
}
switch (inttype) {
case DO_INT:
len = SDL_PrintUnsignedLong(text, left, &info,
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
(unsigned long)
va_arg(ap, unsigned int));
break;
case DO_LONG:
len = SDL_PrintUnsignedLong(text, left, &info,
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
va_arg(ap, unsigned long));
break;
case DO_LONGLONG:
len = SDL_PrintUnsignedLongLong(text, left, &info,
length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
va_arg(ap, Uint64));
break;
}
done = SDL_TRUE;
break;
case 'f':
len = SDL_PrintFloat(text, left, &info, va_arg(ap, double));
length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double));
done = SDL_TRUE;
break;
case 'S':
@ -1902,18 +1867,18 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
if (wide_arg) {
char *arg = SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(wide_arg), (SDL_wcslen(wide_arg)+1)*sizeof(*wide_arg));
info.pad_zeroes = SDL_FALSE;
len = SDL_PrintString(text, left, &info, arg);
length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, arg);
SDL_free(arg);
} else {
info.pad_zeroes = SDL_FALSE;
len = SDL_PrintString(text, left, &info, NULL);
length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, NULL);
}
done = SDL_TRUE;
}
break;
case 's':
info.pad_zeroes = SDL_FALSE;
len = SDL_PrintString(text, left, &info, va_arg(ap, char *));
length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
done = SDL_TRUE;
break;
default:
@ -1922,23 +1887,80 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
}
++fmt;
}
if (len >= left) {
text += (left > 1) ? left - 1 : 0;
left = SDL_min(left, 1);
} else {
text += len;
left -= len;
}
} else {
*text++ = *fmt++;
--left;
if (length < maxlen) {
text[length] = *fmt;
}
++fmt;
++length;
}
}
if (left > 0) {
*text = '\0';
if (length < maxlen) {
text[length] = '\0';
} else if (maxlen > 0) {
text[maxlen - 1] = '\0';
}
return (int)(text - textstart);
return (int)length;
}
#undef TEXT_AND_LEN_ARGS
#endif /* HAVE_VSNPRINTF */
int
SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;
int retval;
va_start(ap, fmt);
retval = SDL_vasprintf(strp, fmt, ap);
va_end(ap);
return retval;
}
int
SDL_vasprintf(char **strp, const char *fmt, va_list ap)
{
int retval;
int size = 100; /* Guess we need no more than 100 bytes */
char *p, *np;
va_list aq;
*strp = NULL;
p = (char *)SDL_malloc(size);
if (p == NULL)
return -1;
while (1) {
/* Try to print in the allocated space */
va_copy(aq, ap);
retval = SDL_vsnprintf(p, size, fmt, aq);
va_end(aq);
/* Check error code */
if (retval < 0)
return retval;
/* If that worked, return the string */
if (retval < size) {
*strp = p;
return retval;
}
/* Else try again with more space */
size = retval + 1; /* Precisely what is needed */
np = (char *)SDL_realloc(p, size);
if (np == NULL) {
SDL_free(p);
return -1;
} else {
p = np;
}
}
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -28,12 +28,9 @@
char *SDL_strtokr(char *s1, const char *s2, char **ptr)
{
#if defined(HAVE_STRTOK_R)
#ifdef HAVE_STRTOK_R
return strtok_r(s1, s2, ptr);
#elif defined(_MSC_VER) && defined(HAVE_STRTOK_S)
return strtok_s(s1, s2, ptr);
#else /* SDL implementation */
/*
* Adapted from _PDCLIB_strtok() of PDClib library at