Add SDL_asprintf and SDL_vasprintf

This commit is contained in:
Cameron Cawley 2021-09-14 20:37:35 +01:00 committed by Sam Lantinga
parent 79b0aae86c
commit 25a614bc3e
5 changed files with 70 additions and 0 deletions

View File

@ -566,6 +566,8 @@ extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING
extern DECLSPEC int SDLCALL SDL_vsscanf(const char *text, const char *fmt, va_list ap); extern DECLSPEC int SDLCALL SDL_vsscanf(const char *text, const char *fmt, va_list ap);
extern DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... ) SDL_PRINTF_VARARG_FUNC(3); extern DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... ) SDL_PRINTF_VARARG_FUNC(3);
extern DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap); extern DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap);
extern DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
extern DECLSPEC int SDLCALL SDL_vasprintf(char **strp, const char *fmt, va_list ap);
#ifndef HAVE_M_PI #ifndef HAVE_M_PI
#ifndef M_PI #ifndef M_PI

View File

@ -89,6 +89,12 @@ static void SDL_InitDynamicAPI(void);
va_end(ap); \ va_end(ap); \
return retval; \ return retval; \
} \ } \
_static int SDLCALL SDL_asprintf##name(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
int retval; va_list ap; initcall; va_start(ap, fmt); \
retval = jump_table.SDL_vasprintf(strp, fmt, ap); \
va_end(ap); \
return retval; \
} \
_static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \ _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
va_list ap; initcall; va_start(ap, fmt); \ va_list ap; initcall; va_start(ap, fmt); \
jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \ jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \

View File

@ -820,3 +820,5 @@
#define SDL_RenderGeometry SDL_RenderGeometry_REAL #define SDL_RenderGeometry SDL_RenderGeometry_REAL
#define SDL_RenderGeometryRaw SDL_RenderGeometryRaw_REAL #define SDL_RenderGeometryRaw SDL_RenderGeometryRaw_REAL
#define SDL_RenderSetVSync SDL_RenderSetVSync_REAL #define SDL_RenderSetVSync SDL_RenderSetVSync_REAL
#define SDL_asprintf SDL_asprintf_REAL
#define SDL_vasprintf SDL_vasprintf_REAL

View File

@ -885,3 +885,7 @@ SDL_DYNAPI_PROC(void*,SDL_GetTextureUserData,(SDL_Texture *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, const SDL_Vertex *c, int d, const int *e, int f),(a,b,c,d,e,f),return) SDL_DYNAPI_PROC(int,SDL_RenderGeometry,(SDL_Renderer *a, SDL_Texture *b, const SDL_Vertex *c, int d, const int *e, int f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(int,SDL_RenderGeometryRaw,(SDL_Renderer *a, SDL_Texture *b, const float *c, int d, const int *e, int f, const float *g, int h, int i, const void *j, int k, int l),(a,b,c,d,e,f,g,h,i,j,k,l),return) SDL_DYNAPI_PROC(int,SDL_RenderGeometryRaw,(SDL_Renderer *a, SDL_Texture *b, const float *c, int d, const int *e, int f, const float *g, int h, int i, const void *j, int k, int l),(a,b,c,d,e,f,g,h,i,j,k,l),return)
SDL_DYNAPI_PROC(int,SDL_RenderSetVSync,(SDL_Renderer *a, int b),(a,b),return) SDL_DYNAPI_PROC(int,SDL_RenderSetVSync,(SDL_Renderer *a, int b),(a,b),return)
#if !SDL_DYNAPI_PROC_NO_VARARGS
SDL_DYNAPI_PROC(int,SDL_asprintf,(char **a, SDL_PRINTF_FORMAT_STRING const char *b, ...),(a,b),return)
#endif
SDL_DYNAPI_PROC(int,SDL_vasprintf,(char **a, const char *b, va_list c),(a,b,c),return)

View File

@ -1911,4 +1911,60 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt,
#undef TEXT_AND_LEN_ARGS #undef TEXT_AND_LEN_ARGS
#endif /* HAVE_VSNPRINTF */ #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: */ /* vi: set ts=4 sw=4 expandtab: */