]> mj.ucw.cz Git - libucw.git/commitdiff
Extended types: Miscellaneous fixes of xt_bool
authorMartin Mares <mj@ucw.cz>
Fri, 25 Jul 2014 10:42:29 +0000 (12:42 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 25 Jul 2014 10:42:29 +0000 (12:42 +0200)
  •  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

index 2abc3acc3671a3871d3323474726d3eda6bfd51f..5fcd0f0d979bda9306b004e1eb465c5662bbff4a 100644 (file)
@@ -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)