Introduce {v,}asprintf for windows

We have now again two functions called h_platform_vasprintf and
h_platform_asprintf. On windows they are implemented in terms of vsnprint
and the like. On BSD/GNU libraries we use the supplied vasprintf and
asprintf.
This commit is contained in:
Nicolas Léveillé 2015-08-16 16:46:52 +02:00
parent 124b4c381b
commit 9557448ae6
5 changed files with 90 additions and 14 deletions

View file

@ -1,7 +1,46 @@
#include "platform.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include <windows.h>
int h_platform_asprintf(char**strp, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int res = h_platform_vasprintf(strp, fmt, ap);
va_end(ap);
return res;
}
int h_platform_vasprintf(char**strp, const char *fmt, va_list args)
{
va_list ap;
va_copy(ap, args);
int non_null_char_count = _vscprintf(fmt, ap);
va_end(ap);
if (non_null_char_count < 0) {
return -1;
}
size_t buffer_size = 1 + non_null_char_count;
char* buffer = malloc(buffer_size);
va_copy(ap, args);
int res = vsnprintf_s(buffer, buffer_size, non_null_char_count, fmt, ap);
if (res < 0) {
free(buffer);
} else {
buffer[non_null_char_count] = 0;
*strp = buffer;
}
va_end(ap);
return res;
}
void h_platform_errx(int err, const char* format, ...) {
// FIXME(windows) TODO(uucidl): to be implemented