anotherim-desktop/plugins/windows-notification/api/src/converter.cpp

29 lines
923 B
C++
Raw Normal View History

2021-03-26 11:22:55 +00:00
#include <stringapiset.h>
#include "converter.hpp"
// Convert a wide Unicode string to an UTF8 string
2021-02-22 09:18:53 +00:00
char* wsview_to_char(const std::wstring_view wstr)
2021-03-26 11:22:55 +00:00
{
if(wstr.empty())
{
return nullptr;
}
int final_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
2021-02-27 15:52:03 +00:00
char* strTo = new char[final_size + 1]{ 0 };
2021-03-26 11:22:55 +00:00
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), strTo, final_size, nullptr, nullptr);
return strTo;
}
// Convert an UTF8 string to a wide Unicode String
2021-02-22 09:18:53 +00:00
std::wstring sview_to_wstr(const std::string_view str)
2021-03-26 11:22:55 +00:00
{
if(str.empty())
{
return std::wstring();
}
int final_size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
std::wstring wstrTo(final_size, 0);
MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), wstrTo.data(), final_size);
return wstrTo;
}