41f99dbda34835c9a5121531e09c9dbebccde824
[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_malloc.h>
33 #include <ecoli_test.h>
34 #include <ecoli_tk.h>
35 #include <ecoli_tk_str.h>
36
37 static struct ec_parsed_tk *parse(const struct ec_tk *gen_tk,
38         const char *str)
39 {
40         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
41         struct ec_parsed_tk *parsed_tk;
42
43         if (strncmp(str, tk->string, tk->len) != 0)
44                 return NULL;
45
46         parsed_tk = ec_parsed_tk_new(gen_tk);
47         if (parsed_tk == NULL)
48                 return NULL;
49
50         parsed_tk->str = ec_strndup(str, tk->len);
51
52         return parsed_tk;
53 }
54
55 static struct ec_completed_tk *complete(const struct ec_tk *gen_tk,
56         const char *str)
57 {
58         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
59         struct ec_completed_tk *completed_tk;
60         struct ec_completed_tk_elt *completed_tk_elt;
61         size_t n;
62
63         for (n = 0; n < tk->len; n++) {
64                 if (str[n] != tk->string[n])
65                         break;
66         }
67
68         if (str[n] != '\0')
69                 return NULL;
70
71         completed_tk = ec_completed_tk_new();
72         if (completed_tk == NULL)
73                 return NULL;
74
75         completed_tk_elt = ec_completed_tk_elt_new(gen_tk, tk->string + n,
76                 tk->string);
77         if (completed_tk_elt == NULL) {
78                 ec_completed_tk_free(completed_tk);
79                 return NULL;
80         }
81
82         ec_completed_tk_add_elt(completed_tk, completed_tk_elt);
83
84         return completed_tk;
85 }
86
87 static void free_priv(struct ec_tk *gen_tk)
88 {
89         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
90
91         ec_free(tk->string);
92 }
93
94 static struct ec_tk_ops str_ops = {
95         .parse = parse,
96         .complete = complete,
97         .free_priv = free_priv,
98 };
99
100 struct ec_tk *ec_tk_str_new(const char *id, const char *str)
101 {
102         struct ec_tk_str *tk = NULL;
103         char *s = NULL;
104
105         tk = (struct ec_tk_str *)ec_tk_new(id, &str_ops, sizeof(*tk));
106         if (tk == NULL)
107                 goto fail;
108
109         s = ec_strdup(str);
110         if (s == NULL)
111                 goto fail;
112
113         tk->string = s;
114         tk->len = strlen(s);
115
116         return &tk->gen;
117
118 fail:
119         ec_free(s);
120         ec_free(tk);
121         return NULL;
122 }
123
124 static int testcase(void)
125 {
126         struct ec_tk *tk;
127         int ret = 0;
128
129         /* all inputs starting with foo should match */
130         tk = ec_tk_str_new(NULL, "foo");
131         if (tk == NULL) {
132                 printf("cannot create tk\n");
133                 return -1;
134         }
135         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
136         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foobar", "foo");
137         ret |= EC_TEST_CHECK_TK_PARSE(tk, " foo", NULL);
138         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", NULL);
139         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
140         ec_tk_free(tk);
141
142         /* all inputs starting with foo should match */
143         tk = ec_tk_str_new(NULL, "Здравствуйте");
144         if (tk == NULL) {
145                 printf("cannot create tk\n");
146                 return -1;
147         }
148         ret |= EC_TEST_CHECK_TK_PARSE(tk, "Здравствуйте", "Здравствуйте");
149         ret |= EC_TEST_CHECK_TK_PARSE(tk, "Здравствуйте John!", "Здравствуйте");
150         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", NULL);
151         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", NULL);
152         ec_tk_free(tk);
153
154         /* an empty token string always matches */
155         tk = ec_tk_str_new(NULL, "");
156         if (tk == NULL) {
157                 printf("cannot create tk\n");
158                 return -1;
159         }
160         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", "");
161         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "");
162         ec_tk_free(tk);
163
164         /* test completion */
165         tk = ec_tk_str_new(NULL, "foo");
166         if (tk == NULL) {
167                 printf("cannot create tk\n");
168                 return -1;
169         }
170         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "", "foo");
171         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "f", "oo");
172         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "foo", "");
173         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "x", NULL);
174         ec_tk_free(tk);
175
176         return ret;
177 }
178
179 static struct ec_test test = {
180         .name = "tk_str",
181         .test = testcase,
182 };
183
184 EC_REGISTER_TEST(test);