dml: Specialize string types

(also, fixed the tests for strings)
This commit is contained in:
Joshua Scott 2018-03-30 18:58:57 +01:00
parent 7903e23d90
commit 977ea2310e
3 changed files with 52 additions and 11 deletions

View File

@ -1,4 +1,5 @@
#include "ki/dml/Field.h"
#include "ki/util/ValueBytes.h"
namespace ki
{
@ -7,19 +8,34 @@ namespace dml
template <>
void StrField::write_to(std::ostream &ostream) const
{
ValueBytes<USHRT> data = { m_value.length() };
if (data.buff[0] == ((m_value.length() & 0xFF00) >> 8))
std::reverse(&data.buff[0], &data.buff[1]);
ostream.write(data.buff, sizeof(USHRT));
ostream.write(m_value.c_str(), m_value.length());
}
template <>
void StrField::read_from(std::istream &istream)
{
// Get the length
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<USHRT> length_data;
istream.read(length_data.buff, sizeof(USHRT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&length_data.buff[0], &length_data.buff[1]);
// Read the data into a buffer
char *data = new char[length_data.value + 1] { 0 };
istream.read(data, length_data.value);
m_value = STR(data);
delete[] data;
}
template <>
size_t StrField::get_size() const
{
return 0;
return sizeof(USHRT) + m_value.length();
}
}
}

View File

@ -1,4 +1,5 @@
#include "ki/dml/Field.h"
#include "ki/util/ValueBytes.h"
namespace ki
{
@ -7,19 +8,40 @@ namespace dml
template <>
void WStrField::write_to(std::ostream &ostream) const
{
ValueBytes<USHRT> data = { m_value.length() };
if (data.buff[0] == ((m_value.length() & 0xFF00) >> 8))
std::reverse(&data.buff[0], &data.buff[1]);
ostream.write(data.buff, sizeof(USHRT));
ostream.write((char *)m_value.c_str(), m_value.length() * sizeof(wchar_t));
}
template <>
void WStrField::read_from(std::istream &istream)
{
// Get the length
const ValueBytes<USHRT> endianness_check = { 0x0102 };
ValueBytes<USHRT> length_data;
istream.read(length_data.buff, sizeof(USHRT));
if (endianness_check.buff[0] == 0x01)
std::reverse(&length_data.buff[0], &length_data.buff[1]);
// Read the data into a buffer
size_t length = length_data.value * sizeof(wchar_t);
char *data = new char[length + sizeof(wchar_t)]{ 0 };
istream.read(data, length);
for (int i = 0; i < length; i += 2)
{
if (endianness_check.buff[0] == 0x01)
std::reverse(&data[i], &data[i + 1]);
}
m_value = WSTR((wchar_t *)data);
delete[] data;
}
template <>
size_t WStrField::get_size() const
{
return 0;
return sizeof(USHRT) + (m_value.length() * sizeof(wchar_t));
}
}
}

View File

@ -98,12 +98,12 @@ TEST_CASE("Field Serialization", "[dml]")
char buff[2];
SHRT length;
} length_bytes;
memcpy(length_bytes.buff, ss.str().data(), 2);
memcpy(length_bytes.buff, ss.str().c_str(), 2);
REQUIRE(length_bytes.length == 0x4);
char value_buff[4];
memcpy(value_buff, ss.str().substr(2).data(), 4);
REQUIRE(value_buff == "TEST");
char value_buff[5] = { 0 };
memcpy(value_buff, ss.str().substr(2).c_str(), 4);
REQUIRE(strcmp(value_buff, "TEST") == 0);
}
SECTION("WSTR Fields")
@ -119,9 +119,9 @@ TEST_CASE("Field Serialization", "[dml]")
memcpy(length_bytes.buff, ss.str().data(), 2);
REQUIRE(length_bytes.length == 0x4);
wchar_t value_buff[4];
wchar_t value_buff[5] = { 0 };
memcpy(value_buff, ss.str().substr(2).data(), 8);
REQUIRE(value_buff == L"TEST");
REQUIRE(wcscmp(value_buff, L"TEST") == 0);
}
SECTION("FLT Fields")
@ -225,7 +225,10 @@ TEST_CASE("Field Deserialization", "[dml]")
SECTION("WSTR Fields")
{
ss.write("\x04\x00T\x00E\x00S\x00T\x00", 10);
char buff[10];
memcpy(buff, "\x04\x00", sizeof(USHRT));
memcpy(&buff[2], (char *)L"TEST", 8);
ss.write(buff, 10);
ss.seekg(std::stringstream::beg);
auto *field = record->add_field<WSTR>("TestWStr");