apps/interpreters/minibasic: Use strtod() instead of fscanf()

This commit is contained in:
Gregory Nutt 2016-08-11 12:25:43 -06:00
parent c17c7f8afe
commit 37b6303751

View File

@ -1169,23 +1169,41 @@ static void doinput(void)
{ {
case FLTID: case FLTID:
{ {
/* REVISIT: fscanf() is not yet available in the NuttX libc. FAR char *ptr;
* Recommendation: Replace fscanf() with this logic. int nch;
* (1) Copy floating point number to a g_iobuffer. Skip
* over leading spaces and terminated with a NUL when a /* Copy floating point number to a g_iobuffer. Skip over leading
* space, newline, EOF, or comma is detected. * spaces and terminate with a NUL when a space, tab, newline, EOF,
* (2) Use strtod() to get the floating point value from the * or comma is detected.
* substring in g_iobuffer.
*/ */
while (fscanf(g_fpin, "%lf", lv.dval) != 1) for (nch = 0, ptr = g_iobuffer; nch < (IOBUFSIZE-1); nch++)
{ {
fgetc(g_fpin); int ch = fgetc(g_fpin);
if (feof(g_fpin)) if (ch == EOF)
{ {
seterror(ERR_EOF); seterror(ERR_EOF);
return; return;
} }
if (ch == ' ' || ch == '\t' || ch == ',' || ch == '\n')
{
ungetc(ch, g_fpin);
break;
}
}
*ptr = '\0';
/* Use strtod() to get the floating point value from the substring
* in g_iobuffer.
*/
*lv.dval = strtod(g_iobuffer, &ptr);
if (ptr == g_iobuffer)
{
seterror(ERR_SYNTAX);
return;
} }
} }
break; break;
@ -1195,7 +1213,7 @@ static void doinput(void)
if (*lv.sval) if (*lv.sval)
{ {
free(*lv.sval); free(*lv.sval);
*lv.sval = 0; *lv.sval = NULL;
} }
if (fgets(g_iobuffer, IOBUFSIZE, g_fpin) == 0) if (fgets(g_iobuffer, IOBUFSIZE, g_fpin) == 0)
@ -1263,8 +1281,8 @@ static void lvalue(FAR struct mb_lvalue_s *lv)
int type; int type;
lv->type = SYNTAX_ERROR; lv->type = SYNTAX_ERROR;
lv->dval = 0; lv->dval = NULL;
lv->sval = 0; lv->sval = NULL;
switch (g_token) switch (g_token)
{ {
@ -1286,7 +1304,7 @@ static void lvalue(FAR struct mb_lvalue_s *lv)
lv->type = FLTID; lv->type = FLTID;
lv->dval = &var->dval; lv->dval = &var->dval;
lv->sval = 0; lv->sval = NULL;
} }
break; break;
@ -1308,7 +1326,7 @@ static void lvalue(FAR struct mb_lvalue_s *lv)
lv->type = STRID; lv->type = STRID;
lv->sval = &var->sval; lv->sval = &var->sval;
lv->dval = 0; lv->dval = NULL;
} }
break; break;
@ -2483,8 +2501,8 @@ static FAR struct mb_variable_s *addfloat(FAR const char *id)
{ {
g_variables = vars; g_variables = vars;
strcpy(g_variables[g_nvariables].id, id); strcpy(g_variables[g_nvariables].id, id);
g_variables[g_nvariables].dval = 0; g_variables[g_nvariables].dval = 0.0;
g_variables[g_nvariables].sval = 0; g_variables[g_nvariables].sval = NULL;
g_nvariables++; g_nvariables++;
return &g_variables[g_nvariables - 1]; return &g_variables[g_nvariables - 1];
} }
@ -2516,8 +2534,8 @@ static FAR struct mb_variable_s *addstring(FAR const char *id)
{ {
g_variables = vars; g_variables = vars;
strcpy(g_variables[g_nvariables].id, id); strcpy(g_variables[g_nvariables].id, id);
g_variables[g_nvariables].sval = 0; g_variables[g_nvariables].sval = NULL;
g_variables[g_nvariables].dval = 0; g_variables[g_nvariables].dval = 0.0;
g_nvariables++; g_nvariables++;
return &g_variables[g_nvariables - 1]; return &g_variables[g_nvariables - 1];
} }
@ -2549,8 +2567,8 @@ static FAR struct mb_dimvar_s *adddimvar(FAR const char *id)
{ {
g_dimvariables = vars; g_dimvariables = vars;
strcpy(g_dimvariables[g_ndimvariables].id, id); strcpy(g_dimvariables[g_ndimvariables].id, id);
g_dimvariables[g_ndimvariables].dval = 0; g_dimvariables[g_ndimvariables].dval = NULL;
g_dimvariables[g_ndimvariables].str = 0; g_dimvariables[g_ndimvariables].str = NULL;
g_dimvariables[g_ndimvariables].ndims = 0; g_dimvariables[g_ndimvariables].ndims = 0;
g_dimvariables[g_ndimvariables].type = strchr(id, '$') ? STRID : FLTID; g_dimvariables[g_ndimvariables].type = strchr(id, '$') ? STRID : FLTID;
g_ndimvariables++; g_ndimvariables++;
@ -2681,7 +2699,6 @@ static FAR char *stringexpr(void)
static FAR char *chrstring(void) static FAR char *chrstring(void)
{ {
double x; double x;
char buff[6];
FAR char *answer; FAR char *answer;
match(CHRSTRING); match(CHRSTRING);
@ -2689,9 +2706,9 @@ static FAR char *chrstring(void)
x = integer(expr()); x = integer(expr());
match(CPAREN); match(CPAREN);
buff[0] = (char)x; g_iobuffer[0] = (char)x;
buff[1] = 0; g_iobuffer[1] = 0;
answer = mystrdup(buff); answer = mystrdup(g_iobuffer);
if (!answer) if (!answer)
{ {
@ -2712,7 +2729,6 @@ static FAR char *chrstring(void)
static FAR char *strstring(void) static FAR char *strstring(void)
{ {
double x; double x;
char buff[64];
FAR char *answer; FAR char *answer;
match(STRSTRING); match(STRSTRING);
@ -2720,8 +2736,8 @@ static FAR char *strstring(void)
x = expr(); x = expr();
match(CPAREN); match(CPAREN);
sprintf(buff, "%g", x); sprintf(g_iobuffer, "%g", x);
answer = mystrdup(buff); answer = mystrdup(g_iobuffer);
if (!answer) if (!answer)
{ {
seterror(ERR_OUTOFMEMORY); seterror(ERR_OUTOFMEMORY);
@ -3566,7 +3582,6 @@ static int gettoken(FAR const char *str)
static int tokenlen(FAR const char *str, int tokenid) static int tokenlen(FAR const char *str, int tokenid)
{ {
int len = 0; int len = 0;
char buff[32];
switch (tokenid) switch (tokenid)
{ {
@ -3583,11 +3598,11 @@ static int tokenlen(FAR const char *str, int tokenid)
case DIMSTRID: case DIMSTRID:
case DIMFLTID: case DIMFLTID:
case STRID: case STRID:
getid(str, buff, &len); getid(str, g_iobuffer, &len);
return len; return len;
case FLTID: case FLTID:
getid(str, buff, &len); getid(str, g_iobuffer, &len);
return len; return len;
case PI: case PI: