1.1 --- a/core/smn_string.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,595 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <string.h>
1.36 -#include <stdlib.h>
1.37 -#include "sm_globals.h"
1.38 -#include "sm_stringutil.h"
1.39 -#include <ITextParsers.h>
1.40 -#include <ctype.h>
1.41 -
1.42 -inline const char *_strstr(const char *str, const char *substr)
1.43 -{
1.44 -#ifdef PLATFORM_WINDOWS
1.45 - return strstr(str, substr);
1.46 -#elif defined PLATFORM_LINUX
1.47 - return (const char *)strstr(str, substr);
1.48 -#endif
1.49 -}
1.50 -
1.51 -/*********************************************
1.52 -* *
1.53 -* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
1.54 -* *
1.55 -*********************************************/
1.56 -
1.57 -static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
1.58 -{
1.59 - char *str;
1.60 - pCtx->LocalToString(params[1], &str);
1.61 -
1.62 - return strlen(str);
1.63 -}
1.64 -
1.65 -static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
1.66 -{
1.67 - typedef const char *(*STRSEARCH)(const char *, const char *);
1.68 - STRSEARCH func;
1.69 - char *str, *substr;
1.70 -
1.71 - pCtx->LocalToString(params[1], &str);
1.72 - pCtx->LocalToString(params[2], &substr);
1.73 -
1.74 - func = (params[3]) ? _strstr : stristr;
1.75 - const char *pos = func(str, substr);
1.76 - if (pos)
1.77 - {
1.78 - return (pos - str);
1.79 - }
1.80 -
1.81 - return -1;
1.82 -}
1.83 -
1.84 -static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
1.85 -{
1.86 - typedef int (*STRCOMPARE)(const char *, const char *);
1.87 - STRCOMPARE func;
1.88 - char *str1, *str2;
1.89 -
1.90 - pCtx->LocalToString(params[1], &str1);
1.91 - pCtx->LocalToString(params[2], &str2);
1.92 -
1.93 - func = (params[3]) ? strcmp : stricmp;
1.94 -
1.95 - return (func(str1, str2));
1.96 -}
1.97 -
1.98 -static cell_t sm_strncmp(IPluginContext *pCtx, const cell_t *params)
1.99 -{
1.100 - char *str1, *str2;
1.101 -
1.102 - pCtx->LocalToString(params[1], &str1);
1.103 - pCtx->LocalToString(params[2], &str2);
1.104 -
1.105 - if (params[4])
1.106 - {
1.107 - return strncmp(str1, str2, (size_t)params[3]);
1.108 - } else {
1.109 - return strncasecmp(str1, str2, (size_t)params[3]);
1.110 - }
1.111 -}
1.112 -
1.113 -static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
1.114 -{
1.115 - char *dest, *src;
1.116 -
1.117 - pCtx->LocalToString(params[1], &dest);
1.118 - pCtx->LocalToString(params[3], &src);
1.119 -
1.120 - return strncopy(dest, src, params[2]);
1.121 -}
1.122 -
1.123 -static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
1.124 -{
1.125 - char *str, *dummy;
1.126 - pCtx->LocalToString(params[1], &str);
1.127 -
1.128 - return static_cast<cell_t>(strtol(str, &dummy, params[2]));
1.129 -}
1.130 -
1.131 -static cell_t StringToIntEx(IPluginContext *pCtx, const cell_t *params)
1.132 -{
1.133 - char *str, *dummy = NULL;
1.134 - cell_t *addr;
1.135 - pCtx->LocalToString(params[1], &str);
1.136 - pCtx->LocalToPhysAddr(params[2], &addr);
1.137 -
1.138 - *addr = static_cast<cell_t>(strtol(str, &dummy, params[3]));
1.139 -
1.140 - return dummy - str;
1.141 -}
1.142 -
1.143 -static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
1.144 -{
1.145 - char *str;
1.146 - pCtx->LocalToString(params[2], &str);
1.147 - size_t res = UTIL_Format(str, params[3], "%d", params[1]);
1.148 -
1.149 - return static_cast<cell_t>(res);
1.150 -}
1.151 -
1.152 -static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
1.153 -{
1.154 - char *str, *dummy;
1.155 - pCtx->LocalToString(params[1], &str);
1.156 -
1.157 - float val = (float)strtod(str, &dummy);
1.158 -
1.159 - return sp_ftoc(val);
1.160 -}
1.161 -
1.162 -static cell_t StringToFloatEx(IPluginContext *pCtx, const cell_t *params)
1.163 -{
1.164 - char *str, *dummy = NULL;
1.165 - cell_t *addr;
1.166 - pCtx->LocalToString(params[1], &str);
1.167 - pCtx->LocalToPhysAddr(params[2], &addr);
1.168 -
1.169 - float val = (float)strtod(str, &dummy);
1.170 -
1.171 - *addr = sp_ftoc(val);
1.172 -
1.173 - return dummy - str;
1.174 -}
1.175 -
1.176 -static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
1.177 -{
1.178 - char *str;
1.179 - pCtx->LocalToString(params[2], &str);
1.180 - size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
1.181 -
1.182 - return static_cast<cell_t>(res);
1.183 -}
1.184 -
1.185 -static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
1.186 -{
1.187 - char *buf, *fmt;
1.188 - size_t res;
1.189 - int arg = 4;
1.190 -
1.191 - pCtx->LocalToString(params[1], &buf);
1.192 - pCtx->LocalToString(params[3], &fmt);
1.193 - res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
1.194 -
1.195 - return static_cast<cell_t>(res);
1.196 -}
1.197 -
1.198 -static char g_formatbuf[2048];
1.199 -static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
1.200 -{
1.201 - char *buf, *fmt, *destbuf;
1.202 - cell_t start_addr, end_addr, maxparam;
1.203 - size_t res, maxlen;
1.204 - int arg = 4;
1.205 - bool copy = false;
1.206 -
1.207 - pCtx->LocalToString(params[1], &destbuf);
1.208 - pCtx->LocalToString(params[3], &fmt);
1.209 -
1.210 - maxlen = static_cast<size_t>(params[2]);
1.211 - start_addr = params[1];
1.212 - end_addr = params[1] + maxlen;
1.213 - maxparam = params[0];
1.214 -
1.215 - for (cell_t i=3; i<=maxparam; i++)
1.216 - {
1.217 - if ((params[i] >= start_addr) && (params[i] <= end_addr))
1.218 - {
1.219 - copy = true;
1.220 - break;
1.221 - }
1.222 - }
1.223 - buf = (copy) ? g_formatbuf : destbuf;
1.224 - res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
1.225 -
1.226 - if (copy)
1.227 - {
1.228 - memcpy(destbuf, g_formatbuf, res+1);
1.229 - }
1.230 -
1.231 - return static_cast<cell_t>(res);
1.232 -}
1.233 -
1.234 -static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
1.235 -{
1.236 - int vargPos = static_cast<int>(params[4]);
1.237 -
1.238 - /* Get the parent parameter array */
1.239 - sp_context_t *ctx = pContext->GetContext();
1.240 - cell_t *local_params = (cell_t *)(ctx->memory + ctx->frm + (2 * sizeof(cell_t)));
1.241 -
1.242 - cell_t max = local_params[0];
1.243 - if (vargPos > (int)max + 1)
1.244 - {
1.245 - return pContext->ThrowNativeError("Argument index is invalid: %d", vargPos);
1.246 - }
1.247 -
1.248 - cell_t addr_start = params[1];
1.249 - cell_t addr_end = addr_start + params[2];
1.250 - bool copy = false;
1.251 - for (int i=vargPos; i<=max; i++)
1.252 - {
1.253 - /* Does this clip bounds? */
1.254 - if ((local_params[i] >= addr_start)
1.255 - && (local_params[i] <= addr_end))
1.256 - {
1.257 - copy = true;
1.258 - break;
1.259 - }
1.260 - }
1.261 -
1.262 - /* Get destination info */
1.263 - char *format, *destination;
1.264 - size_t maxlen = static_cast<size_t>(params[2]);
1.265 -
1.266 - if (copy)
1.267 - {
1.268 - destination = g_formatbuf;
1.269 - } else {
1.270 - pContext->LocalToString(params[1], &destination);
1.271 - }
1.272 -
1.273 - pContext->LocalToString(params[3], &format);
1.274 -
1.275 - size_t total = atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
1.276 -
1.277 - /* Perform copy-on-write if we need to */
1.278 - if (copy)
1.279 - {
1.280 - pContext->StringToLocal(params[1], maxlen, g_formatbuf);
1.281 - }
1.282 -
1.283 - return total;
1.284 -}
1.285 -
1.286 -/* :TODO: make this UTF8 safe */
1.287 -static cell_t BreakString(IPluginContext *pContext, const cell_t *params)
1.288 -{
1.289 - const char *input;
1.290 - char *out;
1.291 - size_t outMax;
1.292 -
1.293 - /* Get parameters */
1.294 - pContext->LocalToString(params[1], (char **)&input);
1.295 - pContext->LocalToString(params[2], &out);
1.296 - outMax = params[3];
1.297 -
1.298 - const char *inptr = input;
1.299 - /* Eat up whitespace */
1.300 - while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
1.301 - {
1.302 - inptr++;
1.303 - }
1.304 -
1.305 - if (*inptr == '\0')
1.306 - {
1.307 - if (outMax)
1.308 - {
1.309 - *out = '\0';
1.310 - }
1.311 - return -1;
1.312 - }
1.313 -
1.314 - const char *start, *end = NULL;
1.315 -
1.316 - bool quoted = (*inptr == '"');
1.317 - if (quoted)
1.318 - {
1.319 - inptr++;
1.320 - start = inptr;
1.321 - /* Read input until we reach a quote. */
1.322 - while (*inptr != '\0' && *inptr != '"')
1.323 - {
1.324 - /* Update the end point, increment the stream. */
1.325 - end = inptr++;
1.326 - }
1.327 - /* Read one more token if we reached an end quote */
1.328 - if (*inptr == '"')
1.329 - {
1.330 - inptr++;
1.331 - }
1.332 - } else {
1.333 - start = inptr;
1.334 - /* Read input until we reach a space */
1.335 - while (*inptr != '\0' && !textparsers->IsWhitespace(inptr))
1.336 - {
1.337 - /* Update the end point, increment the stream. */
1.338 - end = inptr++;
1.339 - }
1.340 - }
1.341 -
1.342 - /* Copy the string we found, if necessary */
1.343 - if (end == NULL)
1.344 - {
1.345 - if (outMax)
1.346 - {
1.347 - *out = '\0';
1.348 - }
1.349 - } else if (outMax) {
1.350 - char *outptr = out;
1.351 - outMax--;
1.352 - for (const char *ptr=start;
1.353 - (ptr <= end) && ((unsigned)(outptr - out) < (outMax));
1.354 - ptr++, outptr++)
1.355 - {
1.356 - *outptr = *ptr;
1.357 - }
1.358 - *outptr = '\0';
1.359 - }
1.360 -
1.361 - /* Consume more of the string until we reach non-whitespace */
1.362 - while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
1.363 - {
1.364 - inptr++;
1.365 - }
1.366 -
1.367 - if (*inptr == '\0')
1.368 - {
1.369 - return -1;
1.370 - }
1.371 -
1.372 - return inptr - input;
1.373 -}
1.374 -
1.375 -static cell_t GetCharBytes(IPluginContext *pContext, const cell_t *params)
1.376 -{
1.377 - char *str;
1.378 - pContext->LocalToString(params[1], &str);
1.379 -
1.380 - return _GetUTF8CharBytes(str);
1.381 -};
1.382 -
1.383 -static cell_t IsCharAlpha(IPluginContext *pContext, const cell_t *params)
1.384 -{
1.385 - char chr = params[1];
1.386 -
1.387 - if (_GetUTF8CharBytes(&chr) != 1)
1.388 - {
1.389 - return 0;
1.390 - }
1.391 -
1.392 - return isalpha(chr);
1.393 -}
1.394 -
1.395 -static cell_t IsCharNumeric(IPluginContext *pContext, const cell_t *params)
1.396 -{
1.397 - char chr = params[1];
1.398 -
1.399 - if (_GetUTF8CharBytes(&chr) != 1)
1.400 - {
1.401 - return 0;
1.402 - }
1.403 -
1.404 - return isdigit(chr);
1.405 -}
1.406 -
1.407 -static cell_t IsCharSpace(IPluginContext *pContext, const cell_t *params)
1.408 -{
1.409 - char chr = params[1];
1.410 -
1.411 - if (_GetUTF8CharBytes(&chr) != 1)
1.412 - {
1.413 - return 0;
1.414 - }
1.415 -
1.416 - return isspace(chr);
1.417 -}
1.418 -
1.419 -static cell_t IsCharMB(IPluginContext *pContext, const cell_t *params)
1.420 -{
1.421 - char chr = params[1];
1.422 -
1.423 - unsigned int bytes = _GetUTF8CharBytes(&chr);
1.424 - if (bytes == 1)
1.425 - {
1.426 - return 0;
1.427 - }
1.428 -
1.429 - return bytes;
1.430 -}
1.431 -
1.432 -static cell_t IsCharUpper(IPluginContext *pContext, const cell_t *params)
1.433 -{
1.434 - char chr = params[1];
1.435 -
1.436 - if (_GetUTF8CharBytes(&chr) != 1)
1.437 - {
1.438 - return 0;
1.439 - }
1.440 -
1.441 - return isupper(chr);
1.442 -}
1.443 -
1.444 -static cell_t IsCharLower(IPluginContext *pContext, const cell_t *params)
1.445 -{
1.446 - char chr = params[1];
1.447 -
1.448 - if (_GetUTF8CharBytes(&chr) != 1)
1.449 - {
1.450 - return 0;
1.451 - }
1.452 -
1.453 - return islower(chr);
1.454 -}
1.455 -
1.456 -static cell_t ReplaceString(IPluginContext *pContext, const cell_t *params)
1.457 -{
1.458 - char *text, *search, *replace;
1.459 - size_t maxlength;
1.460 -
1.461 - pContext->LocalToString(params[1], &text);
1.462 - pContext->LocalToString(params[3], &search);
1.463 - pContext->LocalToString(params[4], &replace);
1.464 - maxlength = (size_t)params[2];
1.465 -
1.466 -
1.467 - return UTIL_ReplaceAll(text, maxlength, search, replace);
1.468 -}
1.469 -
1.470 -static cell_t ReplaceStringEx(IPluginContext *pContext, const cell_t *params)
1.471 -{
1.472 - char *text, *search, *replace;
1.473 - size_t maxlength;
1.474 -
1.475 - pContext->LocalToString(params[1], &text);
1.476 - pContext->LocalToString(params[3], &search);
1.477 - pContext->LocalToString(params[4], &replace);
1.478 - maxlength = (size_t)params[2];
1.479 -
1.480 - size_t searchLen = (params[5] == -1) ? strlen(search) : (size_t)params[5];
1.481 - size_t replaceLen = (params[6] == -1) ? strlen(replace) : (size_t)params[6];
1.482 -
1.483 - char *ptr = UTIL_ReplaceEx(text, maxlength, search, searchLen, replace, replaceLen);
1.484 -
1.485 - if (ptr == NULL)
1.486 - {
1.487 - return -1;
1.488 - }
1.489 -
1.490 - return ptr - text;
1.491 -}
1.492 -
1.493 -static cell_t TrimString(IPluginContext *pContext, const cell_t *params)
1.494 -{
1.495 - char *str;
1.496 - pContext->LocalToString(params[1], &str);
1.497 -
1.498 - size_t chars = strlen(str);
1.499 -
1.500 - if (chars == 0)
1.501 - {
1.502 - return 0;
1.503 - }
1.504 -
1.505 - char *end = str + chars - 1;
1.506 -
1.507 - /* Iterate backwards through string until we reach first non-whitespace char */
1.508 - while (end >= str && textparsers->IsWhitespace(end))
1.509 - {
1.510 - end--;
1.511 - }
1.512 -
1.513 - /* Replace first whitespace char (at the end) with null terminator */
1.514 - *(end + 1) = '\0';
1.515 -
1.516 - /* Iterate forwards through string until first non-whitespace char is reached */
1.517 - while (textparsers->IsWhitespace(str))
1.518 - {
1.519 - str++;
1.520 - }
1.521 -
1.522 - size_t bytes;
1.523 - pContext->StringToLocalUTF8(params[1], chars + 1, str, &bytes);
1.524 -
1.525 - return bytes;
1.526 -}
1.527 -
1.528 -static cell_t SplitString(IPluginContext *pContext, const cell_t *params)
1.529 -{
1.530 - char *text, *split;
1.531 -
1.532 - pContext->LocalToString(params[1], &text);
1.533 - pContext->LocalToString(params[2], &split);
1.534 -
1.535 - size_t maxLen = (size_t)params[4];
1.536 - size_t textLen = strlen(text);
1.537 - size_t splitLen = strlen(split);
1.538 -
1.539 - if (splitLen > textLen)
1.540 - {
1.541 - return -1;
1.542 - }
1.543 -
1.544 - /**
1.545 - * Note that it's <= ... you could also just add 1,
1.546 - * but this is a bit nicer
1.547 - */
1.548 - for (size_t i=0; i<=textLen - splitLen; i++)
1.549 - {
1.550 - if (strncmp(&text[i], split, splitLen) == 0)
1.551 - {
1.552 - /* Split hereeeee */
1.553 - if (i >= maxLen)
1.554 - {
1.555 - pContext->StringToLocalUTF8(params[3], maxLen, text, NULL);
1.556 - } else {
1.557 - pContext->StringToLocalUTF8(params[3], i+1, text, NULL);
1.558 - }
1.559 - return (cell_t)(i + splitLen);
1.560 - }
1.561 - }
1.562 -
1.563 - return -1;
1.564 -}
1.565 -
1.566 -REGISTER_NATIVES(basicStrings)
1.567 -{
1.568 - {"BreakString", BreakString},
1.569 - {"FloatToString", sm_floattostr},
1.570 - {"Format", sm_format},
1.571 - {"FormatEx", sm_formatex},
1.572 - {"GetCharBytes", GetCharBytes},
1.573 - {"IntToString", sm_numtostr},
1.574 - {"IsCharAlpha", IsCharAlpha},
1.575 - {"IsCharLower", IsCharLower},
1.576 - {"IsCharMB", IsCharMB},
1.577 - {"IsCharNumeric", IsCharNumeric},
1.578 - {"IsCharSpace", IsCharSpace},
1.579 - {"IsCharUpper", IsCharUpper},
1.580 - {"ReplaceString", ReplaceString},
1.581 - {"ReplaceStringEx", ReplaceStringEx},
1.582 - {"SplitString", SplitString},
1.583 - {"strlen", sm_strlen},
1.584 - {"StrBreak", BreakString}, /* Backwards compat shim */
1.585 - {"StrContains", sm_contain},
1.586 - {"strcmp", sm_strcmp},
1.587 - {"StrCompare", sm_strcmp}, /* Backwards compat shim */
1.588 - {"strncmp", sm_strncmp},
1.589 - {"strcopy", sm_strcopy},
1.590 - {"StrCopy", sm_strcopy}, /* Backwards compat shim */
1.591 - {"StringToInt", sm_strconvint},
1.592 - {"StringToIntEx", StringToIntEx},
1.593 - {"StringToFloat", sm_strtofloat},
1.594 - {"StringToFloatEx", StringToFloatEx},
1.595 - {"TrimString", TrimString},
1.596 - {"VFormat", sm_vformat},
1.597 - {NULL, NULL},
1.598 -};