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