Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dict.h"
5 #include "kuten.h"
7 /*
8 * Routines for handling dictionaries in the "Languages of the World"
9 * format. worldnextoff *must* be called with <address of valid entry>+1.
10 */
12 #define GSHORT(p) (((p)[0]<<8)|(p)[1])
14 static void putchar(int, int*);
16 #define NONE 0xffff
18 /* adapted from jhelling@cs.ruu.nl (Jeroen Hellingman) */
20 static Rune chartab[] = {
22 /*00*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
23 NONE, NONE, L'\n', 0xe6, 0xf8, 0xe5, 0xe4, 0xf6,
24 /*10*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
25 NONE, NONE, NONE, 0xc6, 0xd8, 0xc5, 0xc4, 0xd6,
27 /*20*/ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, '\'',
28 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
29 /*30*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
30 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
31 /*40*/ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
32 0x48, 0x49, 0x4a, 0x4b, L'L', 0x4d, 0x4e, 0x4f,
33 /*50*/ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
34 0x58, 0x59, 0x5a, 0x5b, L'\\', 0x5d, 0x5e, 0x5f,
35 /*60*/ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
36 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
37 /*70*/ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
38 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, NONE,
40 /*80*/ 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
41 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
42 /*90*/ 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
43 0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x283,
44 /*a0*/ 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
45 0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
47 /*b0*/ 0x254, 0x259, 0xf0, 0x283, 0x292, 0x14b, 0x251, 0x7a,
48 0x26a, 0xf0, 0x292, 0xe3, 0x153, 0x169, 0x28c, 0x265,
49 /*c0*/ 0x280, 0xeb, 0x6c, 0x28c, 0xf5, 0xf1, 0x152, NONE,
50 NONE, 0x53, 0x73, 0x5a, 0x7a, NONE, NONE, NONE,
51 /*d0*/ 0xdf, NONE, NONE, 0x101, 0x12b, 0x16b, 0x113, 0x14d,
52 NONE, NONE, NONE, 0x20, NONE, NONE, NONE, NONE,
54 /*e0*/ 0x3b1, 0x3b2, 0x3b3, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
55 0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0xd8, 0x3b5, 0x2229,
56 /*f0*/ 0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
57 0xb0, 0x2219, 0xb7, NONE, NONE, NONE, NONE, NONE,
58 };
60 enum{ Utf, Kanahi, Kanalo=Kanahi+1, GBhi, GBlo=GBhi+1, };
62 void
63 worldprintentry(Entry e, int cmd)
64 {
65 int nh, state[3];
66 uchar *p, *pe;
68 p = (uchar *)e.start;
69 pe = (uchar *)e.end;
70 nh = GSHORT(p);
71 p += 6;
72 if(cmd == 'h')
73 pe = p+nh;
74 state[0] = Utf;
75 state[1] = 0;
76 state[2] = 0;
77 while(p < pe){
78 if(cmd == 'r')
79 outchar(*p++);
80 else
81 putchar(*p++, state);
82 }
83 outnl(0);
84 }
86 long
87 worldnextoff(long fromoff)
88 {
89 int nh, np, nd;
90 uchar buf[6];
92 if(Bseek(bdict, fromoff-1, 0) < 0)
93 return -1;
94 if(Bread(bdict, buf, 6) != 6)
95 return -1;
96 nh = GSHORT(buf);
97 np = GSHORT(buf+2);
98 nd = GSHORT(buf+4);
99 return fromoff-1 + 6 + nh + np + nd;
102 static void
103 putchar(int c, int *state)
105 int xflag = 0;
106 Rune r;
107 int hi, lo;
109 switch(state[0]){
110 case Kanahi:
111 case GBhi:
112 if(CANS2JH(c) || c == 0xff){
113 state[0]++;
114 state[1] = c;
115 break;
117 /* fall through */
118 case Utf:
119 if(c == 0xfe){
120 state[0] = Kanahi;
121 break;
122 }else if(c == 0xff){
123 state[0] = GBhi;
124 break;
126 r = chartab[c];
127 if(r < 0x80 && state[2] == 0)
128 outchar(r);
129 else if(r == NONE){
130 switch(c){
131 case 0xfb:
132 if(!xflag){
133 state[2] = 1;
134 break;
136 case 0xfc:
137 if(!xflag){
138 state[2] = 0;
139 break;
141 case 0x10:
142 case 0xc7: case 0xc8:
143 case 0xd8: case 0xd9: case 0xda:
144 case 0xdc: case 0xdd: case 0xde: case 0xdf:
145 case 0xfd:
146 if(!xflag)
147 break;
148 /* fall through */
149 default:
150 outprint("\\%.2ux", c);
152 }else if(state[2] == 0)
153 outrune(r);
154 break;
155 case Kanalo:
156 case GBlo:
157 if(state[1] == 0xff && c == 0xff){
158 state[0] = Utf;
159 break;
161 state[0]--;
162 hi = state[1];
163 lo = c;
164 S2J(hi, lo); /* convert to JIS */
165 r = hi*100 + lo - 3232; /* convert to jis208 */
166 if(state[0] == Kanahi && r < JIS208MAX)
167 r = tabjis208[r];
168 else if(state[0] == GBhi && r < GB2312MAX)
169 r = tabgb2312[r];
170 else
171 r = NONE;
172 if(r == NONE)
173 outprint("\\%.2ux\\%.2ux", state[1], c);
174 else
175 outrune(r);
176 break;
180 void
181 worldprintkey(void)
183 Bprint(bout, "No pronunciation key.\n");