bf73a133840285f12bc8546424951dd5094d5f10
[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_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_int.h>
44 #include <ecoli_test.h>
45
46 struct ec_node_int {
47         struct ec_node gen;
48         bool check_min;
49         long long int min;
50         bool check_max;
51         long long int max;
52         unsigned int base;
53 };
54
55 static int parse_llint(struct ec_node_int *node, const char *str,
56         long long *val)
57 {
58         char *endptr;
59
60         errno = 0;
61         *val = strtoll(str, &endptr, node->base);
62
63         /* out of range */
64         if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
65                         (errno != 0 && *val == 0))
66                 return -1;
67
68         if (node->check_min && *val < node->min)
69                 return -1;
70
71         if (node->check_max && *val > node->max)
72                 return -1;
73
74         if (*endptr != 0)
75                 return -1;
76
77         return 0;
78 }
79
80 static struct ec_parsed *ec_node_int_parse(const struct ec_node *gen_node,
81         const struct ec_strvec *strvec)
82 {
83         struct ec_node_int *node = (struct ec_node_int *)gen_node;
84         struct ec_parsed *parsed;
85         struct ec_strvec *match_strvec;
86         const char *str;
87         long long val;
88
89         parsed = ec_parsed();
90         if (parsed == NULL)
91                 goto fail;
92
93         if (ec_strvec_len(strvec) == 0)
94                 return parsed;
95
96         str = ec_strvec_val(strvec, 0);
97         if (parse_llint(node, str, &val) < 0)
98                 return parsed;
99
100         match_strvec = ec_strvec_ndup(strvec, 0, 1);
101         if (match_strvec == NULL)
102                 goto fail;
103
104         ec_parsed_set_match(parsed, gen_node, match_strvec);
105
106         return parsed;
107
108  fail:
109         ec_parsed_free(parsed);
110         return NULL;
111 }
112
113 static struct ec_node_type ec_node_int_type = {
114         .name = "int",
115         .parse = ec_node_int_parse,
116         .complete = ec_node_default_complete,
117         .size = sizeof(struct ec_node_int),
118 };
119
120 EC_NODE_TYPE_REGISTER(ec_node_int_type);
121
122 struct ec_node *ec_node_int(const char *id, long long int min,
123         long long int max, unsigned int base)
124 {
125         struct ec_node *gen_node = NULL;
126         struct ec_node_int *node = NULL;
127
128         gen_node = __ec_node(&ec_node_int_type, id);
129         if (gen_node == NULL)
130                 return NULL;
131         node = (struct ec_node_int *)gen_node;
132
133         node->check_min = true;
134         node->min = min;
135         node->check_max = true;
136         node->max = max;
137         node->base = base;
138
139         return &node->gen;
140 }
141
142 long long ec_node_int_getval(struct ec_node *gen_node, const char *str)
143 {
144         struct ec_node_int *node = (struct ec_node_int *)gen_node;
145         long long val = 0;
146
147         // XXX check type here
148         // if gen_node->type != int fail
149
150         parse_llint(node, str, &val);
151
152         return val;
153 }
154
155 static int ec_node_int_testcase(void)
156 {
157         struct ec_parsed *p;
158         struct ec_node *node;
159         const char *s;
160         int ret = 0;
161
162         node = ec_node_int(NULL, 0, 256, 0);
163         if (node == NULL) {
164                 ec_log(EC_LOG_ERR, "cannot create node\n");
165                 return -1;
166         }
167         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
168         ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
169         ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
170         ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
171         ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
172         ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
173
174         p = ec_node_parse(node, "0");
175         s = ec_strvec_val(ec_parsed_strvec(p), 0);
176         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 0);
177         ec_parsed_free(p);
178
179         p = ec_node_parse(node, "10");
180         s = ec_strvec_val(ec_parsed_strvec(p), 0);
181         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 10);
182         ec_parsed_free(p);
183         ec_node_free(node);
184
185         node = ec_node_int(NULL, -1, LLONG_MAX, 16);
186         if (node == NULL) {
187                 ec_log(EC_LOG_ERR, "cannot create node\n");
188                 return -1;
189         }
190         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
191         ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
192         ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
193         ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
194         ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
195
196         p = ec_node_parse(node, "10");
197         s = ec_strvec_val(ec_parsed_strvec(p), 0);
198         EC_TEST_ASSERT(s != NULL && ec_node_int_getval(node, s) == 16);
199         ec_parsed_free(p);
200         ec_node_free(node);
201
202         node = ec_node_int(NULL, LLONG_MIN, 0, 10);
203         if (node == NULL) {
204                 ec_log(EC_LOG_ERR, "cannot create node\n");
205                 return -1;
206         }
207         ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
208         ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
209         ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
210         ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
211         ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
212         ec_node_free(node);
213
214         /* test completion */
215         node = ec_node_int(NULL, 0, 10, 0);
216         if (node == NULL) {
217                 ec_log(EC_LOG_ERR, "cannot create node\n");
218                 return -1;
219         }
220         ret |= EC_TEST_CHECK_COMPLETE(node,
221                 "", EC_NODE_ENDLIST,
222                 EC_NODE_ENDLIST,
223                 "");
224         ret |= EC_TEST_CHECK_COMPLETE(node,
225                 "x", EC_NODE_ENDLIST,
226                 EC_NODE_ENDLIST,
227                 "");
228         ret |= EC_TEST_CHECK_COMPLETE(node,
229                 "1", EC_NODE_ENDLIST,
230                 EC_NODE_ENDLIST,
231                 "");
232         ec_node_free(node);
233
234         return ret;
235 }
236
237 static struct ec_test ec_node_int_test = {
238         .name = "node_int",
239         .test = ec_node_int_testcase,
240 };
241
242 EC_TEST_REGISTER(ec_node_int_test);