fixed a "-1" bug in atcprintf

moved string functions to the stringutils file
added strncopy

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40235
This commit is contained in:
Borja Ferrer 2006-12-29 23:28:44 +00:00
parent f068587ecc
commit 4153221ca8
3 changed files with 72 additions and 58 deletions

View File

@ -1,3 +1,5 @@
#include <stdlib.h>
#include <ctype.h>
#include "sm_stringutil.h" #include "sm_stringutil.h"
#define ALT 0x00000001 /* alternate form */ #define ALT 0x00000001 /* alternate form */
@ -262,7 +264,7 @@ size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext
int n; int n;
char sign; char sign;
const char *fmt; const char *fmt;
size_t llen = maxlen; size_t llen = maxlen - 1;
buf_p = buffer; buf_p = buffer;
arg = *param; arg = *param;
@ -337,7 +339,7 @@ reswitch:
case 'c': case 'c':
{ {
CHECK_ARGS(0); CHECK_ARGS(0);
if (llen <= 1) if (!llen)
{ {
goto done; goto done;
} }
@ -387,7 +389,7 @@ reswitch:
} }
case '%': case '%':
{ {
if (llen <= 1) if (!llen)
{ {
goto done; goto done;
} }
@ -397,7 +399,7 @@ reswitch:
} }
case '\0': case '\0':
{ {
if (llen <= 1) if (!llen)
{ {
goto done; goto done;
} }
@ -407,7 +409,7 @@ reswitch:
} }
default: default:
{ {
if (llen <= 1) if (!llen)
{ {
goto done; goto done;
} }
@ -423,3 +425,61 @@ done:
*param = arg; *param = arg;
return (maxlen - llen); return (maxlen - llen);
} }
const char *stristr(const char *str, const char *substr)
{
if (!*substr)
{
return ((char *)str);
}
char *needle = (char *)substr;
char *prevloc = (char *)str;
char *haystack = (char *)str;
while (*haystack)
{
if (tolower(*haystack) == tolower(*needle))
{
haystack++;
if (!*++needle)
{
return prevloc;
}
} else {
haystack = ++prevloc;
needle = (char *)substr;
}
}
return NULL;
}
inline int StrConvInt(const char *str)
{
char *dummy;
return strtol(str, &dummy, 10);
}
inline float StrConvFloat(const char *str)
{
char *dummy;
return (float)strtod(str, &dummy);
}
int strncopy(char *dest, const char *src, size_t count)
{
if (!count)
{
return 0;
}
char *start = dest;
while ((*src) && (--count))
{
*dest++ = *src++;
}
*dest = '\0';
return (dest - start);
}

View File

@ -8,5 +8,9 @@
using namespace SourcePawn; using namespace SourcePawn;
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param); size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param);
const char *stristr(const char *str, const char *substr);
int StrConvInt(const char *str);
float StrConvFloat(const char *str);
int strncopy(char *dest, const char *src, size_t count);
#endif // _INCLUDE_SOURCEMOD_STRINGUTIL_H_ #endif // _INCLUDE_SOURCEMOD_STRINGUTIL_H_

View File

@ -1,41 +1,11 @@
#include "sm_platform.h" #include "sm_platform.h"
#include <ctype.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "sp_vm_api.h" #include "sp_vm_api.h"
#include "sp_vm_typeutil.h" #include "sp_vm_typeutil.h"
#include "sm_stringutil.h"
using namespace SourcePawn; using namespace SourcePawn;
const char *stristr(const char *str, const char *substr)
{
if (!*substr)
{
return ((char *)str);
}
char *needle = (char *)substr;
char *prevloc = (char *)str;
char *haystack = (char *)str;
while (*haystack)
{
if (tolower(*haystack) == tolower(*needle))
{
haystack++;
if (!*++needle)
{
return prevloc;
}
} else {
haystack = ++prevloc;
needle = (char *)substr;
}
}
return NULL;
}
inline const char *_strstr(const char *str, const char *substr) inline const char *_strstr(const char *str, const char *substr)
{ {
#ifdef PLATFORM_WINDOWS #ifdef PLATFORM_WINDOWS
@ -45,17 +15,6 @@ inline const char *_strstr(const char *str, const char *substr)
#endif #endif
} }
inline int StrConvInt(const char *str)
{
char *dummy;
return strtol(str, &dummy, 10);
}
inline float StrConvFloat(const char *str)
{
char *dummy;
return (float)strtod(str, &dummy);
}
/********************************************* /*********************************************
* * * *
@ -107,21 +66,12 @@ static cell_t sm_equal(IPluginContext *pCtx, const cell_t *params)
static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params) static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
{ {
char *dest, *src, *start; char *dest, *src;
int len;
pCtx->LocalToString(params[1], &dest); pCtx->LocalToString(params[1], &dest);
pCtx->LocalToString(params[3], &src); pCtx->LocalToString(params[3], &src);
len = params[2];
start = dest; return strncopy(dest, src, params[2]);
while ((*src) && (len--))
{
*dest++ = *src++;
}
*dest = '\0';
return (dest - start);
} }
static cell_t sm_strtonum(IPluginContext *pCtx, const cell_t *params) static cell_t sm_strtonum(IPluginContext *pCtx, const cell_t *params)