save
[protos/libecoli.git] / lib / ecoli_node_int.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <limits.h>
34 #include <ctype.h>
35 #include <errno.h>
36
37 #include <ecoli_log.h>
38 #include <ecoli_malloc.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_node_int.h>
42 #include <ecoli_test.h>
43
44 struct ec_node_int {
45         struct ec_node gen;
46         bool check_min;
47         long long int min;
48         bool check_max;
49         long long int max;
50         unsigned int base;
51 };
52
53 static int parse_llint(struct ec_node_int *node, const char *str,
54         long long *val)
55 {
56         char *endptr;
57
58         errno = 0;
59         *val = strtoll(str, &endptr, node->base);
60
61         /* out of range */
62         if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
63                         (errno != 0 && *val == 0))
64                 return -1;
65
66         if (node->check_min && *val < node->min)
67                 return -1;
68
69         if (node->check_max && *val > node->max)
70                 return -1;
71
72         if (*endptr != 0)
73                 return -1;
74
75         return 0;
76 }
77
78 static struct ec_parsed *ec_node_int_parse(const struct ec_node *gen_node,
79         const struct ec_strvec *strvec)
80 {
81         struct ec_node_int *node = (struct ec_node_int *)gen_node;
82         struct ec_parsed *parsed;
83         struct ec_strvec *match_strvec;
84         const char *str;
85         long long val;
86
87         parsed = ec_parsed();
88         if (parsed == NULL)
89                 goto fail;
90
91         if (ec_strvec_len(strvec) == 0)
92                 return parsed;
93
94         str = ec_strvec_val(strvec, 0);
95         if (parse_llint(node, str, &val) < 0)
96                 return parsed;
97
98         match_strvec = ec_strvec_ndup(strvec, 0, 1);
99         if (match_strvec == NULL)
100                 goto fail;
101
102         ec_parsed_set_match(parsed, gen_node, match_strvec);
103
104         return parsed;
105
106  fail:
107         ec_parsed_free(parsed);
108         return NULL;
109 }
110
111 static struct ec_node_type ec_node_int_type = {
112         .name = "int",
113         .parse = ec_node_int_parse,
114         .complete = ec_node_default_complete,
115         .size = sizeof(struct ec_node_int),
116 };
117
118 EC_NODE_TYPE_REGISTER(ec_node_int_type);
119
120 struct ec_node *ec_node_int(const char *id, long long int min,
121         long long int max, unsigned int base)
122 {
123         struct ec_node *gen_node = NULL;
124         struct ec_node_int *node = NULL;
125
126         gen_node = __ec_node(&ec_node_int_type, id);
127         if (gen_node == NULL)
128                 return NULL;
129         node = (struct ec_node_int *)gen_node;
130
131         node->check_min = true;
132         node->min = min;
133         node->check_max = true;
134         node->max = max;
135         node->base = base;
136
137         return &node->gen;
138 }
139
140 long long ec_node_int_getval(struct ec_node *gen_node, const char *str)
141 {
142         struct ec_node_int *node = (struct ec_node_int *)gen_node;
143         long long val = 0;
144
145         // XXX check type here
146         // if gen_node->type != int fail
147
148         parse_llint(node, str, &val);
149
150         return val;
151 }
152
153 static int ec_node_int_testcase(void)
154 {
155         struct ec_parsed *p;
156         struct ec_node *node;
157         const char *s;
158         int ret = 0;
159
160         node = ec_node_int(NULL, 0, 256, 0);
161         if (node == NULL) {
162                 ec_log(EC_LOG_ERR, "cannot create node\n");
163                 return -1;
164         }
165         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
166         ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
167         ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
168         ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
169         ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
170         ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
171
172         p = ec_node_parse(node, "0");
173         s = ec_strvec_val(ec_parsed_strvec(p), 0);
174         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 0);
175         ec_parsed_free(p);
176
177         p = ec_node_parse(node, "10");
178         s = ec_strvec_val(ec_parsed_strvec(p), 0);
179         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 10);
180         ec_parsed_free(p);
181         ec_node_free(node);
182
183         node = ec_node_int(NULL, -1, LLONG_MAX, 16);
184         if (node == NULL) {
185                 ec_log(EC_LOG_ERR, "cannot create node\n");
186                 return -1;
187         }
188         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
189         ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
190         ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
191         ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
192         ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
193
194         p = ec_node_parse(node, "10");
195         s = ec_strvec_val(ec_parsed_strvec(p), 0);
196         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 16);
197         ec_parsed_free(p);
198         ec_node_free(node);
199
200         node = ec_node_int(NULL, LLONG_MIN, 0, 10);
201         if (node == NULL) {
202                 ec_log(EC_LOG_ERR, "cannot create node\n");
203                 return -1;
204         }
205         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
206         ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
207         ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
208         ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
209         ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
210         ec_node_free(node);
211
212         /* test completion */
213         node = ec_node_int(NULL, 0, 10, 0);
214         if (node == NULL) {
215                 ec_log(EC_LOG_ERR, "cannot create node\n");
216                 return -1;
217         }
218         ret |= EC_TEST_CHECK_COMPLETE(node,
219                 "", EC_NODE_ENDLIST,
220                 EC_NODE_ENDLIST,
221                 "");
222         ret |= EC_TEST_CHECK_COMPLETE(node,
223                 "x", EC_NODE_ENDLIST,
224                 EC_NODE_ENDLIST,
225                 "");
226         ret |= EC_TEST_CHECK_COMPLETE(node,
227                 "1", EC_NODE_ENDLIST,
228                 EC_NODE_ENDLIST,
229                 "");
230         ec_node_free(node);
231
232         return ret;
233 }
234
235 static struct ec_test ec_node_int_test = {
236         .name = "node_int",
237         .test = ec_node_int_testcase,
238 };
239
240 EC_TEST_REGISTER(ec_node_int_test);