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