3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
10 * A collection of unicode conversion functions, using iconv.
15 #include "support/unicode.h"
16 #include "support/debug.h"
20 #include <boost/cstdint.hpp>
36 #ifdef WORDS_BIGENDIAN
37 char const * utf16_codeset = "UTF16-BE";
39 char const * utf16_codeset = "UTF16-LE";
47 #ifdef WORDS_BIGENDIAN
48 char const * ucs4_codeset = "UCS-4BE";
50 char const * ucs4_codeset = "UCS-4LE";
53 static const iconv_t invalid_cd = (iconv_t)(-1);
56 struct IconvProcessor::Impl
58 Impl(std::string const & to, std::string const & from)
59 : cd(invalid_cd), tocode_(to), fromcode_(from)
64 if (cd != invalid_cd && iconv_close(cd) == -1)
65 LYXERR0("Error returned from iconv_close(" << errno << ")");
70 std::string fromcode_;
74 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
75 : pimpl_(new IconvProcessor::Impl(tocode, fromcode))
80 IconvProcessor::IconvProcessor(IconvProcessor const & other)
81 : pimpl_(new IconvProcessor::Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_))
86 IconvProcessor::~IconvProcessor()
92 void IconvProcessor::operator=(IconvProcessor const & other)
95 pimpl_ = new Impl(other.pimpl_->tocode_, other.pimpl_->fromcode_);
99 bool IconvProcessor::init()
101 if (pimpl_->cd != invalid_cd)
104 pimpl_->cd = iconv_open(pimpl_->tocode_.c_str(), pimpl_->fromcode_.c_str());
105 if (pimpl_->cd != invalid_cd)
108 lyxerr << "Error returned from iconv_open" << endl;
111 lyxerr << "EINVAL The conversion from " << pimpl_->fromcode_
112 << " to " << pimpl_->tocode_
113 << " is not supported by the implementation."
117 lyxerr << "\tSome other error: " << errno << endl;
124 int IconvProcessor::convert(char const * buf, size_t buflen,
125 char * outbuf, size_t maxoutsize)
130 if (pimpl_->cd == invalid_cd) {
135 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
136 size_t inbytesleft = buflen;
137 size_t outbytesleft = maxoutsize;
139 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
141 //lyxerr << std::dec;
142 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
143 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
146 // Everything went well.
147 return maxoutsize - outbytesleft;
149 // There are some errors in the conversion
150 lyxerr << "Error returned from iconv" << endl;
153 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
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;
168 lyxerr << std::dec << endl;
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;
183 lyxerr << std::dec << endl;
186 lyxerr << "\tSome other error: " << errno << endl;
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;
194 pimpl_->cd = invalid_cd;
202 template<typename RetType, typename InType>
204 iconv_convert(IconvProcessor & processor, InType const * buf, size_t buflen)
207 return vector<RetType>();
209 char const * inbuf = reinterpret_cast<char const *>(buf);
210 size_t inbytesleft = buflen * sizeof(InType);
212 size_t const outsize = 32768;
213 static char out[outsize];
216 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
219 // FIXME Maybe throw an exception and handle that in the caller?
220 return vector<RetType>();
222 RetType const * tmp = reinterpret_cast<RetType const *>(out);
223 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
229 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
232 return vector<char_type>();
234 return utf8_to_ucs4(&utf8str[0], utf8str.size());
239 utf8_to_ucs4(char const * utf8str, size_t ls)
241 static IconvProcessor processor(ucs4_codeset, "UTF-8");
242 return iconv_convert<char_type>(processor, utf8str, ls);
247 utf16_to_ucs4(unsigned short const * s, size_t ls)
249 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
250 return iconv_convert<char_type>(processor, s, ls);
254 vector<unsigned short>
255 ucs4_to_utf16(char_type const * s, size_t ls)
257 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
258 return iconv_convert<unsigned short>(processor, s, ls);
263 ucs4_to_utf8(char_type c)
265 static IconvProcessor processor("UTF-8", ucs4_codeset);
266 return iconv_convert<char>(processor, &c, 1);
271 ucs4_to_utf8(vector<char_type> const & ucs4str)
274 return vector<char>();
276 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
281 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
283 static IconvProcessor processor("UTF-8", ucs4_codeset);
284 return iconv_convert<char>(processor, ucs4str, ls);
289 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
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));
296 return iconv_convert<char_type>(processors[encoding], s, ls);
301 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
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));
308 return iconv_convert<char>(processors[encoding], ucs4str, ls);
312 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
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;
322 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
329 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
330 string const & encoding)
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;
340 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);