core/smn_string.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jan 25 09:19:38 2007 +0000 (2007-01-25)
changeset 351 7c219cde57e7
parent 274 959c85596341
child 370 d3a484f98f6d
permissions -rw-r--r--
initial import of linux support
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);
dvander@351
    13
#elif defined 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@274
    87
	size_t res = UTIL_Format(str, params[3], "%d", params[1]);
faluco@226
    88
faluco@274
    89
	return static_cast<cell_t>(res);
faluco@226
    90
}
faluco@226
    91
faluco@226
    92
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
faluco@226
    93
{
dvander@233
    94
	char *str, *dummy;
faluco@226
    95
	pCtx->LocalToString(params[1], &str);
faluco@226
    96
dvander@233
    97
	float val = (float)strtod(str, &dummy);
dvander@233
    98
dvander@233
    99
	return sp_ftoc(val);
faluco@226
   100
}
faluco@226
   101
faluco@226
   102
static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
   103
{
faluco@226
   104
	char *str;
faluco@226
   105
	pCtx->LocalToString(params[2], &str);
faluco@274
   106
	size_t res = UTIL_Format(str, params[3], "%f", sp_ctof(params[1]));
faluco@226
   107
faluco@274
   108
	return static_cast<cell_t>(res);
faluco@226
   109
}
dvander@233
   110
faluco@236
   111
static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
faluco@236
   112
{
faluco@236
   113
	char *buf, *fmt;
faluco@236
   114
	size_t res;
faluco@236
   115
	int arg = 4;
faluco@236
   116
faluco@236
   117
	pCtx->LocalToString(params[1], &buf);
faluco@236
   118
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   119
	res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
faluco@236
   120
faluco@236
   121
	return static_cast<cell_t>(res);
faluco@236
   122
}
faluco@236
   123
faluco@236
   124
static char g_formatbuf[2048];
faluco@236
   125
static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
faluco@236
   126
{
faluco@236
   127
	char *buf, *fmt, *destbuf;
faluco@236
   128
	cell_t start_addr, end_addr, maxparam;
faluco@236
   129
	size_t res, maxlen;
faluco@236
   130
	int arg = 4;
faluco@236
   131
	bool copy = false;
faluco@236
   132
faluco@236
   133
	pCtx->LocalToString(params[1], &destbuf);
faluco@236
   134
	pCtx->LocalToString(params[3], &fmt);
faluco@236
   135
faluco@236
   136
	maxlen = static_cast<size_t>(params[2]);
faluco@236
   137
	start_addr = params[1];
faluco@236
   138
	end_addr = params[1] + maxlen;
faluco@236
   139
	maxparam = params[0];
faluco@236
   140
faluco@236
   141
	for (cell_t i=3; i<=maxparam; i++)
faluco@236
   142
	{
faluco@236
   143
		if ((params[i] >= start_addr) && (params[i] <= end_addr))
faluco@236
   144
		{
faluco@236
   145
			copy = true;
faluco@236
   146
			break;
faluco@236
   147
		}
faluco@236
   148
	}
faluco@236
   149
	buf = (copy) ? g_formatbuf : destbuf;
faluco@236
   150
	res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
faluco@236
   151
faluco@236
   152
	if (copy)
faluco@236
   153
	{
faluco@236
   154
		memcpy(destbuf, g_formatbuf, res+1);
faluco@236
   155
	}
faluco@236
   156
faluco@236
   157
	return static_cast<cell_t>(res);
faluco@236
   158
}
faluco@236
   159
dvander@233
   160
REGISTER_NATIVES(basicstrings)
dvander@233
   161
{
dvander@233
   162
	{"strlen",				sm_strlen},
dvander@233
   163
	{"StrContains",			sm_contain},
dvander@233
   164
	{"StrCompare",			sm_strcmp},
dvander@233
   165
	{"StrCopy",				sm_strcopy},
dvander@233
   166
	{"StringToInt",			sm_strconvint},
dvander@233
   167
	{"IntToString",			sm_numtostr},
dvander@233
   168
	{"StringToFloat",		sm_strtofloat},
dvander@233
   169
	{"FloatToString",		sm_floattostr},
dvander@255
   170
	{"Format",				sm_format},
dvander@255
   171
	{"FormatEx",			sm_formatex},
dvander@233
   172
	{NULL,					NULL},
dvander@233
   173
};