ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/encoding.C
Revision: 1.30
Committed: Sat Dec 18 18:17:38 2010 UTC (13 years, 5 months ago) by sf-exg
Content type: text/plain
Branch: MAIN
Changes since 1.29: +2 -1 lines
Log Message:
Add ARRAY_LENGTH macro with obvious meaning and use it.

File Contents

# User Rev Content
1 root 1.25 /*----------------------------------------------------------------------*
2 pcg 1.16 * File: encoding.C
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.24 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
7 pcg 1.16 *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *----------------------------------------------------------------------*/
22    
23 pcg 1.1 #include "../config.h"
24    
25     #include "encoding.h"
26 sf-exg 1.30 #include "rxvtutil.h"
27 pcg 1.1
28     #include <cstdlib>
29     #include <cstring>
30    
31 sf-exg 1.29 static const struct n2cs {
32 pcg 1.1 const char *name;
33     codeset cs;
34     } n2cs[] = {
35     /* first one found is the normalized one */
36 pcg 1.3 { "ISO88591", CS_ISO8859_1 },
37     { "ISO8859PRIMARY", CS_ISO8859_1 }, // some stupid fonts use this (hi tigert)
38     { "ISO88592", CS_ISO8859_2 },
39     { "ISO88593", CS_ISO8859_3 },
40     { "ISO88594", CS_ISO8859_4 },
41     { "ISO88595", CS_ISO8859_5 },
42     { "ISO88596", CS_ISO8859_6 },
43     { "ISO88597", CS_ISO8859_7 },
44     { "ISO88598", CS_ISO8859_8 },
45     { "ISO88599", CS_ISO8859_9 },
46     { "ISO885910", CS_ISO8859_10 },
47     { "ISO885911", CS_ISO8859_11 },
48     { "ISO885913", CS_ISO8859_13 },
49     { "ISO885914", CS_ISO8859_14 },
50     { "ISO885915", CS_ISO8859_15 },
51     { "FCD885915", CS_ISO8859_15 },
52     { "ISO885916", CS_ISO8859_16 },
53 ayin 1.26
54 pcg 1.9 { "TIS620*", CS_ISO8859_11 }, // close enough
55    
56 pcg 1.3 { "ISO10646*", CS_UNICODE },
57     { "UNICODE", CS_UNICODE },
58     { "UTF8", CS_UNICODE },
59 ayin 1.26
60 pcg 1.3 { "ASCII", CS_US_ASCII },
61     { "USASCII", CS_US_ASCII },
62     { "ANSIX341968", CS_US_ASCII },
63 root 1.28 { "ISO6461991IRV", CS_US_ASCII }, // older versions used the currency sign
64 ayin 1.26
65 root 1.22 { "KOI8R*", CS_KOI8_R },
66 pcg 1.3 { "GOST1976874*", CS_KOI8_R },
67     { "KOI8RU", CS_KOI8_U },
68     { "KOI8U", CS_KOI8_U },
69    
70     { "VISCII*", CS_VISCII },
71 ayin 1.26
72 pcg 1.3 { "JISX0201*", CS_JIS0201_1976_0 },
73 root 1.17 { "JISC6226*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP)
74 pcg 1.15 { "JISX0208*", CS_JIS0208_1990_0 }, // also wrongly matches -1987-0? (check Encode::JP)
75 pcg 1.3 { "JISX0212*", CS_JIS0212_1990_0 },
76 pcg 1.15 { "JISX021320001", CS_JIS0213_1 },
77     { "JISX021320002", CS_JIS0213_2 },
78     { "JISX0221*", CS_UNICODE }, // _very_ close
79 ayin 1.26
80 pcg 1.3 { "KSC5601*", CS_KSC5601_1987_0 },
81     { "KSX1001*", CS_KSC5601_1987_0 },
82     { "KSC5700*", CS_UNICODE }, // unicode plus extensions
83 ayin 1.26
84 pcg 1.3 { "BIG5P*", CS_BIG5_PLUS },
85     { "BIG5ETEN*", CS_BIG5_EXT },
86     { "BIG5*", CS_BIG5 },
87     { "GB2312*", CS_GB2312_1980_0 },
88 root 1.21 { "GBK*", CS_GBK_0 },
89 pcg 1.3 { "GB6345*", CS_GB2312_1980_0 }, // slightly different to gb2312??
90     { "GB8565*", CS_GB2312_1980_0 }, // a superset of gb2312??
91     { "GB13000*", CS_UNICODE },
92     { "CNS1164319921", CS_CNS11643_1992_1 },
93     { "CNS1164319922", CS_CNS11643_1992_2 },
94     { "CNS1164319923", CS_CNS11643_1992_3 },
95     { "CNS1164319924", CS_CNS11643_1992_4 },
96     { "CNS1164319925", CS_CNS11643_1992_5 },
97     { "CNS1164319926", CS_CNS11643_1992_6 },
98     { "CNS1164319927", CS_CNS11643_1992_7 },
99     { "CNS116431992F", CS_CNS11643_1992_F },
100 pcg 1.1
101     { 0, CS_UNKNOWN }
102     };
103    
104     static const char *
105     normalize_name (const char *name)
106     {
107     static char res[16];
108     char *r;
109    
110     for (r = res; *name && r < res + 15; name++)
111     if ((*name >= '0' && *name <= '9')
112     || (*name >= 'A' && *name <= 'Z'))
113     *r++ = *name;
114     else if (*name >= 'a' && *name <= 'z')
115     *r++ = *name - ('a' - 'A');
116    
117     *r = 0;
118    
119     return res;
120     }
121    
122     codeset
123     codeset_from_name (const char *name)
124     {
125     if (!name)
126     return CS_UNKNOWN;
127    
128     name = normalize_name (name);
129    
130     const struct n2cs *i = n2cs;
131    
132     do {
133 pcg 1.3 int len = strlen (i->name);
134    
135     if ((i->name[len - 1] == '*'
136     && !strncmp (name, i->name, len - 1))
137     || !strcmp (name, i->name))
138     return i->cs;
139    
140 pcg 1.1 } while ((++i)->name);
141    
142     return CS_UNKNOWN;
143     }
144    
145 root 1.18 static unicode_t cs_unknown_to_unicode (uint32_t enc) { return NOCHAR; }
146     static uint32_t cs_unknown_from_unicode (unicode_t unicode) { return NOCHAR; }
147    
148     static unicode_t cs_unicode_to_unicode (uint32_t enc) { return enc; }
149     static uint32_t cs_unicode_from_unicode (unicode_t unicode) { return unicode; }
150    
151     #define cs_us_ascii_to_unicode cs_unicode_to_unicode
152     static uint32_t cs_us_ascii_from_unicode (unicode_t unicode) { return unicode <= 127 ? unicode : NOCHAR; }
153    
154     #define cs_us_ascii_to_unicode_16 cs_unicode_to_unicode
155     static uint32_t cs_unicode_16_from_unicode (unicode_t unicode) { return unicode <= 65535 ? unicode : NOCHAR; }
156 pcg 1.1
157     #define ENCODING_DEFAULT
158    
159     #include "table/iso8859_1.h"
160     #include "table/iso8859_15.h"
161    
162     //#define ENCODING_EU
163    
164     #include "table/iso8859_2.h"
165     #include "table/iso8859_3.h"
166     #include "table/iso8859_4.h"
167     #include "table/iso8859_5.h"
168     #include "table/iso8859_6.h"
169     #include "table/iso8859_7.h"
170     #include "table/iso8859_8.h"
171     #include "table/iso8859_9.h"
172     #include "table/iso8859_10.h"
173     #include "table/iso8859_11.h"
174     #include "table/iso8859_13.h"
175     #include "table/iso8859_14.h"
176     #include "table/iso8859_16.h"
177    
178     #include "table/koi8_r.h"
179     #include "table/koi8_u.h"
180    
181     //#define ENCODING_KR
182    
183     #include "table/ksc5601_1987_0.h"
184    
185 root 1.20 //#define ENCODING_ZH
186 pcg 1.1
187 root 1.21 #include "table/big5.h"
188     #include "table/gbk_0.h"
189 pcg 1.1 #include "table/gb2312_1980_0.h"
190    
191 root 1.20 //#define ENCODING_ZH_EXT
192 pcg 1.1
193     #include "table/cns11643_1992_1.h"
194     #include "table/cns11643_1992_2.h"
195     #include "table/cns11643_1992_3.h"
196     #include "table/cns11643_1992_4.h"
197     #include "table/cns11643_1992_5.h"
198     #include "table/cns11643_1992_6.h"
199     #include "table/cns11643_1992_7.h"
200     #include "table/cns11643_1992_f.h"
201     #include "table/big5_ext.h"
202     #include "table/big5_plus.h"
203    
204     //#define ENCODING_VN
205    
206     #include "table/viscii.h"
207    
208     //#define ENCODING_JP
209    
210     #include "table/jis0201_1976_0.h"
211 pcg 1.15 #include "table/jis0208_1990_0.h"
212 pcg 1.1 #include "table/jis0212_1990_0.h"
213    
214     //#define ENCODING_JP_EXT
215    
216     #include "table/jis0213_1.h"
217     #include "table/jis0213_2.h"
218    
219 root 1.18 #if ENCODING_TO_UNICODE
220     # define ENC(base) { cs_ ## base ## _from_unicode, cs_ ## base ## _to_unicode }
221     #else
222     # define ENC(base) { cs_ ## base ## _from_unicode }
223     #endif
224 ayin 1.26
225 root 1.18
226 pcg 1.3 // order must match table in encoding.h(!)
227 root 1.18 const rxvt_codeset_conv rxvt_codeset[NUM_CODESETS] = {
228     ENC (unknown),
229 pcg 1.1
230 root 1.18 ENC (us_ascii),
231 pcg 1.1
232 root 1.18 ENC (iso8859_1),
233     ENC (iso8859_2),
234     ENC (iso8859_3),
235     ENC (iso8859_4),
236     ENC (iso8859_5),
237     ENC (iso8859_6),
238     ENC (iso8859_7),
239     ENC (iso8859_8),
240     ENC (iso8859_9),
241     ENC (iso8859_10),
242     ENC (iso8859_11),
243     ENC (iso8859_13),
244     ENC (iso8859_14),
245     ENC (iso8859_15),
246     ENC (iso8859_16),
247    
248     ENC (koi8_r),
249     ENC (koi8_u),
250    
251     ENC (jis0201_1976_0),
252     ENC (jis0208_1990_0),
253     ENC (jis0212_1990_0),
254    
255     ENC (jis0213_1),
256     ENC (jis0213_2),
257    
258     ENC (ksc5601_1987_0),
259    
260     ENC (gb2312_1980_0),
261 root 1.21 ENC (gbk_0),
262 root 1.18
263     ENC (cns11643_1992_1),
264     ENC (cns11643_1992_2),
265     ENC (cns11643_1992_3),
266     ENC (cns11643_1992_4),
267     ENC (cns11643_1992_5),
268     ENC (cns11643_1992_6),
269     ENC (cns11643_1992_7),
270     ENC (cns11643_1992_f),
271     ENC (big5),
272     ENC (big5_ext),
273     ENC (big5_plus),
274 pcg 1.1
275 root 1.18 ENC (viscii),
276 pcg 1.1
277 root 1.18 ENC (unicode_16),
278     ENC (unicode),
279 pcg 1.1 };
280    
281 pcg 1.10 #if ENABLE_COMBINING
282     # define ENCODING_COMPOSE
283     #endif
284 pcg 1.9
285     #include "table/compose.h"
286    
287 pcg 1.15 unicode_t
288     rxvt_compose (unicode_t c1, unicode_t c2)
289 pcg 1.9 {
290     int l = 0;
291 sf-exg 1.30 int r = ARRAY_LENGTH(rxvt_compose_table) - 1;
292 pcg 1.9 int m;
293    
294 root 1.27 while (r >= l)
295 pcg 1.9 {
296     m = (l + r) / 2;
297     rxvt_compose_entry &c = rxvt_compose_table[m];
298    
299     if (c.c1 < c1 || (c.c1 == c1 && c.c2 < c2))
300     l = m + 1;
301     else if (c.c1 > c1 || (c.c1 == c1 && c.c2 > c2))
302     r = m - 1;
303     else
304     return c.r;
305     }
306    
307     return NOCHAR;
308     }
309 pcg 1.15
310     #include "table/category.h"
311    
312     bool unicode::is_space (unicode_t c)
313     {
314 root 1.19 return IS_SPACE (c)
315     || c == 0x09; // exclude tabs, too, as we store them in the buffer
316 pcg 1.15 }