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 static char out[1000];
69 size_t outbytesleft = 1000;
71 size_t res = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
73 if (res == (size_t)(-1)) {
74 lyxerr << "Error returned from iconv" << endl;
77 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
80 lyxerr << "EILSEQ An invalid multibyte sequence"
81 << " has been encountered in the input.\n"
82 << "When converting from " << fromcode
83 << " to " << tocode << ".\n";
84 lyxerr << "Input: " << std::hex;
85 for (size_t i = 0; i < buflen; ++i) {
86 unsigned char const b = buf[i];
87 lyxerr << "0x" << int(b) << " ";
92 lyxerr << "EINVAL An incomplete multibyte sequence"
93 << " has been encountered in the input.\n"
94 << "When converting from " << fromcode
95 << " to " << tocode << ".\n";
96 lyxerr << "Input: " << std::hex;
97 for (size_t i = 0; i < buflen; ++i) {
98 unsigned char const b = buf[i];
99 lyxerr << "0x" << int(b) << " ";
104 lyxerr << "\tSome other error: " << errno << endl;
107 // We got an error so we close down the conversion engine
108 if (iconv_close(*cd) == -1) {
109 lyxerr << "Error returned from iconv_close("
110 << errno << ")" << endl;
115 //lyxerr << std::dec;
116 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
117 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
118 int bytes = 1000 - outbytesleft;
120 RetType const * tmp = reinterpret_cast<RetType const *>(out);
121 return std::vector<RetType>(tmp, tmp + bytes / sizeof(RetType));
127 std::vector<boost::uint32_t> utf8_to_ucs4(std::vector<char> const & utf8str)
129 static iconv_t cd = (iconv_t)(-1);
130 return iconv_convert<boost::uint32_t>(&cd, ucs4_codeset, "UTF-8",
131 &utf8str[0], utf8str.size());
135 std::vector<boost::uint32_t>
136 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
138 static iconv_t cd = (iconv_t)(-1);
139 return iconv_convert<boost::uint32_t>(&cd, ucs4_codeset, ucs2_codeset,
140 &ucs2str[0], ucs2str.size());
144 std::vector<unsigned short>
145 ucs4_to_ucs2(std::vector<boost::uint32_t> const & ucs4str)
147 return ucs4_to_ucs2(&ucs4str[0], ucs4str.size());
151 std::vector<unsigned short>
152 ucs4_to_ucs2(boost::uint32_t const * s, size_t ls)
154 static iconv_t cd = (iconv_t)(-1);
155 return iconv_convert<unsigned short>(&cd, ucs2_codeset, ucs4_codeset,
161 ucs4_to_ucs2(boost::uint32_t c)
163 boost::uint32_t tmp[] = { c, 0 };
164 return ucs4_to_ucs2(tmp, 1)[0];
168 std::vector<char> ucs4_to_utf8(std::vector<boost::uint32_t> const & ucs4str)
170 static iconv_t cd = (iconv_t)(-1);
171 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset,
172 &ucs4str[0], ucs4str.size());
176 std::vector<char> ucs4_to_utf8(boost::uint32_t c)
178 static iconv_t cd = (iconv_t)(-1);
179 return iconv_convert<char>(&cd, "UTF-8", ucs4_codeset, &c, 1);