diff options
author | nOOb3167 <nOOb3167@gmail.com> | 2018-07-22 21:56:06 +0200 |
---|---|---|
committer | SmallJoker <SmallJoker@users.noreply.github.com> | 2018-07-22 21:56:06 +0200 |
commit | 9537cfd3f8264700619f58d15741829489e1099e (patch) | |
tree | 36412677aba19e1a2037db7fad2f934a2daa5046 /src/porting.cpp | |
parent | 9855651c06f84f29780cf6b25a392ac841b4630c (diff) | |
download | minetest-9537cfd3f8264700619f58d15741829489e1099e.tar.gz minetest-9537cfd3f8264700619f58d15741829489e1099e.tar.bz2 minetest-9537cfd3f8264700619f58d15741829489e1099e.zip |
Add a MSVC / Windows compatible snprintf function (#7353)
Use sizeof where applicable for mt_snprintf
Diffstat (limited to 'src/porting.cpp')
-rw-r--r-- | src/porting.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/porting.cpp b/src/porting.cpp index 240ee0af0..302e6989a 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -50,6 +50,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "settings.h" #include <list> +#include <cstdarg> +#include <cstdio> namespace porting { @@ -661,6 +663,28 @@ void attachOrCreateConsole() #endif } +int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...) +{ + // https://msdn.microsoft.com/en-us/library/bt7tawza.aspx + // Many of the MSVC / Windows printf-style functions do not support positional + // arguments (eg. "%1$s"). We just forward the call to vsnprintf for sane + // platforms, but defer to _vsprintf_p on MSVC / Windows. + // https://github.com/FFmpeg/FFmpeg/blob/5ae9fa13f5ac640bec113120d540f70971aa635d/compat/msvcrt/snprintf.c#L46 + // _vsprintf_p has to be shimmed with _vscprintf_p on -1 (for an example see + // above FFmpeg link). + va_list args; + va_start(args, fmt); +#ifndef _MSC_VER + int c = vsnprintf(buf, buf_size, fmt, args); +#else // _MSC_VER + int c = _vsprintf_p(buf, buf_size, fmt, args); + if (c == -1) + c = _vscprintf_p(fmt, args); +#endif // _MSC_VER + va_end(args); + return c; +} + // Load performance counter frequency only once at startup #ifdef _WIN32 |