]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/xtypes-basic.c
xtypes&tableprinter: added parsing of size and tests
[libucw.git] / ucw / xtypes-basic.c
index 0438045709dcb1084f39e57d482f8957a21fbe29..4ee14cfa111a17da40c62eb7cdba7fdcb274cd7b 100644 (file)
@@ -2,6 +2,7 @@
  *     UCW Library -- Basic Extended Types
  *
  *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014 Robert Kessl <robert.kessl@economia.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -13,6 +14,7 @@
 #include <ucw/xtypes.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <inttypes.h>
 
 #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
 {\
   XTYPE_NUM_STRUCT(_type, _typename)
 
 XTYPE_NUM_DEF(int, "%d", int)
-XTYPE_NUM_DEF(s64, PRId64, s64)
+XTYPE_NUM_DEF(s64, "%" PRId64, s64)
 XTYPE_NUM_DEF(intmax_t, "%jd", intmax)
 XTYPE_NUM_DEF(uint, "%u", uint)
-XTYPE_NUM_DEF(u64, PRIu64, u64)
+XTYPE_NUM_DEF(u64, "%" PRIu64, u64)
 XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax)
 
 /* double */
 
 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, "%.2lf", *(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, "%.2lf", *(double *)src);
+    return mp_printf(pool, "%.6lg", *(double *)src);
   }
 }
 
 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(errno == ERANGE) return "Could not parse double: overflow happend during parsing";
+  if(*endptr != 0 || endptr == str) return "Could not parse double.";
+  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 */
 
-static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val)
+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:
@@ -96,6 +115,8 @@ static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *poo
 
 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] == '1') {
       *((bool *)dest) = false;
@@ -120,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) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val)
+
+static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
 {
-  return mp_printf(pool, "%s", *((char **) src));
+  return mp_strdup(pool, *((const char **) src));
 }
 
 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)
@@ -140,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)