ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/encoding.C
Revision: 1.15
Committed: Mon Mar 15 01:27:46 2004 UTC (20 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_7, rel-2_4, rel-2_5
Changes since 1.10: +20 -11 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 #include "../config.h"
2    
3     #include "encoding.h"
4    
5     #include <cstdlib>
6     #include <cstring>
7    
8     const struct n2cs {
9     const char *name;
10     codeset cs;
11     } n2cs[] = {
12     /* first one found is the normalized one */
13 pcg 1.3 { "ISO88591", CS_ISO8859_1 },
14     { "ISO8859PRIMARY", CS_ISO8859_1 }, // some stupid fonts use this (hi tigert)
15     { "ISO88592", CS_ISO8859_2 },
16     { "ISO88593", CS_ISO8859_3 },
17     { "ISO88594", CS_ISO8859_4 },
18     { "ISO88595", CS_ISO8859_5 },
19     { "ISO88596", CS_ISO8859_6 },
20     { "ISO88597", CS_ISO8859_7 },
21     { "ISO88598", CS_ISO8859_8 },
22     { "ISO88599", CS_ISO8859_9 },
23     { "ISO885910", CS_ISO8859_10 },
24     { "ISO885911", CS_ISO8859_11 },
25     { "ISO885913", CS_ISO8859_13 },
26     { "ISO885914", CS_ISO8859_14 },
27     { "ISO885915", CS_ISO8859_15 },
28     { "FCD885915", CS_ISO8859_15 },
29     { "ISO885916", CS_ISO8859_16 },
30    
31 pcg 1.9 { "TIS620*", CS_ISO8859_11 }, // close enough
32    
33 pcg 1.3 { "ISO10646*", CS_UNICODE },
34     { "UNICODE", CS_UNICODE },
35     { "UTF8", CS_UNICODE },
36    
37     { "ASCII", CS_US_ASCII },
38     { "USASCII", CS_US_ASCII },
39     { "ANSIX341968", CS_US_ASCII },
40    
41     { "KOI8R", CS_KOI8_R },
42     { "GOST1976874*", CS_KOI8_R },
43     { "KOI8RU", CS_KOI8_U },
44     { "KOI8U", CS_KOI8_U },
45    
46     { "VISCII*", CS_VISCII },
47    
48     { "JISX0201*", CS_JIS0201_1976_0 },
49 pcg 1.15 { "JISX0208*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP)
50 pcg 1.3 { "JISX0212*", CS_JIS0212_1990_0 },
51 pcg 1.15 { "JISX021320001", CS_JIS0213_1 },
52     { "JISX021320002", CS_JIS0213_2 },
53     { "JISX0221*", CS_UNICODE }, // _very_ close
54 pcg 1.3
55     { "KSC5601*", CS_KSC5601_1987_0 },
56     { "KSX1001*", CS_KSC5601_1987_0 },
57     { "KSC5700*", CS_UNICODE }, // unicode plus extensions
58    
59     { "BIG5P*", CS_BIG5_PLUS },
60     { "BIG5ETEN*", CS_BIG5_EXT },
61     { "BIG5*", CS_BIG5 },
62     { "GB2312*", CS_GB2312_1980_0 },
63     { "GB6345*", CS_GB2312_1980_0 }, // slightly different to gb2312??
64     { "GB8565*", CS_GB2312_1980_0 }, // a superset of gb2312??
65     { "GB13000*", CS_UNICODE },
66     { "CNS1164319921", CS_CNS11643_1992_1 },
67     { "CNS1164319922", CS_CNS11643_1992_2 },
68     { "CNS1164319923", CS_CNS11643_1992_3 },
69     { "CNS1164319924", CS_CNS11643_1992_4 },
70     { "CNS1164319925", CS_CNS11643_1992_5 },
71     { "CNS1164319926", CS_CNS11643_1992_6 },
72     { "CNS1164319927", CS_CNS11643_1992_7 },
73     { "CNS116431992F", CS_CNS11643_1992_F },
74 pcg 1.1
75     { 0, CS_UNKNOWN }
76     };
77    
78     static const char *
79     normalize_name (const char *name)
80     {
81     static char res[16];
82     char *r;
83    
84     for (r = res; *name && r < res + 15; name++)
85     if ((*name >= '0' && *name <= '9')
86     || (*name >= 'A' && *name <= 'Z'))
87     *r++ = *name;
88     else if (*name >= 'a' && *name <= 'z')
89     *r++ = *name - ('a' - 'A');
90    
91     *r = 0;
92    
93     return res;
94     }
95    
96     codeset
97     codeset_from_name (const char *name)
98     {
99     if (!name)
100     return CS_UNKNOWN;
101    
102     name = normalize_name (name);
103    
104     const struct n2cs *i = n2cs;
105    
106     do {
107 pcg 1.3 int len = strlen (i->name);
108    
109     if ((i->name[len - 1] == '*'
110     && !strncmp (name, i->name, len - 1))
111     || !strcmp (name, i->name))
112     return i->cs;
113    
114 pcg 1.1 } while ((++i)->name);
115    
116     return CS_UNKNOWN;
117     }
118    
119     struct rxvt_codeset_conv_unknown : rxvt_codeset_conv {
120 pcg 1.15 unicode_t to_unicode (uint32_t enc) const { return NOCHAR; }
121     uint32_t from_unicode (unicode_t unicode) const { return NOCHAR; }
122 pcg 1.1 } rxvt_codeset_conv_unknown;
123    
124     struct rxvt_codeset_conv_us_ascii : rxvt_codeset_conv {
125 pcg 1.15 uint32_t from_unicode (unicode_t unicode) const { return unicode <= 127 ? unicode : NOCHAR; }
126 pcg 1.1 } rxvt_codeset_conv_us_ascii;
127    
128     struct rxvt_codeset_conv_unicode : rxvt_codeset_conv {
129     /* transparent */
130     } rxvt_codeset_conv_unicode;
131    
132     struct rxvt_codeset_conv_unicode_16 : rxvt_codeset_conv {
133 pcg 1.15 unicode_t to_unicode (uint32_t enc) const { return enc; }
134     uint32_t from_unicode (unicode_t unicode) const { return unicode <= 65535 ? unicode : NOCHAR; }
135 pcg 1.1 } rxvt_codeset_conv_unicode_16;
136    
137     #define ENCODING_DEFAULT
138    
139     #include "table/iso8859_1.h"
140     #include "table/iso8859_15.h"
141    
142     //#define ENCODING_EU
143    
144     #include "table/iso8859_2.h"
145     #include "table/iso8859_3.h"
146     #include "table/iso8859_4.h"
147     #include "table/iso8859_5.h"
148     #include "table/iso8859_6.h"
149     #include "table/iso8859_7.h"
150     #include "table/iso8859_8.h"
151     #include "table/iso8859_9.h"
152     #include "table/iso8859_10.h"
153     #include "table/iso8859_11.h"
154     #include "table/iso8859_13.h"
155     #include "table/iso8859_14.h"
156     #include "table/iso8859_16.h"
157    
158     #include "table/koi8_r.h"
159     #include "table/koi8_u.h"
160    
161     //#define ENCODING_KR
162    
163     #include "table/ksc5601_1987_0.h"
164    
165     //#define ENCODING_CN
166    
167     #include "table/gb2312_1980_0.h"
168 pcg 1.3 #include "table/big5.h"
169 pcg 1.1
170     //#define ENCODING_CN_EXT
171    
172     #include "table/cns11643_1992_1.h"
173     #include "table/cns11643_1992_2.h"
174     #include "table/cns11643_1992_3.h"
175     #include "table/cns11643_1992_4.h"
176     #include "table/cns11643_1992_5.h"
177     #include "table/cns11643_1992_6.h"
178     #include "table/cns11643_1992_7.h"
179     #include "table/cns11643_1992_f.h"
180     #include "table/big5_ext.h"
181     #include "table/big5_plus.h"
182    
183     //#define ENCODING_VN
184    
185     #include "table/viscii.h"
186    
187     //#define ENCODING_JP
188    
189     #include "table/jis0201_1976_0.h"
190 pcg 1.15 #include "table/jis0208_1990_0.h"
191 pcg 1.1 #include "table/jis0212_1990_0.h"
192    
193     //#define ENCODING_JP_EXT
194    
195     #include "table/jis0213_1.h"
196     #include "table/jis0213_2.h"
197    
198 pcg 1.3 // order must match table in encoding.h(!)
199 pcg 1.1 const rxvt_codeset_conv *rxvt_codeset[NUM_CODESETS] = {
200     &rxvt_codeset_conv_unknown,
201    
202     &rxvt_codeset_conv_us_ascii,
203    
204     &rxvt_codeset_conv_iso8859_1,
205     &rxvt_codeset_conv_iso8859_2,
206     &rxvt_codeset_conv_iso8859_3,
207     &rxvt_codeset_conv_iso8859_4,
208     &rxvt_codeset_conv_iso8859_5,
209     &rxvt_codeset_conv_iso8859_6,
210     &rxvt_codeset_conv_iso8859_7,
211     &rxvt_codeset_conv_iso8859_8,
212     &rxvt_codeset_conv_iso8859_9,
213     &rxvt_codeset_conv_iso8859_10,
214     &rxvt_codeset_conv_iso8859_11,
215     &rxvt_codeset_conv_iso8859_13,
216     &rxvt_codeset_conv_iso8859_14,
217     &rxvt_codeset_conv_iso8859_15,
218     &rxvt_codeset_conv_iso8859_16,
219    
220     &rxvt_codeset_conv_koi8_r,
221     &rxvt_codeset_conv_koi8_u,
222    
223     &rxvt_codeset_conv_jis0201_1976_0,
224 pcg 1.15 &rxvt_codeset_conv_jis0208_1990_0,
225 pcg 1.1 &rxvt_codeset_conv_jis0212_1990_0,
226    
227     &rxvt_codeset_conv_jis0213_1,
228     &rxvt_codeset_conv_jis0213_2,
229    
230     &rxvt_codeset_conv_ksc5601_1987_0,
231    
232     &rxvt_codeset_conv_gb2312_1980_0,
233    
234     &rxvt_codeset_conv_cns11643_1992_1,
235     &rxvt_codeset_conv_cns11643_1992_2,
236     &rxvt_codeset_conv_cns11643_1992_3,
237     &rxvt_codeset_conv_cns11643_1992_4,
238     &rxvt_codeset_conv_cns11643_1992_5,
239     &rxvt_codeset_conv_cns11643_1992_6,
240     &rxvt_codeset_conv_cns11643_1992_7,
241     &rxvt_codeset_conv_cns11643_1992_f,
242 pcg 1.3 &rxvt_codeset_conv_big5,
243 pcg 1.1 &rxvt_codeset_conv_big5_ext,
244     &rxvt_codeset_conv_big5_plus,
245    
246     &rxvt_codeset_conv_viscii,
247    
248     &rxvt_codeset_conv_unicode_16,
249     &rxvt_codeset_conv_unicode
250     };
251    
252 pcg 1.10 #if ENABLE_COMBINING
253     # define ENCODING_COMPOSE
254     #endif
255 pcg 1.9
256     #include "table/compose.h"
257    
258 pcg 1.15 unicode_t
259     rxvt_compose (unicode_t c1, unicode_t c2)
260 pcg 1.9 {
261     int l = 0;
262     int r = sizeof (rxvt_compose_table) / sizeof (rxvt_compose_entry) - 1;
263     int m;
264    
265     while (r > l)
266     {
267     m = (l + r) / 2;
268     rxvt_compose_entry &c = rxvt_compose_table[m];
269    
270     if (c.c1 < c1 || (c.c1 == c1 && c.c2 < c2))
271     l = m + 1;
272     else if (c.c1 > c1 || (c.c1 == c1 && c.c2 > c2))
273     r = m - 1;
274     else
275     return c.r;
276     }
277    
278     return NOCHAR;
279     }
280 pcg 1.15
281     #include "table/category.h"
282    
283     bool unicode::is_space (unicode_t c)
284     {
285     return IS_SPACE (c);
286     }