fixed atcprintf counting the null terminator for the return value
authorBorja Ferrer <faluco@alliedmods.net>
Sun Dec 31 03:02:40 2006 +0000 (2006-12-31)
changeset 236123045747879
parent 235 3bf937e72b4d
child 237 b2210331d415
fixed atcprintf counting the null terminator for the return value
added format and formatex natives
core/sm_stringutil.cpp
core/smn_string.cpp
     1.1 --- a/core/sm_stringutil.cpp	Sat Dec 30 22:16:53 2006 +0000
     1.2 +++ b/core/sm_stringutil.cpp	Sun Dec 31 03:02:40 2006 +0000
     1.3 @@ -423,7 +423,7 @@
     1.4  done:
     1.5  	*buf_p = '\0';
     1.6  	*param = arg;
     1.7 -	return (maxlen - llen);
     1.8 +	return (maxlen - llen - 1);
     1.9  }
    1.10  
    1.11  const char *stristr(const char *str, const char *substr)
     2.1 --- a/core/smn_string.cpp	Sat Dec 30 22:16:53 2006 +0000
     2.2 +++ b/core/smn_string.cpp	Sun Dec 31 03:02:40 2006 +0000
     2.3 @@ -106,6 +106,55 @@
     2.4  	return snprintf(str, params[3], "%f", sp_ctof(params[1]));
     2.5  }
     2.6  
     2.7 +static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
     2.8 +{
     2.9 +	char *buf, *fmt;
    2.10 +	size_t res;
    2.11 +	int arg = 4;
    2.12 +
    2.13 +	pCtx->LocalToString(params[1], &buf);
    2.14 +	pCtx->LocalToString(params[3], &fmt);
    2.15 +	res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
    2.16 +
    2.17 +	return static_cast<cell_t>(res);
    2.18 +}
    2.19 +
    2.20 +static char g_formatbuf[2048];
    2.21 +static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
    2.22 +{
    2.23 +	char *buf, *fmt, *destbuf;
    2.24 +	cell_t start_addr, end_addr, maxparam;
    2.25 +	size_t res, maxlen;
    2.26 +	int arg = 4;
    2.27 +	bool copy = false;
    2.28 +
    2.29 +	pCtx->LocalToString(params[1], &destbuf);
    2.30 +	pCtx->LocalToString(params[3], &fmt);
    2.31 +
    2.32 +	maxlen = static_cast<size_t>(params[2]);
    2.33 +	start_addr = params[1];
    2.34 +	end_addr = params[1] + maxlen;
    2.35 +	maxparam = params[0];
    2.36 +
    2.37 +	for (cell_t i=3; i<=maxparam; i++)
    2.38 +	{
    2.39 +		if ((params[i] >= start_addr) && (params[i] <= end_addr))
    2.40 +		{
    2.41 +			copy = true;
    2.42 +			break;
    2.43 +		}
    2.44 +	}
    2.45 +	buf = (copy) ? g_formatbuf : destbuf;
    2.46 +	res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
    2.47 +
    2.48 +	if (copy)
    2.49 +	{
    2.50 +		memcpy(destbuf, g_formatbuf, res+1);
    2.51 +	}
    2.52 +
    2.53 +	return static_cast<cell_t>(res);
    2.54 +}
    2.55 +
    2.56  REGISTER_NATIVES(basicstrings)
    2.57  {
    2.58  	{"strlen",				sm_strlen},