fix: fix Unvirt build issue, but it doesn't work

This commit is contained in:
2024-08-26 21:28:13 +08:00
parent 34015d2d1c
commit 3735a202f3
8 changed files with 343 additions and 238 deletions

View File

@ -153,7 +153,7 @@ namespace LibCmo::CK2 {
* @details It accept a CKSTRING representing the string need to be printed.
* The passed CKSTRING is guaranteen that it can not be nullptr.
*/
using OutputCallback = void(*)(CKSTRING);
using OutputCallback = std::function<void(CKSTRING)>;
/**
* @brief Output plain message.
* @param[in] str Plain message. nullptr is allowed but not suggested.

View File

@ -403,6 +403,9 @@ namespace LibCmo::EncodingHelper {
#if YYCC_OS == YYCC_OS_WINDOWS
struct WindowsEncodingToken {
WindowsEncodingToken(const std::u8string_view& universal_code, UINT cp) :
m_Name(universal_code), m_CodePage(cp) {}
std::u8string m_Name;
UINT m_CodePage;
};
@ -412,7 +415,8 @@ namespace LibCmo::EncodingHelper {
static const iconv_t c_InvalidIconvType = reinterpret_cast<iconv_t>(-1);
struct IconvEncodingToken {
IconvEncodingToken(const std::string_view& iconv_code) :
IconvEncodingToken(const std::u8string_view& universal_code, const std::string_view& iconv_code) :
m_Name(universal_code),
m_FromUTF8(c_InvalidIconvType), m_ToUTF8(c_InvalidIconvType) {
// if iconv code is empty, do nothing
if (iconv_code.empty()) return;
@ -426,6 +430,8 @@ namespace LibCmo::EncodingHelper {
if (this->m_ToUTF8 != c_InvalidIconvType)
iconv_close(token_cast->m_ToUTF8);
}
std::u8string m_Name;
iconv_t m_FromUTF8;
iconv_t m_ToUTF8;
};
@ -508,7 +514,7 @@ namespace LibCmo::EncodingHelper {
if (!YYCC::WinFctHelper::IsValidCodePage(cp))
return INVALID_ENCODING_TOKEN;
// create token and return
WindowsEncodingToken* token = new WindowsEncodingToken { cp };
WindowsEncodingToken* token = new WindowsEncodingToken(enc_name, cp);
return token;
#else
// get iconv code first
@ -516,7 +522,7 @@ namespace LibCmo::EncodingHelper {
if (!GetIconvCode(enc_name, code))
return INVALID_ENCODING_TOKEN;
// create token and set default value
IconvEncodingToken* token = new IconvEncodingToken(code);
IconvEncodingToken* token = new IconvEncodingToken(enc_name, code);
// check whether token has been initialized correctly
if (token->m_FromUTF8 == c_InvalidIconvType || token->m_ToUTF8 == c_InvalidIconvType) {
// failed. free resource and return
@ -541,6 +547,34 @@ namespace LibCmo::EncodingHelper {
#endif
}
std::u8string GetEncodingTokenAssociatedName(EncodingToken token) {
// prepare return value
std::u8string ret;
// if token is invalid, return directly
if (token == INVALID_ENCODING_TOKEN) return ret;
// get associated name
#if YYCC_OS == YYCC_OS_WINDOWS
WindowsEncodingToken* token_cast = static_cast<WindowsEncodingToken*>(token);
ret = token_cast->m_Name;
#else
IconvEncodingToken* token_cast = static_cast<IconvEncodingToken*>(token);
ret = token_cast->m_Name;
#endif
// return value
return ret;
}
bool IsValidEncodingName(const std::u8string_view& enc_name) {
#if YYCC_OS == YYCC_OS_WINDOWS
UINT cp = CP_ACP;
return GetWindowsCodePage(enc_name, cp);
#else
std::string code;
return GetIconvCode(enc_name, code);
#endif
}
#pragma endregion
#pragma region Exposed Convertion Functions

View File

@ -55,6 +55,23 @@ namespace LibCmo::EncodingHelper {
* If token is #INVALID_ENCODING_TOKEN, this function does nothing.
*/
void DestroyEncodingToken(EncodingToken token);
/**
* @brief Get associated universal encoding name of given encoding token.
* @param[in] token The encoding token for getting name.
* @return Encoding token associated name (the universal encoding name used to create this token).
* Blank if given token is invalid or fail to get.
*/
std::u8string GetEncodingTokenAssociatedName(EncodingToken token);
/**
* @brief Check whether given universal encoding name can be used to produce token.
* @param[in] enc_name Universal encoding name.
* @return True if it is, otherwise false.
* @remarks
* Please note this function only check whether given encoding name is acceptable by token creator.
* Hoewver it doesn't mean that the token must be created if this function return true.
* Because there are some runtime issues which can cause that fail to create encoding token.
*/
bool IsValidEncodingName(const std::u8string_view& enc_name);
/**
* @brief Convert native string to UTF8 string by given encoding.