From 2f0ee61b197467bcf52a0e69d302d48531fef86a Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 25 Jul 2014 12:42:29 +0200 Subject: [PATCH] Extended types: Miscellaneous fixes of xt_bool MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit • Streamlined code and unified coding style. • xt_bool_format() returns static strings, there is no need to duplicate them. • xt_bool_parse() returns a more reasonable error message. • Invalid formatting mode is an internal error, which should not be triggerable by the user, so ASSERT is enough. --- ucw/xtypes-basic.c | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/ucw/xtypes-basic.c b/ucw/xtypes-basic.c index 2abc3acc..5fcd0f0d 100644 --- a/ucw/xtypes-basic.c +++ b/ucw/xtypes-basic.c @@ -100,55 +100,35 @@ const struct xtype xt_double = { /* bool */ -static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool) +static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool UNUSED) { - switch(fmt) { + bool val = *((bool *)src); + switch (fmt) + { case XTYPE_FMT_PRETTY: - return mp_printf(pool, "%s", *((bool *)src) ? "true" : "false"); + return val ? "true" : "false"; case XTYPE_FMT_DEFAULT: case XTYPE_FMT_RAW: - return mp_printf(pool, "%s", *((bool *)src) ? "1" : "0"); + return val ? "1" : "0"; default: - die("Unsupported output type."); - } + ASSERT(0); + } } static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED) { - if(!str) return "Cannot parse bool: string is NULL."; - - if(str[1] == 0) { - if(str[0] == '0') { + if (!strcmp(str, "0") || !strcmp(str, "false") || !strcmp(str, "no")) + { *((bool *)dest) = false; return NULL; } - if(str[0] == '1') { + else if (!strcmp(str, "1") || !strcmp(str, "true") || !strcmp(str, "yes")) + { *((bool *)dest) = true; return NULL; } - } - - if(strcasecmp(str, "false") == 0) { - *((bool *)dest) = false; - return NULL; - } - - if(strcasecmp(str, "true") == 0) { - *((bool *)dest) = true; - return NULL; - } - - if(strcasecmp(str, "no") == 0) { - *((bool *)dest) = false; - return NULL; - } - - if(strcasecmp(str, "yes") == 0) { - *((bool *)dest) = true; - return NULL; - } - return "Could not parse bool."; + return "Could not parse a boolean value."; } XTYPE_NUM_STRUCT(bool, bool) -- 2.39.2