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