1 #ifndef lint
2 static char *rcsid = "$Id: segment.c,v 2.0 1992/02/13 18:33:33 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 Xsj3cSeg                    Xsj3cCreateSegment();
32 void                        Xsj3cFreeSegment();
33 void                        Xsj3cResizeSegment();
34 void                        Xsj3cClearSegment();
35 
36 int                         Xsj3cGetSegNum();
37 int                         Xsj3cGetPosition();
38 wchar                      *Xsj3cGetModeStr();
39 wchar                      *Xsj3cGetSeg();
40 wchar                      *Xsj3cGetConvertedStr();
41 int                         Xsj3cGetConvertedLength();
42 
43 /*
44  * Xsj3cCreateSegment()
45  *  Allocate Xsj3cSeg type structure and initialize all flags and buffers.
46  */
47 Xsj3cSeg
Xsj3cCreateSegment(buf)48 Xsj3cCreateSegment(buf)
49     Xsj3cBuf    buf;
50 {
51     Xsj3cSeg    seg;
52 
53     if (!(seg = (Xsj3cSeg)malloc(sizeof(Xsj3cSegRec)))) {
54         return (Xsj3cSeg)NULL;
55     }
56     bzero (seg, sizeof(Xsj3cSegRec));
57     if (!(seg->str = (unsigned char *)malloc(RBUFSIZ))) {
58         return (Xsj3cSeg)NULL;
59     }
60     *seg->str = '\0';
61     seg->sp = seg->str;
62     if (!(seg->oldstr = (unsigned char *)malloc(RBUFSIZ))) {
63         return (Xsj3cSeg)NULL;
64     }
65     *seg->oldstr = '\0';
66     seg->size = 0;
67     seg->yomi = NULL;
68     seg->disp = NULL;
69     Xsj3cResizeSegment(seg, KANABUFSIZ);
70     seg->n_roma = 0;
71     seg->n_kana = -1;
72     seg->value = 0;
73     seg->oldlen = 0;
74     seg->num = 0;
75     seg->cur = 0;
76     seg->status = SEG_NOCONV;
77     seg->cursegmode = buf->inputmode;
78     seg->change = OFF;
79     seg->edit = SEG_NOEDIT;
80     bzero(&seg->dcid, sizeof(seg->dcid));
81     return(seg);
82 }
83 
84 /*
85  * Xsj3cFreeSegment()
86  *  Free Xsj3cSeg type structure.
87  */
88 void
Xsj3cFreeSegment(seg)89 Xsj3cFreeSegment(seg)
90     Xsj3cSeg    seg;
91 {
92     if (!seg)
93         return;
94     if (seg->str)
95         free(seg->str);
96     seg->str = NULL;
97     if (seg->oldstr)
98         free(seg->oldstr);
99     seg->oldstr = NULL;
100     if (seg->yomi) {
101         free(seg->yomi);
102         seg->yomi = NULL;
103     }
104     if (seg->disp) {
105         free(seg->disp);
106         seg->disp = NULL;
107     }
108     seg->size = 0;
109     free(seg);
110 }
111 
112 /*
113  * Xsj3cResizeSegment()
114  *  Allocate segment buffers.
115  */
116 void
Xsj3cResizeSegment(seg,size)117 Xsj3cResizeSegment(seg, size)
118     Xsj3cSeg    seg;
119     int         size;
120 {
121     if (seg->size != size) {
122         if (seg->yomi) {
123             if (!(seg->yomi = (wchar *)realloc(seg->yomi,
124                     seg->size * sizeof(wchar)))) {
125                 Xsj3cError("Failed to reallocate yomi buffer");
126             }
127         } else {
128             if (!(seg->yomi = (wchar *)calloc(KANABUFSIZ, sizeof(wchar)))) {
129                 Xsj3cError("Failed to allocate yomi buffer");
130             }
131         }
132         if (seg->disp) {
133             if (!(seg->disp = (wchar *)realloc(seg->disp,
134                     seg->size * sizeof(wchar)))) {
135                 Xsj3cError("Failed to reallocate display buffer");
136             }
137         } else {
138             if (!(seg->disp = (wchar *)calloc(KANABUFSIZ, sizeof(wchar)))) {
139                 Xsj3cError("Failed to allocate display buffer");
140             }
141         }
142         seg->size = size;
143     }
144 }
145 
146 /*
147  * Xsj3cClearSegment()
148  *  Clear segment.
149  */
150 void
Xsj3cClearSegment(buf,seg)151 Xsj3cClearSegment(buf, seg)
152     Xsj3cBuf    buf;
153     Xsj3cSeg    seg;
154 {
155     *seg->str = '\0';
156     seg->sp = seg->str;
157     *seg->oldstr = '\0';
158     seg->oldlen = 0;
159     Xsj3cResizeSegment(seg, KANABUFSIZ);
160     *seg->yomi = '\0';
161     *seg->disp = '\0';
162     seg->cur = 0;
163     seg->num = 0;
164     seg->value = 0;
165     seg->n_roma = 0;
166     seg->n_kana = -1;
167     seg->dnum = 0;
168     seg->status = SEG_NOCONV;
169     seg->cursegmode = buf->inputmode;
170     seg->change = OFF;
171     seg->edit = SEG_NOEDIT;
172     bzero(&seg->dcid, sizeof(seg->dcid));
173 }
174 
175 /*
176  * Xsj3cGetModeStr()
177  *  Return the strings of current mode guide.
178  */
179 wchar *
Xsj3cGetModeStr(buf,len)180 Xsj3cGetModeStr(buf, len)
181     Xsj3cBuf    buf;
182     int        *len;
183 {
184     *len = buf->modelen[buf->dispmode];
185     return(buf->modestr[buf->dispmode]);
186 }
187 
188 #define CURSOR_VISIBLE      1
189 #define CURSOR_UNVISIBLE    0
190 
191 /*
192  * Xsj3cGetPosition()
193  *  Set current segment number to 2nd argument
194  * and cursor position to 3rd argument.
195  */
196 int
Xsj3cGetPosition(buf,curseg,pos)197 Xsj3cGetPosition(buf, curseg, pos)
198     Xsj3cBuf    buf;
199     int        *curseg;
200     int        *pos;
201 {
202     if (buf->curseg < buf->segnum) {
203         *curseg = buf->curseg;
204         *pos = buf->input[buf->curseg]->cur;
205         if (buf->input[buf->curseg]->status & SEG_CONVED)
206             return CURSOR_UNVISIBLE;
207         else
208             return CURSOR_VISIBLE;
209     } else if (buf->curseg > 0) {
210         *curseg = buf->curseg - 1;
211         *pos = buf->input[buf->curseg - 1]->dnum;
212         return CURSOR_VISIBLE;
213     } else {
214         *curseg = 0;
215         *pos = 0;
216         return CURSOR_UNVISIBLE;
217     }
218 }
219 
220 /*
221  * Xsj3cGetSegNum()
222  *  Return the number of segments.
223  */
224 int
Xsj3cGetSegNum(buf)225 Xsj3cGetSegNum(buf)
226     Xsj3cBuf  buf;
227 {
228     return(buf->segnum);
229 }
230 
231 /*
232  * Xsj3cGetSeg()
233  *  Return the appointed segment in buffers.
234  */
235 wchar *
Xsj3cGetSeg(buf,n,len,attr)236 Xsj3cGetSeg(buf, n, len, attr)
237     Xsj3cBuf    buf;
238     int         n;
239     int        *len;
240     int        *attr;
241 {
242     if (buf->convmode & DictModeMask) {
243         if (n >= buf->curseg && n <= buf->curseg + buf->dict->n_dict)
244             *attr = SEG_REVERSED;
245         else if (buf->convedunderline)
246             *attr = SEG_UNDER_LINE;
247         else
248             *attr = SEG_NORMAL;
249     } else {
250         if (n == buf->curseg) {
251             if (SEG_NOEDIT & buf->input[n]->edit) {
252                 *attr = SEG_REVERSED;
253             } else {
254                 *attr = SEG_UNDER_LINE;
255             }
256         } else {
257             if (buf->input[n]->status & SEG_NOCONV || buf->convedunderline) {
258                 *attr = SEG_UNDER_LINE;
259             } else {
260                 *attr = SEG_NORMAL;
261             }
262         }
263     }
264     *len = buf->input[n]->dnum;
265     return (buf->input[n]->disp);
266 }
267 
268 /*
269  * Xsj3cGetConvertedStr()
270  *  Set converted strings in buffer.
271  */
272 wchar *
Xsj3cGetConvertedStr(buf,data)273 Xsj3cGetConvertedStr(buf, data)
274     Xsj3cBuf                buf;
275     wchar                  *data;
276 {
277     register int            i;
278 
279     _Xsj3cFlushDcid(buf);
280     if (buf->candidate)
281         Xsj3cEndCandidate(buf, ON);
282     *data = '\0';
283     for (i = 0; i < buf->segnum ; i++)
284         _Xsj3cWcat(data, buf->input[i]->disp);
285     return (data);
286 }
287 
288 /*
289  * Xsj3cGetConvertedLength()
290  *  Return length of converted strings.
291  */
292 int
Xsj3cGetConvertedLength(buf)293 Xsj3cGetConvertedLength(buf)
294     Xsj3cBuf                buf;
295 {
296     register int            i,  len;
297 
298     for (i = 0, len = 0; i < buf->segnum ; i++)
299         len += buf->input[i]->dnum;
300     return len;
301 }
302