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.
26 #ifdef WORDS_BIGENDIAN
27 char const * ucs4_codeset = "UCS-4BE";
28 char const * ucs2_codeset = "UCS-2BE";
30 char const * ucs4_codeset = "UCS-4LE";
31 char const * ucs2_codeset = "UCS-2LE";
36 template<typename RetType, typename InType>
38 iconv_convert(iconv_t * cd,
40 char const * fromcode,
45 return std::vector<RetType>();
47 if (*cd == (iconv_t)(-1)) {
48 *cd = iconv_open(tocode, fromcode);
49 if (*cd == (iconv_t)(-1)) {
50 lyxerr << "Error returned from iconv_open" << endl;
53 lyxerr << "EINVAL The conversion from " << fromcode
55 << " is not supported by the implementation."
59 lyxerr << "\tSome other error: " << errno << endl;
65 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(reinterpret_cast<char const *>(buf));
66 size_t inbytesleft = buflen * sizeof(InType);
67 // The preamble of the user guide is more than 11.500 characters, so we go for 32kb
68 size_t const outsize = 32768;
69 static char out[outsize];
71 size_t outbytesleft = outsize;
73 size_t res = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
75 if (res == (size_t)(-1)) {
76 lyxerr << "Error returned from iconv" << endl;
79 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
82 lyxerr << "EILSEQ An invalid multibyte sequence"
83 << " has been encountered in the input.\n"
84 << "When converting from " << fromcode
85 << " to " << tocode << ".\n";
86 lyxerr << "Input: " << std::hex;
87 for (size_t i = 0; i < buflen; ++i) {
88 boost::uint32_t const b = buf[i];
89 lyxerr << "0x" << b << " ";
94 lyxerr << "EINVAL An incomplete multibyte sequence"
95 << " has been encountered in the input.\n"
96 << "When converting from " << fromcode
97 << " to " << tocode << ".\n";
98 lyxerr << "Input: " << std::hex;
99 for (size_t i = 0; i < buflen; ++i) {
100 boost::uint32_t const b = buf[i];
101 lyxerr << "0x" << b << " ";
106 lyxerr << "\tSome other error: " << errno << endl;
109 // We got an error so we close down the conversion engine
110 if (iconv_close(*cd) == -1) {
111 lyxerr << "Error returned from iconv_close("
112 << errno << ")" << endl;
117 //lyxerr << std::dec;
118 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
119 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
120 int bytes = outsize - outbytesleft;
122 RetType const * tmp = reinterpret_cast<RetType const *>(out);
123 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
129 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
132 return std::vector<lyx::char_type>();
134 return utf8_to_ucs4(&utf8str[0], utf8str.size());
138 std::vector<lyx::char_type>
139 utf8_to_ucs4(char const * utf8str, size_t ls)
141 static iconv_t cd = (iconv_t)(-1);
142 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, "UTF-8",
148 ucs2_to_ucs4(unsigned short c)
150 return ucs2_to_ucs4(&c, 1)[0];
154 std::vector<lyx::char_type>
155 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
158 return std::vector<lyx::char_type>();
160 return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
164 std::vector<lyx::char_type>
165 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
167 static iconv_t cd = (iconv_t)(-1);
168 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, ucs2_codeset,
174 ucs4_to_ucs2(lyx::char_type c)
176 return ucs4_to_ucs2(&c, 1)[0];
180 std::vector<unsigned short>
181 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
184 return std::vector<unsigned short>();
186 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
190 std::vector<unsigned short>
191 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
193 static iconv_t cd = (iconv_t)(-1);
194 return iconv_convert<unsigned short>(&cd, ucs2_codeset, ucs4_codeset,
200 ucs4_to_utf8(lyx::char_type c)
202 static iconv_t cd = (iconv_t)(-1);
203 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset, &c, 1);
208 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
211 return std::vector<char>();
213 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
218 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
220 static iconv_t cd = (iconv_t)(-1);
221 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset,