OSDN Git Service

replace tray implementation.
[bbk/bchanl.git] / src / bbsmenufilter.c
1 /*
2  * bbsmenufilter.c
3  *
4  * Copyright (c) 2010 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        <basic.h>
28 #include        <bstdio.h>
29 #include        <bstdlib.h>
30
31 #include    "bbsmenufilter.h"
32 #include    "bbsmenuparser.h"
33
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)
37 #else
38 # define DP(arg) /**/
39 # define DP_ER(msg, err) /**/
40 #endif
41
42 struct bbsmnfilter_t_ {
43         enum {
44                 STATE_REJECT_TITLE,
45                 STATE_HAVING_CATEGORY,
46                 STATE_HAVING_TITLE,
47                 STATE_END
48         } state;
49         bbsmnparser_item_t *category;
50         bbsmnparser_item_t *title;
51 };
52
53 EXPORT VOID bbsmnfilter_inputitem(bbsmnfilter_t *filter, bbsmnparser_item_t *item)
54 {
55         Bool ok;
56
57         if (filter->state == STATE_REJECT_TITLE) {
58                 if (item == NULL) {
59                         filter->state = STATE_END;
60                         return;
61                 }
62                 if (item->title != NULL) {
63                         bbsmnparser_item_delete(item);
64                         return;
65                 }
66                 filter->category = item;
67                 filter->state = STATE_HAVING_CATEGORY;
68                 return;
69         }
70         if (filter->state == STATE_HAVING_CATEGORY) {
71                 if (item == NULL) {
72                         bbsmnparser_item_delete(filter->category);
73                         filter->category = NULL;
74                         filter->state = STATE_END;
75                         return;
76                 }
77                 if (item->category != NULL) {
78                         bbsmnparser_item_delete(filter->category);
79                         filter->category = item;
80                         return;
81                 }
82                 ok = bbsmnparser_item_checkboradurl(item);
83                 if (ok == False) {
84                         bbsmnparser_item_delete(item);
85                         return;
86                 }
87                 filter->title = item;
88                 filter->state = STATE_HAVING_TITLE;
89                 return;
90         }
91         if (filter->state == STATE_HAVING_TITLE) {
92                 if (item == NULL) {
93                         filter->state = STATE_END;
94                         return;
95                 }
96                 if (item->title != NULL) {
97                         ok = bbsmnparser_item_checkboradurl(item);
98                         if (ok == False) {
99                                 bbsmnparser_item_delete(item);
100                                 return;
101                         }
102                         filter->title = item;
103                         return;
104                 }
105                 filter->category = item;
106                 filter->state = STATE_HAVING_CATEGORY;
107                 return;
108         }
109
110         if (item != NULL) {
111                 bbsmnparser_item_delete(item);
112         }
113 }
114
115 EXPORT W bbsmnfilter_outputitem(bbsmnfilter_t *filter, bbsmnparser_item_t **item)
116 {
117         *item = NULL;
118
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;
126                 }
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;
133                 }
134                 if (filter->title != NULL) {
135                         *item = filter->title;
136                         filter->title = NULL;
137                 }
138                 return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
139         case STATE_END:
140                 if (filter->title != NULL) {
141                         *item = filter->title;
142                         filter->title = NULL;
143                 }
144                 return BBSMNFILTER_OUTPUTITEM_END;
145         }
146
147         return BBSMNFILTER_OUTPUTITEM_WAITNEXT;
148 }
149
150 EXPORT VOID bbsmnfilter_clear(bbsmnfilter_t *filter)
151 {
152         if (filter->title != NULL) {
153                 bbsmnparser_item_delete(filter->title);
154                 filter->title = NULL;
155         }
156         if (filter->category != NULL) {
157                 bbsmnparser_item_delete(filter->category);
158                 filter->category = NULL;
159         }
160         filter->state = STATE_REJECT_TITLE;
161 }
162
163 EXPORT bbsmnfilter_t* bbsmnfilter_new()
164 {
165         bbsmnfilter_t *filter;
166
167         filter = (bbsmnfilter_t*)malloc(sizeof(bbsmnfilter_t));
168         if (filter == NULL) {
169                 return NULL;
170         }
171         filter->state = STATE_REJECT_TITLE;
172         filter->category = NULL;
173         filter->title = NULL;
174
175         return filter;
176 }
177
178 EXPORT VOID bbsmnfilter_delete(bbsmnfilter_t *filter)
179 {
180         if (filter->title != NULL) {
181                 bbsmnparser_item_delete(filter->title);
182         }
183         if (filter->category != NULL) {
184                 bbsmnparser_item_delete(filter->category);
185         }
186         free(filter);
187 }
188