Added natives str_to_float and float_to_str; indents are now tabs

This commit is contained in:
Pavol Marko 2005-09-06 16:29:27 +00:00
parent d3e2bad4e7
commit af79fe8e20

View File

@ -223,14 +223,74 @@ static cell AMX_NATIVE_CALL containi(AMX *amx, cell *params) /* 2 param */
static cell AMX_NATIVE_CALL strtonum(AMX *amx, cell *params) /* 1 param */
{
int iLen;
return atoi(get_amxstring(amx,params[1],0,iLen));
return atoi(get_amxstring(amx, params[1], 0, iLen));
}
static cell AMX_NATIVE_CALL numtostr(AMX *amx, cell *params) /* 3 param */
{
char szTemp[32];
sprintf(szTemp,"%d",(int)params[1]);
return set_amxstring(amx,params[2],szTemp,params[3]);
sprintf(szTemp, "%d", (int)params[1]);
return set_amxstring(amx, params[2], szTemp, params[3]);
}
static cell AMX_NATIVE_CALL str_to_float(AMX *amx, cell *params)
{
cell *str = get_amxaddr(amx, params[1]);
bool neg=false;
unsigned long part1 = 0;
if (*str == '-')
{
neg = true;
++str;
}
else if (*str == '+')
++str;
while (*str)
{
if (*str == '.')
{
++str;
break;
}
if (*str < '0' || *str > '9')
{
REAL fl = neg ? -static_cast<REAL>(part1) : static_cast<REAL>(part1);
return amx_ftoc(fl);
}
part1 *= 10;
part1 += *str - '0';
++str;
}
unsigned long part2 = 0;
unsigned long div = 1;
while (*str)
{
if (*str < '0' || *str > '9')
break;
part2 *= 10;
part2 += *str - '0';
div *= 10;
++str;
}
REAL fl = static_cast<REAL>(part1) + (static_cast<REAL>(part2) / div);
if (neg)
fl = -fl;
return amx_ftoc(fl);
}
static cell AMX_NATIVE_CALL float_to_str(AMX *amx, cell *params)
{
char szTemp[32];
sprintf(szTemp, "%f", amx_ctof(params[1]));
return set_amxstring(amx, params[2], szTemp, params[3]);
}
static cell AMX_NATIVE_CALL add(AMX *amx, cell *params) /* 4 param */
@ -749,6 +809,8 @@ AMX_NATIVE_INFO string_Natives[] = {
{ "strcat", n_strcat },
{ "strfind", n_strfind },
{ "strcmp", n_strcmp },
{ "str_to_float", str_to_float },
{ "float_to_str", float_to_str },
{ NULL, NULL }
};