From cf11d2ec9931d745a573d1228404c7213fd883da Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 9 Jul 2017 17:46:53 -0400 Subject: [PATCH] Workaround snprintf() deficiencies on Visual Studio. --- src/physfs.c | 28 ++++++++++++++++++++++++++++ src/physfs_internal.h | 9 +++++++++ 2 files changed, 37 insertions(+) diff --git a/src/physfs.c b/src/physfs.c index 5ca0956..67fea2d 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -2983,5 +2983,33 @@ static void setDefaultAllocator(void) } /* if */ } /* setDefaultAllocator */ + +#if defined(_MSC_VER) +/* this code came from https://stackoverflow.com/a/8712996 */ +int __PHYSFS_msvc_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) +{ + int count = -1; + + if (size != 0) + count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap); + if (count == -1) + count = _vscprintf(format, ap); + + return count; +} + +int __PHYSFS_msvc_snprintf(char *outBuf, size_t size, const char *format, ...) +{ + int count; + va_list ap; + + va_start(ap, format); + count = c99_vsnprintf(outBuf, size, format, ap); + va_end(ap); + + return count; +} +#endif + /* end of physfs.c ... */ diff --git a/src/physfs_internal.h b/src/physfs_internal.h index 36a6bcb..c0c65a8 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -57,6 +57,15 @@ extern "C" { #define _FILE_OFFSET_BITS 64 #endif +/* a real C99-compliant snprintf() is in Visual Studio 2015, + but just use this everywhere for binary compatibility. */ +#if defined(_MSC_VER) +int __PHYSFS_msvc_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap); +int __PHYSFS_msvc_snprintf(char *outBuf, size_t size, const char *format, ...); +#define vsnprintf __PHYSFS_msvc_vsnprintf +#define snprintf __PHYSFS_msvc_snprintf +#endif + /* * Interface for small allocations. If you need a little scratch space for * a throwaway buffer or string, use this. It will make small allocations