use snprintf and atof if possible

This commit is contained in:
Yuichi Nishiwaki 2015-07-04 17:32:16 +09:00
parent 73b7d577d7
commit 3285cc021b
2 changed files with 28 additions and 8 deletions

View File

@ -330,9 +330,6 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) {
const char *p; const char *p;
char *sval; char *sval;
int ival; int ival;
#if PIC_ENABLE_FLOAT
double dval, dint;
#endif
void *vp; void *vp;
int cnt = 0; int cnt = 0;
@ -349,7 +346,16 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) {
cnt += print_int(pic, stream, ival, 10); cnt += print_int(pic, stream, ival, 10);
break; break;
#if PIC_ENABLE_FLOAT #if PIC_ENABLE_FLOAT
case 'f': # if PIC_ENABLE_LIBC
case 'f': {
char buf[100];
sprintf(buf, "%g", va_arg(ap, double));
xfputs(pic, buf, stream);
break;
}
# else
case 'f': {
double dval, dint;
dval = modf(va_arg(ap, double), &dint); dval = modf(va_arg(ap, double), &dint);
if (dint < 0 || dval < 0) { /* either may be zero */ if (dint < 0 || dval < 0) { /* either may be zero */
xputc(pic, '-', stream); xputc(pic, '-', stream);
@ -369,6 +375,8 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) {
cnt += print_int(pic, stream, ival, 10); cnt += print_int(pic, stream, ival, 10);
} }
break; break;
}
# endif
#endif #endif
case 's': case 's':
sval = va_arg(ap, char*); sval = va_arg(ap, char*);

View File

@ -267,15 +267,25 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
{ {
unsigned u; unsigned u;
int exp, s, i, e; int exp, s, i, e;
#if PIC_ENABLE_FLOAT
double f, g;
#endif
u = read_uinteger(pic, port, c); u = read_uinteger(pic, port, c);
switch (peek(pic, port)) { switch (peek(pic, port)) {
#if PIC_ENABLE_FLOAT #if PIC_ENABLE_FLOAT
case '.': # if PIC_ENABLE_LIBC
case '.': {
char buf[256];
i = sprintf(buf, "%d", u);
buf[i++] = next(pic, port);
while (isdigit(c = peek(pic, port))) {
buf[i++] = next(pic, port);
}
sprintf(buf + i, "e%d", read_suffix(pic, port));
return pic_float_value(atof(buf));
}
# else
case '.': {
double f, g;
next(pic, port); next(pic, port);
g = 0, e = 0; g = 0, e = 0;
while (isdigit(c = peek(pic, port))) { while (isdigit(c = peek(pic, port))) {
@ -301,6 +311,8 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
exp >>= 1; exp >>= 1;
} }
return pic_float_value(f); return pic_float_value(f);
}
# endif
#endif #endif
default: default: