20namespace openvpn::wstring {
28inline std::wstring from_utf8(
const std::string &str)
33 const int str_size =
static_cast<int>(str.size());
34 const auto reqSize = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size,
nullptr, 0);
36 throw std::runtime_error(
"MultiByteToWideChar(1) failed with code: [" + std::to_string(::GetLastError()) +
"]");
37 wStr.resize(reqSize, L
'\0');
38 if (::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, wStr.data(), reqSize) == 0)
39 throw std::runtime_error(
"MultiByteToWideChar(2) failed with code: [" + std::to_string(::GetLastError()) +
"]");
49inline std::string to_utf8(
const std::wstring &wstr)
54 const int wstr_size =
static_cast<int>(wstr.size());
55 const auto reqSize = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size,
nullptr, 0,
nullptr,
nullptr);
57 throw std::runtime_error(
"WideCharToMultiByte(1) failed with code: [" + std::to_string(::GetLastError()) +
"]");
58 str.resize(reqSize,
'\0');
59 if (::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, str.data(), reqSize,
nullptr,
nullptr) == 0)
60 throw std::runtime_error(
"WideCharToMultiByte(2) failed with code: [" + std::to_string(::GetLastError()) +
"]");
70inline std::unique_ptr<wchar_t[]> to_wchar_t(
const std::wstring &wstr)
72 const size_t len = wstr.length();
73 std::unique_ptr<wchar_t[]>
ret(
new wchar_t[len + 1]);
75 for (i = 0; i < len; ++i)
92inline std::wstring pack_string_vector(
const std::vector<std::string> &strvec)
96 return std::wstring(2, L
'\0');
99 for (
auto &s : strvec)