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