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.
30 #ifdef WORDS_BIGENDIAN
31 char const * ucs4_codeset = "UCS-4BE";
32 char const * ucs2_codeset = "UCS-2BE";
34 char const * ucs4_codeset = "UCS-4LE";
35 char const * ucs2_codeset = "UCS-2LE";
40 template<typename RetType, typename InType>
42 iconv_convert(iconv_t * cd,
44 char const * fromcode,
49 return std::vector<RetType>();
51 if (*cd == (iconv_t)(-1)) {
52 *cd = iconv_open(tocode, fromcode);
53 if (*cd == (iconv_t)(-1)) {
54 lyxerr << "Error returned from iconv_open" << endl;
57 lyxerr << "EINVAL The conversion from " << fromcode
59 << " is not supported by the implementation."
63 lyxerr << "\tSome other error: " << errno << endl;
69 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(reinterpret_cast<char const *>(buf));
70 size_t inbytesleft = buflen * sizeof(InType);
71 // The preamble of the user guide is more than 11.500 characters, so we go for 32kb
72 size_t const outsize = 32768;
73 static char out[outsize];
75 size_t outbytesleft = outsize;
77 size_t res = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
79 if (res == (size_t)(-1)) {
80 lyxerr << "Error returned from iconv" << endl;
83 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
86 lyxerr << "EILSEQ An invalid multibyte sequence"
87 << " has been encountered in the input.\n"
88 << "When converting from " << fromcode
89 << " to " << tocode << ".\n";
90 lyxerr << "Input: " << std::hex;
91 for (size_t i = 0; i < buflen; ++i) {
92 boost::uint32_t const b = buf[i];
93 lyxerr << "0x" << b << " ";
98 lyxerr << "EINVAL An incomplete multibyte sequence"
99 << " has been encountered in the input.\n"
100 << "When converting from " << fromcode
101 << " to " << tocode << ".\n";
102 lyxerr << "Input: " << std::hex;
103 for (size_t i = 0; i < buflen; ++i) {
104 boost::uint32_t const b = buf[i];
105 lyxerr << "0x" << b << " ";
110 lyxerr << "\tSome other error: " << errno << endl;
113 // We got an error so we close down the conversion engine
114 if (iconv_close(*cd) == -1) {
115 lyxerr << "Error returned from iconv_close("
116 << errno << ")" << endl;
121 //lyxerr << std::dec;
122 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
123 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
124 int bytes = outsize - outbytesleft;
126 RetType const * tmp = reinterpret_cast<RetType const *>(out);
127 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
133 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
136 return std::vector<lyx::char_type>();
138 return utf8_to_ucs4(&utf8str[0], utf8str.size());
142 std::vector<lyx::char_type>
143 utf8_to_ucs4(char const * utf8str, size_t ls)
145 static iconv_t cd = (iconv_t)(-1);
146 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, "UTF-8",
152 ucs2_to_ucs4(unsigned short c)
154 return ucs2_to_ucs4(&c, 1)[0];
158 std::vector<lyx::char_type>
159 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
162 return std::vector<lyx::char_type>();
164 return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
168 std::vector<lyx::char_type>
169 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
171 static iconv_t cd = (iconv_t)(-1);
172 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, ucs2_codeset,
178 ucs4_to_ucs2(lyx::char_type c)
180 return ucs4_to_ucs2(&c, 1)[0];
184 std::vector<unsigned short>
185 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
188 return std::vector<unsigned short>();
190 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
194 std::vector<unsigned short>
195 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
197 static iconv_t cd = (iconv_t)(-1);
198 return iconv_convert<unsigned short>(&cd, ucs2_codeset, ucs4_codeset,
204 ucs4_to_utf8(lyx::char_type c)
206 static iconv_t cd = (iconv_t)(-1);
207 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset, &c, 1);
212 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
215 return std::vector<char>();
217 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
222 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
224 static iconv_t cd = (iconv_t)(-1);
225 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset,
230 std::vector<lyx::char_type>
231 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding)
233 static std::map<std::string, iconv_t> cd;
234 if (cd.find(encoding) == cd.end())
235 cd[encoding] = (iconv_t)(-1);
236 return iconv_convert<char_type>(&cd[encoding], ucs4_codeset,
237 encoding.c_str(), s, ls);
242 ucs4_to_eightbit(lyx::char_type const * ucs4str, size_t ls, std::string const & encoding)
244 static std::map<std::string, iconv_t> cd;
245 if (cd.find(encoding) == cd.end())
246 cd[encoding] = (iconv_t)(-1);
247 return iconv_convert<char>(&cd[encoding], encoding.c_str(),
248 ucs4_codeset, ucs4str, ls);