]> git.lyx.org Git - lyx.git/blob - src/support/unicode.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[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 #include <string>
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::Impl
57 {
58         Impl(std::string const & to, std::string const & from)
59                 : cd(invalid_cd), tocode_(to), fromcode_(from)
60         {}
61
62         ~Impl()
63         {
64                 if (cd != invalid_cd && iconv_close(cd) == -1)
65                                 LYXERR0("Error returned from iconv_close(" << errno << ")");
66         }
67
68         iconv_t cd;
69         std::string tocode_;
70         std::string fromcode_;
71 };
72
73
74 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
75         : pimpl_(new IconvProcessor::Impl(tocode, fromcode))
76 {
77 }
78
79
80 IconvProcessor::IconvProcessor(IconvProcessor const & other)
81         : pimpl_(new IconvProcessor::Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_))
82 {
83 }
84
85
86 IconvProcessor::~IconvProcessor()
87 {
88         delete pimpl_;
89 }
90
91
92 void IconvProcessor::operator=(IconvProcessor const & other)
93 {
94         if (&other != this)
95                 pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
96 }
97
98
99 bool IconvProcessor::init()
100 {
101         if (pimpl_->cd != invalid_cd)
102                 return true;
103
104         pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->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 " << pimpl_->fromcode_
112                                 << " to " << pimpl_->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 " << pimpl_->fromcode_
159                                 << " to " << pimpl_->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 " << pimpl_->fromcode_
174                                 << " to " << pimpl_->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, 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