From 9ba8763873b2618c2aa9133744b9325d4c36e096 Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Fri, 1 Aug 2025 09:47:41 +0800 Subject: [PATCH] update --- TranslateUI/TranslateBasic.cpp | 360 ------------------------ TranslateUI/TranslateBasic.h | 281 +----------------- TranslateUI/TranslateUI.vcxproj | 5 + TranslateUI/TranslateUI.vcxproj.filters | 15 + TranslateUI/common.h | 42 +++ TranslateUI/convert_basic.cpp | 72 +++++ TranslateUI/convert_basic.h | 49 ++++ TranslateUI/extract_basic.cpp | 325 +++++++++++++++++++++ TranslateUI/extract_basic.h | 246 ++++++++++++++++ 9 files changed, 756 insertions(+), 639 deletions(-) create mode 100644 TranslateUI/common.h create mode 100644 TranslateUI/convert_basic.cpp create mode 100644 TranslateUI/convert_basic.h create mode 100644 TranslateUI/extract_basic.cpp create mode 100644 TranslateUI/extract_basic.h diff --git a/TranslateUI/TranslateBasic.cpp b/TranslateUI/TranslateBasic.cpp index 0dee80d..86e0077 100644 --- a/TranslateUI/TranslateBasic.cpp +++ b/TranslateUI/TranslateBasic.cpp @@ -1,361 +1 @@ #include "TranslateBasic.h" - -using namespace extract; -using namespace convert; - -AbstractExtractor::AbstractExtractor(const QString& name, DataType data) - :_name_store(name), _type_store(data), _byte_offset(0), _byte_count(0) { -} - -bool AbstractExtractor::setOffsetSpan(int bytes) -{ - this->_byte_offset = bytes; - return true; -} - -bool AbstractExtractor::setCountWithin(int bytes) -{ - this->_byte_count = bytes; - return true; -} - -QString AbstractExtractor::name() const -{ - return _name_store; -} - -DataType AbstractExtractor::outType() const -{ - return _type_store; -} - -int AbstractExtractor::offsetSpan() const -{ - return _byte_offset; -} - -int AbstractExtractor::countWithin() const -{ - return _byte_count; -} - -void AbstractExtractor::loadFrom(const QJsonObject& obj) -{ - INT32_PEAK(_byte_offset); - INT32_PEAK(_byte_count); -} - -void AbstractExtractor::saveTo(QJsonObject& obj) const -{ - INT32_SAVE(_byte_count); - INT32_SAVE(_byte_offset); -} - -BytesAsHex::BytesAsHex() - : AbstractExtractor(NAME(BytesAsHex), DataType::TextString) { -} - -QVariant BytesAsHex::parse(const QByteArray& bytes) const -{ - QString result; - for (auto char_v : bytes) { - result += QString("%1").arg(char_v, 2, 16, QChar('0')); - } - return result; -} - -std::shared_ptr BytesAsHex::newDefault() const -{ - return std::make_shared(); -} - -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) { - _switch_options[bit_index] = keyword; - return true; - } - return false; -} - -QHash BytesAsBitCombine::switchOptions() const -{ - return _switch_options; -} - -void BytesAsBitCombine::clearOptions() -{ - _switch_options.clear(); -} - -QVariant BytesAsBitCombine::parse(const QByteArray& bytes) const -{ - auto keys = _switch_options.keys(); - 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(_switch_options[idx], "Y"); - else - result += QString("%1<%2>;").arg(_switch_options[idx], "N"); - } - return result; -} - -void BytesAsBitCombine::loadFrom(const QJsonObject& obj) -{ - AbstractExtractor::loadFrom(obj); - - QStringList value_list; - STRLIST_PEAK(value_list); - _switch_options.clear(); - for (auto pair : value_list) { - auto items = pair.split("="); - _switch_options[items.first().toInt()] = items.last(); - } -} - -void BytesAsBitCombine::saveTo(QJsonObject& obj) const -{ - AbstractExtractor::saveTo(obj); - - QStringList value_list; - for (auto key : _switch_options.keys()) { - auto pair = QString("%1=%2").arg(key).arg(_switch_options[key]); - } - STRLIST_SAVE(value_list); -} - -std::shared_ptr BytesAsBitCombine::newDefault() const -{ - return std::make_shared(); -} - -std::shared_ptr BytesAsInteger::newDefault() const -{ - return std::make_shared(); -} - -BytesAsInteger::BytesAsInteger() - :AbstractExtractor(NAME(BytesAsInteger), DataType::Integer) { -} - -bool BytesAsInteger::setCountWithin(int bytes) -{ - bytes = std::min(8, std::max(bytes, 0)); - return AbstractExtractor::setCountWithin(bytes); -} - -QVariant BytesAsInteger::parse(const QByteArray& bytes) const -{ - 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 { - xbuffer.append(8 - bytes.size(), 0xff); - } - - 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; - } - return *((long long*)(&value)); -} - - -#include - -BytesAsString::BytesAsString() - :AbstractExtractor(NAME(BytesAsString), DataType::TextString) { - _conv_with = QTextCodec::codecForName("GBK"); -} - -void BytesAsString::setStrCodec(QTextCodec* ins) -{ - this->_conv_with = ins; -} - -QString BytesAsString::codecName() const -{ - return this->_conv_with->name(); -} - -QVariant BytesAsString::parse(const QByteArray& bytes) const -{ - return _conv_with->toUnicode(bytes); -} - -void BytesAsString::loadFrom(const QJsonObject& obj) -{ - AbstractExtractor::loadFrom(obj); - - QString codec_name; - STRING_PEAK(codec_name); - this->_conv_with = QTextCodec::codecForName(codec_name.toLatin1()); -} - -void BytesAsString::saveTo(QJsonObject& obj) const -{ - AbstractExtractor::saveTo(obj); - - auto codec_name = this->codecName(); - STRING_SAVE(codec_name); -} - -std::shared_ptr BytesAsString::newDefault() const -{ - return std::make_shared(); -} - -AbstractValueConvert::AbstractValueConvert(const QString& nm, DataType in, DataType out) - : _name_store(nm), _in_type(in), _out_type(out) { -} - -QString AbstractValueConvert::name() const -{ - return _name_store; -} - -DataType AbstractValueConvert::inType() const -{ - return _in_type; -} - -DataType AbstractValueConvert::outType() const -{ - return _out_type; -} - -void AbstractValueConvert::loadFrom(const QJsonObject& obj) -{ - auto int_in_type = (int)_in_type; - INT32_PEAK(int_in_type); - _in_type = (DataType)int_in_type; - - auto int_out_type = (int)_out_type; - INT32_PEAK(int_out_type); - _out_type = (DataType)int_out_type; -} - -void AbstractValueConvert::saveTo(QJsonObject& obj) const -{ - auto int_in_type = (int)_in_type; - INT32_SAVE(int_in_type); - - auto int_out_type = (int)_out_type; - INT32_SAVE(int_out_type); -} - -DoubleWithLSB::DoubleWithLSB() - :AbstractValueConvert(NAME(DoubleWithLSB), DataType::Integer, DataType::Dbl64), - _lsb_value(1) {} - -void DoubleWithLSB::setLSB(double lsbv) -{ - this->_lsb_value = lsbv; -} - -double DoubleWithLSB::getLSB() const -{ - return this->_lsb_value; -} - -QVariant DoubleWithLSB::convert(const QVariant& value) const -{ - if (value.canConvert()) - return value.toLongLong() * this->_lsb_value; - return value.toULongLong() * this->_lsb_value; -} - -std::shared_ptr DoubleWithLSB::newDefault() const -{ - return std::make_shared(); -} - -QVariant BytesAsFloat::parse(const QByteArray& bytes) const -{ - return *((float*)bytes.data()); -} - -std::shared_ptr BytesAsFloat::newDefault() const -{ - return std::make_shared(); -} - -bool BytesAsFloat::setCountWithin(int bytes) -{ - AbstractExtractor::setCountWithin(4); - return true; -} - -BytesAsFloat::BytesAsFloat() - :AbstractExtractor(NAME(BytesAsFloat), DataType::Flt32) -{ - -} - -std::shared_ptr BytesAsDouble::newDefault() const -{ - return std::make_shared(); -} - -QVariant BytesAsDouble::parse(const QByteArray& bytes) const -{ - return *((double*)bytes.data()); -} - -bool BytesAsDouble::setCountWithin(int bytes) -{ - AbstractExtractor::setCountWithin(8); - return true; -} - -BytesAsDouble::BytesAsDouble() - :AbstractExtractor(NAME(BytesAsDouble), DataType::Dbl64) -{ - -} - -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); -} - -QVariant BytesAsUnsigned::parse(const QByteArray& bytes) const -{ - 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; - } - return value; -} - -std::shared_ptr BytesAsUnsigned::newDefault() const -{ - return std::make_shared(); -} diff --git a/TranslateUI/TranslateBasic.h b/TranslateUI/TranslateBasic.h index 7e832ca..87ea7f8 100644 --- a/TranslateUI/TranslateBasic.h +++ b/TranslateUI/TranslateBasic.h @@ -1,281 +1,4 @@ #pragma once -#include -#include -#include -#include -/// -/// 序列化实体 -/// -class Serializable { -public: - virtual ~Serializable() = default; - /// - /// 反序列化 - /// - /// - virtual void loadFrom(const QJsonObject& obj) = 0; - /// - /// 序列化 - /// - /// - virtual void saveTo(QJsonObject& obj) const = 0; - /// - /// 创建默认副本 - /// - /// - virtual std::shared_ptr newDefault() const = 0; -}; - - - -/// -/// 内部使用的数据类型 -/// -enum class DataType { - TextString, - Integer, // 整形 - Unsigned,// 无符号整形 - Flt32, - Dbl64, -}; - -/// -/// 翻译单元抽象接口 -/// -class ExtractUnit : public Serializable { -public: - virtual ~ExtractUnit() = default; - - /// - /// 单元名称 - /// - /// - virtual QString name() const = 0; - - /// - /// - /// - /// - virtual DataType outType() const = 0; - - /// - /// 从上一个单元字节偏移字节数量 - /// - /// - virtual int offsetSpan() const = 0; - /// - /// 解析所用的字节数量 - /// - /// - virtual int countWithin() const = 0; - - /// - /// 执行解析过程 - /// - /// - /// - virtual QVariant parse(const QByteArray& bytes) const = 0; -}; - -namespace extract { - class AbstractExtractor : public ExtractUnit { - private: - QString _name_store; - DataType _type_store; - int _byte_offset, _byte_count; - - public: - AbstractExtractor(const QString& name, DataType data); - /// - /// 设置偏移字节数量 - /// - /// - virtual bool setOffsetSpan(int bytes); - /// - /// 设置解析字节数量 - /// - /// - virtual bool setCountWithin(int bytes); - - // ExtractUnit =========================== - QString name() const override; - virtual DataType outType() const; - int offsetSpan() const override; - int countWithin() const override; - - // Serializable ============================== - void loadFrom(const QJsonObject& obj) 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; - - /// - /// 转换源数值未16进制数值,默认分割方式 - /// - class BytesAsHex : public AbstractExtractor { - public: - BytesAsHex(); - - QVariant parse(const QByteArray& bytes) const override; - std::shared_ptr newDefault() const override; - }; - - /// - /// 转换源数值为位联合 - /// - class BytesAsBitCombine : public AbstractExtractor { - private: - QHash _switch_options; - - public: - BytesAsBitCombine(); - - bool setSwitchOption(int bit_index, const QString& keyword); - QHash switchOptions() const; - virtual void clearOptions(); - - // ExtractUnit ============================ - QVariant parse(const QByteArray& bytes) const override; - - // Serializable ============================== - void loadFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; - std::shared_ptr newDefault() const override; - }; - - /// - /// 转换源数据为整形 - /// - class BytesAsInteger : public AbstractExtractor { - public: - BytesAsInteger(); - - // ExtractUnit ============================ - bool setCountWithin(int bytes) override; - QVariant parse(const QByteArray& bytes) const override; - - // Serializable ============================== - std::shared_ptr newDefault() const override; - }; - /// - /// 转换源数据为整形 - /// - class BytesAsUnsigned : public AbstractExtractor { - public: - BytesAsUnsigned(); - - // ExtractUnit ============================ - bool setCountWithin(int bytes) override; - QVariant parse(const QByteArray& bytes) const override; - - // Serializable ============================== - std::shared_ptr newDefault() const override; - }; - - /// - /// 转换源数据为字符串 - /// - class BytesAsString : public AbstractExtractor { - private: - QTextCodec* _conv_with = nullptr; - - public: - BytesAsString(); - - void setStrCodec(QTextCodec* ins); - QString codecName() const; - - // ExtractUnit ============================ - QVariant parse(const QByteArray& bytes) const override; - - // Serializable ============================== - void loadFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; - std::shared_ptr newDefault() const override; - }; - - class BytesAsFloat : public AbstractExtractor { - public: - BytesAsFloat(); - - /// - /// 设置解析字节数量 - /// - /// - virtual bool setCountWithin(int bytes) override; - - QVariant parse(const QByteArray& bytes) const override; - std::shared_ptr newDefault() const override; - }; - - class BytesAsDouble : public AbstractExtractor { - public: - BytesAsDouble(); - - /// - /// 设置解析字节数量 - /// - /// - virtual bool setCountWithin(int bytes) override; - - QVariant parse(const QByteArray& bytes) const override; - std::shared_ptr newDefault() const override; - }; -} - - - - -/// -/// 数值转换抽象接口 -/// -class ValueConvert : public Serializable { -public: - virtual ~ValueConvert() = default; - - virtual QString name() const = 0; - virtual DataType inType() const = 0; - virtual DataType outType() const = 0; - virtual QVariant convert(const QVariant& value) const = 0; -}; - -namespace convert { - class AbstractValueConvert : public ValueConvert { - private: - QString _name_store; - DataType _in_type, _out_type; - - public: - AbstractValueConvert(const QString& nm, DataType in, DataType out); - - QString name() const override; - DataType inType() const override; - DataType outType() const override; - - void loadFrom(const QJsonObject& obj) override; - void saveTo(QJsonObject& obj) const override; - }; - - - class DoubleWithLSB : public AbstractValueConvert { - private: - double _lsb_value; - - public: - DoubleWithLSB(); - - void setLSB(double lsbv); - double getLSB() const; - - QVariant convert(const QVariant& value) const override; - std::shared_ptr newDefault() const override; - }; -} \ No newline at end of file +#include "extract_basic.h" +#include "convert_basic.h" \ No newline at end of file diff --git a/TranslateUI/TranslateUI.vcxproj b/TranslateUI/TranslateUI.vcxproj index ded359c..478c830 100644 --- a/TranslateUI/TranslateUI.vcxproj +++ b/TranslateUI/TranslateUI.vcxproj @@ -92,6 +92,8 @@ + + @@ -103,6 +105,9 @@ + + + diff --git a/TranslateUI/TranslateUI.vcxproj.filters b/TranslateUI/TranslateUI.vcxproj.filters index c15eff0..6140766 100644 --- a/TranslateUI/TranslateUI.vcxproj.filters +++ b/TranslateUI/TranslateUI.vcxproj.filters @@ -52,6 +52,12 @@ Source Files + + Source Files + + + Source Files + @@ -63,6 +69,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + diff --git a/TranslateUI/common.h b/TranslateUI/common.h new file mode 100644 index 0000000..95f6f73 --- /dev/null +++ b/TranslateUI/common.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +/// +/// 序列化实体 +/// +class Serializable { +public: + virtual ~Serializable() = default; + /// + /// 反序列化 + /// + /// + virtual void loadFrom(const QJsonObject& obj) = 0; + /// + /// 序列化 + /// + /// + virtual void saveTo(QJsonObject& obj) const = 0; + /// + /// 创建默认副本 + /// + /// + virtual std::shared_ptr newDefault() const = 0; +}; + + + +/// +/// 内部使用的数据类型 +/// +enum class DataType { + TextString, + Integer, // 整形 + Unsigned,// 无符号整形 + Flt32, + Dbl64, + LIST_COLLECTION, + UNION_COMBINATE, +}; diff --git a/TranslateUI/convert_basic.cpp b/TranslateUI/convert_basic.cpp new file mode 100644 index 0000000..19e449a --- /dev/null +++ b/TranslateUI/convert_basic.cpp @@ -0,0 +1,72 @@ +#include "convert_basic.h" +#include "extract_basic.h" +#include + +using namespace convert; + + +AbstractValueConvert::AbstractValueConvert(const QString& nm, DataType in, DataType out) + : _name_store(nm), _in_type(in), _out_type(out) { +} + +QString AbstractValueConvert::name() const +{ + return _name_store; +} + +DataType AbstractValueConvert::inType() const +{ + return _in_type; +} + +DataType AbstractValueConvert::outType() const +{ + return _out_type; +} + +void AbstractValueConvert::loadFrom(const QJsonObject& obj) +{ + auto int_in_type = (int)_in_type; + INT32_PEAK(int_in_type); + _in_type = (DataType)int_in_type; + + auto int_out_type = (int)_out_type; + INT32_PEAK(int_out_type); + _out_type = (DataType)int_out_type; +} + +void AbstractValueConvert::saveTo(QJsonObject& obj) const +{ + auto int_in_type = (int)_in_type; + INT32_SAVE(int_in_type); + + auto int_out_type = (int)_out_type; + INT32_SAVE(int_out_type); +} + +DoubleWithLSB::DoubleWithLSB() + :AbstractValueConvert(NAME(DoubleWithLSB), DataType::Integer, DataType::Dbl64), + _lsb_value(1) { +} + +void DoubleWithLSB::setLSB(double lsbv) +{ + this->_lsb_value = lsbv; +} + +double DoubleWithLSB::getLSB() const +{ + return this->_lsb_value; +} + +QVariant DoubleWithLSB::convert(const QVariant& value) const +{ + if (value.canConvert()) + return value.toLongLong() * this->_lsb_value; + return value.toULongLong() * this->_lsb_value; +} + +std::shared_ptr DoubleWithLSB::newDefault() const +{ + return std::make_shared(); +} diff --git a/TranslateUI/convert_basic.h b/TranslateUI/convert_basic.h new file mode 100644 index 0000000..1572969 --- /dev/null +++ b/TranslateUI/convert_basic.h @@ -0,0 +1,49 @@ +#pragma once + +#include "common.h" + +/// +/// 数值转换抽象接口 +/// +class ValueConvert : public Serializable { +public: + virtual ~ValueConvert() = default; + + virtual QString name() const = 0; + virtual DataType inType() const = 0; + virtual DataType outType() const = 0; + virtual QVariant convert(const QVariant& value) const = 0; +}; + +namespace convert { + class AbstractValueConvert : public ValueConvert { + private: + QString _name_store; + DataType _in_type, _out_type; + + public: + AbstractValueConvert(const QString& nm, DataType in, DataType out); + + QString name() const override; + DataType inType() const override; + DataType outType() const override; + + void loadFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; + }; + + + class DoubleWithLSB : public AbstractValueConvert { + private: + double _lsb_value; + + public: + DoubleWithLSB(); + + void setLSB(double lsbv); + double getLSB() const; + + QVariant convert(const QVariant& value) const override; + std::shared_ptr newDefault() const override; + }; +} diff --git a/TranslateUI/extract_basic.cpp b/TranslateUI/extract_basic.cpp new file mode 100644 index 0000000..1270e2b --- /dev/null +++ b/TranslateUI/extract_basic.cpp @@ -0,0 +1,325 @@ +#include "extract_basic.h" +#include + +using namespace extract; + + +AbstractExtractor::AbstractExtractor(const QString& name, DataType data) + :_name_store(name), _type_store(data), _byte_offset(0), _byte_count(0) { +} + +bool AbstractExtractor::setOffsetSpan(int bytes) +{ + this->_byte_offset = bytes; + return true; +} + +bool AbstractExtractor::setCountWithin(int bytes) +{ + this->_byte_count = bytes; + return true; +} + +QString AbstractExtractor::name() const +{ + return _name_store; +} + +DataType AbstractExtractor::outType() const +{ + return _type_store; +} + +int AbstractExtractor::offsetSpan() const +{ + return _byte_offset; +} + +int AbstractExtractor::countWithin() const +{ + return this->_byte_count; +} + +void AbstractExtractor::loadFrom(const QJsonObject& obj) +{ + INT32_PEAK(_byte_offset); +} + +void AbstractExtractor::saveTo(QJsonObject& obj) const +{ + INT32_SAVE(_byte_offset); +} + +BytesAsHex::BytesAsHex() + : AbstractExtractor(NAME(BytesAsHex), DataType::TextString) { +} + +QVariant BytesAsHex::parse(const QByteArray& bytes) const +{ + QString result; + for (auto char_v : bytes) { + result += QString("%1").arg(char_v, 2, 16, QChar('0')); + } + return result; +} + +std::shared_ptr BytesAsHex::newDefault() const +{ + return std::make_shared(); +} + +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) { + _switch_options[bit_index] = keyword; + return true; + } + return false; +} + +QHash BytesAsBitCombine::switchOptions() const +{ + return _switch_options; +} + +void BytesAsBitCombine::clearOptions() +{ + _switch_options.clear(); +} + +QVariant BytesAsBitCombine::parse(const QByteArray& bytes) const +{ + auto keys = _switch_options.keys(); + 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(_switch_options[idx], "Y"); + else + result += QString("%1<%2>;").arg(_switch_options[idx], "N"); + } + return result; +} + +void BytesAsBitCombine::loadFrom(const QJsonObject& obj) +{ + AbstractExtractor::loadFrom(obj); + + QStringList value_list; + STRLIST_PEAK(value_list); + _switch_options.clear(); + for (auto pair : value_list) { + auto items = pair.split("="); + _switch_options[items.first().toInt()] = items.last(); + } +} + +void BytesAsBitCombine::saveTo(QJsonObject& obj) const +{ + AbstractExtractor::saveTo(obj); + + QStringList value_list; + for (auto key : _switch_options.keys()) { + auto pair = QString("%1=%2").arg(key).arg(_switch_options[key]); + } + STRLIST_SAVE(value_list); +} + +std::shared_ptr BytesAsBitCombine::newDefault() const +{ + return std::make_shared(); +} + +std::shared_ptr BytesAsInteger::newDefault() const +{ + return std::make_shared(); +} + +BytesAsInteger::BytesAsInteger() + :AbstractExtractor(NAME(BytesAsInteger), DataType::Integer) { +} + +bool BytesAsInteger::setCountWithin(int bytes) +{ + bytes = std::min(8, std::max(bytes, 0)); + return AbstractExtractor::setCountWithin(bytes); +} + +QVariant BytesAsInteger::parse(const QByteArray& bytes) const +{ + 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 { + xbuffer.append(8 - bytes.size(), 0xff); + } + + 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; + } + return *((long long*)(&value)); +} + + +#include + +BytesAsString::BytesAsString() + :AbstractExtractor(NAME(BytesAsString), DataType::TextString) { + _conv_with = QTextCodec::codecForName("GBK"); +} + +void BytesAsString::setStrCodec(QTextCodec* ins) +{ + this->_conv_with = ins; +} + +QString BytesAsString::codecName() const +{ + return this->_conv_with->name(); +} + +QVariant BytesAsString::parse(const QByteArray& bytes) const +{ + return _conv_with->toUnicode(bytes); +} + +void BytesAsString::loadFrom(const QJsonObject& obj) +{ + AbstractExtractor::loadFrom(obj); + + QString codec_name; + STRING_PEAK(codec_name); + this->_conv_with = QTextCodec::codecForName(codec_name.toLatin1()); +} + +void BytesAsString::saveTo(QJsonObject& obj) const +{ + AbstractExtractor::saveTo(obj); + + auto codec_name = this->codecName(); + STRING_SAVE(codec_name); +} + +std::shared_ptr BytesAsString::newDefault() const +{ + return std::make_shared(); +} + +QVariant BytesAsFloat::parse(const QByteArray& bytes) const +{ + return *((float*)bytes.data()); +} + +std::shared_ptr BytesAsFloat::newDefault() const +{ + return std::make_shared(); +} + +bool BytesAsFloat::setCountWithin(int bytes) +{ + AbstractExtractor::setCountWithin(4); + return true; +} + +BytesAsFloat::BytesAsFloat() + :AbstractExtractor(NAME(BytesAsFloat), DataType::Flt32) +{ + +} + +std::shared_ptr BytesAsDouble::newDefault() const +{ + return std::make_shared(); +} + +QVariant BytesAsDouble::parse(const QByteArray& bytes) const +{ + return *((double*)bytes.data()); +} + +bool BytesAsDouble::setCountWithin(int bytes) +{ + AbstractExtractor::setCountWithin(8); + return true; +} + +BytesAsDouble::BytesAsDouble() + :AbstractExtractor(NAME(BytesAsDouble), DataType::Dbl64) +{ + +} + +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); +} + +QVariant BytesAsUnsigned::parse(const QByteArray& bytes) const +{ + 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; + } + return value; +} + +std::shared_ptr BytesAsUnsigned::newDefault() const +{ + return std::make_shared(); +} + +#include +QVariant BytesAsList::parse(const QByteArray& bytes) const +{ + throw std::logic_error("The method or operation is not implemented."); +} + +std::shared_ptr BytesAsList::newDefault() const +{ + throw std::logic_error("The method or operation is not implemented."); +} + +bool BytesAsUnion::setSizeProvider(std::shared_ptr ins) +{ + if(ins->value() < 1) + return false; + + 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 BytesAsUnion::newDefault() const +{ + throw std::logic_error("The method or operation is not implemented."); +} diff --git a/TranslateUI/extract_basic.h b/TranslateUI/extract_basic.h new file mode 100644 index 0000000..5356235 --- /dev/null +++ b/TranslateUI/extract_basic.h @@ -0,0 +1,246 @@ +#pragma once + +#include "common.h" +#include + + +/// +/// 翻译单元抽象接口 +/// +class ExtractUnit : public Serializable { +public: + virtual ~ExtractUnit() = default; + + /// + /// 单元名称 + /// + /// + virtual QString name() const = 0; + + /// + /// + /// + /// + virtual DataType outType() const = 0; + + /// + /// 从上一个单元字节偏移字节数量 + /// + /// + virtual int offsetSpan() const = 0; + /// + /// 解析所用的字节数量 + /// + /// + virtual int countWithin() const = 0; + + /// + /// 执行解析过程 + /// + /// + /// + virtual QVariant parse(const QByteArray& bytes) const = 0; +}; + +namespace extract { + class AbstractExtractor : public ExtractUnit { + private: + QString _name_store; + DataType _type_store; + int _byte_offset, _byte_count; + + public: + AbstractExtractor(const QString& name, DataType data); + /// + /// 设置偏移字节数量 + /// + /// + virtual bool setOffsetSpan(int bytes); + virtual bool setCountWithin(int bytes); + + // ExtractUnit =========================== + QString name() const override; + virtual DataType outType() const; + int offsetSpan() const override; + int countWithin() const override; + + // Serializable ============================== + void loadFrom(const QJsonObject& obj) 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; + + /// + /// 转换源数值未16进制数值,默认分割方式 + /// + class BytesAsHex : public AbstractExtractor { + public: + BytesAsHex(); + + QVariant parse(const QByteArray& bytes) const override; + std::shared_ptr newDefault() const override; + }; + + /// + /// 转换源数值为位联合 + /// + class BytesAsBitCombine : public AbstractExtractor { + private: + QHash _switch_options; + + public: + BytesAsBitCombine(); + + bool setSwitchOption(int bit_index, const QString& keyword); + QHash switchOptions() const; + virtual void clearOptions(); + + // ExtractUnit ============================ + QVariant parse(const QByteArray& bytes) const override; + + // Serializable ============================== + void loadFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; + std::shared_ptr newDefault() const override; + }; + + /// + /// 转换源数据为整形 + /// + class BytesAsInteger : public AbstractExtractor { + public: + BytesAsInteger(); + + // ExtractUnit ============================ + bool setCountWithin(int bytes) override; + QVariant parse(const QByteArray& bytes) const override; + + // Serializable ============================== + std::shared_ptr newDefault() const override; + }; + /// + /// 转换源数据为整形 + /// + class BytesAsUnsigned : public AbstractExtractor { + public: + BytesAsUnsigned(); + + // ExtractUnit ============================ + bool setCountWithin(int bytes) override; + QVariant parse(const QByteArray& bytes) const override; + + // Serializable ============================== + std::shared_ptr newDefault() const override; + }; + + /// + /// 转换源数据为字符串 + /// + class BytesAsString : public AbstractExtractor { + private: + QTextCodec* _conv_with = nullptr; + + public: + BytesAsString(); + + void setStrCodec(QTextCodec* ins); + QString codecName() const; + + // ExtractUnit ============================ + QVariant parse(const QByteArray& bytes) const override; + + // Serializable ============================== + void loadFrom(const QJsonObject& obj) override; + void saveTo(QJsonObject& obj) const override; + std::shared_ptr newDefault() const override; + }; + + class BytesAsFloat : public AbstractExtractor { + public: + BytesAsFloat(); + + /// + /// 设置解析字节数量 + /// + /// + virtual bool setCountWithin(int bytes) override; + + QVariant parse(const QByteArray& bytes) const override; + std::shared_ptr newDefault() const override; + }; + + class BytesAsDouble : public AbstractExtractor { + public: + BytesAsDouble(); + + /// + /// 设置解析字节数量 + /// + /// + virtual bool setCountWithin(int bytes) override; + + QVariant parse(const QByteArray& bytes) const override; + std::shared_ptr newDefault() const override; + }; + + /// + /// 尺寸解析器 + /// + class SizeProvider { + public: + /// + /// 获取值,如果该值无法预先计算得知,返回-1 + /// + /// 尺寸值 + virtual int32_t value() const = 0; + virtual QString expression() const = 0; + }; + class BytesAsList : public AbstractExtractor { + + public: + std::shared_ptr elementRule() const; + bool setElementRule(std::shared_ptr u); + + /// + /// 设置尺寸解析器,返回值标志是否接受该设置 + /// + /// + /// true-有效,false-无效 + virtual bool setSizeProvider(std::shared_ptr ins) = 0; + + + public: + QVariant parse(const QByteArray& bytes) const override; + std::shared_ptr newDefault() const override; + + }; + + class RuleMatch { + public: + virtual std::shared_ptr bindRule() const = 0; + virtual bool checkpass() const = 0; + }; + class BytesAsUnion : public AbstractExtractor { + protected: + std::shared_ptr _provider_ins = nullptr; + + public: + bool setSizeProvider(std::shared_ptr ins); + + + QVariant parse(const QByteArray& bytes) const override; + + + std::shared_ptr newDefault() const override; + + }; +} + +