mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-11 19:54:03 +00:00
COMMON: Implement two simple workaround wrappers for _vsnprintf and _snprintf for MSVC.
This should assure vsnprintf and snprintf will now also always null terminate the result even for MSVC. Currently the functions are placed in scummsys.h, but that causes us to include two standard C library headers there (for MSVC at least). This is not particulary nice, so we should think of a better solution here.
This commit is contained in:
parent
c0d70b6dbb
commit
d2a8e8023e
@ -40,26 +40,42 @@
|
||||
#if defined(WIN32)
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// vnsprintf was introduced with Visual Studio 2008. The 2003 edition
|
||||
// only included a function called _vsnprintf. We do not officially
|
||||
// support MSVC 2003 anymore, but it should not hurt to still have
|
||||
// this around here.
|
||||
#if (_MSC_VER < 1500)
|
||||
#define vsnprintf _vsnprintf
|
||||
#endif
|
||||
|
||||
// FIXME: The placement of the workaround functions for MSVC below
|
||||
// require us to include stdio.h and stdarg.h for MSVC here. This
|
||||
// is not exactly nice...
|
||||
// We should think of a better way of doing this.
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// MSVC's vsnprintf is either non-existant (2003) or bugged since it
|
||||
// does not always include a terminating NULL (2005+). To work around
|
||||
// that we fix up the _vsnprintf included. Note that the return value
|
||||
// will still not match C99's specs!
|
||||
inline int vsnprintf_msvc(char *str, size_t size, const char *format, va_list args) {
|
||||
// We do not pass size - 1 here, to ensure we would get the same
|
||||
// return value as when we would use _vsnprintf directly, since
|
||||
// for example Common::String::format relies on this.
|
||||
int retValue = _vsnprintf(str, size, format, args);
|
||||
str[size - 1] = 0;
|
||||
return retValue;
|
||||
}
|
||||
|
||||
#define vsnprintf vsnprintf_msvc
|
||||
|
||||
// Visual Studio does not include snprintf in its standard C library.
|
||||
// Instead it includes a function called _snprintf with somewhat
|
||||
// similar semantics. The minor difference is that the return value in
|
||||
// case the formatted string exceeds the buffer size is different.
|
||||
// A much more dangerous one is that _snprintf does not always include
|
||||
// a terminating null (Whoops!).
|
||||
//
|
||||
// FIXME: Provide a proper snprintf function for MSVC. It should at
|
||||
// least always include a terminating null!
|
||||
//
|
||||
// See here for more details:
|
||||
// http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=VS.100%29.aspx
|
||||
#define snprintf _snprintf
|
||||
// a terminating null (Whoops!). Instead we map to our fixed vsnprintf.
|
||||
inline int snprintf(char *str, size_t size, const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int len = vsnprintf(str, size, format, args);
|
||||
va_end(args);
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32_WCE)
|
||||
|
Loading…
Reference in New Issue
Block a user