Added SDL_swprintf() and SDL_vswprintf()

This commit is contained in:
Sam Lantinga
2023-05-24 09:41:22 -07:00
parent 6c28546828
commit c9d8a04945
8 changed files with 250 additions and 2 deletions

View File

@@ -123,6 +123,16 @@ static void SDL_InitDynamicAPI(void);
va_end(ap); \
return retval; \
} \
_static int SDLCALL SDL_swprintf##name(SDL_OUT_Z_CAP(maxlen) wchar_t *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...) \
{ \
int retval; \
va_list ap; \
initcall; \
va_start(ap, fmt); \
retval = jump_table.SDL_vswprintf(buf, maxlen, fmt, ap); \
va_end(ap); \
return retval; \
} \
_static int SDLCALL SDL_asprintf##name(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) \
{ \
int retval; \

View File

@@ -859,6 +859,8 @@ SDL3_0.0.0 {
SDL_GetWindowDisplayScale;
SDL_GetWindowPixelDensity;
SDL_wcstol;
SDL_swprintf;
SDL_vswprintf;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -885,3 +885,5 @@
#define SDL_GetWindowDisplayScale SDL_GetWindowDisplayScale_REAL
#define SDL_GetWindowPixelDensity SDL_GetWindowPixelDensity_REAL
#define SDL_wcstol SDL_wcstol_REAL
#define SDL_swprintf SDL_swprintf_REAL
#define SDL_vswprintf SDL_vswprintf_REAL

View File

@@ -41,6 +41,7 @@ SDL_DYNAPI_PROC(void,SDL_LogWarn,(int a, SDL_PRINTF_FORMAT_STRING const char *b,
SDL_DYNAPI_PROC(int,SDL_SetError,(SDL_PRINTF_FORMAT_STRING const char *a, ...),(a),return)
SDL_DYNAPI_PROC(int,SDL_asprintf,(char **a, SDL_PRINTF_FORMAT_STRING const char *b, ...),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_snprintf,(SDL_OUT_Z_CAP(b) char *a, size_t b, SDL_PRINTF_FORMAT_STRING const char *c, ...),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_swprintf,(SDL_OUT_Z_CAP(b) wchar_t *a, size_t b, SDL_PRINTF_FORMAT_STRING const wchar_t *c, ...),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_sscanf,(const char *a, SDL_SCANF_FORMAT_STRING const char *b, ...),(a,b),return)
#endif
@@ -930,3 +931,4 @@ SDL_DYNAPI_PROC(float,SDL_GetDisplayContentScale,(SDL_DisplayID a),(a),return)
SDL_DYNAPI_PROC(float,SDL_GetWindowDisplayScale,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(float,SDL_GetWindowPixelDensity,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_vswprintf,(wchar_t *a, size_t b, const wchar_t *c, va_list d),(a,b,c,d),return)

View File

@@ -144,6 +144,7 @@ def main():
func = func.replace("SDL_PRINTF_VARARG_FUNC(1)", "");
func = func.replace("SDL_PRINTF_VARARG_FUNC(2)", "");
func = func.replace("SDL_PRINTF_VARARG_FUNC(3)", "");
func = func.replace("SDL_WPRINTF_VARARG_FUNC(3)", "");
func = func.replace("SDL_SCANF_VARARG_FUNC(2)", "");
func = func.replace("__attribute__((analyzer_noreturn))", "");
func = func.replace("SDL_MALLOC", "");

View File

@@ -1466,6 +1466,18 @@ int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FOR
return retval;
}
int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
{
va_list ap;
int retval;
va_start(ap, fmt);
retval = SDL_vswprintf(text, maxlen, fmt, ap);
va_end(ap);
return retval;
}
#if defined(HAVE_LIBC) && defined(__WATCOMC__)
/* _vsnprintf() doesn't ensure nul termination */
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
@@ -1979,6 +1991,49 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *f
#undef TEXT_AND_LEN_ARGS
#endif /* HAVE_VSNPRINTF */
int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap)
{
char *text_utf8 = NULL, *fmt_utf8 = NULL;
int retval;
if (fmt) {
fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
if (!fmt_utf8) {
return -1;
}
}
if (!maxlen) {
/* We still need to generate the text to find the final text length */
maxlen = 1024;
}
text_utf8 = (char *)SDL_malloc(maxlen * 4);
if (!text_utf8) {
SDL_free(fmt_utf8);
return -1;
}
retval = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
if (retval >= 0) {
wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1);
if (text_wchar) {
if (text) {
SDL_wcslcpy(text, text_wchar, maxlen);
}
retval = (int)SDL_wcslen(text_wchar);
SDL_free(text_wchar);
} else {
retval = -1;
}
}
SDL_free(text_utf8);
SDL_free(fmt_utf8);
return retval;
}
int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
{
va_list ap;