]> mj.ucw.cz Git - libucw.git/blob - ucw/xtypes-basic.c
Extended types: Parsing of xt_str now copies the string
[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 <stdlib.h>
17 #include <inttypes.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   if(fmt & XTYPE_FMT_DBL_PREC) {
52     uint prec = fmt & ~XTYPE_FMT_DBL_PREC;
53     return mp_printf(pool, "%.*lf", prec, *(double *)src);
54   }
55
56   switch(fmt) {
57   case XTYPE_FMT_RAW:
58     return mp_printf(pool, "%.15lg", *(double *)src);
59   case XTYPE_FMT_PRETTY:
60     return mp_printf(pool, "%.2lf", *(double *)src);
61   case XTYPE_FMT_DEFAULT:
62   default:
63     return mp_printf(pool, "%.6lg", *(double *)src);
64   }
65 }
66
67 static const char *xt_double_parse(const char *str, void *dest, struct mempool *pool UNUSED)
68 {
69   char *endptr = NULL;
70   errno = 0;
71   double result = strtod(str, &endptr);
72   if(*endptr != 0 || endptr == str) return "Could not parse floating point number.";
73   if(errno == ERANGE) return "Could not parse floating point number.";
74
75   *((double *) dest) = result;
76
77   return NULL;
78 }
79
80 static const char * xt_double_fmt_parse(const char *str, u32 *dest, struct mempool *pool)
81 {
82   uint precision = 0;
83   const char *tmp_err = str_to_uint(&precision, str, NULL, 0);
84   if(tmp_err) {
85     return mp_printf(pool, "An error occured while parsing precision: %s.", tmp_err);
86   }
87
88   *dest = XTYPE_FMT_DBL_FIXED_PREC(precision);
89
90   return NULL;
91 }
92
93 const struct xtype xt_double = {
94   .size = sizeof(double),
95   .name = "double",
96   .parse = xt_double_parse,
97   .format = xt_double_format,
98   .parse_fmt = xt_double_fmt_parse
99 };
100
101 /* bool */
102
103 static const char *xt_bool_format(void *src, u32 fmt UNUSED, struct mempool *pool)
104 {
105   switch(fmt) {
106     case XTYPE_FMT_PRETTY:
107       return mp_printf(pool, "%s", *((bool *)src) ? "true" : "false");
108     case XTYPE_FMT_DEFAULT:
109     case XTYPE_FMT_RAW:
110       return mp_printf(pool, "%s", *((bool *)src) ? "1" : "0");
111     default:
112       die("Unsupported output type.");
113   }
114 }
115
116 static const char *xt_bool_parse(const char *str, void *dest, struct mempool *pool UNUSED)
117 {
118   if(!str) return "Cannot parse bool: string is NULL.";
119
120   if(str[1] == 0) {
121     if(str[0] == '0') {
122       *((bool *)dest) = false;
123       return NULL;
124     }
125     if(str[0] == '1') {
126       *((bool *)dest) = true;
127       return NULL;
128     }
129   }
130
131   if(strcasecmp(str, "false") == 0) {
132     *((bool *)dest) = false;
133     return NULL;
134   }
135
136   if(strcasecmp(str, "true") == 0) {
137     *((bool *)dest) = true;
138     return NULL;
139   }
140
141   if(strcasecmp(str, "no") == 0) {
142     *((bool *)dest) = false;
143     return NULL;
144   }
145
146   if(strcasecmp(str, "yes") == 0) {
147     *((bool *)dest) = true;
148     return NULL;
149   }
150
151   return "Could not parse bool.";
152 }
153
154 XTYPE_NUM_STRUCT(bool, bool)
155
156 /* str */
157
158 static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
159 {
160   return mp_strdup(pool, *((const char **) src));
161 }
162
163 static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool)
164 {
165   *((const char **) dest) = mp_strdup(pool, str);
166   return NULL;
167 }
168
169 XTYPE_NUM_STRUCT(char *, str)