refactor: finish migration for libcmo self
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "MgrImpls/CKBaseManager.hpp"
|
||||
#include "MgrImpls/CKObjectManager.hpp"
|
||||
#include "MgrImpls/CKPathManager.hpp"
|
||||
#include <yycc/string/op.hpp>
|
||||
#include <cstdarg>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ namespace LibCmo::CK2 {
|
||||
bool IsValidEncoding();
|
||||
|
||||
protected:
|
||||
XContainer::XArray<EncodingHelper::EncodingToken> m_NameEncoding;
|
||||
XContainer::XArray<EncodingPair> m_NameEncoding;
|
||||
|
||||
// ========== Print utilities ==========
|
||||
public:
|
||||
|
||||
97
LibCmo/LibCmo/VTEncoding.cpp
Normal file
97
LibCmo/LibCmo/VTEncoding.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "VTEncoding.hpp"
|
||||
#include <yycc/carton/pycodec.hpp>
|
||||
|
||||
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
|
||||
29
LibCmo/LibCmo/VTEncoding.hpp
Normal file
29
LibCmo/LibCmo/VTEncoding.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <yycc/macro/class_copy_move.hpp>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user