QBinaryTranslate/TranslateUI/extract_basic.cpp

805 lines
17 KiB
C++
Raw Normal View History

2025-08-01 01:47:41 +00:00
#include "extract_basic.h"
#include <QVariant>
using namespace extract;
2025-08-10 09:02:18 +00:00
AbstractExtractor::AbstractExtractor(const QString& typeAlias, DataType data)
2025-08-02 02:04:30 +00:00
{
2025-08-10 09:02:18 +00:00
_abs_data.typename_store = typeAlias;
2025-08-02 02:04:30 +00:00
_abs_data.type_store = data;
2025-08-01 01:47:41 +00:00
}
bool AbstractExtractor::setCountWithin(int bytes)
{
2025-08-02 02:04:30 +00:00
this->_abs_data.byte_count = bytes;
2025-08-01 01:47:41 +00:00
return true;
}
2025-08-10 09:02:18 +00:00
QString AbstractExtractor::unitType() const
2025-08-01 01:47:41 +00:00
{
2025-08-10 09:02:18 +00:00
return _abs_data.typename_store;
2025-08-01 01:47:41 +00:00
}
DataType AbstractExtractor::outType() const
{
2025-08-02 02:04:30 +00:00
return _abs_data.type_store;
2025-08-01 01:47:41 +00:00
}
int AbstractExtractor::countWithin() const
{
2025-08-02 02:04:30 +00:00
return this->_abs_data.byte_count;
2025-08-01 01:47:41 +00:00
}
void AbstractExtractor::loadFrom(
std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-01 01:47:41 +00:00
{
2025-08-10 09:02:18 +00:00
STRING_PEAK(_abs_data.name_alias, obj);
2025-08-02 07:02:35 +00:00
INT32_PEAK(_abs_data.byte_count, obj);
2025-08-01 01:47:41 +00:00
}
void AbstractExtractor::saveTo(QJsonObject& obj) const
{
2025-08-10 09:02:18 +00:00
STRING_SAVE(_abs_data.name_alias, obj);
2025-08-02 07:02:35 +00:00
INT32_SAVE(_abs_data.byte_count, obj);
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AbstractExtractor::registSubField(std::shared_ptr<ScopeFieldsSetter> inst){}
QString AbstractExtractor::aliasName() const
2025-08-03 00:04:12 +00:00
{
2025-08-10 09:02:18 +00:00
return _abs_data.name_alias;
}
2025-08-03 00:04:12 +00:00
2025-08-10 09:02:18 +00:00
void AbstractExtractor::setAlias(const QString& name)
{
_abs_data.name_alias = name;
2025-08-03 00:04:12 +00:00
}
2025-08-05 14:00:41 +00:00
AsHex::AsHex(): AbstractExtractor(topic(), DataType::TextString) {
2025-08-02 02:04:30 +00:00
setCountWithin(1);
2025-08-01 01:47:41 +00:00
}
2025-08-03 00:04:12 +00:00
#include "TranslateBasic.h"
2025-08-10 09:02:18 +00:00
void AsHex::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
QString result;
for (auto char_v : bytes) {
result += QString("%1").arg(char_v, 2, 16, QChar('0'));
}
2025-08-03 00:04:12 +00:00
out->append(result);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsHex::newDefault() const
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsHex>();
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
QString AsHex::topic()
{
return NAME(AsHex);
}
AsBitCombine::AsBitCombine()
: AbstractExtractor(topic(), DataType::TextString)
2025-08-01 01:47:41 +00:00
{
}
2025-08-05 14:00:41 +00:00
bool AsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
2025-08-01 01:47:41 +00:00
{
if (bit_index >= 0 && bit_index <= this->countWithin() * 8) {
_combine._switch_options[bit_index] = keyword;
2025-08-01 01:47:41 +00:00
return true;
}
return false;
}
2025-08-05 14:00:41 +00:00
QHash<int, QString> AsBitCombine::switchOptions() const
2025-08-01 01:47:41 +00:00
{
return _combine._switch_options;
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
void AsBitCombine::clearOptions()
2025-08-01 01:47:41 +00:00
{
_combine._switch_options.clear();
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AsBitCombine::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
auto keys = _combine._switch_options.keys();
2025-08-01 01:47:41 +00:00
std::sort(keys.begin(), keys.end());
QString result;
for (auto idx : keys) {
auto byte_idx = idx / 8;
auto bit_idx = idx % 8;
auto char_v = bytes.at(byte_idx);
if (char_v & (0x1u << bit_idx))
result += QString("%1<%2>;").arg(_combine._switch_options[idx], "Y");
2025-08-01 01:47:41 +00:00
else
result += QString("%1<%2>;").arg(_combine._switch_options[idx], "N");
2025-08-01 01:47:41 +00:00
}
2025-08-03 00:04:12 +00:00
out->append(result);
2025-08-01 01:47:41 +00:00
}
2025-08-02 07:02:35 +00:00
#include <QJsonArray>
2025-08-05 14:00:41 +00:00
void AsBitCombine::loadFrom(
std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-01 01:47:41 +00:00
{
AbstractExtractor::loadFrom(core, obj);
2025-08-01 01:47:41 +00:00
_combine._switch_options.clear();
2025-08-02 07:02:35 +00:00
QJsonArray arr;
ARRAY_PEAK(arr, obj);
for (auto index = 0; index < arr.size(); ++index) {
auto switch_obj = arr.at(index);
auto key = switch_obj["index"].toInt();
auto content = switch_obj["content"].toString();
_combine._switch_options[key] = content;
2025-08-01 01:47:41 +00:00
}
}
2025-08-05 14:00:41 +00:00
void AsBitCombine::saveTo(QJsonObject& obj) const
2025-08-01 01:47:41 +00:00
{
AbstractExtractor::saveTo(obj);
2025-08-02 07:02:35 +00:00
QJsonArray arr;
for (auto key : _combine._switch_options.keys()) {
2025-08-02 07:02:35 +00:00
QJsonObject switch_obj;
switch_obj["index"] = key;
switch_obj["content"] = _combine._switch_options[key];
arr.append(switch_obj);
2025-08-01 01:47:41 +00:00
}
2025-08-02 07:02:35 +00:00
ARRAY_SAVE(arr, obj);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsBitCombine::newDefault() const
{
return std::make_shared<AsBitCombine>();
}
QString AsBitCombine::topic()
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return NAME(AsBitCombine);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsInteger::newDefault() const
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsInteger>();
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
AsInteger::AsInteger() :AbstractExtractor(topic(), DataType::Integer) {
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
bool AsInteger::setCountWithin(int bytes)
2025-08-01 01:47:41 +00:00
{
bytes = std::min(8, std::max(bytes, 0));
return AbstractExtractor::setCountWithin(bytes);
}
2025-08-10 09:02:18 +00:00
void AsInteger::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
auto last = bytes[bytes.size() - 1];
auto mark = last == 0 ? 0 : last / std::abs(last);
auto xbuffer = bytes;
if (mark >= 0) {
xbuffer.append(8 - bytes.size(), 0);
}
else {
2025-08-02 02:04:30 +00:00
xbuffer.append(8 - bytes.size(), static_cast<char>(0xff));
2025-08-01 01:47:41 +00:00
}
unsigned long long value = 0;
for (auto vidx = xbuffer.size() - 1; vidx >= 0; vidx--) {
auto vchar = xbuffer.at(vidx);
value = value << 8;
value += (uchar)vchar;
}
2025-08-03 00:04:12 +00:00
out->append(*((long long*)(&value)));
}
2025-08-01 01:47:41 +00:00
2025-08-05 14:00:41 +00:00
QString AsInteger::topic()
{
return NAME(AsInteger);
}
2025-08-01 01:47:41 +00:00
#include <QTextCodec>
2025-08-05 14:00:41 +00:00
AsString::AsString()
:AbstractExtractor(topic(), DataType::TextString) {
2025-08-02 02:04:30 +00:00
_strings._conv_with = QTextCodec::codecForName("GBK");
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
void AsString::setStrCodec(QTextCodec* ins)
2025-08-01 01:47:41 +00:00
{
2025-08-02 02:04:30 +00:00
this->_strings._conv_with = ins;
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
QString AsString::codecName() const
2025-08-01 01:47:41 +00:00
{
2025-08-02 02:04:30 +00:00
return this->_strings._conv_with->name();
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AsString::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
2025-08-03 00:04:12 +00:00
auto v = _strings._conv_with->toUnicode(bytes);
out->append(v);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
void AsString::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-01 01:47:41 +00:00
{
AbstractExtractor::loadFrom(core, obj);
2025-08-01 01:47:41 +00:00
QString codec_name;
2025-08-02 07:02:35 +00:00
STRING_PEAK(codec_name, obj);
2025-08-02 02:04:30 +00:00
this->_strings._conv_with = QTextCodec::codecForName(codec_name.toLatin1());
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
void AsString::saveTo(QJsonObject& obj) const
2025-08-01 01:47:41 +00:00
{
AbstractExtractor::saveTo(obj);
auto codec_name = this->codecName();
2025-08-02 07:02:35 +00:00
STRING_SAVE(codec_name, obj);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsString::newDefault() const
{
return std::make_shared<AsString>();
}
QString AsString::topic()
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return NAME(AsString);
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AsFloat::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
2025-08-03 00:04:12 +00:00
out->append(*((float*)bytes.data()));
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsFloat::newDefault() const
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsFloat>();
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
AsFloat::AsFloat()
:AbstractExtractor(topic(), DataType::Flt32)
2025-08-01 01:47:41 +00:00
{
2025-08-01 07:03:35 +00:00
this->setCountWithin(4);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
QString AsFloat::topic()
{
return NAME(AsFloat);
}
std::shared_ptr<Serializable> AsDouble::newDefault() const
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsDouble>();
2025-08-01 01:47:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AsDouble::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
2025-08-03 00:04:12 +00:00
out->append(*((double*)bytes.data()));
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
AsDouble::AsDouble()
:AbstractExtractor(topic(), DataType::Dbl64)
2025-08-01 01:47:41 +00:00
{
2025-08-01 07:03:35 +00:00
this->setCountWithin(8);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
QString AsDouble::topic()
{
return NAME(AsDouble);
}
AsUnsigned::AsUnsigned()
:AbstractExtractor(topic(), DataType::Unsigned)
2025-08-01 01:47:41 +00:00
{
}
2025-08-05 14:00:41 +00:00
bool AsUnsigned::setCountWithin(int bytes)
2025-08-01 01:47:41 +00:00
{
auto count = std::max(1, std::min(8, bytes));
return AbstractExtractor::setCountWithin(count);
}
2025-08-10 09:02:18 +00:00
void AsUnsigned::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 01:47:41 +00:00
{
unsigned long long value = 0;
for (auto vidx = bytes.size() - 1; vidx >= 0; vidx--) {
auto vchar = bytes.at(vidx);
value = value << 8;
value += (uchar)vchar;
}
2025-08-03 00:04:12 +00:00
out->append(value);
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsUnsigned::newDefault() const
{
return std::make_shared<AsUnsigned>();
}
QString AsUnsigned::topic()
2025-08-01 01:47:41 +00:00
{
2025-08-05 14:00:41 +00:00
return NAME(AsUnsigned);
2025-08-01 01:47:41 +00:00
}
#include <stdexcept>
2025-08-10 10:05:58 +00:00
std::shared_ptr<ExtractUnit> AsList::elementRule() const
2025-08-01 23:06:42 +00:00
{
2025-08-02 02:04:30 +00:00
return this->_list._bind_unit;
2025-08-01 23:06:42 +00:00
}
2025-08-10 10:05:58 +00:00
bool AsList::setElementRule(std::shared_ptr<ExtractUnit> u)
2025-08-01 23:06:42 +00:00
{
2025-08-02 02:04:30 +00:00
this->_list._bind_unit = u;
2025-08-01 01:47:41 +00:00
return true;
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<SizeProvider> AsList::sizeProvider() const
2025-08-01 01:47:41 +00:00
{
2025-08-02 02:04:30 +00:00
return this->_list._bind_size_v;
2025-08-01 01:47:41 +00:00
}
2025-08-05 14:00:41 +00:00
bool AsList::setSizeProvider(std::shared_ptr<SizeProvider> ins)
2025-08-01 01:47:41 +00:00
{
2025-08-02 02:04:30 +00:00
this->_list._bind_size_v = ins;
2025-08-01 23:06:42 +00:00
return true;
}
2025-08-10 09:02:18 +00:00
QString AsList::unitType() const
2025-08-01 23:06:42 +00:00
{
2025-08-05 14:00:41 +00:00
return topic();
2025-08-01 23:06:42 +00:00
}
2025-08-05 14:00:41 +00:00
DataType AsList::outType() const
2025-08-01 23:06:42 +00:00
{
return DataType::LIST_COLLECTION;
}
2025-08-05 14:00:41 +00:00
int AsList::countWithin() const
2025-08-01 23:06:42 +00:00
{
2025-08-10 09:02:18 +00:00
return _list.count_acculate;
2025-08-01 23:06:42 +00:00
}
2025-08-05 14:00:41 +00:00
void AsList::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-01 23:06:42 +00:00
{
2025-08-10 09:02:18 +00:00
STRING_PEAK(_list._alias_name, obj);
QString size_provider_name = "";
STRING_PEAK(size_provider_name, obj);
auto xdev = core->sizeProviderList()[size_provider_name]->newDefault();
this->_list._bind_size_v = std::dynamic_pointer_cast<SizeProvider>(xdev);
2025-08-01 23:06:42 +00:00
QString bind_unit_name = "";
2025-08-02 07:02:35 +00:00
STRING_PEAK(bind_unit_name, obj);
2025-08-10 09:02:18 +00:00
xdev = core->operator[](bind_unit_name)->newDefault();
2025-08-10 10:05:58 +00:00
this->_list._bind_unit = std::dynamic_pointer_cast<ExtractUnit>(xdev);
2025-08-10 09:02:18 +00:00
2025-08-02 07:02:35 +00:00
QJsonObject unit_obj;
QJsonObject size_provider_obj;
OBJECT_PEAK(unit_obj, obj);
OBJECT_PEAK(size_provider_obj, obj);
this->_list._bind_size_v->loadFrom(core, size_provider_obj);
this->_list._bind_unit->loadFrom(core, unit_obj);
2025-08-01 23:06:42 +00:00
}
2025-08-05 14:00:41 +00:00
void AsList::saveTo(QJsonObject& obj) const
2025-08-01 23:06:42 +00:00
{
2025-08-10 09:02:18 +00:00
STRING_SAVE(_list._alias_name, obj);
2025-08-10 09:02:18 +00:00
QString size_provider_name = this->_list._bind_size_v->name();
STRING_SAVE(size_provider_name, obj);
QString bind_unit_name = this->_list._bind_unit->name();
2025-08-02 07:02:35 +00:00
STRING_SAVE(bind_unit_name, obj);
2025-08-01 23:06:42 +00:00
2025-08-10 09:02:18 +00:00
QJsonObject unit_obj;
_list._bind_unit->saveTo(unit_obj);
2025-08-02 07:02:35 +00:00
QJsonObject size_provider_obj;
2025-08-10 09:02:18 +00:00
_list._bind_size_v->saveTo(size_provider_obj);
OBJECT_SAVE(unit_obj, obj);
OBJECT_PEAK(size_provider_obj, obj);
2025-08-01 23:06:42 +00:00
}
2025-08-10 09:02:18 +00:00
void AsList::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-01 23:06:42 +00:00
{
2025-08-02 02:04:30 +00:00
auto expr = this->_list._bind_size_v->expression();
auto size_value = this->_list._bind_size_v->value(expr);
2025-08-01 23:06:42 +00:00
2025-08-10 09:02:18 +00:00
auto remains = bytes;
const_cast<AsList*>(this)->_list.count_acculate = 0;
2025-08-01 23:06:42 +00:00
for (auto idx = 0; idx < size_value; ++idx) {
2025-08-10 09:02:18 +00:00
this->_list._bind_unit->parse(remains, out);
2025-08-01 23:06:42 +00:00
2025-08-10 09:02:18 +00:00
auto unit_size = this->_list._bind_unit->countWithin();
remains = remains.mid(unit_size);
const_cast<AsList*>(this)->_list.count_acculate += unit_size;
2025-08-03 00:04:12 +00:00
}
2025-08-01 23:06:42 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsList::newDefault() const
2025-08-01 23:06:42 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsList>();
2025-08-01 01:47:41 +00:00
}
2025-08-02 02:04:30 +00:00
2025-08-10 09:02:18 +00:00
void AsList::registSubField(std::shared_ptr<ScopeFieldsSetter> inst){}
2025-08-03 00:04:12 +00:00
2025-08-05 14:00:41 +00:00
QString AsList::topic()
2025-08-02 02:04:30 +00:00
{
2025-08-05 14:00:41 +00:00
return NAME(AsList);
2025-08-02 02:04:30 +00:00
}
2025-08-10 09:02:18 +00:00
QString AsList::aliasName() const
2025-08-05 14:00:41 +00:00
{
2025-08-10 09:02:18 +00:00
return _list._alias_name;
2025-08-05 14:00:41 +00:00
}
2025-08-10 09:02:18 +00:00
void AsList::setAlias(const QString& name)
2025-08-02 02:04:30 +00:00
{
2025-08-10 09:02:18 +00:00
_list._alias_name = name;
2025-08-02 02:04:30 +00:00
}
2025-08-10 09:02:18 +00:00
DataType AsUnion::outType() const
2025-08-02 02:04:30 +00:00
{
2025-08-10 09:02:18 +00:00
return DataType::UNION_COMBINATE;
2025-08-02 02:04:30 +00:00
}
2025-08-05 14:00:41 +00:00
int AsUnion::countWithin() const
2025-08-02 02:04:30 +00:00
{
return _union.byte_count;
}
2025-08-10 09:02:18 +00:00
void AsUnion::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-02 02:04:30 +00:00
{
for (auto u : this->elementRules())
2025-08-03 00:04:12 +00:00
if (u->checkpass(bytes)) {
2025-08-10 09:02:18 +00:00
u->bindRule()->parse(bytes, out);
break;
2025-08-03 00:04:12 +00:00
}
2025-08-02 02:04:30 +00:00
}
2025-08-10 09:02:18 +00:00
void AsUnion::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-02 02:04:30 +00:00
{
2025-08-02 07:02:35 +00:00
INT32_PEAK(_union.byte_count, obj);
2025-08-10 09:02:18 +00:00
STRING_PEAK(_union.alias_name, obj);
2025-08-02 07:02:35 +00:00
QJsonArray array;
ARRAY_PEAK(array, obj);
this->_union._rule_list.clear();
for (auto index = 0; index < array.size(); ++index) {
auto rule_obj = array.at(index);
QString match_name;
STRING_PEAK(match_name, rule_obj);
QJsonObject match_obj;
OBJECT_PEAK(match_obj, rule_obj);
auto match = core->ruleMatchList()[match_name];
auto nmatch = match->newDefault();
nmatch->loadFrom(core, match_obj);
2025-08-10 09:02:18 +00:00
this->_union._rule_list.append(std::dynamic_pointer_cast<RuleMatch>(nmatch));
2025-08-02 07:02:35 +00:00
}
2025-08-02 02:04:30 +00:00
}
2025-08-05 14:00:41 +00:00
void AsUnion::saveTo(QJsonObject& obj) const
2025-08-02 02:04:30 +00:00
{
2025-08-02 07:02:35 +00:00
INT32_SAVE(_union.byte_count, obj);
2025-08-10 09:02:18 +00:00
STRING_SAVE(_union.alias_name, obj);
2025-08-02 07:02:35 +00:00
QJsonArray array;
for (auto insr : this->_union._rule_list) {
QJsonObject rule_obj;
2025-08-10 09:02:18 +00:00
auto match_name = insr->matchType();
2025-08-02 07:02:35 +00:00
STRING_SAVE(match_name, rule_obj);
QJsonObject match_obj;
insr->saveTo(match_obj);
OBJECT_SAVE(match_obj, rule_obj);
array.append(rule_obj);
}
ARRAY_SAVE(array, obj);
2025-08-02 02:04:30 +00:00
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsUnion::newDefault() const
2025-08-02 02:04:30 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsUnion>();
2025-08-02 02:04:30 +00:00
}
2025-08-05 14:00:41 +00:00
QList<std::shared_ptr<RuleMatch>> AsUnion::elementRules() const
2025-08-02 02:04:30 +00:00
{
return _union._rule_list;
}
2025-08-05 14:00:41 +00:00
bool AsUnion::appendElementRule(std::shared_ptr<RuleMatch> u)
2025-08-02 02:04:30 +00:00
{
this->_union._rule_list << u;
return true;
}
2025-08-05 14:00:41 +00:00
void AsUnion::clearRules()
2025-08-02 02:04:30 +00:00
{
this->_union._rule_list.clear();
}
2025-08-05 14:00:41 +00:00
bool AsUnion::setCountWithin(int bytes)
2025-08-02 02:04:30 +00:00
{
this->_union.byte_count = bytes;
return true;
}
2025-08-05 14:00:41 +00:00
void AsUnion::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
2025-08-02 10:26:13 +00:00
{
2025-08-10 09:02:18 +00:00
for (auto rl : this->_union._rule_list)
rl->bindRule()->registSubField(inst);
2025-08-02 10:26:13 +00:00
}
2025-08-05 14:00:41 +00:00
QString AsUnion::topic()
{
return NAME(AsUnion);
}
2025-08-10 09:02:18 +00:00
QString AsUnion::aliasName() const
{
return _union.alias_name;
}
void AsUnion::setAlias(const QString& name)
{
_union.alias_name = name;
}
QString AsUnion::unitType() const
{
return topic();
}
2025-08-05 14:00:41 +00:00
void AsRuleSet::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
2025-08-02 07:02:35 +00:00
{
2025-08-05 12:51:12 +00:00
for (auto rl_key : this->fieldNames()){
2025-08-10 09:02:18 +00:00
auto rlinst = this->operator[](rl_key);
rlinst->registSubField(inst);
2025-08-03 00:04:12 +00:00
}
}
2025-08-10 09:02:18 +00:00
void AsRuleSet::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-03 00:04:12 +00:00
{
auto bufx = bytes;
2025-08-05 12:51:12 +00:00
for (auto keym : this->fieldNames()) {
2025-08-10 09:02:18 +00:00
auto rule = this->operator[](keym);
rule->parse(bufx, out);
auto size_u = rule->countWithin();
bufx = bufx.mid(size_u);
2025-08-03 00:04:12 +00:00
}
}
2025-08-10 09:02:18 +00:00
QString AsRuleSet::unitType() const
2025-08-03 00:04:12 +00:00
{
2025-08-05 14:00:41 +00:00
return topic();
2025-08-02 07:02:35 +00:00
}
2025-08-05 14:00:41 +00:00
DataType AsRuleSet::outType() const
2025-08-02 07:02:35 +00:00
{
2025-08-10 09:02:18 +00:00
return DataType::COMPLEX_RULESET;
2025-08-02 07:02:35 +00:00
}
2025-08-05 14:00:41 +00:00
int AsRuleSet::countWithin() const
2025-08-02 07:02:35 +00:00
{
2025-08-10 09:02:18 +00:00
auto count = 0;
for (auto inst : _bind.sub_units)
count += inst->countWithin();
return count;
2025-08-02 07:02:35 +00:00
}
2025-08-05 14:00:41 +00:00
void AsRuleSet::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-02 07:02:35 +00:00
{
2025-08-10 09:02:18 +00:00
STRING_PEAK(_bind.alias_name, obj);
2025-08-02 07:02:35 +00:00
QJsonArray array;
ARRAY_SAVE(array, obj);
for (auto index = 0; index < array.size(); ++index) {
auto rule_obj = array.at(index);
2025-08-10 09:02:18 +00:00
QString match_name, field_name;
STRING_PEAK(field_name, rule_obj);
2025-08-02 07:02:35 +00:00
STRING_PEAK(match_name, rule_obj);
2025-08-10 09:02:18 +00:00
QJsonObject match_obj;
2025-08-02 07:02:35 +00:00
OBJECT_PEAK(match_obj, rule_obj);
2025-08-10 09:02:18 +00:00
auto xdev = core->operator[](match_name)->newDefault();
2025-08-02 07:02:35 +00:00
xdev->loadFrom(core, match_obj);
2025-08-10 10:05:58 +00:00
_bind.sub_units << std::dynamic_pointer_cast<ExtractUnit>(xdev);
2025-08-02 07:02:35 +00:00
}
}
2025-08-05 14:00:41 +00:00
void AsRuleSet::saveTo(QJsonObject& obj) const
2025-08-02 07:02:35 +00:00
{
2025-08-10 09:02:18 +00:00
STRING_SAVE(_bind.alias_name, obj);
2025-08-02 07:02:35 +00:00
QJsonArray array;
2025-08-05 10:30:24 +00:00
for (auto pairk : _bind.sub_units) {
2025-08-02 07:02:35 +00:00
QJsonObject rule_obj, match_obj;
2025-08-03 00:04:12 +00:00
2025-08-10 09:02:18 +00:00
auto field_name = pairk->name();
STRING_SAVE(field_name, rule_obj);
QString match_name = pairk->delegateInst()->aliasName();
2025-08-02 07:02:35 +00:00
STRING_SAVE(match_name, rule_obj);
2025-08-10 09:02:18 +00:00
pairk->saveTo(match_obj);
2025-08-02 07:02:35 +00:00
OBJECT_SAVE(match_obj, rule_obj);
array.append(rule_obj);
}
ARRAY_SAVE(array, obj);
}
2025-08-05 14:00:41 +00:00
std::shared_ptr<Serializable> AsRuleSet::newDefault() const
2025-08-02 07:02:35 +00:00
{
2025-08-05 14:00:41 +00:00
return std::make_shared<AsRuleSet>();
2025-08-02 07:02:35 +00:00
}
2025-08-04 01:27:04 +00:00
2025-08-10 10:05:58 +00:00
std::shared_ptr<ExtractUnit> AsRuleSet::operator[](int index) const
2025-08-04 01:27:04 +00:00
{
2025-08-05 12:51:12 +00:00
return this->_bind.sub_units[index];
2025-08-04 01:27:04 +00:00
}
2025-08-10 10:05:58 +00:00
void AsRuleSet::replace(int index, std::shared_ptr<ExtractUnit> inst)
2025-08-04 01:27:04 +00:00
{
2025-08-05 12:51:12 +00:00
this->_bind.sub_units.replace(index, inst);
2025-08-04 01:27:04 +00:00
}
2025-08-10 10:05:58 +00:00
extract::AsRuleSet& AsRuleSet::append(std::shared_ptr<ExtractUnit> u)
2025-08-04 01:27:04 +00:00
{
2025-08-10 09:02:18 +00:00
this->_bind.sub_units.append(u);
2025-08-04 01:27:04 +00:00
return *this;
}
2025-08-05 14:00:41 +00:00
void AsRuleSet::removeAt(int index)
2025-08-04 01:27:04 +00:00
{
2025-08-05 12:51:12 +00:00
this->_bind.sub_units.removeAt(index);
2025-08-04 01:27:04 +00:00
}
2025-08-05 14:00:41 +00:00
QList<QString> AsRuleSet::fieldNames() const
2025-08-04 01:27:04 +00:00
{
QList<QString> fields_store;
2025-08-10 09:02:18 +00:00
for (auto fins : this->_bind.sub_units) {
fields_store << fins->name();
2025-08-04 01:27:04 +00:00
}
return fields_store;;
}
2025-08-10 10:05:58 +00:00
std::shared_ptr<ExtractUnit> AsRuleSet::operator[](const QString& field) const
2025-08-04 01:27:04 +00:00
{
2025-08-05 12:51:12 +00:00
for (auto pair : this->_bind.sub_units) {
2025-08-10 09:02:18 +00:00
if(pair->name() == field)
return pair;
2025-08-04 01:27:04 +00:00
}
2025-08-10 10:05:58 +00:00
return std::shared_ptr<ExtractUnit>();
2025-08-04 01:27:04 +00:00
}
2025-08-05 03:50:14 +00:00
2025-08-10 09:02:18 +00:00
void AsRuleSet::setAlias(const QString& typeAlias)
2025-08-05 03:50:14 +00:00
{
2025-08-10 09:02:18 +00:00
_bind.alias_name = typeAlias;
2025-08-05 03:50:14 +00:00
}
2025-08-10 09:02:18 +00:00
QString AsRuleSet::aliasName() const
2025-08-05 03:50:14 +00:00
{
2025-08-10 09:02:18 +00:00
return _bind.alias_name;
2025-08-05 03:50:14 +00:00
}
2025-08-05 12:51:12 +00:00
2025-08-05 14:00:41 +00:00
int AsRuleSet::memberCount() const
2025-08-05 12:51:12 +00:00
{
return _bind.sub_units.size();
}
2025-08-05 14:00:41 +00:00
QString AsRuleSet::topic()
{
return NAME(AsRuleSet);
}
2025-08-10 09:02:18 +00:00
2025-08-10 10:05:58 +00:00
QString SingleBasedUnit::name() const
2025-08-10 09:02:18 +00:00
{
return _inst._field_name;
}
2025-08-10 10:05:58 +00:00
bool SingleBasedUnit::setOffsetSpan(int bytes)
2025-08-10 09:02:18 +00:00
{
_inst._bytes_offset = bytes;
return true;
}
2025-08-10 10:05:58 +00:00
int SingleBasedUnit::offsetSpan() const
2025-08-10 09:02:18 +00:00
{
return _inst._bytes_offset;
}
2025-08-10 10:05:58 +00:00
std::shared_ptr<ExtractDelegate> SingleBasedUnit::delegateInst() const
2025-08-10 09:02:18 +00:00
{
return _inst._delegate_inst;
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::setDelegate(std::shared_ptr<ExtractDelegate> inst)
2025-08-10 09:02:18 +00:00
{
_inst._delegate_inst = inst;
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
2025-08-10 09:02:18 +00:00
{
inst->setField(name(), _inst._delegate_inst->outType());
auto nlayer = std::make_shared<FieldManagerLayer>(name(), inst);
_inst._delegate_inst->registSubField(nlayer);
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::parse(const QByteArray& bytes, std::shared_ptr<DataContext> out) const
2025-08-10 09:02:18 +00:00
{
auto context = std::make_shared<ValueAccessContext>();
context->init(name(), out);
auto bytes_sec = bytes.mid(this->offsetSpan());
_inst._delegate_inst->parse(bytes_sec, context);
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-10 09:02:18 +00:00
{
QJsonObject field_obj;
OBJECT_PEAK(field_obj, obj);
STRING_PEAK(_inst._field_name, field_obj);
INT32_PEAK(_inst._bytes_offset, field_obj);
QString delegate_alias;
STRING_PEAK(delegate_alias, obj);
auto vcopy = core->operator[](delegate_alias)->newDefault();
2025-08-10 10:03:02 +00:00
_inst._delegate_inst = std::dynamic_pointer_cast<ExtractDelegate>(vcopy);
2025-08-10 09:02:18 +00:00
QJsonObject data_obj;
OBJECT_PEAK(data_obj, field_obj);
_inst._delegate_inst->loadFrom(core, data_obj);
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::saveTo(QJsonObject& obj) const
2025-08-10 09:02:18 +00:00
{
QJsonObject field_obj;
STRING_SAVE(_inst._field_name, field_obj);
INT32_SAVE(_inst._bytes_offset, field_obj);
auto delegate_alias = _inst._delegate_inst->aliasName();
STRING_SAVE(delegate_alias, obj);
QJsonObject data_obj;
_inst._delegate_inst->saveTo(data_obj);
OBJECT_SAVE(data_obj, field_obj);
OBJECT_SAVE(field_obj, obj);
}
2025-08-10 10:05:58 +00:00
std::shared_ptr<Serializable> SingleBasedUnit::newDefault() const
2025-08-10 09:02:18 +00:00
{
2025-08-10 10:05:58 +00:00
return std::make_shared<SingleBasedUnit>();
2025-08-10 09:02:18 +00:00
}
2025-08-10 10:05:58 +00:00
void SingleBasedUnit::setName(const QString& name)
2025-08-10 09:02:18 +00:00
{
_inst._field_name = name;
}
2025-08-10 10:05:58 +00:00
int SingleBasedUnit::countWithin() const
2025-08-10 09:02:18 +00:00
{
return _inst._delegate_inst->countWithin() + offsetSpan();
}