save
[protos/libecoli.git] / lib / ecoli_tk_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 <limits.h>
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <ecoli_log.h>
36 #include <ecoli_malloc.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_tk.h>
39 #include <ecoli_tk_int.h>
40 #include <ecoli_test.h>
41
42 struct ec_tk_int {
43         struct ec_tk gen;
44         long long int min;
45         long long int max;
46         unsigned int base;
47 };
48
49 static int parse_llint(struct ec_tk_int *tk, const char *str,
50         long long *val)
51 {
52         char *endptr;
53
54         errno = 0;
55         *val = strtoll(str, &endptr, tk->base);
56
57         /* out of range */
58         if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
59                         (errno != 0 && *val == 0))
60                 return -1;
61
62         if (*val < tk->min || *val > tk->max)
63                 return -1;
64
65         if (*endptr != 0)
66                 return -1;
67
68         return 0;
69 }
70
71 static struct ec_parsed_tk *ec_tk_int_parse(const struct ec_tk *gen_tk,
72         const struct ec_strvec *strvec)
73 {
74         struct ec_tk_int *tk = (struct ec_tk_int *)gen_tk;
75         struct ec_parsed_tk *parsed_tk;
76         struct ec_strvec *match_strvec;
77         const char *str;
78         long long val;
79
80         parsed_tk = ec_parsed_tk_new();
81         if (parsed_tk == NULL)
82                 goto fail;
83
84         if (ec_strvec_len(strvec) == 0)
85                 return parsed_tk;
86
87         str = ec_strvec_val(strvec, 0);
88         if (parse_llint(tk, str, &val) < 0)
89                 return parsed_tk;
90
91         match_strvec = ec_strvec_ndup(strvec, 0, 1);
92         if (match_strvec == NULL)
93                 goto fail;
94
95         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
96
97         return parsed_tk;
98
99  fail:
100         ec_parsed_tk_free(parsed_tk);
101         return NULL;
102 }
103
104 static struct ec_tk_ops ec_tk_int_ops = {
105         .typename = "int",
106         .parse = ec_tk_int_parse,
107         .complete = ec_tk_default_complete,
108 };
109
110 struct ec_tk *ec_tk_int(const char *id, long long int min,
111         long long int max, unsigned int base)
112 {
113         struct ec_tk *gen_tk = NULL;
114         struct ec_tk_int *tk = NULL;
115
116         gen_tk = ec_tk_new(id, &ec_tk_int_ops, sizeof(*tk));
117         if (gen_tk == NULL)
118                 return NULL;
119         tk = (struct ec_tk_int *)gen_tk;
120
121         tk->min = min;
122         tk->max = max;
123         tk->base = base;
124
125         return &tk->gen;
126 }
127
128 long long ec_tk_int_getval(struct ec_tk *gen_tk, const char *str)
129 {
130         struct ec_tk_int *tk = (struct ec_tk_int *)gen_tk;
131         long long val = 0;
132
133         // XXX check type here
134         // if gen_tk->type != int fail
135
136         parse_llint(tk, str, &val);
137
138         return val;
139 }
140
141 static int ec_tk_int_testcase(void)
142 {
143         struct ec_parsed_tk *p;
144         struct ec_tk *tk;
145         const char *s;
146         int ret = 0;
147
148         tk = ec_tk_int(NULL, 0, 256, 0);
149         if (tk == NULL) {
150                 ec_log(EC_LOG_ERR, "cannot create tk\n");
151                 return -1;
152         }
153         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0");
154         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "256", "foo");
155         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0x100");
156         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " 1");
157         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "-1");
158         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "0x101");
159
160         p = ec_tk_parse(tk, "0");
161         s = ec_strvec_val(ec_parsed_tk_strvec(p), 0);
162         EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 0);
163         ec_parsed_tk_free(p);
164
165         p = ec_tk_parse(tk, "10");
166         s = ec_strvec_val(ec_parsed_tk_strvec(p), 0);
167         EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 10);
168         ec_parsed_tk_free(p);
169         ec_tk_free(tk);
170
171         tk = ec_tk_int(NULL, -1, LLONG_MAX, 16);
172         if (tk == NULL) {
173                 ec_log(EC_LOG_ERR, "cannot create tk\n");
174                 return -1;
175         }
176         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0");
177         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-1");
178         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "7fffffffffffffff");
179         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0x7fffffffffffffff");
180         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "-2");
181
182         p = ec_tk_parse(tk, "10");
183         s = ec_strvec_val(ec_parsed_tk_strvec(p), 0);
184         EC_TEST_ASSERT(s != NULL && ec_tk_int_getval(tk, s) == 16);
185         ec_parsed_tk_free(p);
186         ec_tk_free(tk);
187
188         tk = ec_tk_int(NULL, LLONG_MIN, 0, 10);
189         if (tk == NULL) {
190                 ec_log(EC_LOG_ERR, "cannot create tk\n");
191                 return -1;
192         }
193         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "0");
194         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-1");
195         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "-9223372036854775808");
196         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "0x0");
197         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "1");
198         ec_tk_free(tk);
199
200         /* test completion */
201         tk = ec_tk_int(NULL, 0, 10, 0);
202         if (tk == NULL) {
203                 ec_log(EC_LOG_ERR, "cannot create tk\n");
204                 return -1;
205         }
206         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
207                 "", EC_TK_ENDLIST,
208                 EC_TK_ENDLIST,
209                 "");
210         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
211                 "x", EC_TK_ENDLIST,
212                 EC_TK_ENDLIST,
213                 "");
214         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
215                 "1", EC_TK_ENDLIST,
216                 EC_TK_ENDLIST,
217                 "");
218         ec_tk_free(tk);
219
220         return ret;
221 }
222
223 static struct ec_test ec_tk_int_test = {
224         .name = "tk_int",
225         .test = ec_tk_int_testcase,
226 };
227
228 EC_REGISTER_TEST(ec_tk_int_test);