comparison lib/Xsj3clib/candidate.c @ 0:92745d501b9a

initial import from kinput2-v3.1
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 08 Mar 2010 04:44:30 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:92745d501b9a
1 #ifndef lint
2 static char *rcsid = "$Id: candidate.c,v 2.2 1992/03/18 09:44:48 nao Exp $";
3 #endif
4 /*
5 * Copyright 1991 Sony Corporation
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Sony not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. Sony makes no representations about the
14 * suitability of this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
16 *
17 * SONY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SONY
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24 /*
25 * Author: Naoshi Suzuki, SONY Corporation. (nao@sm.sony.co.jp)
26 */
27
28 #include "common.h"
29 #include "util.h"
30
31 extern Xsj3cCVServerIF serverIF[SERVER_NUM];
32
33 Xsj3cCand _Xsj3cCandidateInit();
34 int Xsj3cGetCandidateNum();
35 Xsj3cCand Xsj3cGetCandidates();
36 wchar *Xsj3cGetCandidate();
37 int Xsj3cSetCandidate();
38 void Xsj3cEndCandidate();
39
40 /*
41 * _Xsj3cCandidateInit()
42 * Initialize candidate list of current segment.
43 * No alloca() used.
44 */
45 Xsj3cCand
46 _Xsj3cCandidateInit(buf)
47 Xsj3cBuf buf;
48 {
49 register int i, j, num, padnum;
50 Xsj3csMode padmode[2];
51 unsigned char *knjbuf;
52 SJ3_DOUON *mbcand;
53
54 if (buf->candidate) {
55 return (buf->candidate);
56 }
57 i = 0;
58 buf->curcand = 0;
59 if ((knjbuf = (unsigned char *)
60 malloc(buf->input[buf->curseg]->size * sizeof(wchar))) == NULL) {
61 Xsj3cWarning("Cannot allocate for candidate yomi buffer");
62 return (NULL);
63 }
64 _Xsj3cwPStomPS(buf, knjbuf, buf->input[buf->curseg]->yomi);
65 num = buf->candnum = serverIF[buf->server].func[FUNC_CANDNUM](knjbuf);
66 if (num < 0) {
67 Xsj3cWarning("sj3serv is down. reconnect please");
68 if (buf->convmode & SelectModeMask)
69 buf->convmode = ConvedModeMask;
70 free(knjbuf);
71 return ((Xsj3cCand)NULL);
72 } else if (num > 0) {
73 mbcand = (SJ3_DOUON *)calloc(num, sizeof(SJ3_DOUON));
74 if (!mbcand) {
75 Xsj3cWarning("Cannot allocate memory for candidate list");
76 if (buf->convmode & SelectModeMask)
77 buf->convmode = ConvedModeMask;
78 free(knjbuf);
79 return ((Xsj3cCand)NULL);
80 }
81 num = buf->candnum
82 = serverIF[buf->server].func[FUNC_CANDIDATE](knjbuf, mbcand);
83 if (num <= 0) {
84 if (num < 0) {
85 Xsj3cWarning("sj3serv is down. reconnect please.");
86 } else {
87 Xsj3cWarning("There is no candidate.");
88 goto candpad;
89 }
90 if (buf->convmode & SelectModeMask)
91 buf->convmode = ConvedModeMask;
92 free(mbcand);
93 free(knjbuf);
94 return ((Xsj3cCand)NULL);
95 } else {
96 if ((buf->candidate = (Xsj3cCand)
97 calloc(num, sizeof(Xsj3cCandRec))) == NULL) {
98 Xsj3cWarning("Cannot allocate memory for candidate list");
99 if (buf->convmode & SelectModeMask)
100 buf->convmode = ConvedModeMask;
101 free(knjbuf);
102 free(mbcand);
103 buf->candnum = -1;
104 return ((Xsj3cCand)NULL);
105 }
106 while (i < num) {
107 buf->candidate[i].len = _Xsj3cmPStowOUT(buf,
108 buf->candidate[i].data, mbcand[i].ddata);
109 buf->candidate[i].dcid = mbcand[i].dcid;
110 i++;
111 }
112 if (buf->candnum > 1) {
113 serverIF[buf->server].func[FUNC_LOCK]();
114 locked[buf->server]++;
115 } else {
116 bzero(&buf->candidate[0].dcid,
117 sizeof(buf->candidate[0].dcid));
118 }
119 }
120 free(mbcand);
121 if (buf->candpadding)
122 goto candpad;
123 else
124 free(knjbuf);
125 } else {
126 candpad:
127 if (buf->input[buf->curseg]->num > CANDBUFSIZ - 1) {
128 free(knjbuf);
129 return (buf->candidate);
130 }
131 switch(buf->input[buf->curseg]->cursegmode) {
132 case MODE_HIRA:
133 padnum = 1;
134 padmode[0] = MODE_ZKATA;
135 break;
136 case MODE_ZKATA:
137 padnum = 1;
138 padmode[0] = MODE_HIRA;
139 break;
140 default:
141 padnum = 2;
142 padmode[0] = MODE_HIRA;
143 padmode[1] = MODE_ZKATA;
144 break;
145 }
146 if (buf->candnum != 1
147 || buf->input[buf->curseg]->cursegmode == MODE_HIRA)
148 num += (padnum + 1);
149 else
150 num += padnum;
151 if (buf->candidate)
152 buf->candidate = (Xsj3cCand)realloc(buf->candidate,
153 num * sizeof(Xsj3cCandRec));
154 else
155 buf->candidate = (Xsj3cCand)calloc(num, sizeof(Xsj3cCandRec));
156 if (!buf->candidate) {
157 Xsj3cWarning("Cannot allocate memory for candidate list");
158 free(knjbuf);
159 buf->candnum = -1;
160 return ((Xsj3cCand)NULL);
161 }
162 if (buf->candnum > 1
163 || buf->input[buf->curseg]->cursegmode == MODE_HIRA) {
164 _Xsj3cwPStowOUT(buf, buf->candidate[i].data,
165 buf->input[buf->curseg]->yomi);
166 buf->candidate[i].len = buf->input[buf->curseg]->num;
167 bzero(&buf->candidate[i].dcid, sizeof(buf->candidate[i].dcid));
168 i++;
169 } else if (!buf->candnum) {
170 _Xsj3cWcpy(buf->candidate[i].data, buf->input[buf->curseg]->disp);
171 buf->candidate[i].len = buf->input[buf->curseg]->dnum;
172 bzero(&buf->candidate[i].dcid, sizeof(buf->candidate[i].dcid));
173 i++;
174 }
175 for (j = 0; j < padnum; i++, j++) {
176 Xsj3cModeConv(buf, knjbuf, padmode[j],
177 buf->input[buf->curseg]->size);
178 buf->candidate[i].len
179 = _Xsj3cmPStowOUT(buf, buf->candidate[i].data, knjbuf);
180 bzero(&buf->candidate[i].dcid,
181 sizeof(buf->candidate[i].dcid));
182 }
183 free(knjbuf);
184 buf->candnum = num;
185 }
186 buf->candseg = buf->curseg;
187 return (buf->candidate);
188 }
189
190 /*
191 * Xsj3cGetCandidateNum()
192 * If list is already initialized, return candidate number,
193 * else initialize list and return candidate number
194 * when it succeeded in initializing list.
195 * If sj3serv is down or failed to allocate memory, return -1.
196 */
197 int
198 Xsj3cGetCandidateNum(buf, cur)
199 Xsj3cBuf buf;
200 int *cur;
201 {
202 if (!buf->candidate) {
203 buf->candidate = _Xsj3cCandidateInit(buf);
204 }
205 *cur = buf->curcand;
206 return (buf->candnum);
207 }
208
209 /*
210 * Xsj3cGetCandidates()
211 * Return candidate data.
212 */
213 Xsj3cCand
214 Xsj3cGetCandidates(buf)
215 Xsj3cBuf buf;
216 {
217 if (!buf->candidate) {
218 buf->candidate = _Xsj3cCandidateInit(buf);
219 }
220 return (buf->candidate);
221 }
222
223 /*
224 * Xsj3cGetCandidate()
225 * Return the appointed (by 2nd argument) candidate.
226 */
227 wchar *
228 Xsj3cGetCandidate(buf, n, len)
229 Xsj3cBuf buf;
230 int n;
231 int *len;
232 {
233 if (!buf->candidate) {
234 buf->candidate = _Xsj3cCandidateInit(buf);
235 }
236 *len = buf->candidate[n].len;
237 return (buf->candidate[n].data);
238 }
239
240 /*
241 * Xsj3cSetCandidate()
242 * Set the selected candidate strings to the buffers.
243 */
244 int
245 Xsj3cSetCandidate(buf, sel_candidate, changed, flush)
246 Xsj3cBuf buf;
247 int sel_candidate;
248 int *changed;
249 int *flush;
250 {
251 register wchar *p, *q;
252 register i, same = 1;
253
254 *flush = OFF;
255 *changed = OFF;
256 if (sel_candidate >= buf->candnum)
257 return -1;
258 p = buf->candidate[sel_candidate].data;
259 q = buf->input[buf->curseg]->disp;
260 i = buf->input[buf->curseg]->dnum;
261 while (i--) {
262 if (*p++ != *q++) {
263 same = 0;
264 break;
265 }
266 }
267 if ((!same || buf->curcand != sel_candidate) &&
268 buf->candseg < buf->segnum && buf->input[buf->candseg]) {
269 buf->input[buf->candseg]->dnum
270 = buf->candidate[sel_candidate].len;
271 _Xsj3cWcpy(buf->input[buf->candseg]->disp,
272 buf->candidate[sel_candidate].data);
273 *changed = ON;
274 if (buf->gakusyuu)
275 buf->input[buf->candseg]->change = ON;
276 buf->curcand = sel_candidate;
277 } else {
278 *changed = OFF;
279 }
280 return 0;
281 }
282
283 /*
284 * Xsj3cEndCandidate()
285 * SelectMode: End candidate select mode(SelectMode) and back to ConvedMode.
286 * ConvedModeMask: Free buffer of candidates and unlock sj3serv.
287 */
288 void
289 Xsj3cEndCandidate(buf, sync)
290 Xsj3cBuf buf;
291 int sync;
292 {
293 if (!((buf->convmode & SelectModeMask)
294 && buf->selectstatus == SELECT_CAND)) {
295 if (buf->candnum > 1) {
296 if (sync) {
297 if ((serverIF[buf->server].func[FUNC_STUDY]
298 (&buf->candidate[buf->curcand].dcid)) < 0) {
299 Xsj3cWarning("sj3serv is down. reconnect please");
300 }
301 }
302 if (locked[buf->server] > 0) {
303 if (!(--locked[buf->server]))
304 serverIF[buf->server].func[FUNC_UNLOCK]();
305 }
306 }
307 free (buf->candidate);
308 buf->candidate = NULL;
309 buf->curcand = 0;
310 buf->candnum = 0;
311 } else {
312 if ((buf->flushsconv == OFF) && buf->input[buf->segnum] &&
313 buf->input[buf->segnum]->num) {
314 buf->curseg = buf->segnum;
315 buf->segnum++;
316 buf->convmode = InputModeMask;
317 } else {
318 buf->convmode = ConvedModeMask;
319 }
320 }
321 }