]> git.lyx.org Git - lyx.git/blob - src/support/unicode.C
Restore inputenc support
[lyx.git] / src / support / unicode.C
1 /**
2  * \file unicode.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * A collection of unicode conversion functions, using iconv.
11  */
12
13 #include <config.h>
14
15 #include "unicode.h"
16
17 #include "debug.h"
18
19 #include <iconv.h>
20
21 #include <cerrno>
22 #include <iomanip>
23 #include <map>
24
25
26 namespace lyx {
27
28 using std::endl;
29
30 #ifdef WORDS_BIGENDIAN
31         char const * ucs4_codeset = "UCS-4BE";
32         char const * ucs2_codeset = "UCS-2BE";
33 #else
34         char const * ucs4_codeset = "UCS-4LE";
35         char const * ucs2_codeset = "UCS-2LE";
36 #endif
37
38 namespace {
39
40 template<typename RetType, typename InType>
41 std::vector<RetType>
42 iconv_convert(iconv_t * cd,
43               char const * tocode,
44               char const * fromcode,
45               InType const * buf,
46               size_t buflen)
47 {
48         if (buflen == 0)
49                 return std::vector<RetType>();
50
51         if (*cd == (iconv_t)(-1)) {
52                 *cd = iconv_open(tocode, fromcode);
53                 if (*cd == (iconv_t)(-1)) {
54                         lyxerr << "Error returned from iconv_open" << endl;
55                         switch (errno) {
56                         case EINVAL:
57                                 lyxerr << "EINVAL The conversion from " << fromcode
58                                        << " to " << tocode
59                                        << " is not supported by the implementation."
60                                        << endl;
61                                 break;
62                         default:
63                                 lyxerr << "\tSome other error: " << errno << endl;
64                                 break;
65                         }
66                 }
67         }
68
69         char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(reinterpret_cast<char const *>(buf));
70         size_t inbytesleft = buflen * sizeof(InType);
71         // The preamble of the user guide is more than 11.500 characters, so we go for 32kb
72         size_t const outsize = 32768;
73         static char out[outsize];
74         char * outbuf = out;
75         size_t outbytesleft = outsize;
76
77         size_t res = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
78
79         if (res == (size_t)(-1)) {
80                 lyxerr << "Error returned from iconv" << endl;
81                 switch (errno) {
82                 case E2BIG:
83                         lyxerr << "E2BIG  There is not sufficient room at *outbuf." << endl;
84                         break;
85                 case EILSEQ:
86                         lyxerr << "EILSEQ An invalid multibyte sequence"
87                                << " has been encountered in the input.\n"
88                                << "When converting from " << fromcode
89                                << " to " << tocode << ".\n";
90                         lyxerr << "Input: " << std::hex;
91                         for (size_t i = 0; i < buflen; ++i) {
92                                 boost::uint32_t const b = buf[i];
93                                 lyxerr << "0x" << b << " ";
94                         }
95                         lyxerr << endl;
96                         break;
97                 case EINVAL:
98                         lyxerr << "EINVAL An incomplete multibyte sequence"
99                                << " has been encountered in the input.\n"
100                                << "When converting from " << fromcode
101                                << " to " << tocode << ".\n";
102                         lyxerr << "Input: " << std::hex;
103                         for (size_t i = 0; i < buflen; ++i) {
104                                 boost::uint32_t const b = buf[i];
105                                 lyxerr << "0x" << b << " ";
106                         }
107                         lyxerr << endl;
108                         break;
109                 default:
110                         lyxerr << "\tSome other error: " << errno << endl;
111                         break;
112                 }
113                 // We got an error so we close down the conversion engine
114                 if (iconv_close(*cd) == -1) {
115                         lyxerr << "Error returned from iconv_close("
116                                << errno << ")" << endl;
117                 }
118                 *cd = (iconv_t)(-1);
119         }
120
121         //lyxerr << std::dec;
122         //lyxerr << "Inbytesleft: " << inbytesleft << endl;
123         //lyxerr << "Outbytesleft: " << outbytesleft << endl;
124         int bytes = outsize - outbytesleft;
125
126         RetType const * tmp = reinterpret_cast<RetType const *>(out);
127         return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
128 }
129
130 } // anon namespace
131
132
133 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
134 {
135         if (utf8str.empty())
136                 return std::vector<lyx::char_type>();
137
138         return utf8_to_ucs4(&utf8str[0], utf8str.size());
139 }
140
141
142 std::vector<lyx::char_type>
143 utf8_to_ucs4(char const * utf8str, size_t ls)
144 {
145         static iconv_t cd = (iconv_t)(-1);
146         return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, "UTF-8",
147                                               utf8str, ls);
148 }
149
150
151 lyx::char_type
152 ucs2_to_ucs4(unsigned short c)
153 {
154         return ucs2_to_ucs4(&c, 1)[0];
155 }
156
157
158 std::vector<lyx::char_type>
159 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
160 {
161         if (ucs2str.empty())
162                 return std::vector<lyx::char_type>();
163
164         return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
165 }
166
167
168 std::vector<lyx::char_type>
169 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
170 {
171         static iconv_t cd = (iconv_t)(-1);
172         return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, ucs2_codeset,
173                                               ucs2str, ls);
174 }
175
176
177 unsigned short
178 ucs4_to_ucs2(lyx::char_type c)
179 {
180         return ucs4_to_ucs2(&c, 1)[0];
181 }
182
183
184 std::vector<unsigned short>
185 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
186 {
187         if (ucs4str.empty())
188                 return std::vector<unsigned short>();
189
190         return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
191 }
192
193
194 std::vector<unsigned short>
195 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
196 {
197         static iconv_t cd = (iconv_t)(-1);
198         return iconv_convert<unsigned short>(&cd, ucs2_codeset, ucs4_codeset,
199                                              s, ls);
200 }
201
202
203 std::vector<char>
204 ucs4_to_utf8(lyx::char_type c)
205 {
206         static iconv_t cd = (iconv_t)(-1);
207         return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset, &c, 1);
208 }
209
210
211 std::vector<char>
212 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
213 {
214         if (ucs4str.empty())
215                 return std::vector<char>();
216
217         return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
218 }
219
220
221 std::vector<char>
222 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
223 {
224         static iconv_t cd = (iconv_t)(-1);
225         return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset,
226                                    ucs4str, ls);
227 }
228
229
230 std::vector<lyx::char_type>
231 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding)
232 {
233         static std::map<std::string, iconv_t> cd;
234         if (cd.find(encoding) == cd.end())
235                 cd[encoding] = (iconv_t)(-1);
236         return iconv_convert<char_type>(&cd[encoding], ucs4_codeset,
237                                         encoding.c_str(), s, ls);
238 }
239
240
241 std::vector<char>
242 ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding)
243 {
244         static std::map<std::string, iconv_t> cd;
245         if (cd.find(encoding) == cd.end())
246                 cd[encoding] = (iconv_t)(-1);
247         return iconv_convert<char>(&cd[encoding], encoding.c_str(),
248                                    ucs4_codeset, ucs4str, ls);
249 }
250
251 } // namespace lyx