Skip to content

Commit

Permalink
COMMON: add a better workaround for {,v}snprintf
Browse files Browse the repository at this point in the history
Since Microsoft's functions are not C99 compliant, we use a
workaround by Valentin Milea found here:
http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
  • Loading branch information
darkstar authored and DrMcCoy committed Jun 7, 2013
1 parent c7d3fb5 commit c87f22c
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/common/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@

#if defined(_MSC_VER)

#define snprintf _snprintf
#define vsnprintf _vsnprintf
#include <cstdarg>
#include <cstdio>

#define snprintf c99_snprintf
#define vsnprintf c99_vsnprintf

#define XOREOS_LITTLE_ENDIAN

Expand All @@ -51,6 +54,30 @@
#define NORETURN_PRE __declspec(noreturn)
#define PLUGIN_EXPORT __declspec(dllexport)

static FORCEINLINE int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
{
int count = -1;

if (size != 0)
count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);

return count;
}

static FORCEINLINE int c99_snprintf(char* str, size_t size, const char* format, ...)
{
int count;
va_list ap;

va_start(ap, format);
count = c99_vsnprintf(str, size, format, ap);
va_end(ap);

return count;
}

#ifndef WIN32
#define WIN32
#endif
Expand Down

0 comments on commit c87f22c

Please sign in to comment.