5 #include "widestring.h"
7 static iconv_t ucs4_iconv;
8 static bool iconv_initialized = false;
10 widestring::widestring()
14 widestring::widestring(const char *from)
19 widestring::widestring(const std::string &from)
24 widestring::widestring(const std::wstring &from)
29 void widestring::operator= (const char *from)
31 if (!iconv_initialized) {
32 #if __BYTE_ORDER == __LITTLE_ENDIAN
33 ucs4_iconv = iconv_open("ucs-4le", "utf-8");
35 ucs4_iconv = iconv_open("ucs-4be", "utf-8");
38 iconv_initialized = true;
41 unsigned bytes = std::strlen(from);
42 char *from_buf = strdup(from);
43 wchar_t *to_buf = new wchar_t[bytes + 1];
45 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
47 size_t in_left = bytes;
48 size_t out_left = bytes * sizeof(wchar_t);
50 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
51 if (ret == (size_t)(-1)) {
52 throw std::runtime_error("Error in iconv during initialization");
55 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
56 if (ret == (size_t)(-1)) {
58 throw std::runtime_error("Error in iconv during conversion");
61 erase(begin(), end());
62 std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
69 void pqxx::from_string<widestring>(const char *from, widestring &to)