1.1 --- a/core/smn_string.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,622 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2008 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 - cell_t *local_params = pContext->GetLocalParams();
1.240 -
1.241 - cell_t max = local_params[0];
1.242 - if (vargPos > (int)max + 1)
1.243 - {
1.244 - return pContext->ThrowNativeError("Argument index is invalid: %d", vargPos);
1.245 - }
1.246 -
1.247 - cell_t addr_start = params[1];
1.248 - cell_t addr_end = addr_start + params[2];
1.249 - bool copy = false;
1.250 - for (int i=vargPos; i<=max; i++)
1.251 - {
1.252 - /* Does this clip bounds? */
1.253 - if ((local_params[i] >= addr_start)
1.254 - && (local_params[i] <= addr_end))
1.255 - {
1.256 - copy = true;
1.257 - break;
1.258 - }
1.259 - }
1.260 -
1.261 - /* Get destination info */
1.262 - char *format, *destination;
1.263 - size_t maxlen = static_cast<size_t>(params[2]);
1.264 -
1.265 - if (copy)
1.266 - {
1.267 - destination = g_formatbuf;
1.268 - } else {
1.269 - pContext->LocalToString(params[1], &destination);
1.270 - }
1.271 -
1.272 - pContext->LocalToString(params[3], &format);
1.273 -
1.274 - size_t total = atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
1.275 -
1.276 - /* Perform copy-on-write if we need to */
1.277 - if (copy)
1.278 - {
1.279 - pContext->StringToLocal(params[1], maxlen, g_formatbuf);
1.280 - }
1.281 -
1.282 - return total;
1.283 -}
1.284 -
1.285 -/* :TODO: make this UTF8 safe */
1.286 -static cell_t BreakString(IPluginContext *pContext, const cell_t *params)
1.287 -{
1.288 - const char *input;
1.289 - char *out;
1.290 - size_t outMax;
1.291 -
1.292 - /* Get parameters */
1.293 - pContext->LocalToString(params[1], (char **)&input);
1.294 - pContext->LocalToString(params[2], &out);
1.295 - outMax = params[3];
1.296 -
1.297 - const char *inptr = input;
1.298 - /* Eat up whitespace */
1.299 - while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
1.300 - {
1.301 - inptr++;
1.302 - }
1.303 -
1.304 - if (*inptr == '\0')
1.305 - {
1.306 - if (outMax)
1.307 - {
1.308 - *out = '\0';
1.309 - }
1.310 - return -1;
1.311 - }
1.312 -
1.313 - const char *start, *end = NULL;
1.314 -
1.315 - bool quoted = (*inptr == '"');
1.316 - if (quoted)
1.317 - {
1.318 - inptr++;
1.319 - start = inptr;
1.320 - /* Read input until we reach a quote. */
1.321 - while (*inptr != '\0' && *inptr != '"')
1.322 - {
1.323 - /* Update the end point, increment the stream. */
1.324 - end = inptr++;
1.325 - }
1.326 - /* Read one more token if we reached an end quote */
1.327 - if (*inptr == '"')
1.328 - {
1.329 - inptr++;
1.330 - }
1.331 - } else {
1.332 - start = inptr;
1.333 - /* Read input until we reach a space */
1.334 - while (*inptr != '\0' && !textparsers->IsWhitespace(inptr))
1.335 - {
1.336 - /* Update the end point, increment the stream. */
1.337 - end = inptr++;
1.338 - }
1.339 - }
1.340 -
1.341 - /* Copy the string we found, if necessary */
1.342 - if (end == NULL)
1.343 - {
1.344 - if (outMax)
1.345 - {
1.346 - *out = '\0';
1.347 - }
1.348 - } else if (outMax) {
1.349 - char *outptr = out;
1.350 - outMax--;
1.351 - for (const char *ptr=start;
1.352 - (ptr <= end) && ((unsigned)(outptr - out) < (outMax));
1.353 - ptr++, outptr++)
1.354 - {
1.355 - *outptr = *ptr;
1.356 - }
1.357 - *outptr = '\0';
1.358 - }
1.359 -
1.360 - /* Consume more of the string until we reach non-whitespace */
1.361 - while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
1.362 - {
1.363 - inptr++;
1.364 - }
1.365 -
1.366 - if (*inptr == '\0')
1.367 - {
1.368 - return -1;
1.369 - }
1.370 -
1.371 - return inptr - input;
1.372 -}
1.373 -
1.374 -static cell_t GetCharBytes(IPluginContext *pContext, const cell_t *params)
1.375 -{
1.376 - char *str;
1.377 - pContext->LocalToString(params[1], &str);
1.378 -
1.379 - return _GetUTF8CharBytes(str);
1.380 -};
1.381 -
1.382 -static cell_t IsCharAlpha(IPluginContext *pContext, const cell_t *params)
1.383 -{
1.384 - char chr = params[1];
1.385 -
1.386 - if (_GetUTF8CharBytes(&chr) != 1)
1.387 - {
1.388 - return 0;
1.389 - }
1.390 -
1.391 - return isalpha(chr);
1.392 -}
1.393 -
1.394 -static cell_t IsCharNumeric(IPluginContext *pContext, const cell_t *params)
1.395 -{
1.396 - char chr = params[1];
1.397 -
1.398 - if (_GetUTF8CharBytes(&chr) != 1)
1.399 - {
1.400 - return 0;
1.401 - }
1.402 -
1.403 - return isdigit(chr);
1.404 -}
1.405 -
1.406 -static cell_t IsCharSpace(IPluginContext *pContext, const cell_t *params)
1.407 -{
1.408 - char chr = params[1];
1.409 -
1.410 - if (_GetUTF8CharBytes(&chr) != 1)
1.411 - {
1.412 - return 0;
1.413 - }
1.414 -
1.415 - return isspace(chr);
1.416 -}
1.417 -
1.418 -static cell_t IsCharMB(IPluginContext *pContext, const cell_t *params)
1.419 -{
1.420 - char chr = params[1];
1.421 -
1.422 - unsigned int bytes = _GetUTF8CharBytes(&chr);
1.423 - if (bytes == 1)
1.424 - {
1.425 - return 0;
1.426 - }
1.427 -
1.428 - return bytes;
1.429 -}
1.430 -
1.431 -static cell_t IsCharUpper(IPluginContext *pContext, const cell_t *params)
1.432 -{
1.433 - char chr = params[1];
1.434 -
1.435 - if (_GetUTF8CharBytes(&chr) != 1)
1.436 - {
1.437 - return 0;
1.438 - }
1.439 -
1.440 - return isupper(chr);
1.441 -}
1.442 -
1.443 -static cell_t IsCharLower(IPluginContext *pContext, const cell_t *params)
1.444 -{
1.445 - char chr = params[1];
1.446 -
1.447 - if (_GetUTF8CharBytes(&chr) != 1)
1.448 - {
1.449 - return 0;
1.450 - }
1.451 -
1.452 - return islower(chr);
1.453 -}
1.454 -
1.455 -static cell_t ReplaceString(IPluginContext *pContext, const cell_t *params)
1.456 -{
1.457 - char *text, *search, *replace;
1.458 - size_t maxlength;
1.459 -
1.460 - pContext->LocalToString(params[1], &text);
1.461 - pContext->LocalToString(params[3], &search);
1.462 - pContext->LocalToString(params[4], &replace);
1.463 - maxlength = (size_t)params[2];
1.464 -
1.465 - if (search[0] == '\0')
1.466 - {
1.467 - return pContext->ThrowNativeError("Cannot replace searches of empty strings");
1.468 - }
1.469 -
1.470 - return UTIL_ReplaceAll(text, maxlength, search, replace);
1.471 -}
1.472 -
1.473 -static cell_t ReplaceStringEx(IPluginContext *pContext, const cell_t *params)
1.474 -{
1.475 - char *text, *search, *replace;
1.476 - size_t maxlength;
1.477 -
1.478 - pContext->LocalToString(params[1], &text);
1.479 - pContext->LocalToString(params[3], &search);
1.480 - pContext->LocalToString(params[4], &replace);
1.481 - maxlength = (size_t)params[2];
1.482 -
1.483 - size_t searchLen = (params[5] == -1) ? strlen(search) : (size_t)params[5];
1.484 - size_t replaceLen = (params[6] == -1) ? strlen(replace) : (size_t)params[6];
1.485 -
1.486 - if (searchLen == 0)
1.487 - {
1.488 - return pContext->ThrowNativeError("Cannot replace searches of empty strings");
1.489 - }
1.490 -
1.491 - char *ptr = UTIL_ReplaceEx(text, maxlength, search, searchLen, replace, replaceLen);
1.492 -
1.493 - if (ptr == NULL)
1.494 - {
1.495 - return -1;
1.496 - }
1.497 -
1.498 - return ptr - text;
1.499 -}
1.500 -
1.501 -static cell_t TrimString(IPluginContext *pContext, const cell_t *params)
1.502 -{
1.503 - char *str;
1.504 - pContext->LocalToString(params[1], &str);
1.505 -
1.506 - size_t chars = strlen(str);
1.507 -
1.508 - if (chars == 0)
1.509 - {
1.510 - return 0;
1.511 - }
1.512 -
1.513 - char *end = str + chars - 1;
1.514 -
1.515 - /* Iterate backwards through string until we reach first non-whitespace char */
1.516 - while (end >= str && textparsers->IsWhitespace(end))
1.517 - {
1.518 - end--;
1.519 - }
1.520 -
1.521 - /* Replace first whitespace char (at the end) with null terminator */
1.522 - *(end + 1) = '\0';
1.523 -
1.524 - /* Iterate forwards through string until first non-whitespace char is reached */
1.525 - while (textparsers->IsWhitespace(str))
1.526 - {
1.527 - str++;
1.528 - }
1.529 -
1.530 - size_t bytes;
1.531 - pContext->StringToLocalUTF8(params[1], chars + 1, str, &bytes);
1.532 -
1.533 - return bytes;
1.534 -}
1.535 -
1.536 -static cell_t SplitString(IPluginContext *pContext, const cell_t *params)
1.537 -{
1.538 - char *text, *split;
1.539 -
1.540 - pContext->LocalToString(params[1], &text);
1.541 - pContext->LocalToString(params[2], &split);
1.542 -
1.543 - size_t maxLen = (size_t)params[4];
1.544 - size_t textLen = strlen(text);
1.545 - size_t splitLen = strlen(split);
1.546 -
1.547 - if (splitLen > textLen)
1.548 - {
1.549 - return -1;
1.550 - }
1.551 -
1.552 - /**
1.553 - * Note that it's <= ... you could also just add 1,
1.554 - * but this is a bit nicer
1.555 - */
1.556 - for (size_t i=0; i<=textLen - splitLen; i++)
1.557 - {
1.558 - if (strncmp(&text[i], split, splitLen) == 0)
1.559 - {
1.560 - /* Split hereeeee */
1.561 - if (i >= maxLen)
1.562 - {
1.563 - pContext->StringToLocalUTF8(params[3], maxLen, text, NULL);
1.564 - } else {
1.565 - pContext->StringToLocalUTF8(params[3], i+1, text, NULL);
1.566 - }
1.567 - return (cell_t)(i + splitLen);
1.568 - }
1.569 - }
1.570 -
1.571 - return -1;
1.572 -}
1.573 -
1.574 -static cell_t StripQuotes(IPluginContext *pContext, const cell_t *params)
1.575 -{
1.576 - char *text;
1.577 - size_t length;
1.578 -
1.579 - pContext->LocalToString(params[1], &text);
1.580 - length = strlen(text);
1.581 -
1.582 - if (text[0] == '"' && text[length-1] == '"')
1.583 - {
1.584 - /* Null-terminate */
1.585 - text[--length] = '\0';
1.586 - /* Move the remaining bytes (including null terminator) down by one */
1.587 - memmove(&text[0], &text[1], length);
1.588 -
1.589 - return 1;
1.590 - }
1.591 -
1.592 - return 0;
1.593 -}
1.594 -
1.595 -REGISTER_NATIVES(basicStrings)
1.596 -{
1.597 - {"BreakString", BreakString},
1.598 - {"FloatToString", sm_floattostr},
1.599 - {"Format", sm_format},
1.600 - {"FormatEx", sm_formatex},
1.601 - {"GetCharBytes", GetCharBytes},
1.602 - {"IntToString", sm_numtostr},
1.603 - {"IsCharAlpha", IsCharAlpha},
1.604 - {"IsCharLower", IsCharLower},
1.605 - {"IsCharMB", IsCharMB},
1.606 - {"IsCharNumeric", IsCharNumeric},
1.607 - {"IsCharSpace", IsCharSpace},
1.608 - {"IsCharUpper", IsCharUpper},
1.609 - {"ReplaceString", ReplaceString},
1.610 - {"ReplaceStringEx", ReplaceStringEx},
1.611 - {"SplitString", SplitString},
1.612 - {"strlen", sm_strlen},
1.613 - {"StrContains", sm_contain},
1.614 - {"strcmp", sm_strcmp},
1.615 - {"strncmp", sm_strncmp},
1.616 - {"strcopy", sm_strcopy},
1.617 - {"StringToInt", sm_strconvint},
1.618 - {"StringToIntEx", StringToIntEx},
1.619 - {"StringToFloat", sm_strtofloat},
1.620 - {"StringToFloatEx", StringToFloatEx},
1.621 - {"StripQuotes", StripQuotes},
1.622 - {"TrimString", TrimString},
1.623 - {"VFormat", sm_vformat},
1.624 - {NULL, NULL},
1.625 -};