unify string padding
This commit is contained in:
parent
310eae05ec
commit
4cc4d16fba
|
@ -49,19 +49,13 @@ size_t string::streamSize() const
|
|||
|
||||
void string::iStream(std::shared_ptr<bufferstream> stream)
|
||||
{
|
||||
stream->readString(value);
|
||||
auto pad = (value.size() + 1) % 4;
|
||||
for (uint i = 0; i < pad; i++)
|
||||
stream->readType<uint8_t>();
|
||||
Message::readString(stream, value);
|
||||
}
|
||||
|
||||
|
||||
void string::oStream(std::shared_ptr<bufferstream> stream) const
|
||||
{
|
||||
stream->writeString(value);
|
||||
auto pad = (value.size() + 1) % 4;
|
||||
for (uint i = 0; i < pad; i++)
|
||||
stream->writeType<uint8_t>(0x00);
|
||||
Message::writeString(stream, value);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -130,6 +130,33 @@ std::vector<std::shared_ptr<Argument>> Message::createArguments(std::string type
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Message::readString
|
||||
* @param buffer
|
||||
* @param string
|
||||
*/
|
||||
void Message::readString(std::shared_ptr<bufferstream> buffer, std::string &string)
|
||||
{
|
||||
buffer->readString(string, 0, true); // variable-length, null terminated
|
||||
uint remainder = (string.size()+1) % 4;
|
||||
for (int i = remainder ? 4 - remainder : 0; i > 0; i--) // 32-bit aligned
|
||||
buffer->get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Message::writeString
|
||||
* @param buffer
|
||||
* @param string
|
||||
*/
|
||||
void Message::writeString(std::shared_ptr<bufferstream> buffer, const std::string &string)
|
||||
{
|
||||
uint remainder = (string.size()+1) % 4;
|
||||
uint padding = remainder ? 4 - remainder : 0;
|
||||
buffer->writeString(string, string.size()+1+padding, true); // null terminated, 32-bit aligned
|
||||
}
|
||||
|
||||
|
||||
size_t Message::streamSize() const
|
||||
{
|
||||
size_t address_size = address_pattern.size(); // character count
|
||||
|
@ -153,18 +180,14 @@ size_t Message::streamSize() const
|
|||
|
||||
void Message::iStream(std::shared_ptr<bufferstream> stream)
|
||||
{
|
||||
stream->readString(address_pattern);
|
||||
Message::readString(stream, address_pattern);
|
||||
if (address_pattern.front() != '/')
|
||||
return stream->setstate(std::ios::failbit);
|
||||
for (uint i = 0; i < (address_pattern.size() + 1) % 4; i++)
|
||||
stream->readType<uint8_t>(); // 32 bit aligned padding
|
||||
|
||||
std::string type_string;
|
||||
stream->readString(type_string);
|
||||
Message::readString(stream, type_string);
|
||||
if (type_string.front() != ',')
|
||||
return stream->setstate(std::ios::failbit);
|
||||
for (uint i = 0; i < (type_string.size() + 1) % 4; i++)
|
||||
stream->readType<uint8_t>(); // 32 bit aligned padding
|
||||
|
||||
arguments = createArguments(type_string);
|
||||
for (auto &argument: arguments)
|
||||
|
@ -174,19 +197,8 @@ void Message::iStream(std::shared_ptr<bufferstream> stream)
|
|||
|
||||
void Message::oStream(std::shared_ptr<bufferstream> stream) const
|
||||
{
|
||||
stream->writeString(address_pattern);
|
||||
auto address_pad = (address_pattern.size() + 1) % 4;
|
||||
address_pad = address_pad ? 4 - address_pad : 0;
|
||||
for (uint i = 0; i < address_pad; i++)
|
||||
stream->writeType<uint8_t>(0x00); // 32 bit aligned padding
|
||||
|
||||
std::string type_string = type_tag();
|
||||
stream->writeString(type_string);
|
||||
auto type_pad = (type_string.size() + 1) % 4;
|
||||
type_pad = type_pad ? 4 - type_pad : 0;
|
||||
for (uint i = 0; i < type_pad; i++)
|
||||
stream->writeType<uint8_t>(0x00); // 32 bit aligned padding
|
||||
|
||||
Message::writeString(stream, address_pattern);
|
||||
Message::writeString(stream, type_tag());
|
||||
for (const auto &argument: arguments)
|
||||
argument->oStream(stream);
|
||||
}
|
||||
|
|
|
@ -43,6 +43,8 @@ struct Message
|
|||
std::vector<std::shared_ptr<Argument>> arguments; //!< zero or more Arguments.
|
||||
|
||||
static std::vector<std::shared_ptr<Argument>> createArguments(std::string);
|
||||
static void readString(std::shared_ptr<bufferstream> buffer, std::string &string);
|
||||
static void writeString(std::shared_ptr<bufferstream> buffer, const std::string &string);
|
||||
|
||||
virtual size_t streamSize() const override;
|
||||
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
||||
|
|
Loading…
Reference in New Issue