Skip to content

ByteReader

Generated from engine/core/byte_reader.h at 862e08b. The text under each declaration is the header's own comment, word for word. About the reference says how these pages are made.

Browse the core module

#include "engine/core/byte_reader.h" · namespace labrador

A cursor over a buffer of bytes that cannot walk off the end.

EVERY READ IS CHECKED, which the obvious alternative - map a packed struct over the buffer - is not. A truncated file is a plausible thing to have on disk (an interrupted copy, a half-written build step), and the difference between the two designs is a named throw and a read past the end of a heap allocation. It is also the difference between a struct layout this compiler happens to produce and the layout the file actually has.

LITTLE-ENDIAN, SPELT OUT. Both file formats this engine reads are, and assembling the value a byte at a time says so - where a memcpy of four bytes would only say "whatever this machine is".

IN core/ RATHER THAN render/, which is where both of its callers are, because nothing about it is a picture: it reads numbers out of a file.

ByteReader(const std::vector<unsigned char>& bytes,
const std::string& what);

bytes is borrowed and must outlive the reader. what names the source in every throw - a path, so the message says which file.

uint32_t read_u32();
int32_t read_i32();
float read_f32();
std::vector<unsigned char> read_bytes(size_t count);
void skip(size_t count);
void require(size_t count) const;

Throws std::runtime_error naming the source if fewer than count bytes are left. Called by every read above, and callable directly by a reader that is about to size a container from a number it has just read - which is the one place a corrupt file can ask for gigabytes.

size_t position() const;
size_t remaining() const;
std::vector<unsigned char> read_file_bytes(const std::string& path);

The whole file at the UTF-8 path, as bytes.

Throws std::out_of_range naming the path when there is no file there, and std::runtime_error when there is one and it cannot be read. Those are different facts: the first is a name nobody produced, which is a content bug, and the second is a disk.

The files that include this header directly. A file can also reach it through another header.

Development documentation (unreleased). Built from Labrador 862e08b of 2026-10-08.