core/smn_string.cpp
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 04:16:31 2007 +0000 (2007-02-16)
changeset 502 4530ea065a8b
parent 432 77112e9620fa
child 631 76dc2aa558e5
permissions -rw-r--r--
fixed a serious potential bug with swapping plugins to debug mode, leaving stale function pointers without plugin removal
dvander@370
     1
/**
dvander@370
     2
 * ===============================================================
dvander@370
     3
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     4
 * ===============================================================
dvander@370
     5
 *
dvander@370
     6
 * This file is not open source and may not be copied without explicit
dvander@370
     7
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     8
 * in whole or significant part.
dvander@370
     9
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    10
 *
dvander@370
    11
 * Version: $Id$
dvander@370
    12
 */
dvander@370
    13
faluco@225
    14
#include <string.h>
dvander@233
    15
#include <stdlib.h>
dvander@233
    16
#include "sm_globals.h"
faluco@230
    17
#include "sm_stringutil.h"
faluco@225
    18
faluco@225
    19
inline const char *_strstr(const char *str, const char *substr)
faluco@225
    20
{
faluco@225
    21
#ifdef PLATFORM_WINDOWS
faluco@225
    22
	return strstr(str, substr);
dvander@351
    23
#elif defined PLATFORM_LINUX
faluco@225
    24
	return (const char *)strstr(str, substr);
faluco@225
    25
#endif
faluco@225
    26
}
faluco@225
    27
faluco@225
    28
/*********************************************
faluco@225
    29
*                                            *
faluco@225
    30
* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
faluco@225
    31
*                                            *
faluco@225
    32
*********************************************/
faluco@225
    33
faluco@225
    34
static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
faluco@225
    35
{
faluco@225
    36
	char *str;
faluco@225
    37
	pCtx->LocalToString(params[1], &str);
faluco@225
    38
faluco@225
    39
	return strlen(str);
faluco@225
    40
}
faluco@225
    41
faluco@225
    42
static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
faluco@225
    43
{
faluco@225
    44
	typedef const char *(*STRSEARCH)(const char *, const char *);
faluco@225
    45
	STRSEARCH func;
faluco@225
    46
	char *str, *substr;
faluco@225
    47
faluco@225
    48
	pCtx->LocalToString(params[1], &str);
faluco@225
    49
	pCtx->LocalToString(params[2], &substr);
faluco@225
    50
faluco@225
    51
	func = (params[3]) ? _strstr : stristr;
faluco@225
    52
	const char *pos = func(str, substr);
faluco@225
    53
	if (pos)
faluco@225
    54
	{
faluco@225
    55
		return (pos - str);
faluco@225
    56
	}
faluco@225
    57
faluco@225
    58
	return -1;
faluco@225
    59
}
faluco@225
    60
dvander@233
    61
static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
faluco@225
    62
{
faluco@225
    63
	typedef int (*STRCOMPARE)(const char *, const char *);
faluco@225
    64
	STRCOMPARE func;
faluco@225
    65
	char *str1, *str2;
faluco@225
    66
faluco@225
    67
	pCtx->LocalToString(params[1], &str1);
faluco@225
    68
	pCtx->LocalToString(params[2], &str2);
faluco@225
    69
faluco@225
    70
	func = (params[3]) ? strcmp : stricmp;
faluco@225
    71
dvander@233
    72
	return (func(str1, str2));
faluco@225
    73
}
faluco@225
    74
faluco@225
    75
static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
faluco@225
    76
{
faluco@230
    77
	char *dest, *src;
faluco@225
    78
faluco@225
    79
	pCtx->LocalToString(params[1], &dest);
faluco@225
    80
	pCtx->LocalToString(params[3], &src);
faluco@225
    81
faluco@230
    82
	return strncopy(dest, src, params[2]);
faluco@225
    83
}
faluco@226
    84
dvander@233
    85
static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
faluco@226
    86
{
dvander@233
    87
	char *str, *dummy;
faluco@226
    88
	pCtx->LocalToString(params[1], &str);
faluco@226
    89
dvander@233
    90
	return static_cast<cell_t>(strtol(str, &dummy, params[2]));
faluco@226
    91
}
faluco@226
    92
faluco@226
    93
static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
    94
{
faluco@226
    95
	char *str;
faluco@226
    96
	pCtx->LocalToString(params[2], &str);
faluco@274
    97
	size_t res = UTIL_Format(str, params[3], "%d", params[1]);
faluco@226
    98
faluco@274
    99
	return static_cast<cell_t>(res);
faluco@226
   100
}
faluco@226
   101
faluco@226
   102
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
faluco@226
   103
{
dvander@233
   104
	char *str, *dummy;
faluco@226
   105
	pCtx->LocalToString(params[1], &str);
faluco@226
   106
dvander@233
   107
	float val = (float)strtod(str, &dummy);
dvander@233
   108
dvander@233
   109
	return sp_ftoc(val);
faluco@226
   110
}
faluco@226
   111
faluco@226
   112
static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
   113
{
faluco@226
   114
	char *str;
faluco@226
   115
	pCtx->LocalToString(params[2], &str);
faluco@274
   116
	size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
faluco@226
   117
faluco@274
   118
	return static_cast<cell_t>(res);
faluco@226
   119
}
dvander@233
   120
faluco@236
   121
