core/smn_string.cpp
changeset 631 76dc2aa558e5
parent 432 77112e9620fa
child 653 ef60870067ff
     1.1 --- a/core/smn_string.cpp	Fri Mar 16 18:19:26 2007 +0000
     1.2 +++ b/core/smn_string.cpp	Fri Mar 16 20:10:13 2007 +0000
     1.3 @@ -15,6 +15,7 @@
     1.4  #include <stdlib.h>
     1.5  #include "sm_globals.h"
     1.6  #include "sm_stringutil.h"
     1.7 +#include "TextParsers.h"
     1.8  
     1.9  inline const char *_strstr(const char *str, const char *substr)
    1.10  {
    1.11 @@ -219,9 +220,99 @@
    1.12  	return total;
    1.13  }
    1.14  
    1.15 -REGISTER_NATIVES(basicstrings)
    1.16 +/* :TODO: make this UTF8 safe */
    1.17 +static cell_t StrBreak(IPluginContext *pContext, const cell_t *params)
    1.18 +{
    1.19 +	const char *input;
    1.20 +	char *out;
    1.21 +	size_t outMax;
    1.22 +
    1.23 +	/* Get parameters */
    1.24 +	pContext->LocalToString(params[1], (char **)&input);
    1.25 +	pContext->LocalToString(params[2], &out);
    1.26 +	outMax = params[3];
    1.27 +
    1.28 +	const char *inptr = input;
    1.29 +	/* Eat up whitespace */
    1.30 +	while (*inptr != '\0' && IsWhitespace(inptr))
    1.31 +	{
    1.32 +		inptr++;
    1.33 +	}
    1.34 +
    1.35 +	if (*inptr == '\0')
    1.36 +	{
    1.37 +		if (outMax)
    1.38 +		{
    1.39 +			*out = '\0';
    1.40 +		}
    1.41 +		return -1;
    1.42 +	}
    1.43 +
    1.44 +	const char *start, *end = NULL;
    1.45 +
    1.46 +	bool quoted = (*inptr == '"');
    1.47 +	if (quoted)
    1.48 +	{
    1.49 +		inptr++;
    1.50 +		start = inptr;
    1.51 +		/* Read input until we reach a quote. */
    1.52 +		while (*inptr != '\0' && *inptr != '"')
    1.53 +		{
    1.54 +			/* Update the end point, increment the stream. */
    1.55 +			end = inptr++;
    1.56 +		}
    1.57 +		/* Read one more token if we reached an end quote */
    1.58 +		if (*inptr == '"')
    1.59 +		{
    1.60 +			inptr++;
    1.61 +		}
    1.62 +	} else {
    1.63 +		start = inptr;
    1.64 +		/* Read input until we reach a space */
    1.65 +		while (*inptr != '\0' && !IsWhitespace(inptr))
    1.66 +		{
    1.67 +			/* Update the end point, increment the stream. */
    1.68 +			end = inptr++;
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	/* Copy the string we found, if necessary */
    1.73 +	if (end == NULL)
    1.74 +	{
    1.75 +		if (outMax)
    1.76 +		{
    1.77 +			*out = '\0';
    1.78 +		}
    1.79 +	} else if (outMax) {
    1.80 +		char *outptr = out;
    1.81 +		outMax--;
    1.82 +		for (const char *ptr=start;
    1.83 +			(ptr <= end) && ((unsigned)(outptr - out) < (outMax));
    1.84 +			ptr++, outptr++)
    1.85 +			{
    1.86 +			*outptr = *ptr;
    1.87 +		}
    1.88 +		*outptr = '\0';
    1.89 +	}
    1.90 +
    1.91 +	/* Consume more of the string until we reach non-whitespace */
    1.92 +	while (*inptr != '\0' && IsWhitespace(inptr))
    1.93 +	{
    1.94 +		inptr++;
    1.95 +	}
    1.96 +
    1.97 +	if (*inptr == '\0')
    1.98 +	{
    1.99 +		return -1;
   1.100 +	}
   1.101 +
   1.102 +	return inptr - input;
   1.103 +}
   1.104 +
   1.105 +REGISTER_NATIVES(basicStrings)
   1.106  {
   1.107  	{"strlen",				sm_strlen},
   1.108 +	{"StrBreak",			StrBreak},
   1.109  	{"StrContains",			sm_contain},
   1.110  	{"StrCompare",			sm_strcmp},
   1.111  	{"StrCopy",				sm_strcopy},