QBinaryTranslate/TranslateUI/extract_basic.cpp

715 lines
16 KiB
C++
Raw Normal View History

2025-08-01 01:47:41 +00:00
#include "extract_basic.h"
#include <QVariant>
using namespace extract;
AbstractExtractor::AbstractExtractor(const QString& name, DataType data)
2025-08-02 02:04:30 +00:00
{
_abs_data.name_store = name;
_abs_data.type_store = data;
2025-08-01 01:47:41 +00:00
}
bool AbstractExtractor::setOffsetSpan(int bytes)
{
2025-08-02 02:04:30 +00:00
this->_abs_data.byte_offset = bytes;
2025-08-01 01:47:41 +00:00
return true;
}
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;
}
QString AbstractExtractor::name() const
{
2025-08-02 02:04:30 +00:00
return _abs_data.name_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::offsetSpan() const
{
2025-08-02 02:04:30 +00:00
return _abs_data.byte_offset;
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-02 07:02:35 +00:00
INT32_PEAK(_abs_data.byte_offset, obj);
INT32_PEAK(_abs_data.byte_count, obj);
2025-08-01 01:47:41 +00:00
}
void AbstractExtractor::saveTo(QJsonObject& obj) const
{
2025-08-02 07:02:35 +00:00
INT32_SAVE(_abs_data.byte_offset, obj);
INT32_SAVE(_abs_data.byte_count, obj);
2025-08-01 01:47:41 +00:00
}
2025-08-03 00:04:12 +00:00
void AbstractExtractor::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
{
}
2025-08-01 01:47:41 +00:00
BytesAsHex::BytesAsHex()
: AbstractExtractor(NAME(BytesAsHex), 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"
void BytesAsHex::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
}
std::shared_ptr<Serializable> BytesAsHex::newDefault() const
{
return std::make_shared<BytesAsHex>();
}
BytesAsBitCombine::BytesAsBitCombine()
: AbstractExtractor(NAME(BytesAsBitCombine), DataType::TextString)
{
}
bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
{
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;
}
QHash<int, QString> BytesAsBitCombine::switchOptions() const
{
return _combine._switch_options;
2025-08-01 01:47:41 +00:00
}
void BytesAsBitCombine::clearOptions()
{
_combine._switch_options.clear();
2025-08-01 01:47:41 +00:00
}
2025-08-03 00:04:12 +00:00
void BytesAsBitCombine::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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>
void BytesAsBitCombine::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
}
}
void BytesAsBitCombine::saveTo(QJsonObject& obj) const
{
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
}
std::shared_ptr<Serializable> BytesAsBitCombine::newDefault() const
{
return std::make_shared<BytesAsBitCombine>();
}
std::shared_ptr<Serializable> BytesAsInteger::newDefault() const
{
return std::make_shared<BytesAsInteger>();
}
BytesAsInteger::BytesAsInteger()
:AbstractExtractor(NAME(BytesAsInteger), DataType::Integer) {
}
bool BytesAsInteger::setCountWithin(int bytes)
{
bytes = std::min(8, std::max(bytes, 0));
return AbstractExtractor::setCountWithin(bytes);
}
2025-08-03 00:04:12 +00:00
void BytesAsInteger::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
#include <QTextCodec>
BytesAsString::BytesAsString()
:AbstractExtractor(NAME(BytesAsString), DataType::TextString) {
2025-08-02 02:04:30 +00:00
_strings._conv_with = QTextCodec::codecForName("GBK");
2025-08-01 01:47:41 +00:00
}
void BytesAsString::setStrCodec(QTextCodec* ins)
{
2025-08-02 02:04:30 +00:00
this->_strings._conv_with = ins;
2025-08-01 01:47:41 +00:00
}
QString BytesAsString::codecName() const
{
2025-08-02 02:04:30 +00:00
return this->_strings._conv_with->name();
2025-08-01 01:47:41 +00:00
}
2025-08-03 00:04:12 +00:00
void BytesAsString::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
}
void BytesAsString::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
}
void BytesAsString::saveTo(QJsonObject& obj) const
{
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
}
std::shared_ptr<Serializable> BytesAsString::newDefault() const
{
return std::make_shared<BytesAsString>();
}
2025-08-03 00:04:12 +00:00
void BytesAsFloat::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
}
std::shared_ptr<Serializable> BytesAsFloat::newDefault() const
{
return std::make_shared<BytesAsFloat>();
}
BytesAsFloat::BytesAsFloat()
:AbstractExtractor(NAME(BytesAsFloat), DataType::Flt32)
{
2025-08-01 07:03:35 +00:00
this->setCountWithin(4);
2025-08-01 01:47:41 +00:00
}
std::shared_ptr<Serializable> BytesAsDouble::newDefault() const
{
return std::make_shared<BytesAsDouble>();
}
2025-08-03 00:04:12 +00:00
void BytesAsDouble::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
}
BytesAsDouble::BytesAsDouble()
:AbstractExtractor(NAME(BytesAsDouble), DataType::Dbl64)
{
2025-08-01 07:03:35 +00:00
this->setCountWithin(8);
2025-08-01 01:47:41 +00:00
}
BytesAsUnsigned::BytesAsUnsigned()
:AbstractExtractor(NAME(BytesAsUnsigned), DataType::Unsigned)
{
}
bool BytesAsUnsigned::setCountWithin(int bytes)
{
auto count = std::max(1, std::min(8, bytes));
return AbstractExtractor::setCountWithin(count);
}
2025-08-03 00:04:12 +00:00
void BytesAsUnsigned::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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
}
std::shared_ptr<Serializable> BytesAsUnsigned::newDefault() const
{
return std::make_shared<BytesAsUnsigned>();
}
#include <stdexcept>
2025-08-01 23:06:42 +00:00
bool BytesAsList::setOffsetSpan(int value)
2025-08-01 01:47:41 +00:00
{
2025-08-02 02:04:30 +00:00
this->_list._bytes_offset = value;
2025-08-01 23:06:42 +00:00
return true;
}
2025-08-01 01:47:41 +00:00
2025-08-01 23:06:42 +00:00
std::shared_ptr<ExtractUnit> BytesAsList::elementRule() const
{
2025-08-02 02:04:30 +00:00
return this->_list._bind_unit;
2025-08-01 23:06:42 +00:00
}
bool BytesAsList::appendElementRule(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-02 02:04:30 +00:00
std::shared_ptr<SizeProvider> BytesAsList::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-01 23:06:42 +00:00
bool BytesAsList::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;
}
QString BytesAsList::name() const
{
return NAME(BytesAsList);
}
DataType BytesAsList::outType() const
{
return DataType::LIST_COLLECTION;
}
int BytesAsList::offsetSpan() const
{
2025-08-02 02:04:30 +00:00
return _list._bytes_offset;
2025-08-01 23:06:42 +00:00
}
int BytesAsList::countWithin() const
{
2025-08-02 02:04:30 +00:00
auto expr = this->_list._bind_size_v->expression();
if (this->_list._bind_size_v->value(expr) < 0) {
2025-08-01 23:06:42 +00:00
return 0;
}
2025-08-02 02:04:30 +00:00
return this->_list._bind_size_v->value(expr)
* this->_list._bind_unit->countWithin();
2025-08-01 23:06:42 +00:00
}
#include "TranslateBasic.h"
void BytesAsList::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-01 23:06:42 +00:00
{
2025-08-02 07:02:35 +00:00
INT32_PEAK(_list._bytes_offset, obj);
2025-08-01 23:06:42 +00:00
QString bind_unit_name = "";
QString bind_size_provider_name = "";
2025-08-02 07:02:35 +00:00
STRING_PEAK(bind_unit_name, obj);
STRING_PEAK(bind_size_provider_name, obj);
auto xdev = core->sizeProviderList()[bind_size_provider_name]->newDefault();
this->_list._bind_size_v = std::dynamic_pointer_cast<SizeProvider>(xdev);
xdev = core->extractUnitList()[bind_unit_name]->newDefault();
this->_list._bind_unit = std::dynamic_pointer_cast<ExtractUnit>(xdev);
2025-08-01 23:06:42 +00:00
QString bind_provider_expr = "";
2025-08-02 07:02:35 +00:00
STRING_SAVE(bind_provider_expr, obj);
this->_list._bind_size_v->setExpression(bind_provider_expr);
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-02 07:02:35 +00:00
#include <QJsonArray>
2025-08-01 23:06:42 +00:00
void BytesAsList::saveTo(QJsonObject& obj) const
{
2025-08-02 07:02:35 +00:00
INT32_SAVE(_list._bytes_offset, obj);
2025-08-01 23:06:42 +00:00
QString bind_unit_name = "";
2025-08-02 07:02:35 +00:00
QJsonObject unit_obj;
if (this->_list._bind_unit) {
2025-08-02 02:04:30 +00:00
bind_unit_name = this->_list._bind_unit->name();
2025-08-02 07:02:35 +00:00
this->_list._bind_unit->saveTo(unit_obj);
}
STRING_SAVE(bind_unit_name, obj);
OBJECT_SAVE(unit_obj, obj);
2025-08-01 23:06:42 +00:00
QString bind_size_provider_name = "";
QString bind_provider_expr = "";
2025-08-02 07:02:35 +00:00
QJsonObject size_provider_obj;
2025-08-02 02:04:30 +00:00
if (this->_list._bind_size_v) {
bind_provider_expr = this->_list._bind_size_v->expression();
bind_size_provider_name = this->_list._bind_size_v->name();
2025-08-02 07:02:35 +00:00
this->_list._bind_size_v->saveTo(size_provider_obj);
2025-08-01 23:06:42 +00:00
}
2025-08-02 07:02:35 +00:00
STRING_SAVE(bind_size_provider_name, obj);
STRING_SAVE(bind_provider_expr, obj);
OBJECT_SAVE(size_provider_obj, obj);
2025-08-01 23:06:42 +00:00
}
2025-08-03 00:04:12 +00:00
void BytesAsList::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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);
auto unit_size = this->_list._bind_unit->countWithin();
2025-08-01 23:06:42 +00:00
for (auto idx = 0; idx < size_value; ++idx) {
auto secn = bytes.mid(unit_size * idx, unit_size);
2025-08-05 12:51:12 +00:00
auto slice_context = std::make_shared<ValueAccessContext>();
2025-08-03 00:04:12 +00:00
slice_context->init(QString("ls<%1>").arg(idx), out);
this->_list._bind_unit->parse(secn, slice_context);
}
2025-08-01 23:06:42 +00:00
}
std::shared_ptr<Serializable> BytesAsList::newDefault() const
{
return std::make_shared<BytesAsList>();
2025-08-01 01:47:41 +00:00
}
2025-08-02 02:04:30 +00:00
2025-08-03 00:04:12 +00:00
void BytesAsList::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
{
inst->setField("ls", this->_list._bind_unit->outType());
2025-08-04 01:27:04 +00:00
auto layer = std::make_shared<FieldManagerLayer>("ls", inst);
2025-08-03 00:04:12 +00:00
this->_list._bind_unit->registSubField(layer);
}
2025-08-02 02:04:30 +00:00
QString BytesAsUnion::name() const
{
return NAME(BytesAsUnion);
}
DataType BytesAsUnion::outType() const
{
return DataType::UNION_COMBINATE;
}
int BytesAsUnion::offsetSpan() const
{
return _union.byte_offset;
}
int BytesAsUnion::countWithin() const
{
return _union.byte_count;
}
2025-08-03 00:04:12 +00:00
void BytesAsUnion::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> 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-05 12:51:12 +00:00
auto enum_context = std::make_shared<ValueAccessContext>();
2025-08-03 00:04:12 +00:00
enum_context->init(u->name(), out);
u->bindRule()->parse(bytes, enum_context);
}
2025-08-02 02:04:30 +00:00
}
void BytesAsUnion::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);
INT32_PEAK(_union.byte_offset, obj);
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);
this->_union._rule_list.append(
std::dynamic_pointer_cast<RuleMatch>(nmatch));
}
2025-08-02 02:04:30 +00:00
}
void BytesAsUnion::saveTo(QJsonObject& obj) const
{
2025-08-02 07:02:35 +00:00
INT32_SAVE(_union.byte_count, obj);
INT32_SAVE(_union.byte_offset, obj);
QJsonArray array;
for (auto insr : this->_union._rule_list) {
QJsonObject rule_obj;
auto match_name = insr->name();
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
}
std::shared_ptr<Serializable> BytesAsUnion::newDefault() const
{
return std::make_shared<BytesAsUnion>();
}
QList<std::shared_ptr<RuleMatch>> BytesAsUnion::elementRules() const
{
return _union._rule_list;
}
bool BytesAsUnion::appendElementRule(std::shared_ptr<RuleMatch> u)
2025-08-02 02:04:30 +00:00
{
this->_union._rule_list << u;
return true;
}
void BytesAsUnion::clearRules()
{
this->_union._rule_list.clear();
}
bool BytesAsUnion::setCountWithin(int bytes)
{
this->_union.byte_count = bytes;
return true;
}
bool BytesAsUnion::setOffsetSpan(int value)
{
this->_union.byte_offset = value;
return true;
}
2025-08-02 07:02:35 +00:00
2025-08-03 00:04:12 +00:00
void BytesAsUnion::registSubField(std::shared_ptr<ScopeFieldsSetter> inst)
2025-08-02 10:26:13 +00:00
{
2025-08-03 00:04:12 +00:00
for (auto rl : this->_union._rule_list) {
inst->setField(rl->name(), rl->bindRule()->outType());
2025-08-04 01:27:04 +00:00
auto layer = std::make_shared<FieldManagerLayer>(rl->name(), inst);
2025-08-03 00:04:12 +00:00
rl->bindRule()->registSubField(layer);
}
2025-08-02 10:26:13 +00:00
}
2025-08-03 00:04:12 +00:00
void BytesAsRuleSet::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()){
auto rlinst = (*this)[rl_key];
2025-08-03 00:04:12 +00:00
inst->setField(rl_key, rlinst->outType());
2025-08-04 01:27:04 +00:00
auto layer = std::make_shared<FieldManagerLayer>(rl_key, inst);
2025-08-03 00:04:12 +00:00
rlinst->registSubField(layer);
}
}
void BytesAsRuleSet::parse(const QByteArray& bytes, std::shared_ptr<DataAccessContext> out) const
{
auto bufx = bytes;
2025-08-05 12:51:12 +00:00
for (auto keym : this->fieldNames()) {
auto rule_context = std::make_shared<ValueAccessContext>();
2025-08-03 00:04:12 +00:00
rule_context->init(keym, out);
2025-08-05 12:51:12 +00:00
auto rule = (*this)[keym];
2025-08-03 00:04:12 +00:00
bufx = bufx.mid(rule->offsetSpan());
auto count = rule->countWithin();
auto secx = bufx.mid(0, count);
rule->parse(secx, rule_context);
bufx = bufx.mid(count);
}
}
QString BytesAsRuleSet::name() const
{
return NAME(BytesAsRuleSet);
2025-08-02 07:02:35 +00:00
}
2025-08-03 00:04:12 +00:00
DataType BytesAsRuleSet::outType() const
2025-08-02 07:02:35 +00:00
{
return DataType::SUB_RULE;
}
2025-08-03 00:04:12 +00:00
bool BytesAsRuleSet::setOffsetSpan(int bytes)
2025-08-02 07:02:35 +00:00
{
2025-08-05 10:30:24 +00:00
this->_bind._byte_offset = bytes;
2025-08-02 07:02:35 +00:00
return true;
}
2025-08-03 00:04:12 +00:00
int BytesAsRuleSet::offsetSpan() const
2025-08-02 07:02:35 +00:00
{
2025-08-05 10:30:24 +00:00
return this->_bind._byte_offset;
2025-08-02 07:02:35 +00:00
}
2025-08-03 00:04:12 +00:00
int BytesAsRuleSet::countWithin() const
2025-08-02 07:02:35 +00:00
{
2025-08-05 10:30:24 +00:00
return this->_bind._byte_count;
2025-08-02 07:02:35 +00:00
}
2025-08-03 00:04:12 +00:00
void BytesAsRuleSet::loadFrom(std::shared_ptr<TranslateBasic> core, const QJsonObject& obj)
2025-08-02 07:02:35 +00:00
{
2025-08-05 10:30:24 +00:00
INT32_PEAK(_bind._byte_count, obj);
INT32_PEAK(_bind._byte_offset, 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-03 00:04:12 +00:00
QString match_name, key;
2025-08-02 07:02:35 +00:00
QJsonObject match_obj;
STRING_PEAK(match_name, rule_obj);
2025-08-03 00:04:12 +00:00
STRING_PEAK(key, rule_obj);
2025-08-02 07:02:35 +00:00
OBJECT_PEAK(match_obj, rule_obj);
auto xdev = core->extractUnitList()[match_name]->newDefault();
xdev->loadFrom(core, match_obj);
2025-08-05 10:30:24 +00:00
_bind.sub_units << std::make_pair(key, std::dynamic_pointer_cast<ExtractUnit>(xdev));
2025-08-02 07:02:35 +00:00
}
}
2025-08-03 00:04:12 +00:00
void BytesAsRuleSet::saveTo(QJsonObject& obj) const
2025-08-02 07:02:35 +00:00
{
2025-08-05 10:30:24 +00:00
INT32_SAVE(_bind._byte_count, obj);
INT32_SAVE(_bind._byte_offset, 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-04 01:27:04 +00:00
auto key = pairk.first;
auto info = pairk.second;
2025-08-03 00:04:12 +00:00
STRING_SAVE(key, rule_obj);
2025-08-02 07:02:35 +00:00
QString match_name = info->name();
STRING_SAVE(match_name, rule_obj);
info->saveTo(match_obj);
OBJECT_SAVE(match_obj, rule_obj);
array.append(rule_obj);
}
ARRAY_SAVE(array, obj);
}
2025-08-03 00:04:12 +00:00
std::shared_ptr<Serializable> BytesAsRuleSet::newDefault() const
2025-08-02 07:02:35 +00:00
{
2025-08-03 00:04:12 +00:00
return std::make_shared<BytesAsRuleSet>();
2025-08-02 07:02:35 +00:00
}
2025-08-04 01:27:04 +00:00
2025-08-05 12:51:12 +00:00
std::pair<QString, std::shared_ptr<ExtractUnit>> BytesAsRuleSet::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-05 12:51:12 +00:00
void BytesAsRuleSet::replace(int index, std::pair<QString, 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-05 12:51:12 +00:00
extract::BytesAsRuleSet& BytesAsRuleSet::append(const QString& nm, std::shared_ptr<ExtractUnit> u)
2025-08-04 01:27:04 +00:00
{
2025-08-05 12:51:12 +00:00
this->_bind.sub_units.append(std::make_pair(nm, u));
2025-08-04 01:27:04 +00:00
return *this;
}
2025-08-05 12:51:12 +00:00
void BytesAsRuleSet::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 12:51:12 +00:00
QList<QString> BytesAsRuleSet::fieldNames() const
2025-08-04 01:27:04 +00:00
{
QList<QString> fields_store;
2025-08-05 12:51:12 +00:00
for (auto fpair : this->_bind.sub_units) {
2025-08-04 01:27:04 +00:00
fields_store << fpair.first;
}
return fields_store;;
}
2025-08-05 12:51:12 +00:00
std::shared_ptr<ExtractUnit> BytesAsRuleSet::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-04 01:27:04 +00:00
if(pair.first == field)
return pair.second;
}
return std::shared_ptr<ExtractUnit>();
}
2025-08-05 03:50:14 +00:00
void BytesAsRuleSet::setCustomName(const QString& name)
{
2025-08-05 10:30:24 +00:00
_bind._name_store = name;
2025-08-05 03:50:14 +00:00
}
QString BytesAsRuleSet::customName() const
{
2025-08-05 10:30:24 +00:00
return _bind._name_store;
2025-08-05 03:50:14 +00:00
}
2025-08-05 12:51:12 +00:00
int BytesAsRuleSet::memberCount() const
{
return _bind.sub_units.size();
}