Files
YYCCommonplace/src/EncodingHelper.cpp

71 lines
2.1 KiB
C++
Raw Normal View History

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-04-29 15:48:10 +08:00
bool WcharToChar(const wchar_t* src, u8string& 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-04-29 15:48:10 +08:00
bool WcharToUTF8(const wchar_t* src, u8string& dest) {
return WcharToChar(src, dest, CP_UTF8);
2024-04-25 10:38:13 +08:00
}
2024-04-29 15:48:10 +08:00
u8string WcharToChar(const wchar_t* src, UINT codepage) {
u8string ret;
2024-04-26 15:37:28 +08:00
if (!WcharToChar(src, ret, codepage)) ret.clear();
return ret;
}
2024-04-29 15:48:10 +08:00
u8string WcharToUTF8(const wchar_t* src) {
return WcharToChar(src, CP_UTF8);
2024-04-26 15:37:28 +08:00
}
2024-04-25 10:38:13 +08:00
2024-04-29 15:48:10 +08:00
bool CharToWchar(const u8char* 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-04-29 15:48:10 +08:00
bool UTF8ToWchar(const u8char* src, std::wstring& dest) {
return CharToWchar(src, dest, CP_UTF8);
2024-04-25 10:38:13 +08:00
}
2024-04-29 15:48:10 +08:00
std::wstring CharToWchar(const u8char* src, UINT codepage) {
2024-04-26 15:37:28 +08:00
std::wstring ret;
if (!CharToWchar(src, ret, codepage)) ret.clear();
return ret;
}
2024-04-29 15:48:10 +08:00
std::wstring UTF8ToWchar(const u8char* src) {
return CharToWchar(src, CP_UTF8);
2024-04-26 15:37:28 +08:00
}
2024-04-25 10:38:13 +08:00
2024-04-29 15:48:10 +08:00
bool CharToChar(const u8char* src, u8string& 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-04-29 15:48:10 +08:00
u8string CharToChar(const u8char* src, UINT src_codepage, UINT dest_codepage) {
u8string 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