core/smn_string.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jan 25 22:36:38 2007 +0000 (2007-01-25)
changeset 370 d3a484f98f6d
parent 351 7c219cde57e7
child 411 d954bff454a0
permissions -rw-r--r--
added license headers
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 "sm_platform.h"
faluco@225
    15
#include <string.h>
dvander@233
    16
#include <stdlib.h>
dvander@233
    17
#include "sm_globals.h"
faluco@230
    18
#include "sm_stringutil.h"
faluco@225
    19
faluco@225
    20
using namespace SourcePawn;
faluco@225
    21
faluco@225
    22
inline const char *_strstr(const char *str, const char *substr)
faluco@225
    23
{
faluco@225
    24
#ifdef PLATFORM_WINDOWS
faluco@225
    25
	return strstr(str, substr);
dvander@351
    26
#elif defined PLATFORM_LINUX
faluco@225
    27
	return (const char *)strstr(str, substr);
faluco@225
    28
#endif
faluco@225
    29
}
faluco@225
    30
faluco@225
    31
/*********************************************
faluco@225
    32
*                                            *
faluco@225
    33
* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
faluco@225
    34
*                                            *
faluco@225
    35
*********************************************/
faluco@225
    36
faluco@225
    37
static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
faluco@225
    38
{
faluco@225
    39
	char *str;
faluco@225
    40
	pCtx->LocalToString(params[1], &str);
faluco@225
    41
faluco@225
    42
	return strlen(str);
faluco@225
    43
}
faluco@225
    44
faluco@225
    45
static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
faluco@225
    46
{
faluco@225
    47
	typedef const char *(*STRSEARCH)(const char *, const char *);
faluco@225
    48
	STRSEARCH func;
faluco@225
    49
	char *str, *substr;
faluco@225
    50
faluco@225
    51
	pCtx->LocalToString(params[1], &str);
faluco@225
    52
	pCtx->LocalToString(params[2], &substr);
faluco@225
    53
faluco@225
    54
	func = (params[3]) ? _strstr : stristr;
faluco@225
    55
	const char *pos = func(str, substr);
faluco@225
    56
	if (pos)
faluco@225
    57
	{
faluco@225
    58
		return (pos - str);
faluco@225
    59
	}
faluco@225
    60
faluco@225
    61
	return -1;
faluco@225
    62
}
faluco@225
    63
dvander@233
    64
static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
faluco@225
    65
{
faluco@225
    66
	typedef int (*STRCOMPARE)(const char *, const char *);
faluco@225
    67
	STRCOMPARE func;
faluco@225
    68
	char *str1, *str2;
faluco@225
    69
faluco@225
    70
	pCtx->LocalToString(params[1], &str1);
faluco@225
    71
	pCtx->LocalToString(params[2], &str2);
faluco@225
    72
faluco@225
    73
	func = (params[3]) ? strcmp : stricmp;
faluco@225
    74
dvander@233
    75
	return (func(str1, str2));
faluco@225
    76
}
faluco@225
    77
faluco@225
    78
static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
faluco@225
    79
{
faluco@230
    80
	char *dest, *src;
faluco@225
    81
faluco@225
    82
	pCtx->LocalToString(params[1], &dest);
faluco@225
    83
	pCtx->LocalToString(params[3], &src);
faluco@225
    84
faluco@230
    85
	return strncopy(dest, src, params[2]);
faluco@225
    86
}
faluco@226
    87
dvander@233
    88
static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
faluco@226
    89
{
dvander@233
    90
	char *str, *dummy;
faluco@226
    91
	pCtx->LocalToString(params[1], &str);
faluco@226
    92
dvander@233
    93
	return static_cast<cell_t>(strtol(str, &dummy, params[2]));
faluco@226
    94
}
faluco@226
    95
faluco@226
    96
