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
     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 
    88 	return snprintf(str, params[3], "%d", params[1]);
    89 }
    90 
    91 static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
    92 {
    93 	char *str, *dummy;
    94 	pCtx->LocalToString(params[1], &str);
    95 
    96 	float val = (float)strtod(str, &dummy);
    97 
    98 	return sp_ftoc(val);
    99 }
   100 
   101 static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
   102 {
   103 	char *str;
   104 	pCtx->LocalToString(params[2], &str);
   105 
   106 	return snprintf(str, params[3], "%f", sp_ctof(params[1]));
   107 }
   108 
   109 static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
   110 {
   111 	char *buf, *fmt;
   112 	size_t res;
   113 	int arg = 4;
   114 
   115 	pCtx->LocalToString(params[1], &buf);
   116 	pCtx->LocalToString(params[3], &fmt);
   117 	res = atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
   118 
   119 	return static_cast<cell_t>(res);
   120 }
   121 
   122 static char g_formatbuf[2048];
   123 static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
   124 {
   125 	char *buf, *fmt, *destbuf;
   126 	cell_t start_addr, end_addr, maxparam;
   127 	size_t res, maxlen;
   128 	int arg = 4;
   129 	bool copy = false;
   130 
   131 	pCtx->LocalToString(params[1], &destbuf);
   132 	pCtx->LocalToString(params[3], &fmt);
   133 
   134 	maxlen = static_cast<size_t>(params[2]);
   135 	start_addr = params[1];
   136 	end_addr = params[1] + maxlen;
   137 	maxparam = params[0];
   138 
   139 	for (cell_t i=3; i<=maxparam; i++)
   140 	{
   141 		if ((params[i] >= start_addr) && (params[i] <= end_addr))
   142 		{
   143 			copy = true;
   144 			break;
   145 		}
   146 	}
   147 	buf = (copy) ? g_formatbuf : destbuf;
   148 	res = atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
   149 
   150 	if (copy)
   151 	{
   152 		memcpy(destbuf, g_formatbuf, res+1);
   153 	}
   154 
   155 	return static_cast<cell_t>(res);
   156 }
   157 
   158 REGISTER_NATIVES(basicstrings)
   159 {
   160 	{"strlen",				sm_strlen},
   161 	{"StrContains",			sm_contain},
   162 	{"StrCompare",			sm_strcmp},
   163 	{"StrCopy",				sm_strcopy},
   164 	{"StringToInt",			sm_strconvint},
   165 	{"IntToString",			sm_numtostr},
   166 	{"StringToFloat",		sm_strtofloat},
   167 	{"FloatToString",		sm_floattostr},
   168 	{"Format",				sm_format},
   169 	{"FormatEx",			sm_formatex},
   170 	{NULL,					NULL},
   171 };