diff --git a/lib/framework/printf_ext.c b/lib/framework/printf_ext.c index 1fae80c94..c662ae12c 100644 --- a/lib/framework/printf_ext.c +++ b/lib/framework/printf_ext.c @@ -22,6 +22,52 @@ #include "printf_ext.h" #include +#if defined(WZ_OS_WIN) +int vasprintf(char** strp, const char* format, va_list ap) +{ + int count; + + // Find out how long the resulting string is + count = _vscprintf(format, ap); + + if (count == 0) + { + return strdup(""); + } + else if (count < 0) + { + // Something went wrong, so return the error code (probably still requires checking of "errno" though) + return count; + } + + assert(strp != NULL); + + // Allocate memory for our string + *strp = malloc(count + 1); + if (*strp == NULL) + { + debug(LOG_ERROR, "vasprintf: Out of memory!"); + abort(); + return -1; + } + + // Do the actual printing into our newly created string + return vsprintf(*strp, format, ap); +} + +int asprintf(char** strp, const char* format, ...) +{ + va_list ap; + int count; + + va_start(ap, format); + count = vasprintf(strp, format, ap); + va_end(ap); + + return count; +} +#endif + #if defined(WZ_CC_MSVC) int vsnprintf(char* str, size_t size, const char* format, va_list ap) { diff --git a/lib/framework/printf_ext.h b/lib/framework/printf_ext.h index 6c487c519..002074c10 100644 --- a/lib/framework/printf_ext.h +++ b/lib/framework/printf_ext.h @@ -23,9 +23,14 @@ #include "frame.h" +#if defined(WZ_OS_WIN) +// These functions are GNU extensions; so make sure they are available on Windows also +extern int vasprintf(char** strp, const char* format, va_list ap); +extern int asprintf(char** strp, const char* format, ...); +#endif + #if defined(WZ_CC_MSVC) // Make sure that these functions are available, and work according to the C99 spec on MSVC also - extern int vsnprintf(char* str, size_t size, const char* format, va_list ap); extern int snprintf(char* str, size_t size, const char* format, ...); #endif