From e53195fa1dae07e97ef4a56eb42106cec188e581 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 28 Jan 2026 20:05:57 +0800 Subject: [PATCH] refactor: finish migration for libcmo self --- LibCmo/CMakeLists.txt | 2 + LibCmo/LibCmo/CK2/CKContext.cpp | 27 ++++----- LibCmo/LibCmo/CK2/CKContext.hpp | 2 +- LibCmo/LibCmo/VTEncoding.cpp | 97 +++++++++++++++++++++++++++++++++ LibCmo/LibCmo/VTEncoding.hpp | 29 ++++++++++ LibCmo/LibCmo/VTInternal.hpp | 2 + 6 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 LibCmo/LibCmo/VTEncoding.cpp create mode 100644 LibCmo/LibCmo/VTEncoding.hpp diff --git a/LibCmo/CMakeLists.txt b/LibCmo/CMakeLists.txt index 39d0328..83d7b3a 100644 --- a/LibCmo/CMakeLists.txt +++ b/LibCmo/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(LibCmo PRIVATE # NeMo Shared LibCmo/VTUtils.cpp + LibCmo/VTEncoding.cpp LibCmo/VTImage.cpp # CK2 LibCmo/CK2/CKBitmapData.cpp @@ -57,6 +58,7 @@ FILES VTAll.hpp LibCmo/VTVersion.hpp LibCmo/VTInternal.hpp + LibCmo/VTEncoding.hpp LibCmo/VTUtils.hpp # CK2 LibCmo/CK2/CKDefines.hpp diff --git a/LibCmo/LibCmo/CK2/CKContext.cpp b/LibCmo/LibCmo/CK2/CKContext.cpp index 5594eca..3ba433f 100644 --- a/LibCmo/LibCmo/CK2/CKContext.cpp +++ b/LibCmo/LibCmo/CK2/CKContext.cpp @@ -3,6 +3,7 @@ #include "MgrImpls/CKBaseManager.hpp" #include "MgrImpls/CKObjectManager.hpp" #include "MgrImpls/CKPathManager.hpp" +#include #include namespace LibCmo::CK2 { @@ -264,7 +265,7 @@ namespace LibCmo::CK2 { va_list argptr; va_start(argptr, fmt); - XContainer::XString result(YYCC::StringHelper::VPrintf(fmt, argptr)); + XContainer::XString result(yycc::string::op::vprintf(fmt, argptr)); va_end(argptr); // use c_str(), not XContainer::NSXString::ToCKSTRING because we want make sure this parameter is not nullptr. @@ -282,10 +283,10 @@ namespace LibCmo::CK2 { bool CKContext::GetUTF8String(const std::string& native_name, XContainer::XString& u8_name) { bool conv_success = false, has_valid_token = false; - for (const auto& token : this->m_NameEncoding) { - if (token == EncodingHelper::INVALID_ENCODING_TOKEN) continue; + for (auto& enc_pair : this->m_NameEncoding) { + if (!enc_pair.IsValid()) continue; has_valid_token = true; - conv_success = EncodingHelper::ToUTF8(native_name, u8_name, token); + conv_success = enc_pair.ToUTF8(native_name, u8_name); if (conv_success) break; } // fallback if failed. @@ -303,10 +304,10 @@ namespace LibCmo::CK2 { bool CKContext::GetOrdinaryString(const XContainer::XString& u8_name, std::string& native_name) { bool conv_success = false, has_valid_token = false; - for (const auto& token : this->m_NameEncoding) { - if (token == EncodingHelper::INVALID_ENCODING_TOKEN) continue; + for (auto& enc_pair : this->m_NameEncoding) { + if (!enc_pair.IsValid()) continue; has_valid_token = true; - conv_success = EncodingHelper::ToOrdinary(u8_name, native_name, token); + conv_success = enc_pair.ToOrdinary(u8_name, native_name); if (conv_success) break; } // fallback if failed. @@ -326,22 +327,18 @@ namespace LibCmo::CK2 { // free all current series this->ClearEncoding(); // add new encoding - for (const auto& encoding_str : encoding_seq) { - this->m_NameEncoding.emplace_back(LibCmo::EncodingHelper::CreateEncodingToken(encoding_str)); + for (auto& encoding_str : encoding_seq) { + this->m_NameEncoding.emplace_back(EncodingPair(encoding_str)); } } void CKContext::ClearEncoding() { - for (const auto& token : this->m_NameEncoding) { - if (token == EncodingHelper::INVALID_ENCODING_TOKEN) continue; - LibCmo::EncodingHelper::DestroyEncodingToken(token); - } this->m_NameEncoding.clear(); } bool CKContext::IsValidEncoding() { - for (const auto& token : this->m_NameEncoding) { - if (token != EncodingHelper::INVALID_ENCODING_TOKEN) return true; + for (const auto& enc_pair : this->m_NameEncoding) { + if (enc_pair.IsValid()) return true; } return false; } diff --git a/LibCmo/LibCmo/CK2/CKContext.hpp b/LibCmo/LibCmo/CK2/CKContext.hpp index e48ad9f..4ab5c46 100644 --- a/LibCmo/LibCmo/CK2/CKContext.hpp +++ b/LibCmo/LibCmo/CK2/CKContext.hpp @@ -147,7 +147,7 @@ namespace LibCmo::CK2 { bool IsValidEncoding(); protected: - XContainer::XArray m_NameEncoding; + XContainer::XArray m_NameEncoding; // ========== Print utilities ========== public: diff --git a/LibCmo/LibCmo/VTEncoding.cpp b/LibCmo/LibCmo/VTEncoding.cpp new file mode 100644 index 0000000..7b93959 --- /dev/null +++ b/LibCmo/LibCmo/VTEncoding.cpp @@ -0,0 +1,97 @@ +#include "VTEncoding.hpp" +#include + +namespace LibCmo { + +#pragma region PrivEncodingPair + + class PrivEncodingPair { + public: + PrivEncodingPair(const std::u8string_view &enc_name) : to_utf8(enc_name), to_ordinary(enc_name) {} + ~PrivEncodingPair() {} + YYCC_DELETE_COPY(PrivEncodingPair) + YYCC_DEFAULT_MOVE(PrivEncodingPair) + + public: + bool ToUTF8(const std::string_view &src, std::u8string &dst) { + auto rv = to_utf8.to_utf8(src); + if (rv.has_value()) { + dst = std::move(rv.value()); + return true; + } else { + return false; + } + } + bool ToOrdinary(const std::u8string_view &src, std::string &dst) { + auto rv = to_ordinary.to_char(src); + if (rv.has_value()) { + dst = std::move(rv.value()); + return true; + } else { + return false; + } + } + + private: + yycc::carton::pycodec::CharToUtf8 to_utf8; + yycc::carton::pycodec::Utf8ToChar to_ordinary; + }; + +#pragma endregion + +#pragma region EncodingPair + +#pragma endregion + + EncodingPair::EncodingPair(const std::u8string_view &enc_name) : inner(nullptr), is_valid(false), encoding_name(enc_name) { + // Create private pair + this->inner = new PrivEncodingPair(this->encoding_name); + // Use empty string probe to check whether it works + // because YYCC do not provide these interface. + std::string ordinary_cache; + std::u8string utf8_cache; + if (!this->inner->ToOrdinary(u8"", ordinary_cache)) return; + if (!this->inner->ToUTF8("", utf8_cache)) return; + // Okey + this->is_valid = true; + } + + EncodingPair::~EncodingPair() { + if (this->inner != nullptr) delete inner; + } + + YYCC_IMPL_MOVE_CTOR(EncodingPair, rhs) : inner(rhs.inner), is_valid(rhs.is_valid), encoding_name(rhs.encoding_name) { + rhs.inner = nullptr; + rhs.is_valid = false; + } + + YYCC_IMPL_MOVE_OPER(EncodingPair, rhs) { + if (this->inner != nullptr) delete inner; + + this->inner = rhs.inner; + this->is_valid = rhs.is_valid; + this->encoding_name = rhs.encoding_name; + + rhs.inner = nullptr; + rhs.is_valid = false; + + return *this; + } + + bool EncodingPair::ToUTF8(const std::string_view &src, std::u8string &dst) { + return inner->ToUTF8(src, dst); + } + + bool EncodingPair::ToOrdinary(const std::u8string_view &src, std::string &dst) { + return inner->ToOrdinary(src, dst); + } + + bool EncodingPair::IsValid() const { + return this->is_valid; + } + + std::u8string_view EncodingPair::GetEncodingName() const { + return this->encoding_name; + } + +} // namespace LibCmo diff --git a/LibCmo/LibCmo/VTEncoding.hpp b/LibCmo/LibCmo/VTEncoding.hpp new file mode 100644 index 0000000..65d6811 --- /dev/null +++ b/LibCmo/LibCmo/VTEncoding.hpp @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include + +namespace LibCmo { + + class PrivEncodingPair; + + class EncodingPair { + public: + EncodingPair(const std::u8string_view& enc_name); + ~EncodingPair(); + YYCC_DELETE_COPY(EncodingPair) + YYCC_DECL_MOVE(EncodingPair) + + public: + bool ToUTF8(const std::string_view& src, std::u8string& dst); + bool ToOrdinary(const std::u8string_view& src, std::string& dst); + bool IsValid() const; + std::u8string_view GetEncodingName() const; + + private: + PrivEncodingPair* inner; + bool is_valid; + std::u8string encoding_name; + }; + +} diff --git a/LibCmo/LibCmo/VTInternal.hpp b/LibCmo/LibCmo/VTInternal.hpp index b77ddb5..07f045e 100644 --- a/LibCmo/LibCmo/VTInternal.hpp +++ b/LibCmo/LibCmo/VTInternal.hpp @@ -27,6 +27,8 @@ // - General LibCmo specific custom exception. // - Enum Helper for convenient C++ enum class type logic operations. #include "VTUtils.hpp" +// Encoding convertion header. +#include "VTEncoding.hpp" // Include CK2 types first. // Because it also include some types or structs used by other module.