diff --git a/src/util.cpp b/src/util.cpp index 315726cb97697a9e632b68b7cc8af184568cb3fb..7589f21314dd71dfe2e1c3918bd5f49a5da95782 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -25,6 +25,7 @@ #include #include #include +#include using namespace boost::filesystem; @@ -137,6 +138,38 @@ int getRandomPort(const std::string &s) { return 30000 + rand() % 10000; } +#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 + + } }