static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
    97
{
faluco@226
    98
	char *str;
faluco@226
    99
	pCtx->LocalToString(params[2], &str);
faluco@274
   100
	size_t res = UTIL_Format(str, params[3], "%d", params[1]);
faluco@226
   101
faluco@274
   102
	return static_cast<cell_t>(res);
faluco@226
   103
}
faluco@226
   104
faluco@226
   105
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
faluco@226
   106
{
dvander@233
   107
	char *str, *dummy;
faluco@226
   108
	pCtx->LocalToString(params[1], &str);
faluco@226
   109
dvander@233
   110
	float val = (float)strtod(str, &dummy);
dvander@233
   111
dvander@233
   112
	return sp_ftoc(val);
faluco@226
   113
}
faluco@226
   114
faluco@226
   115
static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
   116
{
faluco@226
   117
	char *str;
faluco@226
   118
	pCtx->LocalToString(params[2], &str);
faluco@274
   119
	size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
faluco@226
   120
faluco@274
   121
	return static_cast<cell_t>(res);
faluco@226
   122
}
dvander@233
   123
faluco@236
   124
static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
faluco@236
   125
{
faluco@236
   126
	char *buf, *fmt;
faluco@236
   127
	size_t res;
faluco@236
   128
	int arg = 4;
faluco@236
   129
faluco@236
   130
	pCtx->LocalToString(params[1], &buf);
faluco@236
   131
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   132
	res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
faluco@236
   133
faluco@236
   134
	return static_cast<cell_t>(res);
faluco@236
   135
}
faluco@236
   136
faluco@236
   137
static char g_formatbuf[2048];
faluco@236
   138
static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
faluco@236
   139
{
faluco@236
   140
	char *buf, *fmt, *destbuf;
faluco@236
   141
	cell_t start_addr, end_addr, maxparam;
faluco@236
   142
	size_t res, maxlen;
faluco@236
   143
	int arg = 4;
faluco@236
   144
	bool copy = false;
faluco@236
   145
faluco@236
   146
	pCtx->LocalToString(params[1], &destbuf);
faluco@236
   147
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   148
faluco@236
   149
	maxlen = static_cast<size_t>(params[2]);
faluco@236
   150
	start_addr = params[1];
faluco@236
   151
	end_addr = params[1] + maxlen;
faluco@236
   152
	maxparam = params[0];
faluco@236
   153
faluco@236
   154
	for (cell_t i=3; i<=maxparam; i++)
faluco@236
   155
	{
faluco@236
   156
		if ((params[i] >= start_addr) && (params[i] <= end_addr))
faluco@236
   157
		{
faluco@236
   158
			copy = true;
faluco@236
   159
			break;
faluco@236
   160
		}
faluco@236
   161
	}
faluco@236
   162
	buf = (copy) ? g_formatbuf : destbuf;
faluco@236
   163
	res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
faluco@236
   164
faluco@236
   165
	if (copy)
faluco@236
   166
	{
faluco@236
   167
		memcpy(destbuf, g_formatbuf, res+1);
faluco@236
   168
	}
faluco@236
   169
faluco@236
   170
	return static_cast<cell_t>(res);
faluco@236
   171
}
faluco@236
   172
dvander@233
   173
REGISTER_NATIVES(basicstrings)
dvander@233
   174
{
dvander@233
   175
	{"strlen",				sm_strlen},
dvander@233
   176
	{"StrContains",			sm_contain},
dvander@233
   177
	{"StrCompare",			sm_strcmp},
dvander@233
   178
	{"StrCopy",				sm_strcopy},
dvander@233
   179
	{"StringToInt",			sm_strconvint},
dvander@233
   180
	{"IntToString",			sm_numtostr},
dvander@233
   181
	{"StringToFloat",		sm_strtofloat},
dvander@233
   182
	{"FloatToString",		sm_floattostr},
dvander@255
   183
	{"Format",				sm_format},
dvander@255
   184
	{"FormatEx",			sm_formatex},
dvander@233
   185
	{NULL,					NULL},
dvander@233
   186
};