]> git.lyx.org Git - lyx.git/blob - src/support/unicode.cpp
f929d5665378ba122c15fb1d9a6f9d407ebf361f
[lyx.git] / src / support / unicode.cpp
1 /**
2  * \file unicode.cpp
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 <boost/cstdint.hpp>
22
23 #include <cerrno>
24 #include <iomanip>
25 #include <ostream>
26 #include <map>
27
28 using std::endl;
29 using std::map;
30 using std::make_pair;
31 using std::string;
32 using std::vector;
33
34 namespace {
35
36 #ifdef WORDS_BIGENDIAN
37         char const * utf16_codeset = "UTF16-BE";
38 #else
39         char const * utf16_codeset = "UTF16-LE";
40 #endif
41
42 }
43
44
45 namespace lyx {
46
47 #ifdef WORDS_BIGENDIAN
48         char const * ucs4_codeset = "UCS-4BE";
49 #else
50         char const * ucs4_codeset = "UCS-4LE";
51 #endif
52
53 static const iconv_t invalid_cd = (iconv_t)(-1);
54
55
56 struct IconvProcessor::Private {
57         Private(): cd(invalid_cd) {}
58         ~Private()
59         {
60                 if (cd != invalid_cd) {
61                         if (iconv_close(cd) == -1) {
62                                 lyxerr << "Error returned from iconv_close("
63                                        << errno << ")" << endl;
64                         }
65                 }
66         }
67         iconv_t cd;
68 };
69
70
71 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
72         : tocode_(tocode), fromcode_(fromcode),
73                 pimpl_(new IconvProcessor::Private)
74 {
75 }
76
77
78 IconvProcessor::IconvProcessor(IconvProcessor const & other)
79         : tocode_(other.tocode_), fromcode_(other.fromcode_),
80           pimpl_(new IconvProcessor::Private)
81 {
82 }
83
84
85 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
86 {
87         if (&other == this)
88                 return *this;
89         tocode_ = other.tocode_;
90         fromcode_ = other.fromcode_;
91         pimpl_.reset(new Private);
92         return *this;
93 }
94
95
96 IconvProcessor::~IconvProcessor() {}
97
98
99 bool IconvProcessor::init()
100 {
101         if (pimpl_->cd != invalid_cd)
102                 return true;
103
104         pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
105         if (pimpl_->cd != invalid_cd)
106                 return true;
107
108         lyxerr << "Error returned from iconv_open" << endl;
109         switch (errno) {
110                 case EINVAL:
111                         lyxerr << "EINVAL The conversion from " << fromcode_
112                                 << " to " << tocode_
113                                 << " is not supported by the implementation."
114                                 << endl;
115                         break;
116                 default:
117                         lyxerr << "\tSome other error: " << errno << endl;
118                         break;
119         }
120         return false;
121 }
122
123
124 int IconvProcessor::convert(char const * buf, size_t buflen,
125                 char * outbuf, size_t maxoutsize)
126 {
127         if (buflen == 0)
128                 return 0;
129
130         if (pimpl_->cd == invalid_cd) {
131                 if (!init())
132                         return -1;
133         }
134
135         char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
136         size_t inbytesleft = buflen;
137         size_t outbytesleft = maxoutsize;
138
139         int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
140
141         //lyxerr << std::dec;
142         //lyxerr << "Inbytesleft: " << inbytesleft << endl;
143         //lyxerr << "Outbytesleft: " << outbytesleft << endl;
144
145         if (res != -1)
146                 // Everything went well.
147                 return maxoutsize - outbytesleft;
148
149         // There are some errors in the conversion
150         lyxerr << "Error returned from iconv" << endl;
151         switch (errno) {
152                 case E2BIG:
153                         lyxerr << "E2BIG  There is not sufficient room at *outbuf." << endl;
154                         break;
155                 case EILSEQ:
156                         lyxerr << "EILSEQ An invalid multibyte sequence"
157                                 << " has been encountered in the input.\n"
158                                 << "When converting from " << fromcode_
159                                 << " to " << tocode_ << ".\n";
160                         lyxerr << "Input:" << std::hex;
161                         for (size_t i = 0; i < buflen; ++i) {
162                                 // char may be signed, avoid output of
163                                 // something like 0xffffffc2
164                                 boost::uint32_t const b =
165                                         *reinterpret_cast<unsigned char const *>(buf + i);
166                                 lyxerr << " 0x" << (unsigned int)b;
167                         }
168                         lyxerr << std::dec << endl;
169                         break;
170                 case EINVAL:
171                         lyxerr << "EINVAL An incomplete multibyte sequence"
172                                 << " has been encountered in the input.\n"
173                                 << "When converting from " << fromcode_
174                                 << " to " << tocode_ << ".\n";
175                         lyxerr << "Input:" << std::hex;
176                         for (size_t i = 0; i < buflen; ++i) {
177                                 // char may be signed, avoid output of
178                                 // something like 0xffffffc2
179                                 boost::uint32_t const b =
180                                         *reinterpret_cast<unsigned char const *>(buf + i);
181                                 lyxerr << " 0x" << (unsigned int)b;
182                         }
183                         lyxerr << std::dec << endl;
184                         break;
185                 default:
186                         lyxerr << "\tSome other error: " << errno << endl;
187                         break;
188         }
189         // We got an error so we close down the conversion engine
190         if (iconv_close(pimpl_->cd) == -1) {
191                 lyxerr << "Error returned from iconv_close("
192                         << errno << ")" << endl;
193         }
194         pimpl_->cd = invalid_cd;
195         return -1;
196 }
197
198
199 namespace {
200
201
202 template<typename RetType, typename InType>
203 vector<RetType>
204 iconv_convert(IconvProcessor & processor,
205               InType const * buf,
206               size_t buflen)
207 {
208         if (buflen == 0)
209                 return vector<RetType>();
210
211         char const * inbuf = reinterpret_cast<char const *>(buf);
212         size_t inbytesleft = buflen * sizeof(InType);
213
214         size_t const outsize = 32768;
215         static char out[outsize];
216         char * outbuf = out;
217
218         int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
219         if (bytes <= 0)
220                 // Conversion failed
221                 // FIXME Maybe throw an exception and handle that in the caller?
222                 return vector<RetType>();
223
224         RetType const * tmp = reinterpret_cast<RetType const *>(out);
225         return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
226 }
227
228 } // anon namespace
229
230
231 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
232 {
233         if (utf8str.empty())
234                 return vector<char_type>();
235
236         return utf8_to_ucs4(&utf8str[0], utf8str.size());
237 }
238
239
240 vector<char_type>
241 utf8_to_ucs4(char const * utf8str, size_t ls)
242 {
243         static IconvProcessor processor(ucs4_codeset, "UTF-8");
244         return iconv_convert<char_type>(processor, utf8str, ls);
245 }
246
247
248 vector<char_type>
249 utf16_to_ucs4(unsigned short const * s, size_t ls)
250 {
251         static IconvProcessor processor(ucs4_codeset, utf16_codeset);
252         return iconv_convert<char_type>(processor, s, ls);
253 }
254
255
256 vector<unsigned short>
257 ucs4_to_utf16(char_type const * s, size_t ls)
258 {
259         static IconvProcessor processor(utf16_codeset, ucs4_codeset);
260         return iconv_convert<unsigned short>(processor, s, ls);
261 }
262
263
264 vector<char>
265 ucs4_to_utf8(char_type c)
266 {
267         static IconvProcessor processor("UTF-8", ucs4_codeset);
268         return iconv_convert<char>(processor, &c, 1);
269 }
270
271
272 vector<char>
273 ucs4_to_utf8(vector<char_type> const & ucs4str)
274 {
275         if (ucs4str.empty())
276                 return vector<char>();
277
278         return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
279 }
280
281
282 vector<char>
283 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
284 {
285         static IconvProcessor processor("UTF-8", ucs4_codeset);
286         return iconv_convert<char>(processor, ucs4str, ls);
287 }
288
289
290 vector<char_type>
291 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
292 {
293         static map<string, IconvProcessor> processors;
294         if (processors.find(encoding) == processors.end()) {
295                 IconvProcessor processor(ucs4_codeset, encoding.c_str());
296                 processors.insert(make_pair(encoding, processor));
297         }
298         return iconv_convert<char_type>(processors[encoding], s, ls);
299 }
300
301
302 vector<char>
303 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
304 {
305         static map<string, IconvProcessor> processors;
306         if (processors.find(encoding) == processors.end()) {
307                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
308                 processors.insert(make_pair(encoding, processor));
309         }
310         return iconv_convert<char>(processors[encoding], ucs4str, ls);
311 }
312
313
314 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
315 {
316         static map<string, IconvProcessor> processors;
317         map<string, IconvProcessor>::iterator it = processors.find(encoding);
318         if (it == processors.end()) {
319                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
320                 it = processors.insert(make_pair(encoding, processor)).first;
321         }
322
323         char out;
324         int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
325         if (bytes > 0)
326                 return out;
327         return 0;
328 }
329
330
331 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
332         string const & encoding)
333 {
334         static map<string, IconvProcessor> processors;
335         map<string, IconvProcessor>::iterator it = processors.find(encoding);
336         if (it == processors.end()) {
337                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
338                 it = processors.insert(make_pair(encoding, processor)).first;
339         }
340
341         out.resize(4);
342         int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);
343         if (bytes > 0)
344                 out.resize(bytes);
345         else
346                 out.clear();
347 }
348
349 } // namespace lyx