]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/xtypes-basic.c
tableprinter: removed some obsolete FIXME, added some comments
[libucw.git] / ucw / xtypes-basic.c
index 5fcd0f0d979bda9306b004e1eb465c5662bbff4a..26d7cb1f54b191ac64cdbcd2014573d83ed09a73 100644 (file)
@@ -13,8 +13,8 @@
 #include <ucw/strtonum.h>
 #include <ucw/xtypes.h>
 #include <errno.h>
-#include <stdlib.h>
 #include <inttypes.h>
+#include <stdlib.h>
 
 #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
 {\
@@ -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 & XT_DOUBLE_FMT_PREC_FLAG)
+    {
+      uint prec = fmt & ~XT_DOUBLE_FMT_PREC_FLAG;
+      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);
-  }
-
-  *dest = XTYPE_FMT_DBL_FIXED_PREC(precision);
+  if (tmp_err)
+    return mp_printf(pool, "Could not parse floating point number precision: %s", tmp_err);
 
+  *dest = XT_DOUBLE_FMT_PREC(precision);
   return NULL;
 }
 
@@ -109,9 +111,8 @@ static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *poo
       return val ? "true" : "false";
     case XTYPE_FMT_DEFAULT:
     case XTYPE_FMT_RAW:
-      return val ? "1" : "0";
     default:
-      ASSERT(0);
+      return val ? "1" : "0";
     }
 }