From e2f6bd165508a04376af495e0d255a6fb03436af Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Wed, 4 Apr 2018 04:08:06 +0100 Subject: [PATCH 1/3] dml: Add method for getting FieldBase * from Record Needed for niceties in the Python binding --- include/ki/dml/Record.h | 2 ++ src/dml/Record.cpp | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/include/ki/dml/Record.h b/include/ki/dml/Record.h index cc70dd7..f1e1079 100644 --- a/include/ki/dml/Record.h +++ b/include/ki/dml/Record.h @@ -34,6 +34,8 @@ namespace dml return m_field_map.at(name)->is_type(); } + FieldBase *get_field(std::string name) const; + /** * Returns a previously added field with the specified name * and type. diff --git a/src/dml/Record.cpp b/src/dml/Record.cpp index 9389a48..2085ebf 100644 --- a/src/dml/Record.cpp +++ b/src/dml/Record.cpp @@ -30,6 +30,13 @@ namespace dml return m_field_map.count(name); } + FieldBase *Record::get_field(std::string name) const + { + if (has_field(name)) + return m_field_map.at(name); + return nullptr; + } + size_t Record::get_field_count() const { return m_fields.size(); From 8d7348aa81bc31da055a548cbb8e2aed524d7342 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Thu, 5 Apr 2018 17:37:59 +0100 Subject: [PATCH 2/3] dml: Fix bug with BYT/UBYT fields in XML also made get_value_string and set_value_from_string public and part of the abstract base class. --- include/ki/dml/Field.h | 20 ++++++++++++++++---- include/ki/dml/FieldBase.h | 3 +++ src/dml/types/BytField.cpp | 17 +++++++++++++++++ src/dml/types/UBytField.cpp | 17 +++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/include/ki/dml/Field.h b/include/ki/dml/Field.h index b288909..6facc9a 100644 --- a/include/ki/dml/Field.h +++ b/include/ki/dml/Field.h @@ -116,6 +116,9 @@ namespace dml if (!value.empty()) set_value_from_string(value); } + + std::string get_value_string() const override final; + void set_value_from_string(std::string value) override final; private: ValueT m_value; @@ -126,7 +129,7 @@ namespace dml Field *clone() const override final { auto *clone = new Field(m_name); - clone->m_transferable = true; + clone->m_transferable = m_transferable; clone->m_value = m_value; return clone; } @@ -151,9 +154,6 @@ namespace dml throw value_error(oss.str()); } } - - std::string get_value_string() const; - void set_value_from_string(std::string value); }; typedef Field BytField; @@ -183,6 +183,18 @@ namespace dml iss >> m_value; } + template <> + std::string BytField::get_value_string() const; + + template <> + void BytField::set_value_from_string(std::string value); + + template <> + std::string UBytField::get_value_string() const; + + template <> + void UBytField::set_value_from_string(std::string value); + template <> std::string StrField::get_value_string() const; diff --git a/include/ki/dml/FieldBase.h b/include/ki/dml/FieldBase.h index 6f7cc6d..509bebe 100644 --- a/include/ki/dml/FieldBase.h +++ b/include/ki/dml/FieldBase.h @@ -53,6 +53,9 @@ namespace dml * Creates a new Field from XML data. */ static FieldBase *create_from_xml(const rapidxml::xml_node<> *node); + + virtual std::string get_value_string() const = 0; + virtual void set_value_from_string(std::string value) = 0; protected: std::string m_name; bool m_transferable; diff --git a/src/dml/types/BytField.cpp b/src/dml/types/BytField.cpp index a2b088d..7b0a0f4 100644 --- a/src/dml/types/BytField.cpp +++ b/src/dml/types/BytField.cpp @@ -38,5 +38,22 @@ namespace dml { return "BYT"; } + + template <> + std::string BytField::get_value_string() const + { + std::ostringstream oss; + oss << (int16_t)m_value; + return oss.str(); + } + + template <> + void BytField::set_value_from_string(std::string value) + { + std::istringstream iss(value); + int16_t temp = 0; + iss >> temp; + m_value = temp & 0xFF; + } } } \ No newline at end of file diff --git a/src/dml/types/UBytField.cpp b/src/dml/types/UBytField.cpp index e470230..0534c80 100644 --- a/src/dml/types/UBytField.cpp +++ b/src/dml/types/UBytField.cpp @@ -38,5 +38,22 @@ namespace dml { return "UBYT"; } + + template <> + std::string UBytField::get_value_string() const + { + std::ostringstream oss; + oss << (uint16_t)m_value; + return oss.str(); + } + + template <> + void UBytField::set_value_from_string(std::string value) + { + std::istringstream iss(value); + uint16_t temp = 0; + iss >> temp; + m_value = temp & 0xFF; + } } } From 3922ca4b3985baab94780db002a515ab391d4b39 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 6 Apr 2018 04:01:22 +0100 Subject: [PATCH 3/3] dml: Only parse element nodes in Record XML This fixes spaces and other oddities in the official modules. --- src/dml/Record.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dml/Record.cpp b/src/dml/Record.cpp index 2085ebf..6cb7c0e 100644 --- a/src/dml/Record.cpp +++ b/src/dml/Record.cpp @@ -110,6 +110,9 @@ namespace dml for (auto *field_node = node->first_node(); field_node; field_node = field_node->next_sibling()) { + if (field_node->type() != rapidxml::node_type::node_element) + continue; + FieldBase *field = FieldBase::create_from_xml(field_node); if (has_field(field->get_name())) {