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.
29 #ifdef WORDS_BIGENDIAN
30 char const * ucs4_codeset = "UCS-4BE";
31 char const * ucs2_codeset = "UCS-2BE";
33 char const * ucs4_codeset = "UCS-4LE";
34 char const * ucs2_codeset = "UCS-2LE";
37 static const iconv_t invalid_cd = (iconv_t)(-1);
40 struct IconvProcessor::Private {
41 Private(): cd(invalid_cd) {}
44 if (cd != invalid_cd) {
45 if (iconv_close(cd) == -1) {
46 lyxerr << "Error returned from iconv_close("
47 << errno << ")" << endl;
55 IconvProcessor::IconvProcessor(char const * tocode,
56 char const * fromcode): tocode_(tocode), fromcode_(fromcode),
57 pimpl_(new IconvProcessor::Private)
62 IconvProcessor::IconvProcessor(IconvProcessor const & other)
63 : tocode_(other.tocode_), fromcode_(other.fromcode_),
64 pimpl_(new IconvProcessor::Private)
69 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
73 tocode_ = other.tocode_;
74 fromcode_ = other.fromcode_;
75 pimpl_.reset(new Private);
80 IconvProcessor::~IconvProcessor() {}
83 bool IconvProcessor::init()
85 if (pimpl_->cd != invalid_cd)
88 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
89 if (pimpl_->cd != invalid_cd)
92 lyxerr << "Error returned from iconv_open" << endl;
95 lyxerr << "EINVAL The conversion from " << fromcode_
97 << " is not supported by the implementation."
101 lyxerr << "\tSome other error: " << errno << endl;
108 int IconvProcessor::convert(char const * buf, size_t buflen,
109 char * outbuf, size_t maxoutsize)
114 if (pimpl_->cd == invalid_cd) {
119 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
120 size_t inbytesleft = buflen;
121 size_t outbytesleft = maxoutsize;
123 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
125 //lyxerr << std::dec;
126 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
127 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
130 // Everything went well.
131 return maxoutsize - outbytesleft;
133 // There are some errors in the conversion
134 lyxerr << "Error returned from iconv" << endl;
137 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
140 lyxerr << "EILSEQ An invalid multibyte sequence"
141 << " has been encountered in the input.\n"
142 << "When converting from " << fromcode_
143 << " to " << tocode_ << ".\n";
144 lyxerr << "Input: " << std::hex;
145 for (size_t i = 0; i < buflen; ++i) {
146 boost::uint32_t const b = buf[i];
147 lyxerr << "0x" << b << " ";
152 lyxerr << "EINVAL An incomplete multibyte sequence"
153 << " has been encountered in the input.\n"
154 << "When converting from " << fromcode_
155 << " to " << tocode_ << ".\n";
156 lyxerr << "Input: " << std::hex;
157 for (size_t i = 0; i < buflen; ++i) {
158 boost::uint32_t const b = buf[i];
159 lyxerr << "0x" << b << " ";
164 lyxerr << "\tSome other error: " << errno << endl;
167 // We got an error so we close down the conversion engine
168 if (iconv_close(pimpl_->cd) == -1) {
169 lyxerr << "Error returned from iconv_close("
170 << errno << ")" << endl;
172 pimpl_->cd = invalid_cd;
180 template<typename RetType, typename InType>
182 iconv_convert(IconvProcessor & processor,
187 return std::vector<RetType>();
189 char const * inbuf = reinterpret_cast<char const *>(buf);
190 size_t inbytesleft = buflen * sizeof(InType);
192 size_t const outsize = 32768;
193 static char out[outsize];
196 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
198 RetType const * tmp = reinterpret_cast<RetType const *>(out);
199 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
205 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
208 return std::vector<lyx::char_type>();
210 return utf8_to_ucs4(&utf8str[0], utf8str.size());
214 std::vector<lyx::char_type>
215 utf8_to_ucs4(char const * utf8str, size_t ls)
217 static IconvProcessor processor(ucs4_codeset, "UTF-8");
218 return iconv_convert<lyx::char_type>(processor, utf8str, ls);
223 ucs2_to_ucs4(unsigned short c)
225 return ucs2_to_ucs4(&c, 1)[0];
229 std::vector<lyx::char_type>
230 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
233 return std::vector<lyx::char_type>();
235 return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
239 std::vector<lyx::char_type>
240 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
242 static IconvProcessor processor(ucs4_codeset, ucs2_codeset);
243 return iconv_convert<lyx::char_type>(processor, ucs2str, ls);
248 ucs4_to_ucs2(lyx::char_type c)
250 return ucs4_to_ucs2(&c, 1)[0];
254 std::vector<unsigned short>
255 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
258 return std::vector<unsigned short>();
260 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
264 std::vector<unsigned short>
265 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
267 static IconvProcessor processor(ucs2_codeset, ucs4_codeset);
268 return iconv_convert<unsigned short>(processor, s, ls);
273 ucs4_to_utf8(lyx::char_type c)
275 static IconvProcessor processor("UTF-8", ucs4_codeset);
276 return iconv_convert<char>(processor, &c, 1);
281 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
284 return std::vector<char>();
286 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
291 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
293 static IconvProcessor processor("UTF-8", ucs4_codeset);
294 return iconv_convert<char>(processor, ucs4str, ls);
298 std::vector<lyx::char_type>
299 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding)
301 static std::map<std::string, IconvProcessor> processors;
302 if (processors.find(encoding) == processors.end()) {
303 IconvProcessor processor(ucs4_codeset, encoding.c_str());
304 processors.insert(std::make_pair(encoding, processor));
306 return iconv_convert<char_type>(processors[encoding], s, ls);
311 ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding)
313 static std::map<std::string, IconvProcessor> processors;
314 if (processors.find(encoding) == processors.end()) {
315 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
316 processors.insert(std::make_pair(encoding, processor));
318 return iconv_convert<char>(processors[encoding], ucs4str, ls);