]> git.lyx.org Git - lyx.git/blob - src/support/unicode.cpp
remove lyxrc dependence from support/*
[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 <cerrno>
22 #include <iomanip>
23 #include <map>
24
25 using std::endl;
26 using std::map;
27 using std::make_pair;
28 using std::string;
29 using std::vector;
30
31 namespace {
32
33 #ifdef WORDS_BIGENDIAN
34         char const * utf16_codeset = "UTF16-BE";
35 #else
36         char const * utf16_codeset = "UTF16-LE";
37 #endif
38
39 }
40
41
42 namespace lyx {
43
44 #ifdef WORDS_BIGENDIAN
45         char const * ucs4_codeset = "UCS-4BE";
46 #else
47         char const * ucs4_codeset = "UCS-4LE";
48 #endif
49
50 static const iconv_t invalid_cd = (iconv_t)(-1);
51
52
53 struct IconvProcessor::Private {
54         Private(): cd(invalid_cd) {}
55         ~Private()
56         {
57                 if (cd != invalid_cd) {
58                         if (iconv_close(cd) == -1) {
59                                 lyxerr << "Error returned from iconv_close("
60                                        << errno << ")" << endl;
61                         }
62                 }
63         }
64         iconv_t cd;
65 };
66
67
68 IconvProcessor::IconvProcessor(char const * tocode,
69                 char const * fromcode): tocode_(tocode), fromcode_(fromcode),
70                 pimpl_(new IconvProcessor::Private)
71 {
72 }
73
74
75 IconvProcessor::IconvProcessor(IconvProcessor const & other)
76         : tocode_(other.tocode_), fromcode_(other.fromcode_),
77           pimpl_(new IconvProcessor::Private)
78 {
79 }
80
81
82 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
83 {
84         if (&other == this)
85                 return *this;
86         tocode_ = other.tocode_;
87         fromcode_ = other.fromcode_;
88         pimpl_.reset(new Private);
89         return *this;
90 }
91
92
93 IconvProcessor::~IconvProcessor() {}
94
95
96 bool IconvProcessor::init()
97 {
98         if (pimpl_->cd != invalid_cd)
99                 return true;
100
101         pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
102         if (pimpl_->cd != invalid_cd)
103                 return true;
104
105         lyxerr << "Error returned from iconv_open" << endl;
106         switch (errno) {
107                 case EINVAL:
108                         lyxerr << "EINVAL The conversion from " << fromcode_
109                                 << " to " << tocode_
110                                 << " is not supported by the implementation."
111                                 << endl;
112                         break;
113                 default:
114                         lyxerr << "\tSome other error: " << errno << endl;
115                         break;
116         }
117         return false;
118 }
119
120
121 int IconvProcessor::convert(char const * buf, size_t buflen,
122                 char * outbuf, size_t maxoutsize)
123 {
124         if (buflen == 0)
125                 return 0;
126
127         if (pimpl_->cd == invalid_cd) {
128                 if (!init())
129                         return -1;
130         }
131
132         char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
133         size_t inbytesleft = buflen;
134         size_t outbytesleft = maxoutsize;
135
136         int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
137
138         //lyxerr << std::dec;
139         //lyxerr << "Inbytesleft: " << inbytesleft << endl;
140         //lyxerr << "Outbytesleft: " << outbytesleft << endl;
141
142         if (res != -1)
143                 // Everything went well.
144                 return maxoutsize - outbytesleft;
145
146         // There are some errors in the conversion
147         lyxerr << "Error returned from iconv" << endl;
148         switch (errno) {
149                 case E2BIG:
150                         lyxerr << "E2BIG  There is not sufficient room at *outbuf." << endl;
151                         break;
152                 case EILSEQ:
153                         lyxerr << "EILSEQ An invalid multibyte sequence"
154                                 << " has been encountered in the input.\n"
155                                 << "When converting from " << fromcode_
156                                 << " to " << tocode_ << ".\n";
157                         lyxerr << "Input:" << std::hex;
158                         for (size_t i = 0; i < buflen; ++i) {
159                                 // char may be signed, avoid output of
160                                 // something like 0xffffffc2
161                                 boost::uint32_t const b =
162                                         *reinterpret_cast<unsigned char const *>(buf + i);
163                                 lyxerr << " 0x" << b;
164                         }
165                         lyxerr << std::dec << endl;
166                         break;
167                 case EINVAL:
168                         lyxerr << "EINVAL An incomplete multibyte sequence"
169                                 << " has been encountered in the input.\n"
170                                 << "When converting from " << fromcode_
171                                 << " to " << tocode_ << ".\n";
172                         lyxerr << "Input:" << std::hex;
173                         for (size_t i = 0; i < buflen; ++i) {
174                                 // char may be signed, avoid output of
175                                 // something like 0xffffffc2
176                                 boost::uint32_t const b =
177                                         *reinterpret_cast<unsigned char const *>(buf + i);
178                                 lyxerr << " 0x" << b;
179                         }
180                         lyxerr << std::dec << endl;
181                         break;
182                 default:
183                         lyxerr << "\tSome other error: " << errno << endl;
184                         break;
185         }
186         // We got an error so we close down the conversion engine
187         if (iconv_close(pimpl_->cd) == -1) {
188                 lyxerr << "Error returned from iconv_close("
189                         << errno << ")" << endl;
190         }
191         pimpl_->cd = invalid_cd;
192         return -1;
193 }
194
195
196 namespace {
197
198
199 template<typename RetType, typename InType>
200 vector<RetType>
201 iconv_convert(IconvProcessor & processor,
202               InType const * buf,
203               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