X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fxtypes-basic.c;h=c3dc1d6fb7fcb2025e94aee8a7a191987ed459b4;hb=93af84243ca8181a97bcc43142df30114b32f315;hp=5cd74adb26e39268dfb9467f473b04ba773e7fbd;hpb=751371528e0b1fd94612e01ed42ad8ae1bbd4fd0;p=libucw.git diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 5cd74adb..c3dc1d6f 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -55,12 +55,12 @@ static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool) switch(fmt) { case XTYPE_FMT_RAW: - return mp_printf(pool, "%.10lf", *(double *)src); + 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, "%.5lf", *(double *)src); + return mp_printf(pool, "%.6lg", *(double *)src); } } @@ -70,18 +70,32 @@ static const char *xt_double_parse(const char *str, void *dest, struct mempool * errno = 0; double result = strtod(str, &endptr); if(*endptr != 0 || endptr == str) return "Could not parse double."; - if(errno == ERANGE) return "Could not parse double: overflow happend during parsing."; + if(errno == ERANGE) return "Could not parse double."; *((double *) dest) = result; return NULL; } +static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempool *pool) +{ + 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); + } + + *dest = XTYPE_FMT_DBL_FIXED_PREC(precision); + + return NULL; +} + const struct xtype xt_double = { .size = sizeof(double), .name = "double", .parse = xt_double_parse, .format = xt_double_format, + .parse_fmt = xt_double_fmt_parse }; /* bool */ @@ -89,9 +103,9 @@ const struct xtype xt_double = { static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool) { switch(fmt) { - case XTYPE_FMT_DEFAULT: case XTYPE_FMT_PRETTY: return mp_printf(pool, "%s", *((bool *)src) ? "true" : "false"); + case XTYPE_FMT_DEFAULT: case XTYPE_FMT_RAW: return mp_printf(pool, "%s", *((bool *)src) ? "1" : "0"); default: @@ -104,7 +118,7 @@ static const char *xt_bool_parse(const char *str, void *dest, struct mempool *po if(!str) return "Cannot parse bool: string is NULL."; if(str[1] == 0) { - if(str[0] == '1') { + if(str[0] == '0') { *((bool *)dest) = false; return NULL; } @@ -127,18 +141,13 @@ static const char *xt_bool_parse(const char *str, void *dest, struct mempool *po return "Could not parse bool."; } -const struct xtype xt_bool = { - .size = sizeof(bool), - .name = "bool", - .parse = xt_bool_parse, - .format = xt_bool_format, -}; +XTYPE_NUM_STRUCT(bool, bool) /* str */ static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) { - return mp_strdup(pool, (const char *) src); + return mp_strdup(pool, *((const char **) src)); } static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED) @@ -147,9 +156,4 @@ static const char *xt_str_parse(const char *str, void *dest, struct mempool *poo return NULL; } -const struct xtype xt_str = { - .size = sizeof(char *), - .name = "str", - .parse = xt_str_parse, - .format = xt_str_format, -}; +XTYPE_NUM_STRUCT(char *, str)