1 /* Copyright 1992 NEC Corporation, Tokyo, Japan.
2 *
3 * Permission to use, copy, modify, distribute and sell this software
4 * and its documentation for any purpose is hereby granted without
5 * fee, provided that the above copyright notice appear in all copies
6 * and that both that copyright notice and this permission notice
7 * appear in supporting documentation, and that the name of NEC
8 * Corporation not be used in advertising or publicity pertaining to
9 * distribution of the software without specific, written prior
10 * permission. NEC Corporation makes no representations about the
11 * suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
16 * NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19 * OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23 /* filedef
24
25 util.c -- �桼�ƥ���ƥ��ؿ����
26
27 �ʲ��δؿ������롣(�ɲä����ͤϤ����ȽȤ��Ƥ�)
28
29 GlineClear �����ɥ饤�ä����褦�ʥ�����ͤ���
30 Gline2echostr �����ɥ饤����֤����Ȥ�����Τξ���֤�
31 echostr2Gline ���ξ���֤����Ȥ�����Υ����ɥ饤����֤�
32 echostrClear ���ξ줬�����ä����褦�ʥ�����ͤ���
33 checkGLineLen �����ɥ饤���ɽ��������뤫�ɤ����Υ����å�
34 NothingChanged �����Ѳ����ʤ����Ȥ�������ͤ���
35 NothingForGLine �����ɥ饤��˴ؤ��Ƥϲ����Ѳ����ʤ�
36 NothingChangedWithBeep
37 NothingChange �Ƥ���˥ӡ��ײ����Ĥ餹
38 NothingForGLineWithBeep
39 NothingForGLine �Ƥ���˥ӡ��ײ����Ĥ餹
40 ujisncpy EUC ʸ���� n �Х��ȥ��ԡ�����
41 makeGLineMessage ������ʸ�����GLine��ɽ������褦�ʥ�����ͤ���
42 makeGLineMessageFromString
43 ������eucʸ�����GLine��ɽ������褦�ʥ�����ͤ���
44 copyYomiBuffers ¾���ɤߥ���ƥ����Ȥ�����ɤߥǡ����Υ��ԡ�
45 setWStrings ʸ������ν������Ԥ�
46 NoMoreMemory ���꤬�ʤ����饨�顼����Ȥ������顼�ͤ��֤�
47 GLineNGReturn ���顼��å��������ɥ饤��˰ܤ�
48 GLineNGReturnFI �����⡼�ɤ�ȴ���� GLineNGReturn �롣
49 GLineNGReturnTK ��Ͽ�⡼�ɤ�ȴ���� GLineNGReturn �롣
50 WStrlen �磻�ɥ���饯��ʸ�����Ĺ������� (cf. strlen)
51 WStrcat �磻�ɥ���饯��ʸ�����ä��롣(cf. strcat)
52 WStrcpy �磻�ɥ���饯��ʸ����ԡ����롣(cf. strcpy)
53 WStrncpy �磻�ɥ���饯��ʸ������ʸ�����ԡ����롣(cf. strncpy)
54 WStrcmp �磻�ɥ���饯��ʸ�������Ӥ��롣(cf. strcmp)
55 WStrncmp �磻�ɥ���饯��ʸ������ʸ����Ӥ��롣(cf. strncmp)
56 WToupper �磻�ɥ���饯���αѻ���ʸ������ʸ�����Ѵ����롣
57 WWhatGPlain �磻�ɥ���饯����ʸ����°���륰��ե��å��ץ졼����֤�
58 WIsG0 G0�Υ磻�ɥ���饯��ʸ������
59 WIsG1 G1�Υ磻�ɥ���饯��ʸ������
60 WIsG2 G2�Υ磻�ɥ���饯��ʸ������
61 WIsG3 G3�Υ磻�ɥ���饯��ʸ������
62 WGetLeft G1��G3�κ�¦(�����͡�)
63 WGetRight G1��G3�α�¦(�����͡�)
64 MBstowcs EUC ��磻�ɥ���饯��ʸ������Ѵ�
65 CNvW2E �磻�ɥ���饯���� EUC ���Ѵ�(�����å��դ�)
66 WCstombs �磻�ɥ���饯���� EUC ���Ѵ�
67 WSfree WString�dz��ݤ����ΰ��������
68 WString EUC ��磻�ɤ��Ѵ����� malloc �ޤǤ����֤�(free ����)
69 WStringOpen �嵭�ؿ��ν��������
70 WStringClose �嵭�ؿ��ν�λ����
71
72 */
73
74 static char sccs_id[] = "@(#) 102.1 $Id: util.c,v 1.2 2002/10/20 14:29:56 aida_s Exp $";
75
76 #define _WCHAR16 /* ��������ʤ���Τ�ѥ��뤹����ϤϤ����Ƥ� */
77 /* ������ưŪ���ڤ��ؤ�����Ϥ� */
78
79 #include "canna.h"
80
81 #ifdef SOMEONE_USE_THIS
82 /* ï��ȤäƤ��ʤ��ߤ����� */
Insertable(ch)83 Insertable(ch)
84 unsigned char ch;
85 {
86 if ((0x20 <= ch && ch <= 0x7f) || (0xa0 <= ch && ch <= 0xff)) {
87 return 1;
88 }
89 else {
90 return 0;
91 }
92 }
93 #endif /* SOMEONE_USE_THIS */
94
95 /*
96 ujisncpy -- ���Х���ʸ����������ڤ�ʤ��褦�˥��ԡ�����ؿ�
97
98 �ºݤ˥��ԡ�����Х��ȿ��� n �ǻ��ꤵ�줿�Х��ȿ������뤤�Ϥ�����
99 ���������ͤˤʤ롣�ºݤ˥��ԡ������Х��ȿ����֤���
100
101 SS3 �ˤ��б����Ƥ��ʤ���
102 */
103
104 int
ujisncpy(dest,src,n)105 ujisncpy(dest, src, n)
106 unsigned char *dest, *src;
107 int n;
108 {
109 int i = 0;
110 unsigned char c;
111
112 while (i < n) {
113 c = src[i];
114 if ( ! (c & 0x80) ) { /* ASCII �� */
115 dest[i++] = c;
116 }
117 else if (i + 1 < n) { /* ���������ʤξ�硢�����⥳�ԡ��Ǥ����� */
118 dest[i++] = c;
119 dest[i] = src[i];
120 i++;
121 }
122 else { /* �����ǣ��Х��Ȥ������ԡ��Ǥ��ʤ���� */
123 return i; /* ���ԡ������줺��n ��꣱�������ͤǽ���ä��� */
124 }
125 }
126 return i; /* n �Х��ȥ��ԡ������줿 */
127 }
128
setWStrings(ws,s,sz)129 setWStrings(ws, s, sz)
130 wchar_t **ws;
131 unsigned char **s;
132 int sz;
133 {
134 int f = sz;
135 wchar_t *WString();
136
137 for (; (f && sz) || (!f && *s); ws++, s++, sz--)
138 *ws = WString(*s);
139 }
140
141
copyAttribute(dest,src,n)142 copyAttribute(dest, src, n)
143 BYTE *dest;
144 BYTE *src;
145 int n;
146 {
147 for (; n; n--)
148 *dest++ = *src++;
149 }
150
151 /*
152 * �磻�ɥ���饯�����ڥ졼�����
153 *
154 */
155
156 wchar_t
WToupper(w)157 WToupper(w)
158 wchar_t w;
159 {
160 if (WIsG0(w)) {
161 if ('a' <= w && w <= 'z')
162 return((wchar_t) (w - 'a' + 'A'));
163 } else
164 return(w);
165 }
166
167 int
WStrlen(ws)168 WStrlen(ws)
169 wchar_t *ws;
170 {
171 int res = 0;
172 while (*ws++) {
173 res++;
174 }
175 return res;
176 }
177
178 wchar_t *
WStrcpy(ws1,ws2)179 WStrcpy(ws1, ws2)
180 wchar_t *ws1, *ws2;
181 {
182 wchar_t *ws;
183 int cnt, len;
184
185 for (ws = ws2, cnt = 0 ; *ws ; ws++, cnt++) ;
186 len = cnt;
187 if (ws2 < ws1 && ws1 < ws2 + cnt) {
188 while (cnt--) {
189 ws1[cnt] = ws2[cnt];
190 }
191 }
192 else {
193 ws = ws1;
194 while (*ws2) {
195 *ws++ = *ws2++;
196 }
197 }
198 ws1[len] = (wchar_t)0;
199 return ws1;
200 }
201
202 wchar_t *
WStrncpy(ws1,ws2,cnt)203 WStrncpy(ws1, ws2, cnt)
204 wchar_t *ws1, *ws2;
205 int cnt;
206 {
207 wchar_t *ws;
208
209 if (ws2 == (wchar_t *) NULL)
210 return;
211 if (ws2 < ws1 && ws1 < ws2 + cnt) {
212 while (cnt--) {
213 ws1[cnt] = ws2[cnt];
214 }
215 }
216 else {
217 int i = 0;
218 ws = ws1;
219 while (i++ < cnt && *ws2) {
220 *ws++ = *ws2++;
221 }
222 }
223 return ws1;
224 }
225
226 wchar_t *
WStrcat(ws1,ws2)227 WStrcat(ws1, ws2)
228 wchar_t *ws1, *ws2;
229 {
230 wchar_t *ws;
231
232 for (ws = ws1; *ws; ws++);
233 WStrcpy(ws, ws2);
234 return ws1;
235 }
236
237 int
WStrcmp(w1,w2)238 WStrcmp(w1, w2)
239 wchar_t *w1, *w2;
240 {
241 for (; *w1 && *w1 == *w2; w1++, w2++);
242 return(*w1 - *w2);
243 }
244
245 int
WStrncmp(w1,w2,n)246 WStrncmp(w1, w2, n)
247 wchar_t *w1, *w2;
248 int n;
249 {
250 if (n == 0) return(0);
251 for (; --n && *w1 && *w1 == *w2; w1++, w2++);
252 return(*w1 - *w2);
253 }
254
255 /* WWhatGPlain -- �ɤΥ���ե��å��ץ졼���ʸ������
256
257 �����:
258 0 : G0 ASCII
259 1 : G1 ����(JISX0208)
260 2 : G2 Ⱦ�ѥ�������(JISX0201)
261 3 : G3 ����(������� JISX0212)
262 */
263
264 int
WWhatGPlain(wc)265 WWhatGPlain(wc)
266 wchar_t wc;
267 {
268 #ifdef _WCHAR16
269 switch (((unsigned long)wc) & 0x8080) {
270 case 0x0000:
271 return 0;
272 case 0x8080:
273 return 1;
274 case 0x0080:
275 return 2;
276 case 0x8000:
277 return 3;
278 }
279 #else /* !_WCHAR16 */
280 static char plain[4] = {0, 2, 3, 1};
281
282 return plain[(((unsigned long)wc) >> 28) & 3];
283 #endif /* !_WCHAR16 */
284 }
285
286 int
WIsG0(wc)287 WIsG0(wc)
288 wchar_t wc;
289 {
290 return (WWhatGPlain(wc) == 0);
291 }
292
293 int
WIsG1(wc)294 WIsG1(wc)
295 wchar_t wc;
296 {
297 return (WWhatGPlain(wc) == 1);
298 }
299
300 int
WIsG2(wc)301 WIsG2(wc)
302 wchar_t wc;
303 {
304 return (WWhatGPlain(wc) == 2);
305 }
306
307 int
WIsG3(wc)308 WIsG3(wc)
309 wchar_t wc;
310 {
311 return (WWhatGPlain(wc) == 3);
312 }
313
314 /* �ʲ��Σ��Ĥδؿ��ϣ��Х��Ȥޤ�Ŭ�Ѳ� */
315
316 int
WGetLeft(wc)317 WGetLeft(wc)
318 wchar_t wc;
319 {
320 if (WIsG0(wc) || WIsG2(wc))
321 return 0;
322 else {
323 #ifdef _WCHAR16
324 return ((((unsigned long)wc) >> 8) & 0x7f);
325 #else /* !_WCHAR16 */
326 return ((((unsigned long)wc) >> 7) & 0x7f);
327 #endif /* !_WCHAR16 */
328 }
329 }
330
331 int
WGetRight(wc)332 WGetRight(wc)
333 wchar_t wc;
334 {
335 if (WIsG0(wc) || WIsG2(wc))
336 return 0;
337 else {
338 return (((unsigned long)wc) & 0x7f);
339 }
340 }
341
342 int
MBstowcs(dest,src,destlen)343 MBstowcs(dest, src, destlen)
344 wchar_t *dest;
345 unsigned char *src;
346 int destlen;
347 {
348 register int i, j;
349 register unsigned char ec;
350
351 for (i = 0, j = 0 ; (ec = src[i]) && j < destlen ; i++) {
352 if (ec & 0x80) {
353 switch (ec) {
354 case 0x8e: /* SS2 */
355 dest[j++] = (wchar_t)(0x80 | ((unsigned)src[++i] & 0x7f));
356 break;
357 case 0x8f: /* SS3 */
358 dest[j++] = (wchar_t)(0x8000
359 | (((unsigned)src[i + 1] & 0x7f) << 8)
360 | ((unsigned)src[i + 2] & 0x7f));
361 i += 2;
362 break;
363 default:
364 dest[j++] = (wchar_t)(0x8080 | (((unsigned)src[i] & 0x7f) << 8)
365 | ((unsigned)src[i + 1] & 0x7f));
366 i++;
367 break;
368 }
369 }
370 else {
371 dest[j++] = (wchar_t)ec;
372 }
373 }
374 if (j < destlen)
375 dest[j] = (wchar_t)0;
376 return j;
377 }
378
379 int
CNvW2E(src,srclen,dest,destlen)380 CNvW2E(src, srclen, dest, destlen)
381 wchar_t *src;
382 unsigned char *dest;
383 int srclen, destlen;
384 {
385 register int i, j;
386 register wchar_t wc;
387
388 for (i = 0, j = 0 ; i < srclen && j + 2 < destlen ; i++) {
389 wc = src[i];
390 switch (wc & 0x8080) {
391 case 0:
392 /* ASCII */
393 dest[j++] = (unsigned char)((unsigned)wc & 0x7f);
394 break;
395 case 0x0080:
396 /* Ⱦ�ѥ��� */
397 dest[j++] = 0x8e; /* SS2 */
398 dest[j++] = (unsigned char)(((unsigned)wc & 0x7f) | 0x80);
399 break;
400 case 0x8000:
401 /* ���� */
402 dest[j++] = 0x8f; /* SS3 */
403 dest[j++] = (unsigned char)((((unsigned)wc & 0x7f00) >> 8) | 0x80);
404 dest[j++] = (unsigned char)(((unsigned)wc & 0x7f) | 0x80);
405 break;
406 case 0x8080:
407 /* ���� */
408 dest[j++] = (unsigned char)((((unsigned)wc & 0x7f00) >> 8) | 0x80);
409 dest[j++] = (unsigned char)(((unsigned)wc & 0x7f) | 0x80);
410 break;
411 }
412 }
413 dest[j] = (unsigned char)0;
414 return j;
415 }
416
417 int
WCstombs(dest,src,destlen)418 WCstombs(dest, src, destlen)
419 unsigned char *dest;
420 wchar_t *src;
421 int destlen;
422 {
423 return CNvW2E(src, WStrlen(src), dest, destlen);
424 }
425
426 /* cfuncdef
427
428 WString -- EUC����磻�ɥ���饯���ؤΥޥåԥ���� malloc
429
430 WString �ϰ�����ʸ�����磻�ɥ���饯�����Ѵ���������ʸ�����ޤ�
431 �����Υ���� malloc ��������ʸ�����Ǽ���֤���
432
433 ���ѼԤϤ��δؿ��������ݥ��� free ����ɬ�פϤ��ޤ�ʤ���
434
435 ���ʤ�������δؿ�����������ϸ�� WStringClose ��ƤӽФ����Ȥ���
436 free ����롣
437
438 ������������ʤΤǤ��δؿ������ˤ˸ƤӽФ��ƤϤ����ʤ������ޤ�EUC��
439 �������Ǥ��Ƥ���ʸ����ʤɤ�α���٤��Ǥ��롣
440
441 ���ε�ǽ��Ȥ��ͤϺǽ�� WStringOpen ��ƤӽФ��ʤ���Фʤ�ʤ�����
442 �桼�����ե������饤�֥��Ǥϥ����ƥब��ưŪ���ɤ�Ǥ�����
443 �Ǥ���ɬ�פϤʤ���
444
445 */
446
447 static wchar_t **wsmemories = NULL;
448 static int nwsmemories = 0;
449
450 #define WSBLOCKSIZE 128
451
452 int
WStringOpen()453 WStringOpen()
454 {
455 return 0;
456 }
457
458 wchar_t *
WString(s)459 WString(s)
460 unsigned char *s;
461 {
462 int i, len;
463 wchar_t *temp;
464
465 if (wsmemories == (wchar_t **)NULL) {
466 nwsmemories = WSBLOCKSIZE;
467 wsmemories = (wchar_t **)calloc(nwsmemories, sizeof(wchar_t *));
468 /* calloc ���줿����ϥ��ꥢ����Ƥ��� */
469 }
470
471 for (i = 0 ; i < nwsmemories && wsmemories[i] ; i++);
472
473 if (i == nwsmemories) { /* �Ȥ��ڤä��Τ����䤹 */
474 wsmemories = (wchar_t **)realloc(wsmemories,
475 (nwsmemories + WSBLOCKSIZE)
476 * sizeof(wchar_t *));
477 for (; i < nwsmemories + WSBLOCKSIZE ; i++) {
478 wsmemories[i] = (wchar_t *)0;
479 }
480 i = nwsmemories;
481 nwsmemories += WSBLOCKSIZE;
482 }
483
484 /* �Ȥꤢ�����礭���ȤäƤ����ơ����Υ����������٤Υ�������
485 ľ�����֤� */
486
487 len = strlen((char *)s);
488 temp = (wchar_t *)malloc((len + 1) * WCHARSIZE);
489 len = MBstowcs(temp, s, len);
490 wsmemories[i] = (wchar_t *)malloc((len + 1) * WCHARSIZE);
491 WStrncpy(wsmemories[i], temp, len);
492 free(temp);
493 wsmemories[i][len] = (wchar_t)0;
494 return wsmemories[i];
495 }
496
497 int
WStringClose()498 WStringClose()
499 {
500 int i;
501
502 for (i = 0 ; i < nwsmemories ; i++) {
503 if (wsmemories[i]) {
504 free(wsmemories[i]);
505 }
506 }
507 free(wsmemories);
508 wsmemories = (wchar_t **)0;
509 nwsmemories = 0;
510 }
511
WSfree(s)512 WSfree(s)
513 wchar_t *s;
514 {
515 int i;
516 wchar_t **t;
517 for (t = wsmemories, i = nwsmemories; s != *t && i; t++, i--)
518 ;
519 free(*t);
520 *t = (wchar_t *) 0;
521 }
522
523
524