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.
21 #include <boost/cstdint.hpp>
35 #ifdef WORDS_BIGENDIAN
36 char const * utf16_codeset = "UTF16-BE";
38 char const * utf16_codeset = "UTF16-LE";
46 #ifdef WORDS_BIGENDIAN
47 char const * ucs4_codeset = "UCS-4BE";
49 char const * ucs4_codeset = "UCS-4LE";
52 static const iconv_t invalid_cd = (iconv_t)(-1);
55 struct IconvProcessor::Private {
56 Private(): cd(invalid_cd) {}
59 if (cd != invalid_cd) {
60 if (iconv_close(cd) == -1) {
61 lyxerr << "Error returned from iconv_close("
62 << errno << ")" << endl;
70 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
71 : tocode_(tocode), fromcode_(fromcode),
72 pimpl_(new IconvProcessor::Private)
77 IconvProcessor::IconvProcessor(IconvProcessor const & other)
78 : tocode_(other.tocode_), fromcode_(other.fromcode_),
79 pimpl_(new IconvProcessor::Private)
84 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
88 tocode_ = other.tocode_;
89 fromcode_ = other.fromcode_;
90 pimpl_.reset(new Private);
95 IconvProcessor::~IconvProcessor() {}
98 bool IconvProcessor::init()
100 if (pimpl_->cd != invalid_cd)
103 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
104 if (pimpl_->cd != invalid_cd)
107 lyxerr << "Error returned from iconv_open" << endl;
110 lyxerr << "EINVAL The conversion from " << fromcode_
112 << " is not supported by the implementation."
116 lyxerr << "\tSome other error: " << errno << endl;
123 int IconvProcessor::convert(char const * buf, size_t buflen,
124 char * outbuf, size_t maxoutsize)
129 if (pimpl_->cd == invalid_cd) {
134 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
135 size_t inbytesleft = buflen;
136 size_t outbytesleft = maxoutsize;
138 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
140 //lyxerr << std::dec;
141 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
142 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
145 // Everything went well.
146 return maxoutsize - outbytesleft;
148 // There are some errors in the conversion
149 lyxerr << "Error returned from iconv" << endl;
152 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
155 lyxerr << "EILSEQ An invalid multibyte sequence"
156 << " has been encountered in the input.\n"
157 << "When converting from " << fromcode_
158 << " to " << 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" << b;
167 lyxerr << std::dec << endl;
170 lyxerr << "EINVAL An incomplete multibyte sequence"
171 << " has been encountered in the input.\n"
172 << "When converting from " << fromcode_
173 << " to " << 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" << b;
182 lyxerr << std::dec << endl;
185 lyxerr << "\tSome other error: " << errno << endl;
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;
193 pimpl_->cd = invalid_cd;
201 template<typename RetType, typename InType>
203 iconv_convert(IconvProcessor & processor,
208 return vector<RetType>();
210 char const * inbuf = reinterpret_cast<char const *>(buf);
211 size_t inbytesleft = buflen * sizeof(InType);
213 size_t const outsize = 32768;
214 static char out[outsize];
217 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
220 // FIXME Maybe throw an exception and handle that in the caller?
221 return vector<RetType>();
223 RetType const * tmp = reinterpret_cast<RetType const *>(out);
224 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
230 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
233 return vector<char_type>();
235 return utf8_to_ucs4(&utf8str[0], utf8str.size());
240 utf8_to_ucs4(char const * utf8str, size_t ls)
242 static IconvProcessor processor(ucs4_codeset, "UTF-8");
243 return iconv_convert<char_type>(processor, utf8str, ls);
248 utf16_to_ucs4(unsigned short const * s, size_t ls)
250 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
251 return iconv_convert<char_type>(processor, s, ls);
255 vector<unsigned short>
256 ucs4_to_utf16(char_type const * s, size_t ls)
258 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
259 return iconv_convert<unsigned short>(processor, s, ls);
264 ucs4_to_utf8(char_type c)
266 static IconvProcessor processor("UTF-8", ucs4_codeset);
267 return iconv_convert<char>(processor, &c, 1);
272 ucs4_to_utf8(vector<char_type> const & ucs4str)
275 return vector<char>();
277 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
282 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
284 static IconvProcessor processor("UTF-8", ucs4_codeset);
285 return iconv_convert<char>(processor, ucs4str, ls);
290 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
292 static map<string, IconvProcessor> processors;
293 if (processors.find(encoding) == processors.end()) {
294 IconvProcessor processor(ucs4_codeset, encoding.c_str());
295 processors.insert(make_pair(encoding, processor));
297 return iconv_convert<char_type>(processors[encoding], s, ls);
302 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
304 static map<string, IconvProcessor> processors;
305 if (processors.find(encoding) == processors.end()) {
306 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
307 processors.insert(make_pair(encoding, processor));
309 return iconv_convert<char>(processors[encoding], ucs4str, ls);
313 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
315 static map<string, IconvProcessor> processors;
316 map<string, IconvProcessor>::iterator it = processors.find(encoding);
317 if (it == processors.end()) {
318 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
319 it = processors.insert(make_pair(encoding, processor)).first;
323 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
330 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
331 string const & encoding)
333 static map<string, IconvProcessor> processors;
334 map<string, IconvProcessor>::iterator it = processors.find(encoding);
335 if (it == processors.end()) {
336 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
337 it = processors.insert(make_pair(encoding, processor)).first;
341 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);