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) {}
46 IconvProcessor::IconvProcessor(char const * tocode,
47 char const * fromcode): tocode_(tocode), fromcode_(fromcode),
48 pimpl_(new IconvProcessor::Private)
53 IconvProcessor::~IconvProcessor()
55 if (iconv_close(pimpl_->cd) == -1) {
56 lyxerr << "Error returned from iconv_close("
57 << errno << ")" << endl;
63 bool IconvProcessor::init()
65 if (pimpl_->cd != invalid_cd)
68 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
69 if (pimpl_->cd != invalid_cd)
72 lyxerr << "Error returned from iconv_open" << endl;
75 lyxerr << "EINVAL The conversion from " << fromcode_
77 << " is not supported by the implementation."
81 lyxerr << "\tSome other error: " << errno << endl;
88 int IconvProcessor::convert(char const * buf, size_t buflen,
89 char * outbuf, size_t maxoutsize)
94 if (pimpl_->cd == invalid_cd) {
99 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
100 size_t inbytesleft = buflen;
101 size_t outbytesleft = maxoutsize;
103 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
105 //lyxerr << std::dec;
106 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
107 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
110 // Everything went well.
111 return maxoutsize - outbytesleft;
113 // There are some errors in the conversion
114 lyxerr << "Error returned from iconv" << endl;
117 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
120 lyxerr << "EILSEQ An invalid multibyte sequence"
121 << " has been encountered in the input.\n"
122 << "When converting from " << fromcode_
123 << " to " << tocode_ << ".\n";
124 lyxerr << "Input: " << std::hex;
125 for (size_t i = 0; i < buflen; ++i) {
126 boost::uint32_t const b = buf[i];
127 lyxerr << "0x" << b << " ";
132 lyxerr << "EINVAL An incomplete multibyte sequence"
133 << " has been encountered in the input.\n"
134 << "When converting from " << fromcode_
135 << " to " << tocode_ << ".\n";
136 lyxerr << "Input: " << std::hex;
137 for (size_t i = 0; i < buflen; ++i) {
138 boost::uint32_t const b = buf[i];
139 lyxerr << "0x" << b << " ";
144 lyxerr << "\tSome other error: " << errno << endl;
147 // We got an error so we close down the conversion engine
148 if (iconv_close(pimpl_->cd) == -1) {
149 lyxerr << "Error returned from iconv_close("
150 << errno << ")" << endl;
152 pimpl_->cd = invalid_cd;
160 template<typename RetType, typename InType>
162 iconv_convert(IconvProcessor & processor,
167 return std::vector<RetType>();
169 char const * inbuf = reinterpret_cast<char const *>(buf);
170 size_t inbytesleft = buflen * sizeof(InType);
172 size_t const outsize = 32768;
173 static char out[outsize];
176 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
178 RetType const * tmp = reinterpret_cast<RetType const *>(out);
179 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
185 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
188 return std::vector<lyx::char_type>();
190 return utf8_to_ucs4(&utf8str[0], utf8str.size());
194 std::vector<lyx::char_type>
195 utf8_to_ucs4(char const * utf8str, size_t ls)
197 static IconvProcessor processor(ucs4_codeset, "UTF-8");
198 return iconv_convert<lyx::char_type>(processor, utf8str, ls);
203 ucs2_to_ucs4(unsigned short c)
205 return ucs2_to_ucs4(&c, 1)[0];
209 std::vector<lyx::char_type>
210 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
213 return std::vector<lyx::char_type>();
215 return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
219 std::vector<lyx::char_type>
220 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
222 static IconvProcessor processor(ucs4_codeset, ucs2_codeset);
223 return iconv_convert<lyx::char_type>(processor, ucs2str, ls);
228 ucs4_to_ucs2(lyx::char_type c)
230 return ucs4_to_ucs2(&c, 1)[0];
234 std::vector<unsigned short>
235 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
238 return std::vector<unsigned short>();
240 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
244 std::vector<unsigned short>
245 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
247 static IconvProcessor processor(ucs2_codeset, ucs4_codeset);
248 return iconv_convert<unsigned short>(processor, s, ls);
253 ucs4_to_utf8(lyx::char_type c)
255 static IconvProcessor processor("UTF-8", ucs4_codeset);
256 return iconv_convert<char>(processor, &c, 1);
261 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
264 return std::vector<char>();
266 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
271 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
273 static IconvProcessor processor("UTF-8", ucs4_codeset);
274 return iconv_convert<char>(processor, ucs4str, ls);
278 std::vector<lyx::char_type>
279 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding)
281 static std::map<std::string, IconvProcessor> processors;
282 if (processors.find(encoding) == processors.end()) {
283 IconvProcessor processor(ucs4_codeset, encoding.c_str());
284 processors.insert(std::make_pair(encoding, processor));
286 return iconv_convert<char_type>(processors[encoding], s, ls);
291 ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding)
293 static std::map<std::string, IconvProcessor> processors;
294 if (processors.find(encoding) == processors.end()) {
295 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
296 processors.insert(std::make_pair(encoding, processor));
298 return iconv_convert<char>(processors[encoding], ucs4str, ls);