2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_platform.h"
17 #include "sm_globals.h"
18 #include "sm_stringutil.h"
20 using namespace SourcePawn;
22 inline const char *_strstr(const char *str, const char *substr)
24 #ifdef PLATFORM_WINDOWS
25 return strstr(str, substr);
26 #elif defined PLATFORM_LINUX
27 return (const char *)strstr(str, substr);
31 /*********************************************
33 * STRING MANIPULATION NATIVE IMPLEMENTATIONS *
35 *********************************************/
37 static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
40 pCtx->LocalToString(params[1], &str);
45 static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
47 typedef const char *(*STRSEARCH)(const char *, const char *);
51 pCtx->LocalToString(params[1], &str);
52 pCtx->LocalToString(params[2], &substr);
54 func = (params[3]) ? _strstr : stristr;
55 const char *pos = func(str, substr);
64 static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
66 typedef int (*STRCOMPARE)(const char *, const char *);
70 pCtx->LocalToString(params[1], &str1);
71 pCtx->LocalToString(params[2], &str2);
73 func = (params[3]) ? strcmp : stricmp;
75 return (func(str1, str2));
78 static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
82 pCtx->LocalToString(params[1], &dest);
83 pCtx->LocalToString(params[3], &src);
85 return strncopy(dest, src, params[2]);
88 static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
91 pCtx->LocalToString(params[1], &str);
93 return static_cast<cell_t>(strtol(str, &dummy, params[2]));
96 static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
99 pCtx->LocalToString(params[2], &str);
100 size_t res = UTIL_Format(str, params[3], "%d", params[1]);
102 return static_cast<cell_t>(res);
105 static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
108 pCtx->LocalToString(params[1], &str);
110 float val = (float)strtod(str, &dummy);
115 static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
118 pCtx->LocalToString(params[2], &str);
119 size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
121 return static_cast<cell_t>(res);
124 static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
130 pCtx->LocalToString(params[1], &buf);
131 pCtx->LocalToString(params[3], &fmt);
132 res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
134 return static_cast<cell_t>(res);
137 static char g_formatbuf[2048];
138 static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
140 char *buf, *fmt, *destbuf;
141 cell_t start_addr, end_addr, maxparam;
146 pCtx->LocalToString(params[1], &destbuf);
147 pCtx->LocalToString(params[3], &fmt);
149 maxlen = static_cast<size_t>(params[2]);
150 start_addr = params[1];
151 end_addr = params[1] + maxlen;
152 maxparam = params[0];
154 for (cell_t i=3; i<=maxparam; i++)
156 if ((params[i] >= start_addr) && (params[i] <= end_addr))
162 buf = (copy) ? g_formatbuf : destbuf;
163 res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
167 memcpy(destbuf, g_formatbuf, res+1);
170 return static_cast<cell_t>(res);
173 REGISTER_NATIVES(basicstrings)
175 {"strlen", sm_strlen},
176 {"StrContains", sm_contain},
177 {"StrCompare", sm_strcmp},
178 {"StrCopy", sm_strcopy},
179 {"StringToInt", sm_strconvint},
180 {"IntToString", sm_numtostr},
181 {"StringToFloat", sm_strtofloat},
182 {"FloatToString", sm_floattostr},
183 {"Format", sm_format},
184 {"FormatEx", sm_formatex},