7 #include "widestring.h"
9 static iconv_t ucs4_iconv;
10 static bool iconv_initialized = false;
12 widestring::widestring()
16 widestring::widestring(const char *from)
21 widestring::widestring(const std::string &from)
26 widestring::widestring(const std::wstring &from)
31 void widestring::operator= (const char *from)
33 if (!iconv_initialized) {
34 #if __BYTE_ORDER == __LITTLE_ENDIAN
35 ucs4_iconv = iconv_open("ucs-4le", "utf-8");
37 ucs4_iconv = iconv_open("ucs-4be", "utf-8");
40 iconv_initialized = true;
43 unsigned bytes = std::strlen(from);
44 char *from_buf = strdup(from);
45 wchar_t *to_buf = new wchar_t[bytes + 1];
47 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
49 size_t in_left = bytes;
50 size_t out_left = bytes * sizeof(wchar_t);
52 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
53 if (ret == (size_t)(-1)) {
54 throw std::runtime_error("Error in iconv during initialization");
57 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
58 if (ret == (size_t)(-1)) {
60 throw std::runtime_error("Error in iconv during conversion");
63 erase(begin(), end());
64 std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
74 void from_string<widestring>(const char *from, widestring &to)