* Add implementations of vasprintf and asprintf for Windows (GNU extensions to the C library that malloc the memory they need)
git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2804 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
7711572f0a
commit
6486b5270b
|
@ -22,6 +22,52 @@
|
|||
#include "printf_ext.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#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)
|
||||
{
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue