From 0d540b3e313c3d99f50bdac9a1aeffc911c1ec6c Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 25 Jul 2014 12:49:22 +0200 Subject: [PATCH] Extended types: Cleanup of xt_double --- ucw/xtypes-basic.c | 44 +++++++++++++++++++++++--------------------- ucw/xtypes.h | 6 +++--- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 5fcd0f0d..1c4696b3 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -48,20 +48,24 @@ XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax) static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool) { - if(fmt & XTYPE_FMT_DBL_PREC) { - uint prec = fmt & ~XTYPE_FMT_DBL_PREC; - return mp_printf(pool, "%.*lf", prec, *(double *)src); - } - - switch(fmt) { - case XTYPE_FMT_RAW: - return mp_printf(pool, "%.15lg", *(double *)src); - case XTYPE_FMT_PRETTY: - return mp_printf(pool, "%.2lf", *(double *)src); - case XTYPE_FMT_DEFAULT: - default: - return mp_printf(pool, "%.6lg", *(double *)src); - } + double val = *((double *)src); + + if (fmt & XTYPE_FMT_DBL_PREC) + { + uint prec = fmt & ~XTYPE_FMT_DBL_PREC; + return mp_printf(pool, "%.*lf", prec, val); + } + + switch(fmt) + { + case XTYPE_FMT_RAW: + return mp_printf(pool, "%.15lg", val); + case XTYPE_FMT_PRETTY: + return mp_printf(pool, "%.2lf", val); + case XTYPE_FMT_DEFAULT: + default: + return mp_printf(pool, "%.6lg", val); + } } static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED) @@ -69,11 +73,11 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool * char *endptr = NULL; errno = 0; double result = strtod(str, &endptr); - if(*endptr != 0 || endptr == str) return "Could not parse floating point number."; - if(errno == ERANGE) return "Could not parse floating point number."; + if (*endptr != 0 || endptr == str || + errno == ERANGE) + return "Could not parse floating point number."; *((double *) dest) = result; - return NULL; } @@ -81,12 +85,10 @@ static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempo { uint precision = 0; const char *tmp_err = str_to_uint(&precision, str, NULL, 0); - if(tmp_err) { - return mp_printf(pool, "An error occured while parsing precision: %s.", tmp_err); - } + if (tmp_err) + return mp_printf(pool, "Could not parse floating point number precision: %s", tmp_err); *dest = XTYPE_FMT_DBL_FIXED_PREC(precision); - return NULL; } diff --git a/ucw/xtypes.h b/ucw/xtypes.h index d57e8a95..0d801ad4 100644 --- a/ucw/xtypes.h +++ b/ucw/xtypes.h @@ -130,8 +130,8 @@ extern const struct xtype xt_uintmax; extern const struct xtype xt_bool; extern const struct xtype xt_double; - -#define XTYPE_FMT_DBL_PREC XTYPE_FMT_CUSTOM -#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_CUSTOM) +// Fixed-precision formats for xt_double +#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_DBL_PREC) +#define XTYPE_FMT_DBL_PREC XTYPE_FMT_CUSTOM #endif -- 2.39.2