Fix memory leak issues
[platform/core/uifw/anthy.git] / src-worddic / wtype.c
1 /*
2  * Éʻ췿¤ò´ÉÍý¤¹¤ë
3  * Ãæ¿È¤Ïwtype_t¤ÎÆâÉô¤Î¥ì¥¤¥¢¥¦¥È¤Ë¶¯¤¯°Í¸¤¹¤ë¡£
4  *
5  * Copyright (C) 2000-2007 TABATA Yusuke
6  */
7 /*
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public
10   License as published by the Free Software Foundation; either
11   version 2 of the License, or (at your option) any later version.
12
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public
19   License along with this library; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
21  */
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <anthy/wtype.h>
26 #include "dic_main.h"
27
28 wtype_t anthy_wt_none, anthy_wt_all;
29
30 struct wttable {
31   const char *name;
32   int pos;
33   int cos;
34   int scos;
35   int cc;
36   int ct;/*¥«ÊѤʤÉ*/
37   int flags;
38 };
39
40 /* ÉÊ»ì¤ÎÆüËܸì¤Î̾Á°¤òÉÊ»ì¤ËÊÑ´¹¤¹¤ë¥Æ¡¼¥Ö¥ë */
41 static struct wttable pos_name_tab[]= {
42 #include "ptab.h"
43 };
44
45 /* ¼­½ñÃæ¤ÎÉÊ»ì¤Î̾Á°¤òÉÊ»ì¤ËÊÑ´¹¤¹¤ë¥Æ¡¼¥Ö¥ë */
46 static struct wttable wt_name_tab[]= {
47 #include "wtab.h"
48 };
49
50 static struct wttable *
51 find_wttab(struct wttable *array, const char *name)
52 {
53   struct wttable *w;
54   for (w = array; w->name; w++) {
55     if (!strcmp(w->name, name)) {
56       return w;
57     }
58   }
59   return NULL;
60 }
61
62 void
63 anthy_init_wtypes(void)
64 {
65   anthy_wt_all.pos = POS_NONE;
66   anthy_wt_all.cc = CC_NONE;
67   anthy_wt_all.ct = CT_NONE;
68   anthy_wt_all.cos = COS_NONE;
69   anthy_wt_all.scos = SCOS_NONE;
70   anthy_wt_all.wf = WF_NONE;
71
72   anthy_wt_none = anthy_wt_all;
73   anthy_wt_none.pos = POS_INVAL;
74 }
75
76 /*
77  * ÊÖ¤êÃͤˤÏÉÊ»ì¤Î̾Á°
78  * t¤Ë¤ÏÉʻ줬ÊÖ¤µ¤ì¤ë
79  */
80 const char *
81 anthy_type_to_wtype(const char *s, wtype_t *t)
82 {
83   struct wttable *w;
84   if (s[0] != '#') {
85     *t = anthy_wt_none;
86     return NULL;
87   }
88   w = find_wttab(wt_name_tab, s);
89   if (!w) {
90     *t = anthy_wt_all;
91     return NULL;
92   }
93   *t = anthy_get_wtype(w->pos, w->cos, w->scos, w->cc, w->ct, w->flags);
94   return w->name;
95 }
96
97 wtype_t
98 anthy_init_wtype_by_name(const char *name)
99 {
100   struct wttable *p;
101   p = find_wttab(pos_name_tab, name);
102
103   if (p) {
104     return anthy_get_wtype(p->pos, p->cos, p->scos, p->cc, p->ct, p->flags);
105   }
106
107   printf("Failed to find wtype(%s).\n", name);
108   return anthy_wt_all;
109 }
110
111 /* Æó¤Ä¤ÎÉʻ줬´°Á´¤Ë°ìÃפ·¤Æ¤¤¤ë¤«¤É¤¦¤« */
112 int
113 anthy_wtype_equal(wtype_t lhs, wtype_t rhs)
114 {
115   if (lhs.pos == rhs.pos &&
116       lhs.cos == rhs.cos &&
117       lhs.scos == rhs.scos &&
118       lhs.cc == rhs.cc &&
119       lhs.ct == rhs.ct &&
120       lhs.wf == rhs.wf) {
121     return 1;
122   } else {
123     return 0;
124   }
125 }
126
127
128 /* n ¤Ï hs ¤Î°ìÉô¤«¤É¤¦¤«¡© */
129 int
130 anthy_wtype_include(wtype_t hs, wtype_t n)
131 {
132   /*printf("POS %d,%d\n", hs.type[WT_POS], n.type[WT_POS]);*/
133   if (hs.pos != POS_NONE &&
134       hs.pos != n.pos) {
135     return 0;
136   }
137   if (hs.cc != CC_NONE &&
138       hs.cc != n.cc) {
139     return 0;
140   }
141   if (hs.ct != CT_NONE &&
142       hs.ct != n.ct) {
143     return 0;
144   }
145   if (hs.cos != COS_NONE &&
146       hs.cos != n.cos) {
147     return 0;
148   }
149   if (hs.scos != SCOS_NONE &&
150       hs.scos != n.scos) {
151     return 0;
152   }
153   return 1;
154 }
155
156 int
157 anthy_wtype_get_cc(wtype_t t)
158 {
159   return t.cc;
160 }
161
162 int
163 anthy_wtype_get_ct(wtype_t t)
164 {
165   return t.ct;
166 }
167
168 int
169 anthy_wtype_get_pos(wtype_t t)
170 {
171   return t.pos;
172 }
173
174 int
175 anthy_wtype_get_cos(wtype_t t)
176 {
177   return t.cos;
178 }
179
180 int
181 anthy_wtype_get_scos(wtype_t t)
182 {
183   return t.scos;
184 }
185
186 int
187 anthy_wtype_get_wf(wtype_t t)
188 {
189   return t.wf;
190 }
191
192 int
193 anthy_wtype_get_indep(wtype_t t)
194 {
195   return t.wf & WF_INDEP;
196 }
197
198 int
199 anthy_wtype_get_meisi(wtype_t w)
200 {
201   return w.wf & WF_MEISI;
202 }
203
204 int
205 anthy_wtype_get_sv(wtype_t w)
206 {
207   return w.wf & WF_SV;
208 }
209
210 int
211 anthy_wtype_get_ajv(wtype_t w)
212 {
213   return w.wf & WF_AJV;
214 }
215
216 void
217 anthy_wtype_set_cc(wtype_t *w, int cc)
218 {
219   w->cc = cc;
220 }
221
222 void
223 anthy_wtype_set_ct(wtype_t *w, int ct)
224 {
225   w->ct = ct;
226 }
227
228 void
229 anthy_wtype_set_pos(wtype_t *w, int pos)
230 {
231   w->pos = pos;
232 }
233
234 void
235 anthy_wtype_set_cos(wtype_t *w, int cs)
236 {
237   w->cos = cs;
238 }
239
240 void
241 anthy_wtype_set_scos(wtype_t *w, int sc)
242 {
243   w->scos = sc;
244 }
245
246 void
247 anthy_wtype_set_dep(wtype_t *w, int isDep)
248 {
249   if (isDep) {
250     w->wf &= (~WF_INDEP);
251   }else{
252     w->wf |= WF_INDEP;
253   }
254 }
255
256 void
257 anthy_print_wtype(wtype_t w)
258 {
259   printf("(POS=%d,COS=%d,SCOS=%d,CC=%d,CT=%d,flags=%d)\n",
260          anthy_wtype_get_pos(w),
261          anthy_wtype_get_cos(w),
262          anthy_wtype_get_scos(w),
263          anthy_wtype_get_cc(w),
264          anthy_wtype_get_ct(w),
265          anthy_wtype_get_wf(w));
266 }
267
268 wtype_t
269 anthy_get_wtype_with_ct(wtype_t base, int ct)
270 {
271   wtype_t w;
272
273   w = base;
274   w.ct = ct;
275
276   return w;
277 }
278
279 wtype_t
280 anthy_get_wtype(int pos, int cos, int scos, int cc, int ct, int wf)
281 {
282   wtype_t w;
283
284   w.pos = pos;
285   w.cos = cos;
286   w.scos = scos;
287   w.cc = cc;
288   w.ct = ct;
289   w.wf = wf;
290
291   return w;
292 }