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 * utf16_codeset = "UTF16-BE";
32 char const * utf16_codeset = "UTF16-LE";
40 #ifdef WORDS_BIGENDIAN
41 char const * ucs4_codeset = "UCS-4BE";
43 char const * ucs4_codeset = "UCS-4LE";
46 static const iconv_t invalid_cd = (iconv_t)(-1);
49 struct IconvProcessor::Private {
50 Private(): cd(invalid_cd) {}
53 if (cd != invalid_cd) {
54 if (iconv_close(cd) == -1) {
55 lyxerr << "Error returned from iconv_close("
56 << errno << ")" << endl;
64 IconvProcessor::IconvProcessor(char const * tocode,
65 char const * fromcode): tocode_(tocode), fromcode_(fromcode),
66 pimpl_(new IconvProcessor::Private)
71 IconvProcessor::IconvProcessor(IconvProcessor const & other)
72 : tocode_(other.tocode_), fromcode_(other.fromcode_),
73 pimpl_(new IconvProcessor::Private)
78 IconvProcessor & IconvProcessor::operator=(IconvProcessor const & other)
82 tocode_ = other.tocode_;
83 fromcode_ = other.fromcode_;
84 pimpl_.reset(new Private);
89 IconvProcessor::~IconvProcessor() {}
92 bool IconvProcessor::init()
94 if (pimpl_->cd != invalid_cd)
97 pimpl_->cd = iconv_open(tocode_.c_str(), fromcode_.c_str());
98 if (pimpl_->cd != invalid_cd)
101 lyxerr << "Error returned from iconv_open" << endl;
104 lyxerr << "EINVAL The conversion from " << fromcode_
106 << " is not supported by the implementation."
110 lyxerr << "\tSome other error: " << errno << endl;
117 int IconvProcessor::convert(char const * buf, size_t buflen,
118 char * outbuf, size_t maxoutsize)
123 if (pimpl_->cd == invalid_cd) {
128 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(buf);
129 size_t inbytesleft = buflen;
130 size_t outbytesleft = maxoutsize;
132 int res = iconv(pimpl_->cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
134 //lyxerr << std::dec;
135 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
136 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
139 // Everything went well.
140 return maxoutsize - outbytesleft;
142 // There are some errors in the conversion
143 lyxerr << "Error returned from iconv" << endl;
146 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
149 lyxerr << "EILSEQ An invalid multibyte sequence"
150 << " has been encountered in the input.\n"
151 << "When converting from " << fromcode_
152 << " to " << tocode_ << ".\n";
153 lyxerr << "Input:" << std::hex;
154 for (size_t i = 0; i < buflen; ++i) {
155 // char may be signed, avoid output of
156 // something like 0xffffffc2
157 boost::uint32_t const b =
158 *reinterpret_cast<unsigned char const *>(buf + i);
159 lyxerr << " 0x" << b;
164 lyxerr << "EINVAL An incomplete multibyte sequence"
165 << " has been encountered in the input.\n"
166 << "When converting from " << fromcode_
167 << " to " << tocode_ << ".\n";
168 lyxerr << "Input:" << std::hex;
169 for (size_t i = 0; i < buflen; ++i) {
170 // char may be signed, avoid output of
171 // something like 0xffffffc2
172 boost::uint32_t const b =
173 *reinterpret_cast<unsigned char const *>(buf + i);
174 lyxerr << " 0x" << b;
179 lyxerr << "\tSome other error: " << errno << endl;
182 // We got an error so we close down the conversion engine
183 if (iconv_close(pimpl_->cd) == -1) {
184 lyxerr << "Error returned from iconv_close("
185 << errno << ")" << endl;
187 pimpl_->cd = invalid_cd;
195 template<typename RetType, typename InType>
197 iconv_convert(IconvProcessor & processor,
202 return std::vector<RetType>();
204 char const * inbuf = reinterpret_cast<char const *>(buf);
205 size_t inbytesleft = buflen * sizeof(InType);
207 size_t const outsize = 32768;
208 static char out[outsize];
211 int bytes = processor.convert(inbuf, inbytesleft, outbuf, outsize);
214 // FIXME Maybe throw an exception and handle that in the caller?
215 return std::vector<RetType>();
217 RetType const * tmp = reinterpret_cast<RetType const *>(out);
218 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
224 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
227 return std::vector<lyx::char_type>();
229 return utf8_to_ucs4(&utf8str[0], utf8str.size());
233 std::vector<lyx::char_type>
234 utf8_to_ucs4(char const * utf8str, size_t ls)
236 static IconvProcessor processor(ucs4_codeset, "UTF-8");
237 return iconv_convert<lyx::char_type>(processor, utf8str, ls);
241 std::vector<char_type>
242 utf16_to_ucs4(unsigned short const * s, size_t ls)
244 static IconvProcessor processor(ucs4_codeset, utf16_codeset);
245 return iconv_convert<char_type>(processor, s, ls);
249 std::vector<unsigned short>
250 ucs4_to_utf16(char_type const * s, size_t ls)
252 static IconvProcessor processor(utf16_codeset, ucs4_codeset);
253 return iconv_convert<unsigned short>(processor, s, ls);
258 ucs4_to_utf8(lyx::char_type c)
260 static IconvProcessor processor("UTF-8", ucs4_codeset);
261 return iconv_convert<char>(processor, &c, 1);
266 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
269 return std::vector<char>();
271 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
276 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
278 static IconvProcessor processor("UTF-8", ucs4_codeset);
279 return iconv_convert<char>(processor, ucs4str, ls);
283 std::vector<lyx::char_type>
284 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding)
286 static std::map<std::string, IconvProcessor> processors;
287 if (processors.find(encoding) == processors.end()) {
288 IconvProcessor processor(ucs4_codeset, encoding.c_str());
289 processors.insert(std::make_pair(encoding, processor));
291 return iconv_convert<char_type>(processors[encoding], s, ls);
296 ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding)
298 static std::map<std::string, IconvProcessor> processors;
299 if (processors.find(encoding) == processors.end()) {
300 IconvProcessor processor(encoding.c_str(), ucs4_codeset);
301 processors.insert(std::make_pair(encoding, processor));
303 return iconv_convert<char>(processors[encoding], ucs4str, ls);