2006-12-23 03:20:53 +01:00
|
|
|
#include "sm_platform.h"
|
|
|
|
#include <string.h>
|
2006-12-30 08:23:17 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "sm_globals.h"
|
2006-12-30 00:28:44 +01:00
|
|
|
#include "sm_stringutil.h"
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
using namespace SourcePawn;
|
|
|
|
|
|
|
|
inline const char *_strstr(const char *str, const char *substr)
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
return strstr(str, substr);
|
|
|
|
#elif PLATFORM_LINUX
|
|
|
|
return (const char *)strstr(str, substr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************
|
|
|
|
* *
|
|
|
|
* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
|
|
|
|
* *
|
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
pCtx->LocalToString(params[1], &str);
|
|
|
|
|
|
|
|
return strlen(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
typedef const char *(*STRSEARCH)(const char *, const char *);
|
|
|
|
STRSEARCH func;
|
|
|
|
char *str, *substr;
|
|
|
|
|
|
|
|
pCtx->LocalToString(params[1], &str);
|
|
|
|
pCtx->LocalToString(params[2], &substr);
|
|
|
|
|
|
|
|
func = (params[3]) ? _strstr : stristr;
|
|
|
|
const char *pos = func(str, substr);
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
return (pos - str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
|
2006-12-23 03:20:53 +01:00
|
|
|
{
|
|
|
|
typedef int (*STRCOMPARE)(const char *, const char *);
|
|
|
|
STRCOMPARE func;
|
|
|
|
char *str1, *str2;
|
|
|
|
|
|
|
|
pCtx->LocalToString(params[1], &str1);
|
|
|
|
pCtx->LocalToString(params[2], &str2);
|
|
|
|
|
|
|
|
func = (params[3]) ? strcmp : stricmp;
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return (func(str1, str2));
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 00:28:44 +01:00
|
|
|
char *dest, *src;
|
2006-12-23 03:20:53 +01:00
|
|
|
|
|
|
|
pCtx->LocalToString(params[1], &dest);
|
|
|
|
pCtx->LocalToString(params[3], &src);
|
|
|
|
|
2006-12-30 00:28:44 +01:00
|
|
|
return strncopy(dest, src, params[2]);
|
2006-12-23 03:20:53 +01:00
|
|
|
}
|
2006-12-28 01:48:09 +01:00
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
|
2006-12-28 01:48:09 +01:00
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
char *str, *dummy;
|
2006-12-28 01:48:09 +01:00
|
|
|
pCtx->LocalToString(params[1], &str);
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
return static_cast<cell_t>(strtol(str, &dummy, params[2]));
|
2006-12-28 01:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
pCtx->LocalToString(params[2], &str);
|
2007-01-07 02:30:28 +01:00
|
|
|
size_t res = UTIL_Format(str, params[3], "%d", params[1]);
|
2006-12-28 01:48:09 +01:00
|
|
|
|
2007-01-07 02:30:28 +01:00
|
|
|
return static_cast<cell_t>(res);
|
2006-12-28 01:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
2006-12-30 08:23:17 +01:00
|
|
|
char *str, *dummy;
|
2006-12-28 01:48:09 +01:00
|
|
|
pCtx->LocalToString(params[1], &str);
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
float val = (float)strtod(str, &dummy);
|
|
|
|
|
|
|
|
return sp_ftoc(val);
|
2006-12-28 01:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
pCtx->LocalToString(params[2], &str);
|
2007-01-07 02:30:28 +01:00
|
|
|
size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
|
2006-12-28 01:48:09 +01:00
|
|
|
|
2007-01-07 02:30:28 +01:00
|
|
|
return static_cast<cell_t>(res);
|
2006-12-28 01:48:09 +01:00
|
|
|
}
|
2006-12-30 08:23:17 +01:00
|
|
|
|
2006-12-31 04:02:40 +01:00
|
|
|
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<size_t>(params[2]), fmt, pCtx, params, &arg);
|
|
|
|
|
|
|
|
return static_cast<cell_t>(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<size_t>(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<cell_t>(res);
|
|
|
|
}
|
|
|
|
|
2006-12-30 08:23:17 +01:00
|
|
|
REGISTER_NATIVES(basicstrings)
|
|
|
|
{
|
|
|
|
{"strlen", sm_strlen},
|
|
|
|
{"StrContains", sm_contain},
|
|
|
|
{"StrCompare", sm_strcmp},
|
|
|
|
{"StrCopy", sm_strcopy},
|
|
|
|
{"StringToInt", sm_strconvint},
|
|
|
|
{"IntToString", sm_numtostr},
|
|
|
|
{"StringToFloat", sm_strtofloat},
|
|
|
|
{"FloatToString", sm_floattostr},
|
2007-01-01 22:18:56 +01:00
|
|
|
{"Format", sm_format},
|
|
|
|
{"FormatEx", sm_formatex},
|
2006-12-30 08:23:17 +01:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|