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.
28 #ifdef WORDS_BIGENDIAN
29 char const * ucs4_codeset = "UCS-4BE";
30 char const * ucs2_codeset = "UCS-2BE";
32 char const * ucs4_codeset = "UCS-4LE";
33 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 size_t const outsize = 1000;
68 static char out[outsize];
70 size_t outbytesleft = outsize;
72 size_t res = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
74 if (res == (size_t)(-1)) {
75 lyxerr << "Error returned from iconv" << endl;
78 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
81 lyxerr << "EILSEQ An invalid multibyte sequence"
82 << " has been encountered in the input.\n"
83 << "When converting from " << fromcode
84 << " to " << tocode << ".\n";
85 lyxerr << "Input: " << std::hex;
86 for (size_t i = 0; i < buflen; ++i) {
87 unsigned char const b = buf[i];
88 lyxerr << "0x" << int(b) << " ";
93 lyxerr << "EINVAL An incomplete multibyte sequence"
94 << " has been encountered in the input.\n"
95 << "When converting from " << fromcode
96 << " to " << tocode << ".\n";
97 lyxerr << "Input: " << std::hex;
98 for (size_t i = 0; i < buflen; ++i) {
99 unsigned char const b = buf[i];
100 lyxerr << "0x" << int(b) << " ";
105 lyxerr << "\tSome other error: " << errno << endl;
108 // We got an error so we close down the conversion engine
109 if (iconv_close(*cd) == -1) {
110 lyxerr << "Error returned from iconv_close("
111 << errno << ")" << endl;
116 //lyxerr << std::dec;
117 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
118 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
119 int bytes = outsize - outbytesleft;
121 RetType const * tmp = reinterpret_cast<RetType const *>(out);
122 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
128 std::vector<lyx::char_type> utf8_to_ucs4(std::vector<char> const & utf8str)
130 return utf8_to_ucs4(&utf8str[0], utf8str.size());
134 std::vector<lyx::char_type>
135 utf8_to_ucs4(char const * utf8str, size_t ls)
137 static iconv_t cd = (iconv_t)(-1);
138 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, "UTF-8",
144 ucs2_to_ucs4(unsigned short c)
146 return ucs2_to_ucs4(&c, 1)[0];
150 std::vector<lyx::char_type>
151 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
153 return ucs2_to_ucs4(&ucs2str[0], ucs2str.size());
157 std::vector<lyx::char_type>
158 ucs2_to_ucs4(unsigned short const * ucs2str, size_t ls)
160 static iconv_t cd = (iconv_t)(-1);
161 return iconv_convert<lyx::char_type>(&cd, ucs4_codeset, ucs2_codeset,
167 ucs4_to_ucs2(lyx::char_type c)
169 return ucs4_to_ucs2(&c, 1)[0];
173 std::vector<unsigned short>
174 ucs4_to_ucs2(std::vector<lyx::char_type> const & ucs4str)
176 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
180 std::vector<unsigned short>
181 ucs4_to_ucs2(lyx::char_type const * s, size_t ls)
183 static iconv_t cd = (iconv_t)(-1);
184 return iconv_convert<unsigned short>(&cd, ucs2_codeset, ucs4_codeset,
190 ucs4_to_utf8(lyx::char_type c)
192 static iconv_t cd = (iconv_t)(-1);
193 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset, &c, 1);
198 ucs4_to_utf8(std::vector<lyx::char_type> const & ucs4str)
200 return ucs4_to_utf8(&ucs4str[0], ucs4str.size());
205 ucs4_to_utf8(lyx::char_type const * ucs4str, size_t ls)
207 static iconv_t cd = (iconv_t)(-1);
208 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset,