251 lines
5.1 KiB
C++
251 lines
5.1 KiB
C++
#include "TranslateBasic.h"
|
|
|
|
AbstractTranslateUnit::AbstractTranslateUnit(const QString& name)
|
|
:_name_store(name), _byte_offset(0), _byte_count(0) {
|
|
}
|
|
|
|
bool AbstractTranslateUnit::setOffsetFromPrevious(int bytes)
|
|
{
|
|
this->_byte_offset = bytes;
|
|
return true;
|
|
}
|
|
|
|
bool AbstractTranslateUnit::setCountWithinParse(int bytes)
|
|
{
|
|
this->_byte_count = bytes;
|
|
return true;
|
|
}
|
|
|
|
QString AbstractTranslateUnit::name() const
|
|
{
|
|
return _name_store;
|
|
}
|
|
|
|
int AbstractTranslateUnit::offsetFromPrevious() const
|
|
{
|
|
return _byte_offset;
|
|
}
|
|
|
|
int AbstractTranslateUnit::countWithinParse() const
|
|
{
|
|
return _byte_count;
|
|
}
|
|
|
|
void AbstractTranslateUnit::loadFrom(const QJsonObject& obj)
|
|
{
|
|
INT32_PEAK(_byte_offset);
|
|
INT32_PEAK(_byte_count);
|
|
}
|
|
|
|
void AbstractTranslateUnit::saveTo(QJsonObject& obj) const
|
|
{
|
|
INT32_SAVE(_byte_count);
|
|
INT32_SAVE(_byte_offset);
|
|
}
|
|
|
|
BytesAsHex::BytesAsHex()
|
|
: AbstractTranslateUnit(NAME(BytesAsHex)) {
|
|
}
|
|
|
|
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 + "H";
|
|
}
|
|
|
|
std::shared_ptr<Serializable> BytesAsHex::newDefault() const
|
|
{
|
|
return std::make_shared<BytesAsHex>();
|
|
}
|
|
|
|
BytesAsBitCombine::BytesAsBitCombine()
|
|
: AbstractTranslateUnit(NAME(BytesAsBitCombine))
|
|
{
|
|
|
|
}
|
|
|
|
bool BytesAsBitCombine::setSwitchOption(int bit_index, const QString& keyword)
|
|
{
|
|
if (bit_index >= 0 && bit_index <= this->countWithinParse() * 8) {
|
|
_switch_options[bit_index] = keyword;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
QHash<int, QString> 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)
|
|
{
|
|
AbstractTranslateUnit::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
|
|
{
|
|
AbstractTranslateUnit::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<Serializable> BytesAsBitCombine::newDefault() const
|
|
{
|
|
return std::make_shared<BytesAsBitCombine>();
|
|
}
|
|
|
|
void BytesAsInteger::loadFrom(const QJsonObject& obj)
|
|
{
|
|
AbstractTranslateUnit::loadFrom(obj);
|
|
INT32_PEAK(unsigned_mark);
|
|
}
|
|
|
|
void BytesAsInteger::saveTo(QJsonObject& obj) const
|
|
{
|
|
AbstractTranslateUnit::saveTo(obj);
|
|
INT32_SAVE(unsigned_mark);
|
|
}
|
|
|
|
std::shared_ptr<Serializable> BytesAsInteger::newDefault() const
|
|
{
|
|
return std::make_shared<BytesAsInteger>();
|
|
}
|
|
|
|
BytesAsInteger::BytesAsInteger()
|
|
:AbstractTranslateUnit(NAME(BytesAsInteger)) {
|
|
setCountWithinParse(8);
|
|
}
|
|
|
|
void BytesAsInteger::setUnsignedMark(bool ste)
|
|
{
|
|
this->unsigned_mark = ste;
|
|
}
|
|
|
|
bool BytesAsInteger::unsignedMark() const
|
|
{
|
|
return unsigned_mark;
|
|
}
|
|
|
|
bool BytesAsInteger::setCountWithinParse(int bytes)
|
|
{
|
|
if (bytes >= 0 && bytes <= 8)
|
|
return AbstractTranslateUnit::setCountWithinParse(bytes);
|
|
return false;
|
|
}
|
|
|
|
QVariant BytesAsInteger::parse(const QByteArray& bytes) const
|
|
{
|
|
if (unsigned_mark) {
|
|
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;
|
|
}
|
|
else {
|
|
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 <QTextCodec>
|
|
|
|
BytesAsString::BytesAsString()
|
|
:AbstractTranslateUnit(NAME(BytesAsString)){
|
|
_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)
|
|
{
|
|
AbstractTranslateUnit::loadFrom(obj);
|
|
|
|
QString codec_name;
|
|
STRING_PEAK(codec_name);
|
|
this->_conv_with = QTextCodec::codecForName(codec_name.toLatin1());
|
|
}
|
|
|
|
void BytesAsString::saveTo(QJsonObject& obj) const
|
|
{
|
|
AbstractTranslateUnit::saveTo(obj);
|
|
|
|
auto codec_name = this->codecName();
|
|
STRING_SAVE(codec_name);
|
|
}
|
|
|
|
std::shared_ptr<Serializable> BytesAsString::newDefault() const
|
|
{
|
|
return std::make_shared<BytesAsString>();
|
|
}
|