Workaround snprintf() deficiencies on Visual Studio.

This commit is contained in:
Ryan C. Gordon 2017-07-09 17:46:53 -04:00
parent c057f2d7dd
commit cf11d2ec99
2 changed files with 37 additions and 0 deletions

View File

@ -2983,5 +2983,33 @@ static void setDefaultAllocator(void)
} /* if */ } /* if */
} /* setDefaultAllocator */ } /* 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 ... */ /* end of physfs.c ... */

View File

@ -57,6 +57,15 @@ extern "C" {
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#endif #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 * Interface for small allocations. If you need a little scratch space for
* a throwaway buffer or string, use this. It will make small allocations * a throwaway buffer or string, use this. It will make small allocations