1 #include "sm_platform.h"
4 #include "sm_globals.h"
5 #include "sm_stringutil.h"
7 using namespace SourcePawn;
9 inline const char *_strstr(const char *str, const char *substr)
11 #ifdef PLATFORM_WINDOWS
12 return strstr(str, substr);
14 return (const char *)strstr(str, substr);
18 /*********************************************
20 * STRING MANIPULATION NATIVE IMPLEMENTATIONS *
22 *********************************************/
24 static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
27 pCtx->LocalToString(params[1], &str);
32 static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
34 typedef const char *(*STRSEARCH)(const char *, const char *);
38 pCtx->LocalToString(params[1], &str);
39 pCtx->LocalToString(params[2], &substr);
41 func = (params[3]) ? _strstr : stristr;
42 const char *pos = func(str, substr);
51 static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
53 typedef int (*STRCOMPARE)(const char *, const char *);
57 pCtx->LocalToString(params[1], &str1);
58 pCtx->LocalToString(params[2], &str2);
60 func = (params[3]) ? strcmp : stricmp;
62 return (func(str1, str2));
65 static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
69 pCtx->LocalToString(params[1], &dest);
70 pCtx->LocalToString(params[3], &src);
72 return strncopy(dest, src, params[2]);
75 static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
78 pCtx->LocalToString(params[1], &str);
80 return static_cast<cell_t>(strtol(str, &dummy, params[2]));
83 static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
86 pCtx->LocalToString(params[2], &str);
87 size_t res = UTIL_Format(str, params[3], "%d", params[1]);
89 return static_cast<cell_t>(res);
92 static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
95 pCtx->LocalToString(params[1], &str);
97 float val = (float)strtod(str, &dummy);
102 static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
105 pCtx->LocalToString(params[2], &str);
106 size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
108 return static_cast<cell_t>(res);
111 static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
117 pCtx->LocalToString(params[1], &buf);
118 pCtx->LocalToString(params[3], &fmt);
119 res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
121 return static_cast<cell_t>(res);
124 static char g_formatbuf[2048];
125 static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
127 char *buf, *fmt, *destbuf;
128 cell_t start_addr, end_addr, maxparam;
133 pCtx->LocalToString(params[1], &destbuf);
134 pCtx->LocalToString(params[3], &fmt);
136 maxlen = static_cast<size_t>(params[2]);
137 start_addr = params[1];
138 end_addr = params[1] + maxlen;
139 maxparam = params[0];
141 for (cell_t i=3; i<=maxparam; i++)
143 if ((params[i] >= start_addr) && (params[i] <= end_addr))
149 buf = (copy) ? g_formatbuf : destbuf;
150 res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
154 memcpy(destbuf, g_formatbuf, res+1);
157 return static_cast<cell_t>(res);
160 REGISTER_NATIVES(basicstrings)
162 {"strlen", sm_strlen},
163 {"StrContains", sm_contain},
164 {"StrCompare", sm_strcmp},
165 {"StrCopy", sm_strcopy},
166 {"StringToInt", sm_strconvint},
167 {"IntToString", sm_numtostr},
168 {"StringToFloat", sm_strtofloat},
169 {"FloatToString", sm_floattostr},
170 {"Format", sm_format},
171 {"FormatEx", sm_formatex},