include utils.h

This commit is contained in:
HanzZ 2012-09-05 18:06:33 +02:00
parent 51481c8c44
commit 31fb1af807
3 changed files with 36 additions and 3 deletions

View file

@ -1,7 +1,5 @@
#include "purple_defs.h"
#include "transport/util.h"
using namespace Transport::Util;
#include "utils.h"
#if PURPLE_RUNTIME

View file

@ -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<int>(str.size()), NULL, 0);
if (!numRequiredBytes)
return L"";
// Allocate memory for the UTF-16 string
std::vector<wchar_t> utf16Str(numRequiredBytes);
int numConverted = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast<int>(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

View file

@ -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