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