2 * UCW Library -- Basic Extended Types
4 * (c) 2014 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/mempool.h>
12 #include <ucw/strtonum.h>
13 #include <ucw/xtypes.h>
17 #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
19 return mp_printf(pool, _fmt, *(_type *)src);\
22 #define XTYPE_NUM_PARSE(_typename) static const char *xt_##_typename##_parse(const char *str, void *dest, struct mempool *pool UNUSED)\
24 return str_to_##_typename(dest, str, NULL, 10 | STN_WHOLE | STN_MINUS | STN_PLUS | STN_HEX | STN_BIN | STN_OCT);\
27 #define XTYPE_NUM_STRUCT(_type, _typename) const struct xtype xt_##_typename = {\
28 .size = sizeof(_type),\
30 .parse = xt_##_typename##_parse,\
31 .format = xt_##_typename##_format,\
34 #define XTYPE_NUM_DEF(_type, _fmt, _typename) XTYPE_NUM_FORMAT(_type, _fmt, _typename) \
35 XTYPE_NUM_PARSE(_typename)\
36 XTYPE_NUM_STRUCT(_type, _typename)
38 XTYPE_NUM_DEF(int, "%d", int)
39 XTYPE_NUM_DEF(s64, PRId64, s64)
40 XTYPE_NUM_DEF(intmax_t, "%jd", intmax)
41 XTYPE_NUM_DEF(uint, "%u", uint)
42 XTYPE_NUM_DEF(u64, PRIu64, u64)
43 XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax)
47 static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool)
51 return mp_printf(pool, "%.2lf", *(double *)src);
52 case XTYPE_FMT_PRETTY:
53 return mp_printf(pool, "%.2lf", *(double *)src);
55 case XTYPE_FMT_DEFAULT:
57 return mp_printf(pool, "%.2lf", *(double *)src);
61 static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED)
64 size_t sz = strlen(str);
66 double result = strtod(str, &endptr);
67 if(endptr != str + sz) return "Could not parse double.";
68 if(errno == ERANGE) return "Could not parse double: overflow happend during parsing";
70 *((double *) dest) = result;
75 const struct xtype xt_double = {
76 .size = sizeof(double),
78 .parse = xt_double_parse,
79 .format = xt_double_format,
84 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)
87 case XTYPE_FMT_DEFAULT:
88 case XTYPE_FMT_PRETTY:
89 return mp_printf(pool, "%s", *((bool *)src) ? "true" : "false");
91 return mp_printf(pool, "%s", *((bool *)src) ? "1" : "0");
93 die("Unsupported output type.");
97 static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED)
101 *((bool *)dest) = false;
105 *((bool *)dest) = true;
110 if(strcasecmp(str, "false") == 0) {
111 *((bool *)dest) = false;
115 if(strcasecmp(str, "true") == 0) {
116 *((bool *)dest) = true;
120 return "Could not parse bool.";
123 const struct xtype xt_bool = {
124 .size = sizeof(bool),
126 .parse = xt_bool_parse,
127 .format = xt_bool_format,
132 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)
134 return mp_printf(pool, "%s", *((char **) src));
137 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)
139 *((const char **) dest) = str;
143 const struct xtype xt_str = {
144 .size = sizeof(char *),
146 .parse = xt_str_parse,
147 .format = xt_str_format,