9276c70559033d7077119047d952eb2d3e5ae293
[protos/libecoli.git] / lib / ecoli_tk_or.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 <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdarg.h>
33
34 #include <ecoli_malloc.h>
35 #include <ecoli_tk.h>
36 #include <ecoli_tk_or.h>
37 #include <ecoli_tk_str.h>
38 #include <ecoli_test.h>
39
40 static struct ec_parsed_tk *parse(const struct ec_tk *gen_tk,
41         const char *str)
42 {
43         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
44         struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
45         unsigned int i;
46
47         parsed_tk = ec_parsed_tk_new(gen_tk);
48         if (parsed_tk == NULL)
49                 return NULL;
50
51         for (i = 0; i < tk->len; i++) {
52                 child_parsed_tk = ec_tk_parse(tk->table[i], str);
53                 if (child_parsed_tk != NULL)
54                         break;
55         }
56
57         if (child_parsed_tk == NULL)
58                 goto fail;
59
60         ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
61
62         parsed_tk->str = ec_strndup(child_parsed_tk->str,
63                 strlen(child_parsed_tk->str));
64
65         return parsed_tk;
66
67  fail:
68         ec_parsed_tk_free(parsed_tk);
69         return NULL;
70 }
71
72 static struct ec_completed_tk *complete(const struct ec_tk *gen_tk,
73         const char *str)
74 {
75         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
76         struct ec_completed_tk *completed_tk = NULL, *child_completed_tk;
77         size_t n;
78
79         for (n = 0; n < tk->len; n++) {
80                 child_completed_tk = ec_tk_complete(tk->table[n], str);
81
82                 if (child_completed_tk == NULL)
83                         continue;
84
85                 completed_tk = ec_completed_tk_merge(completed_tk,
86                         child_completed_tk);
87         }
88
89         return completed_tk;
90 }
91
92 static void free_priv(struct ec_tk *gen_tk)
93 {
94         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
95         unsigned int i;
96
97         for (i = 0; i < tk->len; i++)
98                 ec_tk_free(tk->table[i]);
99         ec_free(tk->table);
100 }
101
102 static struct ec_tk_ops or_ops = {
103         .parse = parse,
104         .complete = complete,
105         .free_priv = free_priv,
106 };
107
108 struct ec_tk *ec_tk_or_new(const char *id)
109 {
110         struct ec_tk_or *tk = NULL;
111
112         tk = (struct ec_tk_or *)ec_tk_new(id, &or_ops, sizeof(*tk));
113         if (tk == NULL)
114                 return NULL;
115
116         tk->table = NULL;
117         tk->len = 0;
118
119         return &tk->gen;
120 }
121
122 struct ec_tk *ec_tk_or_new_list(const char *id, ...)
123 {
124         struct ec_tk_or *tk = NULL;
125         struct ec_tk *child;
126         va_list ap;
127
128         va_start(ap, id);
129
130         tk = (struct ec_tk_or *)ec_tk_or_new(id);
131         if (tk == NULL)
132                 goto fail;
133
134         for (child = va_arg(ap, struct ec_tk *);
135              child != EC_TK_ENDLIST;
136              child = va_arg(ap, struct ec_tk *)) {
137                 if (child == NULL)
138                         goto fail;
139
140                 ec_tk_or_add(&tk->gen, child);
141         }
142
143         va_end(ap);
144         return &tk->gen;
145
146 fail:
147         ec_tk_free(&tk->gen); /* will also free children */
148         va_end(ap);
149         return NULL;
150 }
151
152 int ec_tk_or_add(struct ec_tk *gen_tk, struct ec_tk *child)
153 {
154         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
155         struct ec_tk **table;
156
157         assert(tk != NULL);
158         assert(child != NULL);
159
160         table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
161         if (table == NULL)
162                 return -1;
163
164         tk->table = table;
165         table[tk->len] = child;
166         tk->len ++;
167
168         return 0;
169 }
170
171 static int testcase(void)
172 {
173         struct ec_tk *tk;
174         int ret = 0;
175
176         /* all inputs starting with foo should match */
177         tk = ec_tk_or_new_list(NULL,
178                 ec_tk_str_new(NULL, "foo"),
179                 ec_tk_str_new(NULL, "bar"),
180                 EC_TK_ENDLIST);
181         if (tk == NULL) {
182                 printf("cannot create tk\n");
183                 return -1;
184         }
185         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
186         ret |= EC_TEST_CHECK_TK_PARSE(tk, "fooxxx", "foo");
187         ret |= EC_TEST_CHECK_TK_PARSE(tk, "bar", "bar");
188         ret |= EC_TEST_CHECK_TK_PARSE(tk, "oo", NULL);
189         ec_tk_free(tk);
190
191         /* test completion */
192         tk = ec_tk_or_new_list(NULL,
193                 ec_tk_str_new(NULL, "foo"),
194                 ec_tk_str_new(NULL, "bar"),
195                 ec_tk_str_new(NULL, "bar2"),
196                 ec_tk_str_new(NULL, "toto"),
197                 ec_tk_str_new(NULL, "titi"),
198                 EC_TK_ENDLIST);
199         if (tk == NULL) {
200                 printf("cannot create tk\n");
201                 return -1;
202         }
203         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "", "");
204         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "f", "oo");
205         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "b", "ar");
206         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "t", "");
207         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "to", "to");
208         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "x", NULL);
209         ec_tk_free(tk);
210
211         return ret;
212 }
213
214 static struct ec_test test = {
215         .name = "tk_or",
216         .test = testcase,
217 };
218
219 EC_REGISTER_TEST(test);