]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-basic.c
XTypes: Added support to configuration and option parser.
[libucw.git] / ucw / xtypes-basic.c
1 /*
2  *      UCW Library -- Basic Extended Types
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  *      (c) 2014 Robert Kessl <robert.kessl@economia.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <ucw/mempool.h>
13 #include <ucw/strtonum.h>
14 #include <ucw/xtypes.h>
15 #include <errno.h>
16 #include <inttypes.h>
17 #include <stdlib.h>
18
19 #define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
20 {\
21   return mp_printf(pool, _fmt, *(_type *)src);\
22 }
23
24 #define XTYPE_NUM_PARSE(_typename) static const char *xt_##_typename##_parse(const char *str, void *dest, struct mempool *pool UNUSED)\
25 {\
26   return str_to_##_typename(dest, str, NULL, 10 | STN_WHOLE | STN_MINUS | STN_PLUS | STN_HEX | STN_BIN | STN_OCT);\
27 }
28
29 #define XTYPE_NUM_STRUCT(_type, _typename) const struct xtype xt_##_typename = {\
30   .size = sizeof(_type),\
31   .name = #_typename,\
32   .parse = xt_##_typename##_parse,\
33   .format = xt_##_typename##_format,\
34 };
35
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)
39
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)
46
47 /* double */
48
49 static const char *xt_double_format(void *src, u32 fmt, struct mempool *pool)
50 {
51   double val = *((double *)src);
52
53   if (fmt & XT_DOUBLE_FMT_PREC_FLAG)
54     {
55       uint prec = fmt & ~XT_DOUBLE_FMT_PREC_FLAG;
56       return mp_printf(pool, "%.*lf", prec, val);
57     }
58
59   switch(fmt)
60     {
61     case XTYPE_FMT_RAW:
62       return mp_printf(pool, "%.15lg", val);
63     case XTYPE_FMT_PRETTY:
64       return mp_printf(pool, "%.2lf", val);
65     case XTYPE_FMT_DEFAULT:
66     default:
67       return mp_printf(pool, "%.6lg", val);
68     }
69 }
70
71 static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED)
72 {
73   char *endptr = NULL;
74   errno = 0;
75   double result = strtod(str, &endptr);
76   if (*endptr != 0 || endptr == str ||
77       errno == ERANGE)
78     return "Could not parse floating point number.";
79
80   *((double *) dest) = result;
81   return NULL;
82 }
83
84 static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempool *pool)
85 {
86   uint precision = 0;
87   const char *tmp_err = str_to_uint(&precision, str, NULL, 10);
88   if (tmp_err)
89     return mp_printf(pool, "Could not parse floating point number precision: %s", tmp_err);
90
91   *dest = XT_DOUBLE_FMT_PREC(precision);
92   return NULL;
93 }
94
95 const struct xtype xt_double = {
96   .size = sizeof(double),
97   .name = "double",
98   .parse = xt_double_parse,
99   .format = xt_double_format,
100   .parse_fmt = xt_double_fmt_parse
101 };
102
103 /* bool */
104
105 static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool UNUSED)
106 {
107   bool val = *((bool *)src);
108   switch (fmt)
109     {
110     case XTYPE_FMT_PRETTY:
111       return val ? "true" : "false";
112     case XTYPE_FMT_DEFAULT:
113     case XTYPE_FMT_RAW:
114     default:
115       return val ? "1" : "0";
116     }
117 }
118
119 static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED)
120 {
121   if (!strcmp(str, "0") || !strcmp(str, "false") || !strcmp(str, "no"))
122     {
123       *((bool *)dest) = false;
124       return NULL;
125     }
126   else if (!strcmp(str, "1") || !strcmp(str, "true") || !strcmp(str, "yes"))
127     {
128       *((bool *)dest) = true;
129       return NULL;
130     }
131
132   return "Could not parse a boolean value.";
133 }
134
135 XTYPE_NUM_STRUCT(bool, bool)
136
137 /* str */
138
139 static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
140 {
141   return mp_strdup(pool, *((const char **) src));
142 }
143
144 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool)
145 {
146   *((const char **) dest) = mp_strdup(pool, str);
147   return NULL;
148 }
149
150 XTYPE_NUM_STRUCT(char *, str)