Optimized forwards, now paused functions are stored in a temp list so we dont have to check if they're runnable on each function execution.
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 static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
175 int vargPos = static_cast<int>(params[4]);
177 /* Get the parent parameter array */
178 sp_context_t *ctx = pContext->GetContext();
179 cell_t *local_params = (cell_t *)(ctx->memory + ctx->frm + (2 * sizeof(cell_t)));
181 cell_t max = local_params[0];
182 if (vargPos > (int)max + 1)
184 return pContext->ThrowNativeError("Argument index is invalid: %d", vargPos);
187 cell_t addr_start = params[1];
188 cell_t addr_end = addr_start + params[2];
190 for (int i=vargPos; i<=max; i++)
192 /* Does this clip bounds? */
193 if ((local_params[i] >= addr_start)
194 && (local_params[i] <= addr_end))
201 /* Get destination info */
202 char *format, *destination;
203 size_t maxlen = static_cast<size_t>(params[2]);
207 destination = g_formatbuf;
209 pContext->LocalToString(params[1], &destination);
212 pContext->LocalToString(params[3], &format);
214 size_t total = atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
216 /* Perform copy-on-write if we need to */
219 pContext->StringToLocal(params[1], maxlen, g_formatbuf);
225 REGISTER_NATIVES(basicstrings)
227 {"strlen", sm_strlen},
228 {"StrContains", sm_contain},
229 {"StrCompare", sm_strcmp},
230 {"StrCopy", sm_strcopy},
231 {"StringToInt", sm_strconvint},
232 {"IntToString", sm_numtostr},
233 {"StringToFloat", sm_strtofloat},
234 {"FloatToString", sm_floattostr},
235 {"Format", sm_format},
236 {"FormatEx", sm_formatex},
237 {"VFormat", sm_vformat},