From 31fb1af807d319f0dba68d2695cc1f38d3aa6fb5 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Wed, 5 Sep 2012 18:06:33 +0200 Subject: [PATCH] include utils.h --- backends/libpurple/purple_defs.cpp | 4 +--- backends/libpurple/utils.cpp | 31 ++++++++++++++++++++++++++++++ backends/libpurple/utils.h | 4 ++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/backends/libpurple/purple_defs.cpp b/backends/libpurple/purple_defs.cpp index d21e3217..bca0c2ff 100644 --- a/backends/libpurple/purple_defs.cpp +++ b/backends/libpurple/purple_defs.cpp @@ -1,7 +1,5 @@ #include "purple_defs.h" -#include "transport/util.h" - -using namespace Transport::Util; +#include "utils.h" #if PURPLE_RUNTIME diff --git a/backends/libpurple/utils.cpp b/backends/libpurple/utils.cpp index 4cb02509..5269e4eb 100644 --- a/backends/libpurple/utils.cpp +++ b/backends/libpurple/utils.cpp @@ -156,3 +156,34 @@ int create_socket(const char *host, int portno) { // fcntl(main_socket, F_SETFL, flags); return main_socket; } + +#ifdef _WIN32 +std::wstring utf8ToUtf16(const std::string& str) +{ + try + { + if (str.empty()) + return L""; + + // First request the size of the required UTF-16 buffer + int numRequiredBytes = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast(str.size()), NULL, 0); + if (!numRequiredBytes) + return L""; + + // Allocate memory for the UTF-16 string + std::vector utf16Str(numRequiredBytes); + + int numConverted = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast(str.size()), &utf16Str[0], numRequiredBytes); + if (!numConverted) + return L""; + + std::wstring wstr(&utf16Str[0], numConverted); + return wstr; + } + catch (...) + { + // I don't believe libtransport is exception-safe so we'll just return an empty string instead + return L""; + } +} +#endif // _WIN32 diff --git a/backends/libpurple/utils.h b/backends/libpurple/utils.h index 956b86f6..8324f46a 100644 --- a/backends/libpurple/utils.h +++ b/backends/libpurple/utils.h @@ -31,3 +31,7 @@ int create_socket(const char *host, int portno); GHashTable *spectrum_ui_get_info(void); void execute_purple_plugin_action(PurpleConnection *gc, const std::string &name); + +#ifdef _WIN32 + std::wstring utf8ToUtf16(const std::string& str); +#endif