#include "nodedef.h" // Needed for determining pointing to nodes
#include "nodemetadata.h"
#include "particles.h"
+#include "porting.h"
#include "profiler.h"
#include "quicktune_shortcutter.h"
#include "raycast.h"
char control_text_buf[600];
- snprintf(control_text_buf, ARRLEN(control_text_buf), control_text_template.c_str(),
+ porting::mt_snprintf(control_text_buf, sizeof(control_text_buf), control_text_template.c_str(),
GET_KEY_NAME(keymap_forward),
GET_KEY_NAME(keymap_backward),
GET_KEY_NAME(keymap_left),
#include <IGUIStaticText.h>
#include <IGUIFont.h>
#include "intlGUIEditBox.h"
+#include "porting.h"
#include "gettext.h"
"Join to confirm account creation or click Cancel to "
"abort.");
char info_text_buf[1024];
- snprintf(info_text_buf, sizeof(info_text_buf), info_text_template.c_str(),
- address.c_str(), m_playername.c_str());
+ porting::mt_snprintf(info_text_buf, sizeof(info_text_buf),
+ info_text_template.c_str(), address.c_str(),
+ m_playername.c_str());
wchar_t *info_text_buf_wide = utf8_to_wide_c(info_text_buf);
gui::IGUIEditBox *e = new gui::intlGUIEditBox(info_text_buf_wide, true,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "porting.h"
#include "profilergraph.h"
#include "util/string.h"
s32 texth = 15;
char buf[10];
- snprintf(buf, 10, "%.3g", show_max);
+ porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - graphh, textx2,
y - graphh + texth),
meta.color);
- snprintf(buf, 10, "%.3g", show_min);
+ porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
font->draw(utf8_to_wide(buf).c_str(),
core::rect<s32>(textx, y - texth, textx2, y), meta.color);
font->draw(utf8_to_wide(id).c_str(),
#include "guiscalingfilter.h"
#include "imagefilters.h"
+#include "porting.h"
#include "settings.h"
#include "util/numeric.h"
#include <cstdio>
// Calculate scaled texture name.
char rectstr[200];
- snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d",
+ porting::mt_snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d",
srcrect.UpperLeftCorner.X,
srcrect.UpperLeftCorner.Y,
srcrect.getWidth(),
switch(layout)
{
case 1:
- snprintf(cc, 9, "%.4x%.4x",
+ porting::mt_snprintf(cc, sizeof(cc), "%.4x%.4x",
(unsigned int) pos.X & 0xffff,
(unsigned int) pos.Y & 0xffff);
return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
case 2:
- snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
+ porting::mt_snprintf(cc, sizeof(cc), (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
(unsigned int) pos.X & 0xfff,
(unsigned int) pos.Y & 0xfff);
std::string ServerMap::getBlockFilename(v3s16 p)
{
char cc[5];
- snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
+ porting::mt_snprintf(cc, sizeof(cc), "%.4x", (unsigned int)p.Y&0xffff);
return cc;
}
#ifndef SERVER
#include "mapblock_mesh.h"
#endif
+#include "porting.h"
#include "util/string.h"
#include "util/serialize.h"
#include "util/basic_macros.h"
v3s16 p = block->getPos();
char spos[25];
- snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
+ porting::mt_snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
desc<<spos;
switch(block->getModified())
#include "util/string.h"
#include "settings.h"
#include <list>
+#include <cstdarg>
+#include <cstdio>
namespace porting
{
#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
// This attaches to the parents process console, or creates a new one if it doesnt exist.
void attachOrCreateConsole();
+
+int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
} // namespace porting
#ifdef __ANDROID__
#include "common/c_internal.h"
#include "debug.h"
#include "log.h"
+#include "porting.h"
#include "settings.h"
std::string script_get_backtrace(lua_State *L)
err_descr = "<no description>";
char buf[256];
- snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
+ porting::mt_snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
err_type, mod, fxn);
std::string err_msg(buf);
break;
case LUA_TNUMBER: /* numbers */ {
char buf[10];
- snprintf(buf, 10, "%lf", lua_tonumber(m_luastack, i));
+ porting::mt_snprintf(buf, sizeof(buf), "%lf", lua_tonumber(m_luastack, i));
o << buf;
break;
}
#include "nodemetadata.h"
#include "gamedef.h"
#include "map.h"
+#include "porting.h"
#include "profiler.h"
#include "raycast.h"
#include "remoteplayer.h"
int i = i0 + di;
char buf[4];
if(di<thislinelength)
- snprintf(buf, 4, "%.2x ", data[i]);
+ porting::mt_snprintf(buf, sizeof(buf), "%.2x ", data[i]);
else
- snprintf(buf, 4, " ");
+ porting::mt_snprintf(buf, sizeof(buf), " ");
o<<buf;
}
o<<" ";
#include "modchannels.h"
#include "content/mods.h"
#include "util/numeric.h"
+#include "porting.h"
content_t t_CONTENT_STONE;
content_t t_CONTENT_GRASS;
return m_test_dir;
char buf[32];
- snprintf(buf, sizeof(buf), "%08X", myrand());
+ porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
if (!fs::CreateDir(m_test_dir))
std::string TestBase::getTestTempFile()
{
char buf[32];
- snprintf(buf, sizeof(buf), "%08X", myrand());
+ porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());
return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
}
#include "test.h"
#include "log.h"
+#include "porting.h"
#include "settings.h"
#include "util/serialize.h"
#include "network/connection.h"
if (i % 2 == 0)
infostream << " ";
char buf[10];
- snprintf(buf, 10, "%.2X",
+ porting::mt_snprintf(buf, sizeof(buf), "%.2X",
((int)((const char *)pkt.getU8Ptr(0))[i]) & 0xff);
infostream<<buf;
}
if (i % 2 == 0)
infostream << " ";
char buf[10];
- snprintf(buf, 10, "%.2X", ((int)(recvdata[i])) & 0xff);
+ porting::mt_snprintf(buf, sizeof(buf), "%.2X", ((int)(recvdata[i])) & 0xff);
infostream << buf;
}
if (size > 20)