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.
33 #ifdef WORDS_BIGENDIAN
34 char const * utf16_codeset = "UTF16-BE";
36 char const * utf16_codeset = "UTF16-LE";
44 #ifdef WORDS_BIGENDIAN
45 char const * ucs4_codeset = "UCS-4BE";
47 char const * ucs4_codeset = "UCS-4LE";
50 static const iconv_t invalid_cd = (iconv_t)(-1);
53 struct IconvProcessor::Private {
54 Private(): cd(invalid_cd) {}
57 if (cd != invalid_cd) {
58 if (iconv_close(cd) == -1) {
59 lyxerr << "Error returned from iconv_close("
60 << errno << ")" << endl;
68 IconvProcessor::IconvProcessor(char const * tocode,
69 char const * fromcode): tocode_(tocode), fromcode_(fromcode),
70 pimpl_(new IconvProcessor::Private)
75 IconvProcessor::IconvProcessor(IconvProcessor const & other)
76 : tocode_(other.tocode_), fromcode_(other.fromcode_),
77 pimpl_(new IconvProcessor::Private)
82 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
86 tocode_ = other.tocode_;
87 fromcode_ = other.fromcode_;
88 pimpl_.reset(new Private);
93 IconvProcessor::~IconvProcessor() {}
96 bool IconvProcessor::init()
98 if (pimpl_->cd != invalid_cd)
101 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
102 if (pimpl_->cd != invalid_cd)
105 lyxerr << "Error returned from iconv_open" << endl;
108 lyxerr << "EINVAL The conversion from " << fromcode_
110 << " is not supported by the implementation."
114 lyxerr << "\tSome other error: " << errno << endl;
121 int IconvProcessor::convert(char const * buf, size_t buflen,
122 char * outbuf, size_t maxoutsize)
127 if (pimpl_->cd == invalid_cd) {
132 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
133 size_t inbytesleft = buflen;
134 size_t outbytesleft = maxoutsize;
136 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
138 //lyxerr << std::dec;
139 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
140 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
143 // Everything went well.
144 return maxoutsize - outbytesleft;
146 // There are some errors in the conversion
147 lyxerr << "Error returned from iconv" << endl;
150 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
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;
165 lyxerr << std::dec << endl;
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;
180 lyxerr << std::dec << endl;
183 lyxerr << "\tSome other error: " << errno << endl;
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;
191 pimpl_->cd = invalid_cd;
199 template<typename RetType, typename InType>
201 iconv_convert(IconvProcessor & processor,
206 return vector<RetType>();
208 char const * inbuf = reinterpret_cast<char const *>(buf);
209 size_t inbytesleft = buflen * sizeof(InType);
211 size_t const outsize = 32768;
212 static char out[outsize];
215 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
218 // FIXME Maybe throw an exception and handle that in the caller?
219 return vector<RetType>();
221 RetType const * tmp = reinterpret_cast<RetType const *>(out);
222 return vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
228 vector<char_type> utf8_to_ucs4(vector<char> const & utf8str)
231 return vector<char_type>();
233 return utf8_to_ucs4(&utf8str[0], utf8str.size());
238 utf8_to_ucs4(char const * utf8str, size_t ls)
240 static IconvProcessor processor(ucs4_codeset, "UTF-8");
241 return iconv_convert<char_type>(processor, utf8str, ls);
246 utf16_to_ucs4(unsigned short const * s, size_t ls)
248 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
249 return iconv_convert<char_type>(processor, s, ls);
253 vector<unsigned short>
254 ucs4_to_utf16(char_type const * s, size_t ls)
256 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
257 return iconv_convert<unsigned short>(processor, s, ls);
262 ucs4_to_utf8(char_type c)
264 static IconvProcessor processor("UTF-8", ucs4_codeset);
265 return iconv_convert<char>(processor, &c, 1);
270 ucs4_to_utf8(vector<char_type> const & ucs4str)
273 return vector<char>();
275 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
280 ucs4_to_utf8(char_type const * ucs4str, size_t ls)
282 static IconvProcessor processor("UTF-8", ucs4_codeset);
283 return iconv_convert<char>(processor, ucs4str, ls);
288 eightbit_to_ucs4(char const * s, size_t ls, string const & encoding)
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));
295 return iconv_convert<char_type>(processors[encoding], s, ls);
300 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, string const & encoding)
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));
307 return iconv_convert<char>(processors[encoding], ucs4str, ls);
311 char ucs4_to_eightbit(char_type ucs4, string const & encoding)
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;
321 int const bytes = it->second.convert((char *)(&ucs4), 4, &out, 1);
328 void ucs4_to_multibytes(char_type ucs4, vector<char> & out,
329 string const & encoding)
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;
339 int bytes = it->second.convert((char *)(&ucs4), 4, &out[0], 4);