static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
faluco@236
   122
{
faluco@236
   123
	char *buf, *fmt;
faluco@236
   124
	size_t res;
faluco@236
   125
	int arg = 4;
faluco@236
   126
faluco@236
   127
	pCtx->LocalToString(params[1], &buf);
faluco@236
   128
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   129
	res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
faluco@236
   130
faluco@236
   131
	return static_cast<cell_t>(res);
faluco@236
   132
}
faluco@236
   133
faluco@236
   134
static char g_formatbuf[2048];
faluco@236
   135
static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
faluco@236
   136
{
faluco@236
   137
	char *buf, *fmt, *destbuf;
faluco@236
   138
	cell_t start_addr, end_addr, maxparam;
faluco@236
   139
	size_t res, maxlen;
faluco@236
   140
	int arg = 4;
faluco@236
   141
	bool copy = false;
faluco@236
   142
faluco@236
   143
	pCtx->LocalToString(params[1], &destbuf);
faluco@236
   144
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   145
faluco@236
   146
	maxlen = static_cast<size_t>(params[2]);
faluco@236
   147
	start_addr = params[1];
faluco@236
   148
	end_addr = params[1] + maxlen;
faluco@236
   149
	maxparam = params[0];
faluco@236
   150
faluco@236
   151
	for (cell_t i=3; i<=maxparam; i++)
faluco@236
   152
	{
faluco@236
   153
		if ((params[i] >= start_addr) && (params[i] <= end_addr))
faluco@236
   154
		{
faluco@236
   155
			copy = true;
faluco@236
   156
			break;
faluco@236
   157
		}
faluco@236
   158
	}
faluco@236
   159
	buf = (copy) ? g_formatbuf : destbuf;
faluco@236
   160
	res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
faluco@236
   161
faluco@236
   162
	if (copy)
faluco@236
   163
	{
faluco@236
   164
		memcpy(destbuf, g_formatbuf, res+1);
faluco@236
   165
	}
faluco@236
   166
faluco@236
   167
	return static_cast<cell_t>(res);
faluco@236
   168
}
faluco@236
   169
dvander@411
   170
static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
dvander@411
   171
{
dvander@411
   172
	int vargPos = static_cast<int>(params[4]);
dvander@411
   173
dvander@411
   174
	/* Get the parent parameter array */
dvander@411
   175
	sp_context_t *ctx = pContext->GetContext();
dvander@411
   176
	cell_t *local_params = (cell_t *)(ctx->memory + ctx->frm + (2 * sizeof(cell_t)));
dvander@411
   177
dvander@411
   178
	cell_t max = local_params[0];
dvander@411
   179
	if (vargPos > (int)max + 1)
dvander@411
   180
	{
dvander@411
   181
		return pContext->ThrowNativeError("Argument index is invalid: %d", vargPos);
dvander@411
   182
	}
dvander@411
   183
dvander@411
   184
	cell_t addr_start = params[1];
dvander@411
   185
	cell_t addr_end = addr_start + params[2];
dvander@411
   186
	bool copy = false;
dvander@411
   187
	for (int i=vargPos; i<=max; i++)
dvander@411
   188
	{
dvander@411
   189
		/* Does this clip bounds? */
dvander@411
   190
		if ((local_params[i] >= addr_start)
dvander@411
   191
			&& (local_params[i] <= addr_end))
dvander@411
   192
		{
dvander@411
   193
			copy = true;
dvander@411
   194
			break;
dvander@411
   195
		}
dvander@411
   196
	}
dvander@411
   197
dvander@411
   198
	/* Get destination info */
dvander@411
   199
	char *format, *destination;
dvander@411
   200
	size_t maxlen = static_cast<size_t>(params[2]);
dvander@411
   201
dvander@411
   202
	if (copy)
dvander@411
   203
	{
dvander@411
   204
		destination = g_formatbuf;
dvander@411
   205
	} else {
dvander@411
   206
		pContext->LocalToString(params[1], &destination);
dvander@411
   207
	}
dvander@411
   208
dvander@411
   209
	pContext->LocalToString(params[3], &format);
dvander@411
   210
dvander@411
   211
	size_t total = atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
dvander@411
   212
dvander@411
   213
	/* Perform copy-on-write if we need to */
dvander@411
   214
	if (copy)
dvander@411
   215
	{
dvander@411
   216
		pContext->StringToLocal(params[1], maxlen, g_formatbuf);
dvander@411
   217
	}
dvander@411
   218
dvander@411
   219
	return total;
dvander@411
   220
}
dvander@411
   221
dvander@233
   222
REGISTER_NATIVES(basicstrings)
dvander@233
   223
{
dvander@233
   224
	{"strlen",				sm_strlen},
dvander@233
   225
	{"StrContains",			sm_contain},
dvander@233
   226
	{"StrCompare",			sm_strcmp},
dvander@233
   227
	{"StrCopy",				sm_strcopy},
dvander@233
   228
	{"StringToInt",			sm_strconvint},
dvander@233
   229
	{"IntToString",			sm_numtostr},
dvander@233
   230
	{"StringToFloat",		sm_strtofloat},
dvander@233
   231
	{"FloatToString",		sm_floattostr},
dvander@255
   232
	{"Format",				sm_format},
dvander@255
   233
	{"FormatEx",			sm_formatex},
dvander@411
   234
	{"VFormat",				sm_vformat},
dvander@233
   235
	{NULL,					NULL},
dvander@233
   236
};