2904713eb1b12b0e138bc63696d77c7f328b1250
[protos/libecoli.git] / lib / ecoli_tk_str.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 <errno.h>
32
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
37 #include <ecoli_tk.h>
38 #include <ecoli_tk_str.h>
39
40 struct ec_tk_str {
41         struct ec_tk gen;
42         char *string;
43         unsigned len;
44 };
45
46 static struct ec_parsed_tk *ec_tk_str_parse(const struct ec_tk *gen_tk,
47         const struct ec_strvec *strvec)
48 {
49         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
50         struct ec_strvec *match_strvec;
51         struct ec_parsed_tk *parsed_tk = NULL;
52         const char *str;
53
54         parsed_tk = ec_parsed_tk_new();
55         if (parsed_tk == NULL)
56                 goto fail;
57
58         if (ec_strvec_len(strvec) == 0)
59                 return parsed_tk;
60
61         str = ec_strvec_val(strvec, 0);
62         if (strcmp(str, tk->string) != 0)
63                 return parsed_tk;
64
65         match_strvec = ec_strvec_ndup(strvec, 0, 1);
66         if (match_strvec == NULL)
67                 goto fail;
68
69         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
70
71         return parsed_tk;
72
73  fail:
74         ec_parsed_tk_free(parsed_tk);
75         return NULL;
76 }
77
78 static struct ec_completed_tk *ec_tk_str_complete(const struct ec_tk *gen_tk,
79         const struct ec_strvec *strvec)
80 {
81         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
82         struct ec_completed_tk *completed_tk;
83         struct ec_completed_tk_elt *completed_tk_elt;
84         const char *str, *add;
85         size_t n = 0;
86
87         completed_tk = ec_completed_tk_new();
88         if (completed_tk == NULL)
89                 return NULL;
90
91         if (ec_strvec_len(strvec) > 1)
92                 return completed_tk;
93
94         if (ec_strvec_len(strvec) == 1) {
95                 str = ec_strvec_val(strvec, 0);
96                 for (n = 0; n < tk->len; n++) {
97                         if (str[n] != tk->string[n])
98                                 break;
99                 }
100
101                 if (str[n] != '\0')
102                         add = NULL;
103                 else
104                         add = tk->string + n;
105         }
106
107         completed_tk_elt = ec_completed_tk_elt_new(gen_tk, add);
108         if (completed_tk_elt == NULL) {
109                 ec_completed_tk_free(completed_tk);
110                 return NULL;
111         }
112
113         ec_completed_tk_add_elt(completed_tk, completed_tk_elt);
114
115         return completed_tk;
116 }
117
118 static const char *ec_tk_str_desc(const struct ec_tk *gen_tk)
119 {
120         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
121
122         return tk->string;
123 }
124
125 static void ec_tk_str_free_priv(struct ec_tk *gen_tk)
126 {
127         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
128
129         ec_free(tk->string);
130 }
131
132 static const struct ec_tk_ops ec_tk_str_ops = {
133         .typename = "str",
134         .parse = ec_tk_str_parse,
135         .complete = ec_tk_str_complete,
136         .desc = ec_tk_str_desc,
137         .free_priv = ec_tk_str_free_priv,
138 };
139
140 struct ec_tk *ec_tk_str_new(const char *id)
141 {
142         struct ec_tk *gen_tk = NULL;
143
144         gen_tk = ec_tk_new(id, &ec_tk_str_ops, sizeof(struct ec_tk_str));
145         if (gen_tk == NULL)
146                 return NULL;
147
148         return gen_tk;
149 }
150
151 int ec_tk_str_set_str(struct ec_tk *gen_tk, const char *str)
152 {
153         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
154
155         if (str == NULL)
156                 return -EINVAL;
157         if (tk->string != NULL)
158                 return -EEXIST;
159         if (gen_tk->flags & EC_TK_F_INITIALIZED)
160                 return -EPERM;
161
162         tk->string = ec_strdup(str);
163         if (tk->string == NULL)
164                 return -ENOMEM;
165
166         tk->len = strlen(tk->string);
167
168         return 0;
169 }
170
171 int ec_tk_str_start(struct ec_tk *gen_tk)
172 {
173         if (gen_tk->flags & EC_TK_F_INITIALIZED)
174                 return -EPERM;
175
176         gen_tk->flags |= EC_TK_F_INITIALIZED;
177
178         return 0;
179 }
180
181 struct ec_tk *ec_tk_str(const char *id, const char *str)
182 {
183         struct ec_tk *gen_tk = NULL;
184
185         gen_tk = ec_tk_str_new(id);
186         if (gen_tk == NULL)
187                 goto fail;
188
189         if (ec_tk_str_set_str(gen_tk, str) < 0)
190                 goto fail;
191
192         if (ec_tk_str_start(gen_tk) < 0)
193                 goto fail;
194
195         return gen_tk;
196
197 fail:
198         ec_tk_free(gen_tk);
199         return NULL;
200 }
201
202 static int ec_tk_str_testcase(void)
203 {
204         struct ec_tk *tk;
205         int ret = 0;
206
207         tk = ec_tk_str(NULL, "foo");
208         if (tk == NULL) {
209                 ec_log(EC_LOG_ERR, "cannot create tk\n");
210                 return -1;
211         }
212         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", EC_TK_ENDLIST);
213         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar", EC_TK_ENDLIST);
214         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foobar", EC_TK_ENDLIST);
215         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " foo", EC_TK_ENDLIST);
216         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", EC_TK_ENDLIST);
217         ec_tk_free(tk);
218
219         tk = ec_tk_str(NULL, "Здравствуйте");
220         if (tk == NULL) {
221                 ec_log(EC_LOG_ERR, "cannot create tk\n");
222                 return -1;
223         }
224         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "Здравствуйте", EC_TK_ENDLIST);
225         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "Здравствуйте",
226                 "John!", EC_TK_ENDLIST);
227         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", EC_TK_ENDLIST);
228         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", EC_TK_ENDLIST);
229         ec_tk_free(tk);
230
231         /* an empty token string always matches */
232         tk = ec_tk_str(NULL, "");
233         if (tk == NULL) {
234                 ec_log(EC_LOG_ERR, "cannot create tk\n");
235                 return -1;
236         }
237         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "", EC_TK_ENDLIST);
238         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "", "foo", EC_TK_ENDLIST);
239         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", EC_TK_ENDLIST);
240         ec_tk_free(tk);
241
242         /* test completion */
243         tk = ec_tk_str(NULL, "foo");
244         if (tk == NULL) {
245                 ec_log(EC_LOG_ERR, "cannot create tk\n");
246                 return -1;
247         }
248         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
249                 "", EC_TK_ENDLIST,
250                 "foo", EC_TK_ENDLIST,
251                 "foo");
252         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
253                 "f", EC_TK_ENDLIST,
254                 "oo", EC_TK_ENDLIST,
255                 "oo");
256         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
257                 "foo", EC_TK_ENDLIST,
258                 "", EC_TK_ENDLIST,
259                 "");
260         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
261                 "x", EC_TK_ENDLIST,
262                 EC_TK_ENDLIST,
263                 "");
264         ec_tk_free(tk);
265
266         return ret;
267 }
268
269 static struct ec_test ec_tk_str_test = {
270         .name = "tk_str",
271         .test = ec_tk_str_testcase,
272 };
273
274 EC_REGISTER_TEST(ec_tk_str_test);