From 3285cc021ba28824038e7dcd6521520cc6d9de0d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 4 Jul 2015 17:32:16 +0900 Subject: [PATCH] use snprintf and atof if possible --- extlib/benz/file.c | 16 ++++++++++++---- extlib/benz/read.c | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 3f62bf57..96f25c74 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -330,9 +330,6 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { const char *p; char *sval; int ival; -#if PIC_ENABLE_FLOAT - double dval, dint; -#endif void *vp; 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); break; #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); if (dint < 0 || dval < 0) { /* either may be zero */ 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); } break; + } +# endif #endif case 's': sval = va_arg(ap, char*); diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 537437d0..77cce237 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -267,15 +267,25 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) { unsigned u; int exp, s, i, e; -#if PIC_ENABLE_FLOAT - double f, g; -#endif u = read_uinteger(pic, port, c); switch (peek(pic, port)) { #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); g = 0, e = 0; while (isdigit(c = peek(pic, port))) { @@ -301,6 +311,8 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) exp >>= 1; } return pic_float_value(f); + } +# endif #endif default: