sirit/src/stream.cpp

47 lines
1.1 KiB
C++
Raw Normal View History

2018-08-26 00:16:37 +01:00
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
2018-08-27 03:28:39 +01:00
* Lesser General Public License version 2.1 or any later version.
2018-08-26 00:16:37 +01:00
*/
#include "stream.h"
namespace Sirit {
2018-10-03 04:32:45 +01:00
Stream::Stream(std::vector<u8>& bytes_) : bytes(bytes_) {}
2018-08-26 00:16:37 +01:00
Stream::~Stream() = default;
void Stream::Write(std::string string) {
2018-10-03 04:32:45 +01:00
const auto size{string.size()};
const auto data{reinterpret_cast<u8*>(string.data())};
for (std::size_t i = 0; i < size; i++) {
2018-08-26 00:16:37 +01:00
Write(data[i]);
}
2018-10-03 04:32:45 +01:00
for (std::size_t i = 0; i < 4 - size % 4; i++) {
2018-08-26 00:16:37 +01:00
Write(static_cast<u8>(0));
}
}
void Stream::Write(u64 value) {
2018-10-03 04:32:45 +01:00
const auto mem{reinterpret_cast<u32*>(&value)};
2018-08-26 00:16:37 +01:00
Write(mem[0]);
Write(mem[1]);
}
void Stream::Write(u32 value) {
2018-10-03 04:32:45 +01:00
const auto mem{reinterpret_cast<u16*>(&value)};
2018-08-26 00:16:37 +01:00
Write(mem[0]);
Write(mem[1]);
}
void Stream::Write(u16 value) {
2018-10-03 04:32:45 +01:00
const auto mem{reinterpret_cast<u8*>(&value)};
2018-08-26 00:16:37 +01:00
Write(mem[0]);
Write(mem[1]);
}
2018-10-03 04:32:45 +01:00
void Stream::Write(u8 value) { bytes.push_back(value); }
2018-08-26 00:16:37 +01:00
} // namespace Sirit