fixed atoi / atof

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@1631 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
Spoike 2005-11-30 01:02:10 +00:00
parent 1592df5945
commit 673a336982
2 changed files with 9 additions and 0 deletions

View file

@ -24,6 +24,9 @@ char *strstr(char *str, const char *sub);
void strlcpy(char *d, const char *s, int n); void strlcpy(char *d, const char *s, int n);
char *strchr(char *str, char sub); char *strchr(char *str, char sub);
float atof(char *str);
int atoi(char *str);
#else #else
#include <string.h> #include <string.h>

View file

@ -419,6 +419,7 @@ int atoi(char *str)
sign = -1; sign = -1;
str++; str++;
} }
else sign = 1;
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
{ {
@ -435,9 +436,11 @@ int atoi(char *str)
else if (*str >= 'A' && *str <= 'A'+base-10) else if (*str >= 'A' && *str <= 'A'+base-10)
num = num*base + (*str - 'A')+10; num = num*base + (*str - 'A')+10;
else break; //bad char else break; //bad char
str++;
} }
return num*sign; return num*sign;
} }
float atof(char *str) float atof(char *str)
{ {
int sign; int sign;
@ -452,12 +455,14 @@ float atof(char *str)
sign = -1; sign = -1;
str++; str++;
} }
else sign = 1;
while(1) while(1)
{//each time we find a new digit, increase the value of the previous digets by a factor of ten, and add the new {//each time we find a new digit, increase the value of the previous digets by a factor of ten, and add the new
if (*str >= '0' && *str <= '9') if (*str >= '0' && *str <= '9')
num = num*10 + (*str - '0'); num = num*10 + (*str - '0');
else break; //bad char else break; //bad char
str++;
} }
if (*str == '.') if (*str == '.')
{ //each time we find a new digit, decrease the value of the following digits. { //each time we find a new digit, decrease the value of the following digits.
@ -469,6 +474,7 @@ float atof(char *str)
num = num + (*str - '0')*unit; num = num + (*str - '0')*unit;
} }
else break; //bad char else break; //bad char
str++;
} }
} }
return num*sign; return num*sign;