4 * Copyright (c) 2010 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
31 #include "bbsmenufilter.h"
32 #include "bbsmenuparser.h"
34 #ifdef BCHANL_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 struct bbsmnfilter_t_ {
45 STATE_HAVING_CATEGORY,
49 bbsmnparser_item_t *category;
50 bbsmnparser_item_t *title;
53 EXPORT VOID bbsmnfilter_inputitem(bbsmnfilter_t *filter, bbsmnparser_item_t *item)
57 if (filter->state == STATE_REJECT_TITLE) {
59 filter->state = STATE_END;
62 if (item->title != NULL) {
63 bbsmnparser_item_delete(item);
66 filter->category = item;
67 filter->state = STATE_HAVING_CATEGORY;
70 if (filter->state == STATE_HAVING_CATEGORY) {
72 bbsmnparser_item_delete(filter->category);
73 filter->category = NULL;
74 filter->state = STATE_END;
77 if (item->category != NULL) {
78 bbsmnparser_item_delete(filter->category);
79 filter->category = item;
82 ok = bbsmnparser_item_checkboradurl(item);
84 bbsmnparser_item_delete(item);
88 filter->state = STATE_HAVING_TITLE;
91 if (filter->state == STATE_HAVING_TITLE) {
93 filter->state = STATE_END;
96 if (item->title != NULL) {
97 ok = bbsmnparser_item_checkboradurl(item);
99 bbsmnparser_item_delete(item);
102 filter->title = item;
105 filter->category = item;
106 filter->state = STATE_HAVING_CATEGORY;
111 bbsmnparser_item_delete(item);
115 EXPORT W bbsmnfilter_outputitem(bbsmnfilter_t *filter, bbsmnparser_item_t **item)
119 switch (filter->state) {
120 case STATE_REJECT_TITLE:
121 return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
122 case STATE_HAVING_CATEGORY:
123 if (filter->title != NULL) {
124 *item = filter->title;
125 filter->title = NULL;
127 return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
128 case STATE_HAVING_TITLE:
129 if (filter->category != NULL) {
130 *item = filter->category;
131 filter->category = NULL;
132 return BBSMNFILTER_OUTPUTITEM_CONTINUE;
134 if (filter->title != NULL) {
135 *item = filter->title;
136 filter->title = NULL;
138 return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
140 if (filter->title != NULL) {
141 *item = filter->title;
142 filter->title = NULL;
144 return BBSMNFILTER_OUTPUTITEM_END;
147 return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
150 EXPORT VOID bbsmnfilter_clear(bbsmnfilter_t *filter)
152 if (filter->title != NULL) {
153 bbsmnparser_item_delete(filter->title);
154 filter->title = NULL;
156 if (filter->category != NULL) {
157 bbsmnparser_item_delete(filter->category);
158 filter->category = NULL;
160 filter->state = STATE_REJECT_TITLE;
163 EXPORT bbsmnfilter_t* bbsmnfilter_new()
165 bbsmnfilter_t *filter;
167 filter = (bbsmnfilter_t*)malloc(sizeof(bbsmnfilter_t));
168 if (filter == NULL) {
171 filter->state = STATE_REJECT_TITLE;
172 filter->category = NULL;
173 filter->title = NULL;
178 EXPORT VOID bbsmnfilter_delete(bbsmnfilter_t *filter)
180 if (filter->title != NULL) {
181 bbsmnparser_item_delete(filter->title);
183 if (filter->category != NULL) {
184 bbsmnparser_item_delete(filter->category);