From bcc8a953a92a428e2f02a796a0232aa93d5562d8 Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Fri, 18 Jul 2014 14:00:12 +0200 Subject: [PATCH] xtypes: update of parsing/formatting of xt_double --- ucw/xtypes-basic.c | 8 ++++++-- ucw/xtypes.h | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 01247ebb..5cd74adb 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -48,6 +48,11 @@ 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, "%.10lf", *(double *)src); @@ -62,10 +67,9 @@ static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool) static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED) { char *endptr = NULL; - size_t sz = strlen(str); errno = 0; double result = strtod(str, &endptr); - if(endptr != str + sz) return "Could not parse double."; + if(*endptr != 0 || endptr == str) return "Could not parse double."; if(errno == ERANGE) return "Could not parse double: overflow happend during parsing."; *((double *) dest) = result; diff --git a/ucw/xtypes.h b/ucw/xtypes.h index 380524e8..840713be 100644 --- a/ucw/xtypes.h +++ b/ucw/xtypes.h @@ -98,4 +98,8 @@ extern const struct xtype xt_uintmax; extern const struct xtype xt_bool; extern const struct xtype xt_double; + +#define XTYPE_FMT_DBL_PREC 0x40000000 +#define XTYPE_FMT_DBL_FIXED_PREC(_prec) (_prec | XTYPE_FMT_DBL_PREC) + #endif -- 2.39.2