]> git.sesse.net Git - ccbs/blobdiff - bigscreen/widestring.cpp
Move widestring into its own file.
[ccbs] / bigscreen / widestring.cpp
diff --git a/bigscreen/widestring.cpp b/bigscreen/widestring.cpp
new file mode 100644 (file)
index 0000000..c31f4fc
--- /dev/null
@@ -0,0 +1,54 @@
+#include <iconv.h>
+#include <endian.h>
+#include <exception>
+#include <pqxx/util>
+#include "widestring.h"
+
+static iconv_t ucs4_iconv;
+static bool iconv_initialized = false;
+
+void widestring::operator= (const char *from)
+{
+       if (!iconv_initialized) {
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+               ucs4_iconv = iconv_open("ucs-4le", "utf-8");
+#else
+               ucs4_iconv = iconv_open("ucs-4be", "utf-8");
+#endif
+
+               iconv_initialized = true;
+       }
+       
+       unsigned bytes = std::strlen(from);
+       char *from_buf = strdup(from);
+       wchar_t *to_buf = new wchar_t[bytes + 1];
+
+       char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
+
+       size_t in_left = bytes;
+       size_t out_left = bytes * sizeof(wchar_t);
+
+       size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
+       if (ret == (size_t)(-1)) {
+               throw std::runtime_error("Error in iconv during initialization");
+       }
+
+       ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
+       if (ret == (size_t)(-1)) {
+               perror("iconv");
+               throw std::runtime_error("Error in iconv during conversion");
+       }
+
+       erase(begin(), end());
+       std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
+
+       free(from_buf);
+       delete[] to_buf;
+}
+
+template<>
+void pqxx::from_string<widestring>(const char *from, widestring &to)
+{
+       to = from;
+}
+