2024-04-25 10:38:13 +08:00
|
|
|
#include "EncodingHelper.hpp"
|
2024-04-26 15:37:28 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
2024-04-25 10:38:13 +08:00
|
|
|
|
|
|
|
namespace YYCC::EncodingHelper {
|
2024-04-26 15:37:28 +08:00
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
bool WcharToChar(const wchar_t* src, std::string& dest, UINT codepage) {
|
2024-04-25 10:38:13 +08:00
|
|
|
int count, write_result;
|
|
|
|
|
|
|
|
//converter to CHAR
|
2024-04-29 15:48:10 +08:00
|
|
|
count = WideCharToMultiByte(codepage, 0, reinterpret_cast<LPCWCH>(src), -1, NULL, 0, NULL, NULL);
|
2024-04-25 10:38:13 +08:00
|
|
|
if (count <= 0) return false;
|
|
|
|
|
|
|
|
dest.resize(count - 1);
|
2024-04-29 15:48:10 +08:00
|
|
|
write_result = WideCharToMultiByte(codepage, 0, reinterpret_cast<LPCWCH>(src), -1, reinterpret_cast<LPSTR>(dest.data()), count, NULL, NULL);
|
2024-04-25 10:38:13 +08:00
|
|
|
if (write_result <= 0) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
bool WcharToUTF8(const wchar_t* src, std::string& dest) {
|
2024-04-29 15:48:10 +08:00
|
|
|
return WcharToChar(src, dest, CP_UTF8);
|
2024-04-25 10:38:13 +08:00
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
std::string WcharToChar(const wchar_t* src, UINT codepage) {
|
|
|
|
std::string ret;
|
2024-04-26 15:37:28 +08:00
|
|
|
if (!WcharToChar(src, ret, codepage)) ret.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
std::string WcharToUTF8(const wchar_t* src) {
|
2024-04-29 15:48:10 +08:00
|
|
|
return WcharToChar(src, CP_UTF8);
|
2024-04-26 15:37:28 +08:00
|
|
|
}
|
2024-04-25 10:38:13 +08:00
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
bool CharToWchar(const char* src, std::wstring& dest, UINT codepage) {
|
2024-04-25 10:38:13 +08:00
|
|
|
int wcount, write_result;
|
|
|
|
|
|
|
|
// convert to WCHAR
|
2024-04-29 15:48:10 +08:00
|
|
|
wcount = MultiByteToWideChar(codepage, 0, reinterpret_cast<LPCCH>(src), -1, NULL, 0);
|
2024-04-25 10:38:13 +08:00
|
|
|
if (wcount <= 0) return false;
|
|
|
|
|
|
|
|
dest.resize(wcount - 1);
|
2024-04-29 15:48:10 +08:00
|
|
|
write_result = MultiByteToWideChar(codepage, 0, reinterpret_cast<LPCCH>(src), -1, reinterpret_cast<LPWSTR>(dest.data()), wcount);
|
2024-04-25 10:38:13 +08:00
|
|
|
if (write_result <= 0) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
bool UTF8ToWchar(const char* src, std::wstring& dest) {
|
2024-04-29 15:48:10 +08:00
|
|
|
return CharToWchar(src, dest, CP_UTF8);
|
2024-04-25 10:38:13 +08:00
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
std::wstring CharToWchar(const char* src, UINT codepage) {
|
2024-04-26 15:37:28 +08:00
|
|
|
std::wstring ret;
|
|
|
|
if (!CharToWchar(src, ret, codepage)) ret.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
std::wstring UTF8ToWchar(const char* src) {
|
2024-04-29 15:48:10 +08:00
|
|
|
return CharToWchar(src, CP_UTF8);
|
2024-04-26 15:37:28 +08:00
|
|
|
}
|
2024-04-25 10:38:13 +08:00
|
|
|
|
2024-05-20 21:41:48 +08:00
|
|
|
bool CharToChar(const char* src, std::string& dest, UINT src_codepage, UINT dest_codepage) {
|
2024-04-25 10:38:13 +08:00
|
|
|
std::wstring intermediary;
|
|
|
|
if (!CharToWchar(src, intermediary, src_codepage)) return false;
|
2024-04-29 15:48:10 +08:00
|
|
|
if (!WcharToChar(intermediary.c_str(), dest, dest_codepage)) return false;
|
2024-04-25 10:38:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
2024-05-20 21:41:48 +08:00
|
|
|
std::string CharToChar(const char* src, UINT src_codepage, UINT dest_codepage) {
|
|
|
|
std::string ret;
|
2024-04-26 15:37:28 +08:00
|
|
|
if (!CharToChar(src, ret, src_codepage, dest_codepage)) ret.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
2024-04-25 10:38:13 +08:00
|
|
|
|
|
|
|
}
|
2024-04-26 15:37:28 +08:00
|
|
|
|
|
|
|
#endif
|