comparison lib/Xsj3clib/symbol.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: symbol.c,v 2.0 1992/02/13 18:33:43 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 <stdio.h>
29 #include "common.h"
30 #include "util.h"
31 #include "func.h"
32 #include "table.h"
33
34 Xsj3cSymbol _Xsj3cSymbolInit();
35 int Xsj3cGetSymbolNum();
36 Xsj3cSymbol Xsj3cGetSymbols();
37 wchar *Xsj3cGetSymbol();
38 int Xsj3cSetSymbol();
39 void Xsj3cEndSymbol();
40
41 static int _Xsj3cReadSymbol();
42
43 static Xsj3cSymbol symbollist = NULL;
44 static int symbolnum = 0;
45
46 /*
47 * _Xsj3cSymbolInit()
48 * Decide sjsb file to read, then read it and make table for symbles.
49 */
50 Xsj3cSymbol
51 _Xsj3cSymbolInit(sjsb, home)
52 char *sjsb;
53 char *home;
54 {
55 extern char *getenv();
56 register char *p;
57 char sbfile[BUFSIZ];
58 int value;
59
60 if (symbollist) {
61 return(symbollist);
62 }
63 if (sjsb) {
64 if ((value = _Xsj3cReadSymbol(sjsb)) > 0)
65 Xsj3cError("can't open sjsb file %s", sjsb);
66 else if ( value < 0 )
67 Xsj3cError("read failed sjsb file %s", sjsb);
68 } else {
69 sbfile[0] = '\0';
70 if ((p = getenv("SJSB")) && *p != '\0') {
71 if (*p != '/') {
72 if (home)
73 strcpy(sbfile, home);
74 strcat(sbfile, "/");
75 }
76 strcat(sbfile, p);
77 } else if (home) {
78 strcpy(sbfile, home);
79 strcat(sbfile, "/.sjsb");
80 } else {
81 strcpy(sbfile, SJ3DEFPATH);
82 strcat(sbfile, DEF_SJSB_FILE);
83 }
84 if ((value = _Xsj3cReadSymbol(sbfile)) > 0) {
85 strcpy(sbfile, SJ3DEFPATH);
86 strcat(sbfile, DEF_SJSB_FILE);
87 if ((value = _Xsj3cReadSymbol(sbfile)) > 0) {
88 Xsj3cError("can't open sjsb file %s", sbfile);
89 } else if ( value < 0 ) {
90 Xsj3cError("read failed sjsb file %s", sbfile);
91 }
92 } else if ( value < 0 ) {
93 Xsj3cError("read failed sjsb file %s", sbfile);
94 }
95 }
96 return(symbollist);
97 }
98
99 /*
100 * _Xsj3cReadSymbol()
101 * Read sjsb file that is symbol table file.
102 * Make symbol table and return it.
103 */
104 static int
105 _Xsj3cReadSymbol(file)
106 char *file;
107 {
108 FILE *fp;
109 unsigned char line[256];
110 unsigned char *p;
111 int len;
112 wchar data[RBUFSIZ];
113 register int i, j;
114
115 if ((fp = fopen(file, "r")) == NULL) {
116 return (OPEN_FAILED);
117 }
118
119 i = 0;
120 while (fgets(line, sizeof(line), fp) != NULL) {
121 if (line[0] != '#' && line[0] != '\n')
122 i++;
123 }
124
125 symbolnum = i;
126 if ((symbollist = (Xsj3cSymbol)calloc(i, sizeof(Xsj3cSymbolRec))) == NULL) {
127 return (ALLOC_FAILED);
128 }
129
130 rewind(fp);
131
132 j = 0;
133 while (fgets(line, sizeof(line), fp) != NULL && j < i) {
134 p = line;
135 while (*p != '\n' && *p != '#') {
136 p += _Xsj3cmINtowOUT(file, p, data, &len);
137 SKIP(p);
138 if (data[0] == '\0')
139 break;
140 symbollist[j].data = _Xsj3cStoreWchar(data, len + 1);
141 symbollist[j].len = len;
142 if (!symbollist[j].data)
143 return (ALLOC_FAILED);
144 j++;
145 }
146 }
147 fclose(fp);
148 return (OK);
149 }
150
151 /*
152 * Xsj3cGetSymbolNum()
153 * Set the appointed (by 2nd argument) symbol to 3rd argument.
154 */
155 int
156 Xsj3cGetSymbolNum(buf, cur)
157 Xsj3cBuf buf;
158 int *cur;
159 {
160 if (!buf->symbol) {
161 buf->symbol = _Xsj3cSymbolInit(NULL, NULL);
162 }
163 *cur = buf->cursymbol;
164 return (symbolnum);
165 }
166
167 /*
168 * Xsj3cGetSymbols()
169 * Set the appointed (by 2nd argument) symbol to 3rd argument.
170 */
171 Xsj3cSymbol
172 Xsj3cGetSymbols(buf)
173 Xsj3cBuf buf;
174 {
175 if (!buf->symbol) {
176 buf->symbol = _Xsj3cSymbolInit(NULL, NULL);
177 }
178 return (buf->symbol);
179 }
180
181 /*
182 * Xsj3cGetSymbol()
183 * Set the appointed (by 2nd argument) symbol to 3rd argument.
184 */
185 wchar *
186 Xsj3cGetSymbol(buf, n, len)
187 Xsj3cBuf buf;
188 int n;
189 int *len;
190 {
191 if (!buf->symbol) {
192 buf->symbol = _Xsj3cSymbolInit(NULL, NULL);
193 }
194 *len = buf->symbol[n].len;
195 return (buf->symbol[n].data);
196 }
197
198 /*
199 * Xsj3cSetSymbol()
200 * Set the selected symbol to the buffers.
201 */
202 int
203 Xsj3cSetSymbol(buf, sel_symbol, changed, flush)
204 Xsj3cBuf buf;
205 int sel_symbol;
206 int *changed;
207 int *flush;
208 {
209 Xsj3cSeg seg;
210 int change_pos;
211 register int i;
212 wchar symbol[RBUFSIZ];
213
214 if (buf->segnum && buf->input[buf->curseg]
215 && (buf->input[buf->curseg]->edit & SEG_NOEDIT)) {
216 /* $B8=J8@a$,JQ49:Q$_$N>l9g(B */
217 if (buf->candidate)
218 Xsj3cEndCandidate(buf, ON);
219 /* FlushInConverion on $B$N;~$O3NDj$9$k(B */
220 switch (buf->flushiconv) {
221 case ON:
222 for (i = 1; i < buf->backsegnum + 1; i++) {
223 Xsj3cFreeSegment(buf->backup[i]);
224 buf->backup[i] = NULL;
225 }
226 if (!buf->backup) {
227 if ((buf->backup = (Xsj3cSeg *)calloc(BUNBUFSIZ,
228 sizeof(Xsj3cSeg))) == NULL) {
229 Xsj3cError("Cannot allocate for backup buffers");
230 }
231 }
232 if (seg = buf->backup[0]) {
233 Xsj3cClearSegment(buf, seg);
234 } else
235 seg = buf->backup[0] = (Xsj3cSeg)Xsj3cCreateSegment(buf);
236 buf->backsegnum = 1;
237 *flush = ON;
238 break;
239 case EDIT:
240 if (!(seg = buf->input[buf->curseg]))
241 seg = buf->input[buf->curseg]
242 = (Xsj3cSeg)Xsj3cCreateSegment(buf);
243 *seg->str = '\0';
244 seg->sp = seg->str;
245 *seg->oldstr = '\0';
246 seg->oldlen = 0;
247 seg->n_roma = 0;
248 seg->n_kana = -1;
249 if (buf->curseg == buf->segnum)
250 buf->segnum++;
251 else {
252 _Xsj3cUnConvSeg(buf, ONE, buf->muhencurlast);
253 _Xsj3cStoreYomi(buf, seg, 0);
254 }
255 *flush = OFF;
256 break;
257 case OFF:
258 if (seg = buf->input[buf->segnum]) {
259 Xsj3cClearSegment(buf, seg);
260 } else {
261 seg = buf->input[buf->segnum]
262 = (Xsj3cSeg)Xsj3cCreateSegment(buf);
263 }
264 buf->curseg = buf->segnum;
265 buf->segnum++;
266 *flush = OFF;
267 break;
268 case NONE:
269 default:
270 *flush = OFF;
271 *changed = OFF;
272 return 0;
273 }
274 } else {
275 seg = buf->input[buf->curseg];
276 if (buf->curseg == buf->segnum) {
277 buf->segnum++;
278 }
279 if (!seg) {
280 seg = buf->input[buf->curseg] = (Xsj3cSeg)Xsj3cCreateSegment(buf);
281 } else {
282 *seg->str = '\0';
283 seg->sp = seg->str;
284 seg->n_roma = 0;
285 seg->n_kana = -1;
286 *seg->oldstr = '\0';
287 seg->oldlen = 0;
288 }
289 }
290 seg->edit = SEG_EDIT;
291 change_pos = seg->cur;
292 _Xsj3cwOUTtowPS(buf, symbol, buf->symbol[sel_symbol].data);
293 _Xsj3cInsertWchar(seg, symbol, buf->symbol[sel_symbol].len);
294 _Xsj3cStoreYomi(buf, seg, change_pos);
295 buf->cursymbol = sel_symbol;
296 *changed = ON;
297
298 return 0;
299 }
300
301 /*
302 * Xsj3cEndSymbol()
303 * End symbol input mode(SelectMode) and back to InputMode.
304 */
305 void
306 Xsj3cEndSymbol(buf)
307 Xsj3cBuf buf;
308 {
309 buf->convmode = InputModeMask;
310 }