OSDN Git Service

fix copyright year.
[bbk/bchan.git] / src / wordlist.c
1 /*
2  * wordlist.c
3  *
4  * Copyright (c) 2011 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include    "wordlist.h"
28
29 #include        <bstdio.h>
30 #include        <bstdlib.h>
31 #include        <tstring.h>
32 #include        <bsys/queue.h>
33
34 #ifdef BCHAN_CONFIG_DEBUG
35 # define DP(arg) printf arg
36 # define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
37 #else
38 # define DP(arg) /**/
39 # define DP_ER(msg, err) /**/
40 #endif
41
42 LOCAL VOID wordlist_node_insert(wordlist_node_t *entry, wordlist_node_t *node)
43 {
44         QueInsert(&(entry->queue), &(node->queue));
45 }
46
47 LOCAL wordlist_node_t* wordlist_node_next(wordlist_node_t *node)
48 {
49         return (wordlist_node_t *)(node->queue.next);
50 }
51
52 LOCAL wordlist_node_t* wordlist_node_new(TC *str, W strlen)
53 {
54         wordlist_node_t *node;
55
56         node = (wordlist_node_t*)malloc(sizeof(wordlist_node_t));
57         if (node == NULL) {
58                 return NULL;
59         }
60
61         node->str = malloc(sizeof(TC)*strlen);
62         if (node->str == NULL) {
63                 free(node);
64                 return NULL;
65         }
66         memcpy(node->str, str, sizeof(TC)*strlen);
67         node->len = strlen;
68         QueInit(&(node->queue));
69
70         return node;
71 }
72
73 LOCAL VOID wordlist_node_delete(wordlist_node_t *node)
74 {
75         QueRemove(&(node->queue));
76         free(node->str);
77         free(node);
78 }
79
80 EXPORT Bool wordlist_searchwordbyindex(wordlist_t *list, W index, TC **str, W *len)
81 {
82         wordlist_node_t *node;
83         W i;
84
85         i = 0;
86         node = wordlist_node_next(&list->node);
87         for (; node != &list->node;) {
88                 if (i == index) {
89                         *str = node->str;
90                         *len = node->len;
91                         return True;
92                 }
93                 node = wordlist_node_next(node);
94                 i++;
95         }
96
97         return False;
98 }
99
100 LOCAL Bool wordlist_searchnodebyword(wordlist_t *list, TC *str, W len, wordlist_node_t **node)
101 {
102         wordlist_node_t *node0;
103         W result;
104
105         node0 = wordlist_node_next(&list->node);
106         for (; node0 != &list->node;) {
107                 if (node0->len == len) {
108                         result = tc_strncmp(node0->str, str, len);
109                         if (result == 0) {
110                                 *node = node0;
111                                 return True;
112                         }
113                 }
114                 node0 = wordlist_node_next(node0);
115         }
116         return False;
117 }
118
119 EXPORT Bool wordlist_checkexistbyword(wordlist_t *list, TC *str, W len)
120 {
121         wordlist_node_t *node;
122         return wordlist_searchnodebyword(list, str, len, &node);
123 }
124
125 EXPORT W wordlist_appendword(wordlist_t *list, TC *str, W len)
126 {
127         wordlist_node_t *newnode;
128
129         newnode = wordlist_node_new(str, len);
130         if (newnode == NULL) {
131                 DP_ER("wordlist_node_new error", 0);
132                 return -1; /* TODO */
133         }
134         wordlist_node_insert(newnode, &list->node);
135         return 0;
136 }
137
138 EXPORT Bool wordlist_removeword(wordlist_t *list, TC *str, W len)
139 {
140         wordlist_node_t *node;
141         Bool found;
142         found = wordlist_searchnodebyword(list, str, len, &node);
143         if (found == True) {
144                 wordlist_node_delete(node);
145         }
146         return found;
147 }
148
149 EXPORT Bool wordlist_isempty(wordlist_t *list)
150 {
151         return isQueEmpty(&(list->node.queue));
152 }
153
154 EXPORT W wordlist_initialize(wordlist_t *list)
155 {
156         QueInit(&(list->node.queue));
157         return True;
158 }
159
160 EXPORT VOID wordlist_finalize(wordlist_t *list)
161 {
162         wordlist_node_t *node;
163
164         node = wordlist_node_next(&list->node);
165         for (; node != &list->node;) {
166                 wordlist_node_delete(node);
167                 node = wordlist_node_next(&list->node);
168         }
169 }
170
171 EXPORT Bool wordlist_iterator_next(wordlist_iterator_t *iter, TC **str, W *len)
172 {
173         if (iter->node == &iter->wordlist->node) {
174                 return False;
175         }
176
177         *str = iter->node->str;
178         *len = iter->node->len;
179
180         iter->node = wordlist_node_next(iter->node);
181
182         return True;
183 }
184
185 EXPORT VOID wordlist_iterator_initialize(wordlist_iterator_t *iter, wordlist_t *target)
186 {
187         iter->wordlist = target;
188         iter->node = wordlist_node_next(&target->node);
189 }
190
191 EXPORT VOID wordlist_iterator_finalize(wordlist_iterator_t *iter)
192 {
193 }