2 * UCW Library -- Basic Extended Types
4 * (c) 2014 Martin Mares <mj@ucw.cz>
5 * (c) 2014 Robert Kessl <robert.kessl@economia.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include <ucw/mempool.h>
13 #include <ucw/strtonum.h>
14 #include <ucw/xtypes.h>
19 #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
21 return mp_printf(pool, _fmt, *(_type *)src);\
24 #define XTYPE_NUM_PARSE(_typename) static const char *xt_##_typename##_parse(const char *str, void *dest, struct mempool *pool UNUSED)\
26 return str_to_##_typename(dest, str, NULL, 10 | STN_WHOLE | STN_MINUS | STN_PLUS | STN_HEX | STN_BIN | STN_OCT);\
29 #define XTYPE_NUM_STRUCT(_type, _typename) const struct xtype xt_##_typename = {\
30 .size = sizeof(_type),\
32 .parse = xt_##_typename##_parse,\
33 .format = xt_##_typename##_format,\
36 #define XTYPE_NUM_DEF(_type, _fmt, _typename) XTYPE_NUM_FORMAT(_type, _fmt, _typename) \
37 XTYPE_NUM_PARSE(_typename)\
38 XTYPE_NUM_STRUCT(_type, _typename)
40 XTYPE_NUM_DEF(int, "%d", int)
41 XTYPE_NUM_DEF(s64, "%" PRId64, s64)
42 XTYPE_NUM_DEF(intmax_t, "%jd", intmax)
43 XTYPE_NUM_DEF(uint, "%u", uint)
44 XTYPE_NUM_DEF(u64, "%" PRIu64, u64)
45 XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax)
49 static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool)
53 return mp_printf(pool, "%.10lf", *(double *)src);
54 case XTYPE_FMT_PRETTY:
55 return mp_printf(pool, "%.2lf", *(double *)src);
56 case XTYPE_FMT_DEFAULT:
58 return mp_printf(pool, "%.5lf", *(double *)src);
62 static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED)
65 size_t sz = strlen(str);
67 double result = strtod(str, &endptr);
68 if(endptr != str + sz) return "Could not parse double.";
69 if(errno == ERANGE) return "Could not parse double: overflow happend during parsing.";
71 *((double *) dest) = result;
76 const struct xtype xt_double = {
77 .size = sizeof(double),
79 .parse = xt_double_parse,
80 .format = xt_double_format,
85 static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool)
88 case XTYPE_FMT_DEFAULT:
89 case XTYPE_FMT_PRETTY:
90 return mp_printf(pool, "%s", *((bool *)src) ? "true" : "false");
92 return mp_printf(pool, "%s", *((bool *)src) ? "1" : "0");
94 die("Unsupported output type.");
98 static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED)
100 if(!str) return "Cannot parse bool: string is NULL.";
104 *((bool *)dest) = false;
108 *((bool *)dest) = true;
113 if(strcasecmp(str, "false") == 0) {
114 *((bool *)dest) = false;
118 if(strcasecmp(str, "true") == 0) {
119 *((bool *)dest) = true;
123 return "Could not parse bool.";
126 const struct xtype xt_bool = {
127 .size = sizeof(bool),
129 .parse = xt_bool_parse,
130 .format = xt_bool_format,
135 static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
137 return mp_strdup(pool, (const char *) src);
140 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)
142 *((const char **) dest) = str;
146 const struct xtype xt_str = {
147 .size = sizeof(char *),
149 .parse = xt_str_parse,
150 .format = xt_str_format,