From cf15783eb036e245ebd88b47e96071209779e0f9 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sun, 31 Dec 2006 03:02:40 +0000 Subject: [PATCH] fixed atcprintf counting the null terminator for the return value added format and formatex natives --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40241 --- core/sm_stringutil.cpp | 2 +- core/smn_string.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index 5edf6c3f..25a37ba0 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -423,7 +423,7 @@ reswitch: done: *buf_p = '\0'; *param = arg; - return (maxlen - llen); + return (maxlen - llen - 1); } const char *stristr(const char *str, const char *substr) diff --git a/core/smn_string.cpp b/core/smn_string.cpp index e5dbfd23..168ff923 100644 --- a/core/smn_string.cpp +++ b/core/smn_string.cpp @@ -106,6 +106,55 @@ static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params) return snprintf(str, params[3], "%f", sp_ctof(params[1])); } +static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params) +{ + char *buf, *fmt; + size_t res; + int arg = 4; + + pCtx->LocalToString(params[1], &buf); + pCtx->LocalToString(params[3], &fmt); + res = atcprintf(buf, static_cast(params[2]), fmt, pCtx, params, &arg); + + return static_cast(res); +} + +static char g_formatbuf[2048]; +static cell_t sm_format(IPluginContext *pCtx, const cell_t *params) +{ + char *buf, *fmt, *destbuf; + cell_t start_addr, end_addr, maxparam; + size_t res, maxlen; + int arg = 4; + bool copy = false; + + pCtx->LocalToString(params[1], &destbuf); + pCtx->LocalToString(params[3], &fmt); + + maxlen = static_cast(params[2]); + start_addr = params[1]; + end_addr = params[1] + maxlen; + maxparam = params[0]; + + for (cell_t i=3; i<=maxparam; i++) + { + if ((params[i] >= start_addr) && (params[i] <= end_addr)) + { + copy = true; + break; + } + } + buf = (copy) ? g_formatbuf : destbuf; + res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg); + + if (copy) + { + memcpy(destbuf, g_formatbuf, res+1); + } + + return static_cast(res); +} + REGISTER_NATIVES(basicstrings) { {"strlen", sm_strlen},