mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 21:27:52 -04:00
45 lines
989 B
C++
45 lines
989 B
C++
#pragma once
|
|
|
|
/*!
|
|
* @file BinaryReader.h
|
|
* Read raw data like a stream.
|
|
*/
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
|
#include "common/util/Assert.h"
|
|
|
|
#include "third-party/span.hpp"
|
|
|
|
class BinaryReader {
|
|
public:
|
|
explicit BinaryReader(nonstd::span<const uint8_t> _span) : m_span(_span) {}
|
|
|
|
template <typename T>
|
|
T read() {
|
|
ASSERT(m_seek + sizeof(T) <= m_span.size());
|
|
T obj;
|
|
memcpy(&obj, m_span.data() + m_seek, sizeof(T));
|
|
m_seek += sizeof(T);
|
|
return obj;
|
|
}
|
|
|
|
void ffwd(int amount) {
|
|
m_seek += amount;
|
|
ASSERT(m_seek <= m_span.size());
|
|
}
|
|
|
|
uint32_t bytes_left() const { return m_span.size() - m_seek; }
|
|
const uint8_t* here() { return m_span.data() + m_seek; }
|
|
BinaryReader at(u32 pos) { return BinaryReader(m_span.subspan(pos)); }
|
|
uint32_t get_seek() const { return m_seek; }
|
|
void set_seek(u32 seek) { m_seek = seek; }
|
|
|
|
private:
|
|
nonstd::span<const u8> m_span;
|
|
uint32_t m_seek = 0;
|
|
};
|