Skip to content

Commit

Permalink
Workaround snprintf() deficiencies on Visual Studio.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 9, 2017
1 parent c057f2d commit cf11d2e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/physfs.c
Expand Up @@ -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 ... */

9 changes: 9 additions & 0 deletions src/physfs_internal.h
Expand Up @@ -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
Expand Down

0 comments on commit cf11d2e

Please sign in to comment.