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