core/smn_string.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Dec 29 23:28:44 2006 +0000 (2006-12-29)
changeset 230 f6abafc7c96c
parent 226 4663ea3fc2d3
child 233 968f92668138
permissions -rw-r--r--
fixed a "-1" bug in atcprintf
moved string functions to the stringutils file
added strncopy
faluco@225
     1
#include "sm_platform.h"
faluco@225
     2
#include <string.h>
faluco@225
     3
#include "sp_vm_api.h"
faluco@226
     4
#include "sp_vm_typeutil.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@226
    18
faluco@225
    19
/*********************************************
faluco@225
    20
*                                            *
faluco@225
    21
* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
faluco@225
    22
*                                            *
faluco@225
    23
*********************************************/
faluco@225
    24
faluco@225
    25
faluco@225
    26
static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
faluco@225
    27
{
faluco@225
    28
	char *str;
faluco@225
    29
	pCtx->LocalToString(params[1], &str);
faluco@225
    30
faluco@225
    31
	return strlen(str);
faluco@225
    32
}
faluco@225
    33
faluco@225
    34
static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
faluco@225
    35
{
faluco@225
    36
	typedef const char *(*STRSEARCH)(const char *, const char *);
faluco@225
    37
	STRSEARCH func;
faluco@225
    38
	char *str, *substr;
faluco@225
    39
faluco@225
    40
	pCtx->LocalToString(params[1], &str);
faluco@225
    41
	pCtx->LocalToString(params[2], &substr);
faluco@225
    42
faluco@225
    43
	func = (params[3]) ? _strstr : stristr;
faluco@225
    44
	const char *pos = func(str, substr);
faluco@225
    45
	if (pos)
faluco@225
    46
	{
faluco@225
    47
		return (pos - str);
faluco@225
    48
	}
faluco@225
    49
faluco@225
    50
	return -1;
faluco@225
    51
}
faluco@225
    52
faluco@225
    53
static cell_t sm_equal(IPluginContext *pCtx, const cell_t *params)
faluco@225
    54
{
faluco@225
    55
	typedef int (*STRCOMPARE)(const char *, const char *);
faluco@225
    56
	STRCOMPARE func;
faluco@225
    57
	char *str1, *str2;
faluco@225
    58
faluco@225
    59
	pCtx->LocalToString(params[1], &str1);
faluco@225
    60
	pCtx->LocalToString(params[2], &str2);
faluco@225
    61
faluco@225
    62
	func = (params[3]) ? strcmp : stricmp;
faluco@225
    63
faluco@225
    64
	return (func(str1, str2)) ? 0 : 1;
faluco@225
    65
}
faluco@225
    66
faluco@225
    67
static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
faluco@225
    68
{
faluco@230
    69
	char *dest, *src;
faluco@225
    70
faluco@225
    71
	pCtx->LocalToString(params[1], &dest);
faluco@225
    72
	pCtx->LocalToString(params[3], &src);
faluco@225
    73
faluco@230
    74
	return strncopy(dest, src, params[2]);
faluco@225
    75
}
faluco@226
    76
faluco@226
    77
static cell_t sm_strtonum(IPluginContext *pCtx, const cell_t *params)
faluco@226
    78
{
faluco@226
    79
	char *str;
faluco@226
    80
	pCtx->LocalToString(params[1], &str);
faluco@226
    81
faluco@226
    82
	return StrConvInt(str);
faluco@226
    83
}
faluco@226
    84
faluco@226
    85
static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
faluco@226
    86
{
faluco@226
    87
	char *str;
faluco@226
    88
	pCtx->LocalToString(params[2], &str);
faluco@226
    89
faluco@226
    90
	return snprintf(str, params[3], "%d", params[1]);
faluco@226
    91
}
faluco@226
    92
faluco@226
    93
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
faluco@226
    94
{
faluco@226
    95
	char *str;
faluco@226
    96
	pCtx->LocalToString(params[1], &str);
faluco@226
    97
faluco@226
    98
	return ftoc(StrConvFloat(str));
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
faluco@226
   106
	return snprintf(str, params[3], "%f", ctof(params[1]));
faluco@226
   107
}