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>
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::Private {
57 Private(): cd(invalid_cd) {}
60 if (cd != invalid_cd) {
61 if (iconv_close(cd) == -1) {
62 lyxerr << "Error returned from iconv_close("
63 << errno << ")" << endl;
71 IconvProcessor::IconvProcessor(char const * tocode, char const * fromcode)
72 : tocode_(tocode), fromcode_(fromcode),
73 pimpl_(new IconvProcessor::Private)
78 IconvProcessor::IconvProcessor(IconvProcessor const & other)
79 : tocode_(other.tocode_), fromcode_(other.fromcode_),
80 pimpl_(new IconvProcessor::Private)
85 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
89 tocode_ = other.tocode_;
90 fromcode_ = other.fromcode_;
91 pimpl_.reset(new Private);
96 IconvProcessor::~IconvProcessor() {}
99 bool IconvProcessor::init()
101 if (pimpl_->cd != invalid_cd)
104 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
105 if (pimpl_->cd != invalid_cd)
108 lyxerr << "Error returned from iconv_open" << endl;
111 lyxerr << "EINVAL The conversion from " << fromcode_
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 " << fromcode_
159 << " to " << 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 " << fromcode_
174 << " to " << 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,
209 return vector<RetType>();
211 char const * inbuf = reinterpret_cast<char const *>(buf);
212 size_t inbytesleft = buflen * sizeof(InType);
214 size_t const outsize = 32768;
215 static char out[outsize];
218 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
221 // FIXME Maybe throw an exception and handle that in the caller?
222 return vector<RetType>();
224 RetType const * tmp = reinterpret_cast<RetType const *>(out);
225 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
231 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
234 return vector<char_type>();
236 return utf8_to_ucs4(&utf8str[0], utf8str.size());
241 utf8_to_ucs4(char const * utf8str, size_t ls)
243 static IconvProcessor processor(ucs4_codeset, "UTF-8");
244 return iconv_convert<char_type>(processor, utf8str, ls);
249 utf16_to_ucs4(unsigned short const * s, size_t ls)
251 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
252 return iconv_convert<char_type>(processor, s, ls);
256 vector<unsigned short>
257 ucs4_to_utf16(char_type const * s, size_t ls)
259 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
260 return iconv_convert<unsigned short>(processor, s, ls);
265 ucs4_to_utf8(char_type c)
267 static IconvProcessor processor("UTF-8", ucs4_codeset);
268 return iconv_convert<char>(processor, &c, 1);
273 ucs4_to_utf8(vector<char_type> const & ucs4str)
276 return vector<char>();
278 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
283 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
285 static IconvProcessor processor("UTF-8", ucs4_codeset);
286 return iconv_convert<char>(processor, ucs4str, ls);
291 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
293 static map<string, IconvProcessor> processors;
294 if (processors.find(encoding) == processors.end()) {
295 IconvProcessor processor(ucs4_codeset, encoding.c_str());
296 processors.insert(make_pair(encoding, processor));
298 return iconv_convert<char_type>(processors[encoding], s, ls);
303 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
305 static map<string, IconvProcessor> processors;
306 if (processors.find(encoding) == processors.end()) {
307 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
308 processors.insert(make_pair(encoding, processor));
310 return iconv_convert<char>(processors[encoding], ucs4str, ls);
314 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
316 static map<string, IconvProcessor> processors;
317 map<string, IconvProcessor>::iterator it = processors.find(encoding);
318 if (it == processors.end()) {
319 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
320 it = processors.insert(make_pair(encoding, processor)).first;
324 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
331 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
332 string const & encoding)
334 static map<string, IconvProcessor> processors;
335 map<string, IconvProcessor>::iterator it = processors.find(encoding);
336 if (it == processors.end()) {
337 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
338 it = processors.insert(make_pair(encoding, processor)).first;
342 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);