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