4 * Copyright (c) 2011 project bchan
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.
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:
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.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
32 #include <bsys/queue.h>
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)
39 # define DP_ER(msg, err) /**/
42 LOCAL VOID wordlist_node_insert(wordlist_node_t *entry, wordlist_node_t *node)
44 QueInsert(&(entry->queue), &(node->queue));
47 LOCAL wordlist_node_t* wordlist_node_next(wordlist_node_t *node)
49 return (wordlist_node_t *)(node->queue.next);
52 LOCAL wordlist_node_t* wordlist_node_new(TC *str, W strlen)
54 wordlist_node_t *node;
56 node = (wordlist_node_t*)malloc(sizeof(wordlist_node_t));
61 node->str = malloc(sizeof(TC)*strlen);
62 if (node->str == NULL) {
66 memcpy(node->str, str, sizeof(TC)*strlen);
68 QueInit(&(node->queue));
73 LOCAL VOID wordlist_node_delete(wordlist_node_t *node)
75 QueRemove(&(node->queue));
80 EXPORT Bool wordlist_searchwordbyindex(wordlist_t *list, W index, TC **str, W *len)
82 wordlist_node_t *node;
86 node = wordlist_node_next(&list->node);
87 for (; node != &list->node;) {
93 node = wordlist_node_next(node);
100 LOCAL Bool wordlist_searchnodebyword(wordlist_t *list, TC *str, W len, wordlist_node_t **node)
102 wordlist_node_t *node0;
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);
114 node0 = wordlist_node_next(node0);
119 EXPORT Bool wordlist_checkexistbyword(wordlist_t *list, TC *str, W len)
121 wordlist_node_t *node;
122 return wordlist_searchnodebyword(list, str, len, &node);
125 EXPORT W wordlist_appendword(wordlist_t *list, TC *str, W len)
127 wordlist_node_t *newnode;
129 newnode = wordlist_node_new(str, len);
130 if (newnode == NULL) {
131 DP_ER("wordlist_node_new error", 0);
132 return -1; /* TODO */
134 wordlist_node_insert(newnode, &list->node);
138 EXPORT Bool wordlist_removeword(wordlist_t *list, TC *str, W len)
140 wordlist_node_t *node;
142 found = wordlist_searchnodebyword(list, str, len, &node);
144 wordlist_node_delete(node);
149 EXPORT Bool wordlist_isempty(wordlist_t *list)
151 return isQueEmpty(&(list->node.queue));
154 EXPORT W wordlist_initialize(wordlist_t *list)
156 QueInit(&(list->node.queue));
160 EXPORT VOID wordlist_finalize(wordlist_t *list)
162 wordlist_node_t *node;
164 node = wordlist_node_next(&list->node);
165 for (; node != &list->node;) {
166 wordlist_node_delete(node);
167 node = wordlist_node_next(&list->node);
171 EXPORT Bool wordlist_iterator_next(wordlist_iterator_t *iter, TC **str, W *len)
173 if (iter->node == &iter->wordlist->node) {
177 *str = iter->node->str;
178 *len = iter->node->len;
180 iter->node = wordlist_node_next(iter->node);
185 EXPORT VOID wordlist_iterator_initialize(wordlist_iterator_t *iter, wordlist_t *target)
187 iter->wordlist = target;
188 iter->node = wordlist_node_next(&target->node);
191 EXPORT VOID wordlist_iterator_finalize(wordlist_iterator_t *iter)