2015-09-06 20:33:27 +02:00
|
|
|
#include "./field.h"
|
|
|
|
#include "./parsingexception.h"
|
2015-04-22 19:06:29 +02:00
|
|
|
|
|
|
|
#include <c++utilities/io/binaryreader.h>
|
|
|
|
#include <c++utilities/io/binarywriter.h>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 22:44:29 +02:00
|
|
|
using namespace CppUtilities;
|
2015-04-22 19:06:29 +02:00
|
|
|
|
|
|
|
namespace Io {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \class Field
|
|
|
|
* \brief The Field class holds field information which consists of a name and a value
|
|
|
|
* and is able to serialize and deserialize this information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new account entry for the specified account with the specified \a name
|
|
|
|
* and \a value.
|
|
|
|
*/
|
2017-05-01 03:25:30 +02:00
|
|
|
Field::Field(AccountEntry *tiedAccount, const string &name, const string &value)
|
|
|
|
: m_name(name)
|
|
|
|
, m_value(value)
|
|
|
|
, m_type(FieldType::Normal)
|
|
|
|
, m_tiedAccount(tiedAccount)
|
|
|
|
{
|
|
|
|
}
|
2015-04-22 19:06:29 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Constructs a new account entry for the specified account which is deserialize from
|
|
|
|
* the specified \a stream.
|
|
|
|
* \throws Throws ParsingException when an parsing error occurs.
|
|
|
|
*/
|
|
|
|
Field::Field(AccountEntry *tiedAccount, istream &stream)
|
|
|
|
{
|
|
|
|
BinaryReader reader(&stream);
|
2018-06-09 21:17:01 +02:00
|
|
|
const int version = reader.readByte();
|
|
|
|
if (version != 0x0 && version != 0x1) {
|
2015-04-22 19:06:29 +02:00
|
|
|
throw ParsingException("Field version is not supported.");
|
|
|
|
}
|
2018-06-09 21:17:01 +02:00
|
|
|
m_name = reader.readLengthPrefixedString();
|
|
|
|
m_value = reader.readLengthPrefixedString();
|
2019-03-13 19:08:30 +01:00
|
|
|
std::uint8_t type = reader.readByte();
|
2018-06-09 21:17:01 +02:00
|
|
|
if (!isValidType(type)) {
|
|
|
|
throw ParsingException("Field type is not supported.");
|
|
|
|
}
|
|
|
|
m_type = static_cast<FieldType>(type);
|
|
|
|
// read extended header for version 0x1
|
|
|
|
if (version == 0x1) {
|
2019-03-13 19:08:30 +01:00
|
|
|
const std::uint16_t extendedHeaderSize = reader.readUInt16BE();
|
2018-06-09 21:17:01 +02:00
|
|
|
// currently there's nothing to read here
|
|
|
|
m_extendedData = reader.readString(extendedHeaderSize);
|
|
|
|
}
|
|
|
|
m_tiedAccount = tiedAccount;
|
2015-04-22 19:06:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Serializes the current instance to the specified \a stream.
|
|
|
|
*/
|
|
|
|
void Field::make(ostream &stream) const
|
|
|
|
{
|
|
|
|
BinaryWriter writer(&stream);
|
2015-06-07 00:14:00 +02:00
|
|
|
writer.writeByte(m_extendedData.empty() ? 0x0 : 0x1); // version
|
2015-04-22 19:06:29 +02:00
|
|
|
writer.writeLengthPrefixedString(m_name);
|
|
|
|
writer.writeLengthPrefixedString(m_value);
|
2019-03-13 19:08:30 +01:00
|
|
|
writer.writeByte(static_cast<std::uint8_t>(m_type));
|
2017-05-01 03:25:30 +02:00
|
|
|
if (!m_extendedData.empty()) {
|
2021-03-20 21:55:40 +01:00
|
|
|
writer.writeUInt16BE(static_cast<std::uint16_t>(m_extendedData.size()));
|
2015-06-07 00:14:00 +02:00
|
|
|
writer.writeString(m_extendedData);
|
|
|
|
}
|
2015-04-22 19:06:29 +02:00
|
|
|
}
|
2018-03-20 20:11:31 +01:00
|
|
|
} // namespace Io
|