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