为灵活性做改造
This commit is contained in:
parent
4503ee0d88
commit
0e68e0c66e
|
@ -192,7 +192,7 @@ std::shared_ptr<ExtractUnit> TranslateBasic::defaultRule() const
|
||||||
return _default_translate_rule;
|
return _default_translate_rule;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<extract::SizeProvider> TranslateBasic::defaultSizeProvider() const
|
std::shared_ptr<SizeProvider> TranslateBasic::defaultSizeProvider() const
|
||||||
{
|
{
|
||||||
return _default_size_provider;
|
return _default_size_provider;
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ QHash<QString, std::shared_ptr<ExtractUnit>> TranslateBasic::extactors() const
|
||||||
return _extractor_types;
|
return _extractor_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<QString, std::shared_ptr<extract::SizeProvider>> TranslateBasic::sizeProviders() const
|
QHash<QString, std::shared_ptr<SizeProvider>> TranslateBasic::sizeProviders() const
|
||||||
{
|
{
|
||||||
return _size_provider_types;
|
return _size_provider_types;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,24 +9,6 @@
|
||||||
#include "TranslateBasic.h"
|
#include "TranslateBasic.h"
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ·Òë³ÌÐòºËÐÄ
|
|
||||||
/// </summary>
|
|
||||||
class TranslateBasic {
|
|
||||||
private:
|
|
||||||
QHash<QString, std::shared_ptr<ExtractUnit>> _extractor_types;
|
|
||||||
std::shared_ptr<ExtractUnit> _default_translate_rule = nullptr;
|
|
||||||
QHash<QString, std::shared_ptr<extract::SizeProvider>> _size_provider_types;
|
|
||||||
std::shared_ptr<extract::SizeProvider> _default_size_provider = nullptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
TranslateBasic();
|
|
||||||
|
|
||||||
std::shared_ptr<ExtractUnit> defaultRule() const;
|
|
||||||
std::shared_ptr<extract::SizeProvider> defaultSizeProvider() const;
|
|
||||||
QHash<QString, std::shared_ptr<ExtractUnit>> extactors() const;
|
|
||||||
QHash<QString, std::shared_ptr<extract::SizeProvider>> sizeProviders() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -70,7 +52,7 @@ public:
|
||||||
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConstNumberProvider : public extract::SizeProvider {
|
class ConstNumberProvider : public SizeProvider {
|
||||||
private:
|
private:
|
||||||
QString _number_value;
|
QString _number_value;
|
||||||
|
|
||||||
|
|
|
@ -2,3 +2,23 @@
|
||||||
|
|
||||||
#include "extract_basic.h"
|
#include "extract_basic.h"
|
||||||
#include "convert_basic.h"
|
#include "convert_basic.h"
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ·Òë³ÌÐòºËÐÄ
|
||||||
|
/// </summary>
|
||||||
|
class TranslateBasic {
|
||||||
|
private:
|
||||||
|
QHash<QString, std::shared_ptr<ExtractUnit>> _extractor_types;
|
||||||
|
std::shared_ptr<ExtractUnit> _default_translate_rule = nullptr;
|
||||||
|
QHash<QString, std::shared_ptr<SizeProvider>> _size_provider_types;
|
||||||
|
std::shared_ptr<SizeProvider> _default_size_provider = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
TranslateBasic();
|
||||||
|
|
||||||
|
std::shared_ptr<ExtractUnit> defaultRule() const;
|
||||||
|
std::shared_ptr<SizeProvider> defaultSizeProvider() const;
|
||||||
|
QHash<QString, std::shared_ptr<ExtractUnit>> extactors() const;
|
||||||
|
QHash<QString, std::shared_ptr<SizeProvider>> sizeProviders() const;
|
||||||
|
};
|
||||||
|
|
|
@ -3,6 +3,14 @@
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#define NAME(u) #u
|
||||||
|
#define INT32_SAVE(varx) obj[NAME(varx)] = varx;
|
||||||
|
#define INT32_PEAK(varx) varx = obj[NAME(varx)].toInt();
|
||||||
|
#define STRLIST_SAVE(list) obj[NAME(list)] = list.join(";");
|
||||||
|
#define STRLIST_PEAK(list) list = obj[NAME(list)].toString().split(";");
|
||||||
|
#define STRING_PEAK(codec_name) codec_name = obj[NAME(codec_name)].toString();
|
||||||
|
#define STRING_SAVE(codec_name) obj[NAME(codec_name)] = codec_name;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 序列化实体
|
/// 序列化实体
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -6,12 +6,12 @@ using namespace convert;
|
||||||
|
|
||||||
|
|
||||||
AbstractValueConvert::AbstractValueConvert(const QString& nm, DataType in, DataType out)
|
AbstractValueConvert::AbstractValueConvert(const QString& nm, DataType in, DataType out)
|
||||||
: _name_store(nm), _in_type(in), _out_type(out) {
|
: name_store(nm), _in_type(in), _out_type(out) {
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AbstractValueConvert::name() const
|
QString AbstractValueConvert::name() const
|
||||||
{
|
{
|
||||||
return _name_store;
|
return name_store;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataType AbstractValueConvert::inType() const
|
DataType AbstractValueConvert::inType() const
|
||||||
|
|
|
@ -18,7 +18,7 @@ public:
|
||||||
namespace convert {
|
namespace convert {
|
||||||
class AbstractValueConvert : public ValueConvert {
|
class AbstractValueConvert : public ValueConvert {
|
||||||
private:
|
private:
|
||||||
QString _name_store;
|
QString name_store;
|
||||||
DataType _in_type, _out_type;
|
DataType _in_type, _out_type;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -5,55 +5,58 @@ using namespace extract;
|
||||||
|
|
||||||
|
|
||||||
AbstractExtractor::AbstractExtractor(const QString& name, DataType data)
|
AbstractExtractor::AbstractExtractor(const QString& name, DataType data)
|
||||||
:_name_store(name), _type_store(data), _byte_offset(0), _byte_count(1) {
|
{
|
||||||
|
_abs_data.name_store = name;
|
||||||
|
_abs_data.type_store = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AbstractExtractor::setOffsetSpan(int bytes)
|
bool AbstractExtractor::setOffsetSpan(int bytes)
|
||||||
{
|
{
|
||||||
this->_byte_offset = bytes;
|
this->_abs_data.byte_offset = bytes;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AbstractExtractor::setCountWithin(int bytes)
|
bool AbstractExtractor::setCountWithin(int bytes)
|
||||||
{
|
{
|
||||||
this->_byte_count = bytes;
|
this->_abs_data.byte_count = bytes;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AbstractExtractor::name() const
|
QString AbstractExtractor::name() const
|
||||||
{
|
{
|
||||||
return _name_store;
|
return _abs_data.name_store;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataType AbstractExtractor::outType() const
|
DataType AbstractExtractor::outType() const
|
||||||
{
|
{
|
||||||
return _type_store;
|
return _abs_data.type_store;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AbstractExtractor::offsetSpan() const
|
int AbstractExtractor::offsetSpan() const
|
||||||
{
|
{
|
||||||
return _byte_offset;
|
return _abs_data.byte_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AbstractExtractor::countWithin() const
|
int AbstractExtractor::countWithin() const
|
||||||
{
|
{
|
||||||
return this->_byte_count;
|
return this->_abs_data.byte_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractExtractor::loadFrom(const QJsonObject& obj)
|
void AbstractExtractor::loadFrom(const QJsonObject& obj)
|
||||||
{
|
{
|
||||||
INT32_PEAK(_byte_offset);
|
INT32_PEAK(_abs_data.byte_offset);
|
||||||
INT32_PEAK(_byte_count);
|
INT32_PEAK(_abs_data.byte_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AbstractExtractor::saveTo(QJsonObject& obj) const
|
void AbstractExtractor::saveTo(QJsonObject& obj) const
|
||||||
{
|
{
|
||||||
INT32_SAVE(_byte_offset);
|
INT32_SAVE(_abs_data.byte_offset);
|
||||||
INT32_SAVE(_byte_count);
|
INT32_SAVE(_abs_data.byte_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
BytesAsHex::BytesAsHex()
|
BytesAsHex::BytesAsHex()
|
||||||
: AbstractExtractor(NAME(BytesAsHex), DataType::TextString) {
|
: AbstractExtractor(NAME(BytesAsHex), DataType::TextString) {
|
||||||
|
setCountWithin(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant BytesAsHex::parse(const QByteArray& bytes) const
|
QVariant BytesAsHex::parse(const QByteArray& bytes) const
|
||||||
|
@ -79,7 +82,7 @@ BytesAsBitCombine::BytesAsBitCombine()
|
||||||
bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
|
bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
|
||||||
{
|
{
|
||||||
if (bit_index >= 0 && bit_index <= this->countWithin() * 8) {
|
if (bit_index >= 0 && bit_index <= this->countWithin() * 8) {
|
||||||
_switch_options[bit_index] = keyword;
|
_combines._switch_options[bit_index] = keyword;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -87,17 +90,17 @@ bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
|
||||||
|
|
||||||
QHash<int, QString> BytesAsBitCombine::switchOptions() const
|
QHash<int, QString> BytesAsBitCombine::switchOptions() const
|
||||||
{
|
{
|
||||||
return _switch_options;
|
return _combines._switch_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsBitCombine::clearOptions()
|
void BytesAsBitCombine::clearOptions()
|
||||||
{
|
{
|
||||||
_switch_options.clear();
|
_combines._switch_options.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant BytesAsBitCombine::parse(const QByteArray& bytes) const
|
QVariant BytesAsBitCombine::parse(const QByteArray& bytes) const
|
||||||
{
|
{
|
||||||
auto keys = _switch_options.keys();
|
auto keys = _combines._switch_options.keys();
|
||||||
std::sort(keys.begin(), keys.end());
|
std::sort(keys.begin(), keys.end());
|
||||||
|
|
||||||
QString result;
|
QString result;
|
||||||
|
@ -106,9 +109,9 @@ QVariant BytesAsBitCombine::parse(const QByteArray& bytes) const
|
||||||
auto bit_idx = idx % 8;
|
auto bit_idx = idx % 8;
|
||||||
auto char_v = bytes.at(byte_idx);
|
auto char_v = bytes.at(byte_idx);
|
||||||
if (char_v & (0x1u << bit_idx))
|
if (char_v & (0x1u << bit_idx))
|
||||||
result += QString("%1<%2>;").arg(_switch_options[idx], "Y");
|
result += QString("%1<%2>;").arg(_combines._switch_options[idx], "Y");
|
||||||
else
|
else
|
||||||
result += QString("%1<%2>;").arg(_switch_options[idx], "N");
|
result += QString("%1<%2>;").arg(_combines._switch_options[idx], "N");
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -119,10 +122,10 @@ void BytesAsBitCombine::loadFrom(const QJsonObject& obj)
|
||||||
|
|
||||||
QStringList value_list;
|
QStringList value_list;
|
||||||
STRLIST_PEAK(value_list);
|
STRLIST_PEAK(value_list);
|
||||||
_switch_options.clear();
|
_combines._switch_options.clear();
|
||||||
for (auto pair : value_list) {
|
for (auto pair : value_list) {
|
||||||
auto items = pair.split("=");
|
auto items = pair.split("=");
|
||||||
_switch_options[items.first().toInt()] = items.last();
|
_combines._switch_options[items.first().toInt()] = items.last();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,8 +134,9 @@ void BytesAsBitCombine::saveTo(QJsonObject& obj) const
|
||||||
AbstractExtractor::saveTo(obj);
|
AbstractExtractor::saveTo(obj);
|
||||||
|
|
||||||
QStringList value_list;
|
QStringList value_list;
|
||||||
for (auto key : _switch_options.keys()) {
|
for (auto key : _combines._switch_options.keys()) {
|
||||||
auto pair_str = QString("%1=%2").arg(key).arg(_switch_options[key]);
|
auto pair_str = QString("%1=%2").arg(key)
|
||||||
|
.arg(_combines._switch_options[key]);
|
||||||
value_list << pair_str;
|
value_list << pair_str;
|
||||||
}
|
}
|
||||||
STRLIST_SAVE(value_list);
|
STRLIST_SAVE(value_list);
|
||||||
|
@ -168,7 +172,7 @@ QVariant BytesAsInteger::parse(const QByteArray& bytes) const
|
||||||
xbuffer.append(8 - bytes.size(), 0);
|
xbuffer.append(8 - bytes.size(), 0);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
xbuffer.append(8 - bytes.size(), 0xff);
|
xbuffer.append(8 - bytes.size(), static_cast<char>(0xff));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long long value = 0;
|
unsigned long long value = 0;
|
||||||
|
@ -185,22 +189,22 @@ QVariant BytesAsInteger::parse(const QByteArray& bytes) const
|
||||||
|
|
||||||
BytesAsString::BytesAsString()
|
BytesAsString::BytesAsString()
|
||||||
:AbstractExtractor(NAME(BytesAsString), DataType::TextString) {
|
:AbstractExtractor(NAME(BytesAsString), DataType::TextString) {
|
||||||
_conv_with = QTextCodec::codecForName("GBK");
|
_strings._conv_with = QTextCodec::codecForName("GBK");
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsString::setStrCodec(QTextCodec* ins)
|
void BytesAsString::setStrCodec(QTextCodec* ins)
|
||||||
{
|
{
|
||||||
this->_conv_with = ins;
|
this->_strings._conv_with = ins;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString BytesAsString::codecName() const
|
QString BytesAsString::codecName() const
|
||||||
{
|
{
|
||||||
return this->_conv_with->name();
|
return this->_strings._conv_with->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant BytesAsString::parse(const QByteArray& bytes) const
|
QVariant BytesAsString::parse(const QByteArray& bytes) const
|
||||||
{
|
{
|
||||||
return _conv_with->toUnicode(bytes);
|
return _strings._conv_with->toUnicode(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsString::loadFrom(const QJsonObject& obj)
|
void BytesAsString::loadFrom(const QJsonObject& obj)
|
||||||
|
@ -209,7 +213,7 @@ void BytesAsString::loadFrom(const QJsonObject& obj)
|
||||||
|
|
||||||
QString codec_name;
|
QString codec_name;
|
||||||
STRING_PEAK(codec_name);
|
STRING_PEAK(codec_name);
|
||||||
this->_conv_with = QTextCodec::codecForName(codec_name.toLatin1());
|
this->_strings._conv_with = QTextCodec::codecForName(codec_name.toLatin1());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsString::saveTo(QJsonObject& obj) const
|
void BytesAsString::saveTo(QJsonObject& obj) const
|
||||||
|
@ -287,47 +291,31 @@ std::shared_ptr<Serializable> BytesAsUnsigned::newDefault() const
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
bool BytesAsUnion::setSizeProvider(std::shared_ptr<SizeProvider> ins)
|
|
||||||
{
|
|
||||||
this->_provider_ins = ins;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant BytesAsUnion::parse(const QByteArray& bytes) const
|
|
||||||
{
|
|
||||||
throw std::logic_error("The method or operation is not implemented.");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Serializable> BytesAsUnion::newDefault() const
|
|
||||||
{
|
|
||||||
throw std::logic_error("The method or operation is not implemented.");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BytesAsList::setOffsetSpan(int value)
|
bool BytesAsList::setOffsetSpan(int value)
|
||||||
{
|
{
|
||||||
this->_bytes_offset = value;
|
this->_list._bytes_offset = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<ExtractUnit> BytesAsList::elementRule() const
|
std::shared_ptr<ExtractUnit> BytesAsList::elementRule() const
|
||||||
{
|
{
|
||||||
return this->_bind_unit;
|
return this->_list._bind_unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BytesAsList::setElementRule(std::shared_ptr<ExtractUnit> u)
|
bool BytesAsList::setElementRule(std::shared_ptr<ExtractUnit> u)
|
||||||
{
|
{
|
||||||
this->_bind_unit = u;
|
this->_list._bind_unit = u;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<extract::SizeProvider> BytesAsList::sizeProvider() const
|
std::shared_ptr<SizeProvider> BytesAsList::sizeProvider() const
|
||||||
{
|
{
|
||||||
return this->_bind_size_v;
|
return this->_list._bind_size_v;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BytesAsList::setSizeProvider(std::shared_ptr<SizeProvider> ins)
|
bool BytesAsList::setSizeProvider(std::shared_ptr<SizeProvider> ins)
|
||||||
{
|
{
|
||||||
this->_bind_size_v = ins;
|
this->_list._bind_size_v = ins;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,43 +331,44 @@ DataType BytesAsList::outType() const
|
||||||
|
|
||||||
int BytesAsList::offsetSpan() const
|
int BytesAsList::offsetSpan() const
|
||||||
{
|
{
|
||||||
return _bytes_offset;
|
return _list._bytes_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BytesAsList::countWithin() const
|
int BytesAsList::countWithin() const
|
||||||
{
|
{
|
||||||
auto expr = this->_bind_size_v->expression();
|
auto expr = this->_list._bind_size_v->expression();
|
||||||
if (this->_bind_size_v->value(expr) < 0) {
|
if (this->_list._bind_size_v->value(expr) < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this->_bind_size_v->value(expr) * this->_bind_unit->countWithin();
|
return this->_list._bind_size_v->value(expr)
|
||||||
|
* this->_list._bind_unit->countWithin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsList::loadFrom(const QJsonObject& obj)
|
void BytesAsList::loadFrom(const QJsonObject& obj)
|
||||||
{
|
{
|
||||||
INT32_PEAK(_bytes_offset);
|
INT32_PEAK(_list._bytes_offset);
|
||||||
|
|
||||||
QString bind_provider_expr = "";
|
QString bind_provider_expr = "";
|
||||||
STRING_SAVE(bind_provider_expr);
|
STRING_SAVE(bind_provider_expr);
|
||||||
if (this->_bind_size_v)
|
if (this->_list._bind_size_v)
|
||||||
this->_bind_size_v->setExpression(bind_provider_expr);
|
this->_list._bind_size_v->setExpression(bind_provider_expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytesAsList::saveTo(QJsonObject& obj) const
|
void BytesAsList::saveTo(QJsonObject& obj) const
|
||||||
{
|
{
|
||||||
QString bind_unit_name = "";
|
QString bind_unit_name = "";
|
||||||
if (this->_bind_unit)
|
if (this->_list._bind_unit)
|
||||||
bind_unit_name = this->_bind_unit->name();
|
bind_unit_name = this->_list._bind_unit->name();
|
||||||
STRING_SAVE(bind_unit_name);
|
STRING_SAVE(bind_unit_name);
|
||||||
|
|
||||||
INT32_SAVE(_bytes_offset);
|
INT32_SAVE(_list._bytes_offset);
|
||||||
|
|
||||||
QString bind_size_provider_name = "";
|
QString bind_size_provider_name = "";
|
||||||
QString bind_provider_expr = "";
|
QString bind_provider_expr = "";
|
||||||
if (this->_bind_size_v) {
|
if (this->_list._bind_size_v) {
|
||||||
bind_provider_expr = this->_bind_size_v->expression();
|
bind_provider_expr = this->_list._bind_size_v->expression();
|
||||||
bind_size_provider_name = this->_bind_size_v->name();
|
bind_size_provider_name = this->_list._bind_size_v->name();
|
||||||
}
|
}
|
||||||
STRING_SAVE(bind_size_provider_name);
|
STRING_SAVE(bind_size_provider_name);
|
||||||
STRING_SAVE(bind_provider_expr);
|
STRING_SAVE(bind_provider_expr);
|
||||||
|
@ -389,13 +378,13 @@ QVariant BytesAsList::parse(const QByteArray& bytes) const
|
||||||
{
|
{
|
||||||
QVariantList value_list;
|
QVariantList value_list;
|
||||||
|
|
||||||
auto expr = this->_bind_size_v->expression();
|
auto expr = this->_list._bind_size_v->expression();
|
||||||
auto size_value = this->_bind_size_v->value(expr);
|
auto size_value = this->_list._bind_size_v->value(expr);
|
||||||
auto unit_size = this->_bind_unit->countWithin();
|
auto unit_size = this->_list._bind_unit->countWithin();
|
||||||
|
|
||||||
for (auto idx = 0; idx < size_value; ++idx) {
|
for (auto idx = 0; idx < size_value; ++idx) {
|
||||||
auto secn = bytes.mid(unit_size * idx, unit_size);
|
auto secn = bytes.mid(unit_size * idx, unit_size);
|
||||||
auto value_v = this->_bind_unit->parse(secn);
|
auto value_v = this->_list._bind_unit->parse(secn);
|
||||||
value_list << value_v;
|
value_list << value_v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,3 +395,76 @@ std::shared_ptr<Serializable> BytesAsList::newDefault() const
|
||||||
{
|
{
|
||||||
return std::make_shared<BytesAsList>();
|
return std::make_shared<BytesAsList>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant BytesAsUnion::parse(const QByteArray& bytes) const
|
||||||
|
{
|
||||||
|
for (auto u : this->elementRules())
|
||||||
|
if (u->checkpass(bytes))
|
||||||
|
return u->bindRule()->parse(bytes);
|
||||||
|
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BytesAsUnion::loadFrom(const QJsonObject& obj)
|
||||||
|
{
|
||||||
|
throw std::logic_error("The method or operation is not implemented.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void BytesAsUnion::saveTo(QJsonObject& obj) const
|
||||||
|
{
|
||||||
|
INT32_SAVE(_union.byte_count);
|
||||||
|
INT32_SAVE(_union.byte_offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
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::setElementRule(std::shared_ptr<RuleMatch> u)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 翻译单元抽象接口
|
/// 翻译单元抽象接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -46,14 +45,42 @@ public:
|
||||||
virtual QVariant parse(const QByteArray& bytes) const = 0;
|
virtual QVariant parse(const QByteArray& bytes) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尺寸解析器
|
||||||
|
/// </summary>
|
||||||
|
class SizeProvider {
|
||||||
|
public:
|
||||||
|
virtual QString name() const = 0;
|
||||||
|
/// <summary>
|
||||||
|
/// 获取值,如果该值无法预先计算得知,返回-1
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>尺寸值</returns>
|
||||||
|
virtual int32_t value(const QString& expr) const = 0;
|
||||||
|
|
||||||
|
virtual void setExpression(const QString& expr) = 0;
|
||||||
|
virtual QString expression() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 规则匹配器
|
||||||
|
/// </summary>
|
||||||
|
class RuleMatch {
|
||||||
|
public:
|
||||||
|
virtual QString name() const = 0;
|
||||||
|
virtual std::shared_ptr<ExtractUnit> bindRule() const = 0;
|
||||||
|
virtual bool checkpass(const QByteArray& buffer) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
namespace extract {
|
namespace extract {
|
||||||
class AbstractExtractor : public ExtractUnit {
|
class AbstractExtractor : public ExtractUnit {
|
||||||
private:
|
|
||||||
QString _name_store;
|
|
||||||
DataType _type_store;
|
|
||||||
int _byte_offset, _byte_count;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
struct __Private {
|
||||||
|
QString name_store = "";
|
||||||
|
DataType type_store = DataType::Integer;
|
||||||
|
int byte_offset = 0, byte_count = 1;
|
||||||
|
} _abs_data;
|
||||||
|
|
||||||
AbstractExtractor(const QString& name, DataType data);
|
AbstractExtractor(const QString& name, DataType data);
|
||||||
virtual bool setCountWithin(int bytes);
|
virtual bool setCountWithin(int bytes);
|
||||||
|
|
||||||
|
@ -73,14 +100,6 @@ namespace extract {
|
||||||
void saveTo(QJsonObject& obj) const override;
|
void saveTo(QJsonObject& obj) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define NAME(u) #u
|
|
||||||
#define INT32_SAVE(varx) obj[NAME(varx)] = varx;
|
|
||||||
#define INT32_PEAK(varx) varx = obj[NAME(varx)].toInt();
|
|
||||||
#define STRLIST_SAVE(list) obj[NAME(list)] = list.join(";");
|
|
||||||
#define STRLIST_PEAK(list) list = obj[NAME(list)].toString().split(";");
|
|
||||||
#define STRING_PEAK(codec_name) codec_name = obj[NAME(codec_name)].toString();
|
|
||||||
#define STRING_SAVE(codec_name) obj[NAME(codec_name)] = codec_name;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转换源数值未16进制数值,默认分割方式
|
/// 转换源数值未16进制数值,默认分割方式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -96,10 +115,11 @@ namespace extract {
|
||||||
/// 转换源数值为位联合
|
/// 转换源数值为位联合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class BytesAsBitCombine : public AbstractExtractor {
|
class BytesAsBitCombine : public AbstractExtractor {
|
||||||
private:
|
|
||||||
QHash<int, QString> _switch_options;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
struct __Private {
|
||||||
|
QHash<int, QString> _switch_options;
|
||||||
|
} _combines;
|
||||||
|
|
||||||
BytesAsBitCombine();
|
BytesAsBitCombine();
|
||||||
|
|
||||||
bool setSwitchOption(int bit_index, const QString& keyword);
|
bool setSwitchOption(int bit_index, const QString& keyword);
|
||||||
|
@ -129,6 +149,7 @@ namespace extract {
|
||||||
// Serializable ==============================
|
// Serializable ==============================
|
||||||
std::shared_ptr<Serializable> newDefault() const override;
|
std::shared_ptr<Serializable> newDefault() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转换源数据为整形
|
/// 转换源数据为整形
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -148,8 +169,10 @@ namespace extract {
|
||||||
/// 转换源数据为字符串
|
/// 转换源数据为字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class BytesAsString : public AbstractExtractor {
|
class BytesAsString : public AbstractExtractor {
|
||||||
private:
|
public:
|
||||||
|
struct __Private {
|
||||||
QTextCodec* _conv_with = nullptr;
|
QTextCodec* _conv_with = nullptr;
|
||||||
|
} _strings;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BytesAsString();
|
BytesAsString();
|
||||||
|
@ -182,28 +205,14 @@ namespace extract {
|
||||||
std::shared_ptr<Serializable> newDefault() const override;
|
std::shared_ptr<Serializable> newDefault() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尺寸解析器
|
|
||||||
/// </summary>
|
|
||||||
class SizeProvider {
|
|
||||||
public:
|
|
||||||
virtual QString name() const = 0;
|
|
||||||
/// <summary>
|
|
||||||
/// 获取值,如果该值无法预先计算得知,返回-1
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>尺寸值</returns>
|
|
||||||
virtual int32_t value(const QString& expr) const = 0;
|
|
||||||
|
|
||||||
virtual void setExpression(const QString& expr) = 0;
|
|
||||||
virtual QString expression() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BytesAsList : public ExtractUnit {
|
class BytesAsList : public ExtractUnit {
|
||||||
private:
|
public:
|
||||||
|
struct __Private {
|
||||||
std::shared_ptr<ExtractUnit> _bind_unit = nullptr;
|
std::shared_ptr<ExtractUnit> _bind_unit = nullptr;
|
||||||
int _bytes_offset = 0;
|
int _bytes_offset = 0;
|
||||||
|
|
||||||
std::shared_ptr<SizeProvider> _bind_size_v = nullptr;
|
std::shared_ptr<SizeProvider> _bind_size_v = nullptr;
|
||||||
|
} _list;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool setOffsetSpan(int value);
|
bool setOffsetSpan(int value);
|
||||||
|
@ -213,7 +222,6 @@ namespace extract {
|
||||||
bool setSizeProvider(std::shared_ptr<SizeProvider> ins);
|
bool setSizeProvider(std::shared_ptr<SizeProvider> ins);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
QString name() const override;
|
QString name() const override;
|
||||||
DataType outType() const override;
|
DataType outType() const override;
|
||||||
int offsetSpan() const override;
|
int offsetSpan() const override;
|
||||||
|
@ -227,22 +235,31 @@ namespace extract {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class RuleMatch {
|
class BytesAsUnion : public ExtractUnit {
|
||||||
public:
|
public:
|
||||||
virtual std::shared_ptr<ExtractUnit> bindRule() const = 0;
|
struct __Private {
|
||||||
virtual bool checkpass() const = 0;
|
int byte_offset, byte_count;
|
||||||
};
|
QList<std::shared_ptr<RuleMatch>> _rule_list;
|
||||||
class BytesAsUnion : public AbstractExtractor {
|
} _union;
|
||||||
protected:
|
|
||||||
std::shared_ptr<SizeProvider> _provider_ins = nullptr;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool setSizeProvider(std::shared_ptr<SizeProvider> ins);
|
bool setCountWithin(int bytes);
|
||||||
|
|
||||||
|
void clearRules();
|
||||||
|
QList<std::shared_ptr<RuleMatch>> elementRules() const;
|
||||||
|
bool setElementRule(std::shared_ptr<RuleMatch> u);
|
||||||
|
|
||||||
|
public:
|
||||||
|
QString name() const override;
|
||||||
|
DataType outType() const override;
|
||||||
|
bool setOffsetSpan(int value);
|
||||||
|
int offsetSpan() const override;
|
||||||
|
int countWithin() const override;
|
||||||
|
|
||||||
QVariant parse(const QByteArray& bytes) const override;
|
QVariant parse(const QByteArray& bytes) const override;
|
||||||
|
void loadFrom(const QJsonObject& obj) override;
|
||||||
|
|
||||||
|
void saveTo(QJsonObject& obj) const override;
|
||||||
std::shared_ptr<Serializable> newDefault() const override;
|
std::shared_ptr<Serializable> newDefault() const override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue