2018-08-26 01:16:37 +02:00
|
|
|
/* This file is part of the sirit project.
|
2019-07-14 23:48:59 +02:00
|
|
|
* Copyright (c) 2019 sirit
|
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* 3-Clause BSD License
|
2018-08-26 01:16:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stream.h"
|
|
|
|
|
|
|
|
namespace Sirit {
|
|
|
|
|
2019-11-27 09:46:03 +01:00
|
|
|
Stream::Stream(std::vector<u32>& words_) : words{words_} {}
|
2018-08-26 01:16:37 +02:00
|
|
|
|
|
|
|
Stream::~Stream() = default;
|
|
|
|
|
2019-03-16 05:15:45 +01:00
|
|
|
void Stream::Write(std::string_view string) {
|
2019-10-18 08:44:49 +02:00
|
|
|
constexpr std::size_t word_size = 4;
|
|
|
|
const auto size = string.size();
|
2019-11-27 09:25:27 +01:00
|
|
|
const auto read = [string, size](std::size_t offset) {
|
|
|
|
return offset < size ? static_cast<u8>(string[offset]) : u8(0);
|
|
|
|
};
|
2019-03-16 05:17:18 +01:00
|
|
|
|
2019-10-18 08:44:49 +02:00
|
|
|
words.reserve(words.size() + size / word_size + 1);
|
|
|
|
for (std::size_t i = 0; i < size; i += word_size) {
|
|
|
|
Write(read(i), read(i + 1), read(i + 2), read(i + 3));
|
|
|
|
}
|
|
|
|
if (size % word_size == 0) {
|
|
|
|
Write(u32(0));
|
2018-08-26 01:16:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Write(u64 value) {
|
2019-10-18 08:44:49 +02:00
|
|
|
const u32 dword[] = {static_cast<u32>(value), static_cast<u32>(value >> 32)};
|
|
|
|
words.insert(std::begin(words), std::cbegin(dword), std::cend(dword));
|
2018-08-26 01:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Write(u32 value) {
|
2019-10-18 08:44:49 +02:00
|
|
|
words.push_back(value);
|
2018-08-26 01:16:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 08:44:49 +02:00
|
|
|
void Stream::Write(u16 first, u16 second) {
|
|
|
|
const u32 word = static_cast<u32>(first) | static_cast<u32>(second) << 16;
|
|
|
|
Write(word);
|
2018-08-26 01:16:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 08:44:49 +02:00
|
|
|
void Stream::Write(u8 first, u8 second, u8 third, u8 fourth) {
|
|
|
|
const u32 word = static_cast<u32>(first) | static_cast<u32>(second) << 8 |
|
|
|
|
static_cast<u32>(third) << 16 | static_cast<u32>(fourth) << 24;
|
|
|
|
Write(word);
|
2019-03-11 07:26:21 +01:00
|
|
|
}
|
2018-08-26 01:16:37 +02:00
|
|
|
|
|
|
|
} // namespace Sirit
|