diff --git a/backends/libpurple/utils.cpp b/backends/libpurple/utils.cpp index 4cb0250939a4fef7167a8eea18f5552e33cd8065..7b935e35fb642733911cbb8d2cbc0ec563e94a6b 100644 --- a/backends/libpurple/utils.cpp +++ b/backends/libpurple/utils.cpp @@ -28,6 +28,7 @@ #include "purple.h" #include #include +#include #include "errno.h" @@ -54,6 +55,10 @@ #include "purple_defs.h" +#include + +using std::vector; + static GHashTable *ui_info = NULL; void execute_purple_plugin_action(PurpleConnection *gc, const std::string &name) { @@ -156,3 +161,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