]> git.lyx.org Git - lyx.git/blob - src/support/unicode.cpp
35dde63014f1bbfb7247bf7014411214f69b3edc
[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 "support/unicode.h"
16 #include "support/debug.h"
17
18 #include <iconv.h>
19
20 #include <boost/cstdint.hpp>
21
22 #include <cerrno>
23 #include <iomanip>
24 #include <map>
25 #include <string>
26
27 using namespace std;
28
29 namespace {
30
31 #ifdef WORDS_BIGENDIAN
32         char const * utf16_codeset = "UTF16-BE";
33 #else
34         char const * utf16_codeset = "UTF16-LE";
35 #endif
36
37 }
38
39
40 namespace lyx {
41
42 #ifdef WORDS_BIGENDIAN
43         char const * ucs4_codeset = "UCS-4BE";
44 #else
45         char const * ucs4_codeset = "UCS-4LE";
46 #endif
47
48 static const iconv_t invalid_cd = (iconv_t)(-1);
49
50
51 struct IconvProcessor::Impl
52 {
53         Impl(string const & to, string const & from)
54                 : cd(invalid_cd), tocode_(to), fromcode_(from)
55         {}
56
57         ~Impl()
58         {
59                 if (cd != invalid_cd && iconv_close(cd) == -1)
60                                 LYXERR0("Error returned from iconv_close(" << errno << ")");
61         }
62
63         iconv_t cd;
64         string tocode_;
65         string fromcode_;
66 };
67
68
69 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
70         : pimpl_(new IconvProcessor::Impl(tocode, fromcode))
71 {
72 }
73
74
75 IconvProcessor::IconvProcessor(IconvProcessor const & other)
76         : pimpl_(new IconvProcessor::Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_))
77 {
78 }
79
80
81 IconvProcessor::~IconvProcessor()
82 {
83         delete pimpl_;
84 }
85
86
87 void IconvProcessor::operator=(IconvProcessor const & other)
88 {
89         if (&other != this)
90                 pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
91 }
92
93
94 bool IconvProcessor::init()
95 {
96         if (pimpl_->cd != invalid_cd)
97                 return true;
98
99         pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->fromcode_.c_str());
100         if (pimpl_->cd != invalid_cd)
101                 return true;
102
103         lyxerr << "Error returned from iconv_open" << endl;
104         switch (errno) {
105                 case EINVAL:
106                         lyxerr << "EINVAL The conversion from " << pimpl_->fromcode_
107                                 << " to " << pimpl_->tocode_
108                                 << " is not supported by the implementation."
109                                 << endl;
110                         break;
111                 default:
112                         lyxerr << "\tSome other error: " << errno << endl;
113                         break;
114         }
115         return false;
116 }
117
118
119 int IconvProcessor::convert(char const * buf, size_t buflen,
120                 char * outbuf, size_t maxoutsize)
121 {
122         if (buflen == 0)
123                 return 0;
124
125         if (pimpl_->cd == invalid_cd) {
126                 if (!init())
127                         return -1;
128         }
129
130         char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
131         size_t inbytesleft = buflen;
132         size_t outbytesleft = maxoutsize;
133
134         int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
135
136         // flush out remaining data. This is needed because iconv sometimes
137         // holds back chars in the stream, waiting for a combination character
138         // (see e.g. http://sources.redhat.com/bugzilla/show_bug.cgi?id=1124)
139         iconv(pimpl_->cd, NULL, NULL, &outbuf, &outbytesleft);
140
141         //lyxerr << 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 " << pimpl_->fromcode_
159                                 << " to " << pimpl_->tocode_ << ".\n";
160                         lyxerr << "Input:" << 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 << 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 " << pimpl_->fromcode_
174                                 << " to " << pimpl_->tocode_ << ".\n";
175                         lyxerr << "Input:" << 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 << 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, InType const * buf, size_t buflen)
205 {
206         if (buflen == 0)
207                 return vector<RetType>();
208
209         char const * inbuf = reinterpret_cast<char const *>(buf);
210         size_t inbytesleft = buflen * sizeof(InType);
211
212         size_t const outsize = 32768;
213         static char out[outsize];
214         char * outbuf = out;
215
216         int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
217         if (bytes <= 0)
218                 // Conversion failed
219                 // FIXME Maybe throw an exception and handle that in the caller?
220                 return vector<RetType>();
221
222         RetType const * tmp = reinterpret_cast<RetType const *>(out);
223         return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
224 }
225
226 } // anon namespace
227
228
229 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
230 {
231         if (utf8str.empty())
232                 return vector<char_type>();
233
234         return utf8_to_ucs4(&utf8str[0], utf8str.size());
235 }
236
237
238 vector<char_type>
239 utf8_to_ucs4(char const * utf8str, size_t ls)
240 {
241         static IconvProcessor processor(ucs4_codeset, "UTF-8");
242         return iconv_convert<char_type>(processor, utf8str, ls);
243 }
244
245
246 vector<char_type>
247 utf16_to_ucs4(unsigned short const * s, size_t ls)
248 {
249         static IconvProcessor processor(ucs4_codeset, utf16_codeset);
250         return iconv_convert<char_type>(processor, s, ls);
251 }
252
253
254 vector<unsigned short>
255 ucs4_to_utf16(char_type const * s, size_t ls)
256 {
257         static IconvProcessor processor(utf16_codeset, ucs4_codeset);
258         return iconv_convert<unsigned short>(processor, s, ls);
259 }
260
261
262 vector<char>
263 ucs4_to_utf8(char_type c)
264 {
265         static IconvProcessor processor("UTF-8", ucs4_codeset);
266         return iconv_convert<char>(processor, &c, 1);
267 }
268
269
270 vector<char>
271 ucs4_to_utf8(vector<char_type> const & ucs4str)
272 {
273         if (ucs4str.empty())
274                 return vector<char>();
275
276         return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
277 }
278
279
280 vector<char>
281 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
282 {
283         static IconvProcessor processor("UTF-8", ucs4_codeset);
284         return iconv_convert<char>(processor, ucs4str, ls);
285 }
286
287
288 vector<char_type>
289 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
290 {
291         static map<string, IconvProcessor> processors;
292         if (processors.find(encoding) == processors.end()) {
293                 IconvProcessor processor(ucs4_codeset, encoding.c_str());
294                 processors.insert(make_pair(encoding, processor));
295         }
296         return iconv_convert<char_type>(processors[encoding], s, ls);
297 }
298
299
300 vector<char>
301 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
302 {
303         static map<string, IconvProcessor> processors;
304         if (processors.find(encoding) == processors.end()) {
305                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
306                 processors.insert(make_pair(encoding, processor));
307         }
308         return iconv_convert<char>(processors[encoding], ucs4str, ls);
309 }
310
311
312 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
313 {
314         static map<string, IconvProcessor> processors;
315         map<string, IconvProcessor>::iterator it = processors.find(encoding);
316         if (it == processors.end()) {
317                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
318                 it = processors.insert(make_pair(encoding, processor)).first;
319         }
320
321         char out;
322         int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
323         if (bytes > 0)
324                 return out;
325         return 0;
326 }
327
328
329 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
330         string const & encoding)
331 {
332         static map<string, IconvProcessor> processors;
333         map<string, IconvProcessor>::iterator it = processors.find(encoding);
334         if (it == processors.end()) {
335                 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
336                 it = processors.insert(make_pair(encoding, processor)).first;
337         }
338
339         out.resize(4);
340         int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);
341         if (bytes > 0)
342                 out.resize(bytes);
343         else
344                 out.clear();
345 }
346
347 } // namespace lyx