mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add a MSVC / Windows compatible snprintf function (#7353)
Use sizeof where applicable for mt_snprintf
This commit is contained in:
parent
9855651c06
commit
9537cfd3f8
13 changed files with 54 additions and 18 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue