diff --git a/include/ki/util/ValueBytes.h b/include/ki/util/ValueBytes.h new file mode 100644 index 0000000..34f07f2 --- /dev/null +++ b/include/ki/util/ValueBytes.h @@ -0,0 +1,10 @@ +#pragma once + +namespace ki +{ + template + union ValueBytes { + ValueT value = 0; + char buff[sizeof(ValueT)]; + }; +} diff --git a/src/dml/types/BytField.cpp b/src/dml/types/BytField.cpp index 06afa33..5a6f7a4 100644 --- a/src/dml/types/BytField.cpp +++ b/src/dml/types/BytField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,23 @@ namespace dml template <> void BytField::write_to(std::ostream &ostream) const { - + ValueBytes data; + data.value = m_value; + ostream.write(data.buff, sizeof(BYT)); } template <> void BytField::read_from(std::istream &istream) { - + ValueBytes data; + istream.read(data.buff, sizeof(BYT)); + m_value = data.value; } template <> size_t BytField::get_size() const { - return 0; + return sizeof(BYT); } } } \ No newline at end of file diff --git a/src/dml/types/GidField.cpp b/src/dml/types/GidField.cpp index 1381616..8139f4c 100644 --- a/src/dml/types/GidField.cpp +++ b/src/dml/types/GidField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,27 @@ namespace dml template <> void GidField::write_to(std::ostream &ostream) const { - + ValueBytes data = { m_value }; + if (data.buff[0] == ((m_value & 0xFF00000000000000) >> 56)) + std::reverse(&data.buff[0], &data.buff[7]); + ostream.write(data.buff, sizeof(GID)); } template <> void GidField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(GID)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[7]); + m_value = data.value; } template <> size_t GidField::get_size() const { - return 0; + return sizeof(GID); } } -} \ No newline at end of file +} diff --git a/src/dml/types/IntField.cpp b/src/dml/types/IntField.cpp index edb2010..25efd24 100644 --- a/src/dml/types/IntField.cpp +++ b/src/dml/types/IntField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,27 @@ namespace dml template <> void IntField::write_to(std::ostream &ostream) const { - + ValueBytes data = { m_value }; + if (data.buff[0] == ((m_value & 0xFF000000) >> 24)) + std::reverse(&data.buff[0], &data.buff[3]); + ostream.write(data.buff, sizeof(INT)); } template <> void IntField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(INT)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[3]); + m_value = data.value; } template <> size_t IntField::get_size() const { - return 0; + return sizeof(INT); } } } diff --git a/src/dml/types/ShrtField.cpp b/src/dml/types/ShrtField.cpp index 4cd727b..60a0fcd 100644 --- a/src/dml/types/ShrtField.cpp +++ b/src/dml/types/ShrtField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,27 @@ namespace dml template <> void ShrtField::write_to(std::ostream &ostream) const { - + ValueBytes data = { m_value }; + if (data.buff[0] == ((m_value & 0xFF00) >> 8)) + std::reverse(&data.buff[0], &data.buff[1]); + ostream.write(data.buff, sizeof(SHRT)); } template <> void ShrtField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(SHRT)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[1]); + m_value = data.value; } template <> size_t ShrtField::get_size() const { - return 0; + return sizeof(USHRT); } } } diff --git a/src/dml/types/UBytField.cpp b/src/dml/types/UBytField.cpp index 4caab2a..f1e3166 100644 --- a/src/dml/types/UBytField.cpp +++ b/src/dml/types/UBytField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,23 @@ namespace dml template <> void UBytField::write_to(std::ostream &ostream) const { - + ValueBytes data; + data.value = m_value; + ostream.write(data.buff, sizeof(UBYT)); } template <> void UBytField::read_from(std::istream &istream) { - + ValueBytes data; + istream.read(data.buff, sizeof(UBYT)); + m_value = data.value; } template <> size_t UBytField::get_size() const { - return 0; + return sizeof(BYT); } } } diff --git a/src/dml/types/UIntField.cpp b/src/dml/types/UIntField.cpp index ba998ef..653a552 100644 --- a/src/dml/types/UIntField.cpp +++ b/src/dml/types/UIntField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,27 @@ namespace dml template <> void UIntField::write_to(std::ostream &ostream) const { - + ValueBytes data = { m_value }; + if (data.buff[0] == ((m_value & 0xFF000000) >> 24)) + std::reverse(&data.buff[0], &data.buff[3]); + ostream.write(data.buff, sizeof(UINT)); } template <> void UIntField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(UINT)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[3]); + m_value = data.value; } template <> size_t UIntField::get_size() const { - return 0; + return sizeof(UINT); } } } diff --git a/src/dml/types/UShrtField.cpp b/src/dml/types/UShrtField.cpp index f8a98d8..e4c3747 100644 --- a/src/dml/types/UShrtField.cpp +++ b/src/dml/types/UShrtField.cpp @@ -1,4 +1,5 @@ #include "ki/dml/Field.h" +#include "ki/util/ValueBytes.h" namespace ki { @@ -7,19 +8,27 @@ namespace dml template <> void UShrtField::write_to(std::ostream &ostream) const { - + ValueBytes data = { m_value }; + if (data.buff[0] == ((m_value & 0xFF00) >> 8)) + std::reverse(&data.buff[0], &data.buff[1]); + ostream.write(data.buff, sizeof(USHRT)); } template <> void UShrtField::read_from(std::istream &istream) { - + const ValueBytes endianness_check = { 0x0102 }; + ValueBytes data; + istream.read(data.buff, sizeof(USHRT)); + if (endianness_check.buff[0] == 0x01) + std::reverse(&data.buff[0], &data.buff[1]); + m_value = data.value; } template <> size_t UShrtField::get_size() const { - return 0; + return sizeof(USHRT); } } }