A file-based binary reader with buffering and configurable endianness.
More...
#include <BinaryReader.hpp>
template<Endianness E = Endianness::kLittle>
class intns::io::FileReader< E >
A file-based binary reader with buffering and configurable endianness.
FileReader provides efficient reading of binary data from files with automatic buffering and endianness conversion. It supports the same operations as MemoryReader but reads data from a file stream.
- Template Parameters
-
| E | The endianness for data interpretation (default: little endian). |
Usage
Open a file and read data sequentially:
A memory-based binary reader with configurable endianness.
Definition BinaryReader.hpp:40
uint32_t read_u32()
Reads an unsigned 32-bit integer with endianness conversion.
Definition BinaryReader.hpp:159
std::string read_string(size_t length)
Reads a fixed-length string from the buffer.
Definition BinaryReader.hpp:307
Safety
Constructor throws std::runtime_error if file cannot be opened. Read operations throw std::out_of_range if attempting to read beyond EOF. File operations may throw std::ios_base::failure based on stream state.
◆ FileReader()
template<Endianness E = Endianness::kLittle>
Constructs a FileReader and opens the specified file.
- Parameters
-
| filename | Path to the file to read. |
| buffer_size | Size of the internal buffer (default: 8192 bytes). |
- Exceptions
-
| std::runtime_error | If the file cannot be opened. |
| std::invalid_argument | If buffer_size is 0. |
◆ position()
template<Endianness E = Endianness::kLittle>
Returns the current read position in the file.
- Returns
- The current position within the file.
◆ read_bytes()
template<Endianness E = Endianness::kLittle>
Reads raw bytes into a destination buffer.
- Parameters
-
| dest | Pointer to the destination buffer. |
| bytes | Number of bytes to read. |
- Exceptions
-
| std::invalid_argument | If dest is nullptr. |
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_cstring()
template<Endianness E = Endianness::kLittle>
Reads a null-terminated C-style string from the file.
- Returns
- The string read from the file (without null terminator).
- Exceptions
-
| std::out_of_range | If at end of file before null terminator. |
- Note
- This implementation reads byte-by-byte for simplicity with buffering.
◆ read_f32()
template<Endianness E = Endianness::kLittle>
Reads a 32-bit floating-point value with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_f64()
template<Endianness E = Endianness::kLittle>
Reads a 64-bit floating-point value with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_s16()
template<Endianness E = Endianness::kLittle>
Reads a signed 16-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_s32()
template<Endianness E = Endianness::kLittle>
Reads a signed 32-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_s64()
template<Endianness E = Endianness::kLittle>
Reads a signed 64-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_s8()
template<Endianness E = Endianness::kLittle>
Reads a signed 8-bit integer.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If at end of file. |
◆ read_string()
template<Endianness E = Endianness::kLittle>
Reads a fixed-length string from the file.
- Parameters
-
| length | Number of bytes to read. |
- Returns
- The string read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_u16()
template<Endianness E = Endianness::kLittle>
Reads an unsigned 16-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_u32()
template<Endianness E = Endianness::kLittle>
Reads an unsigned 32-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_u64()
template<Endianness E = Endianness::kLittle>
Reads an unsigned 64-bit integer with endianness conversion.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If insufficient bytes remain in the file. |
◆ read_u8()
template<Endianness E = Endianness::kLittle>
Reads an unsigned 8-bit integer.
- Returns
- The value read from the file.
- Exceptions
-
| std::out_of_range | If at end of file. |
◆ remaining()
template<Endianness E = Endianness::kLittle>
Returns the number of bytes remaining to be read.
- Returns
- The number of bytes from current position to end of file.
◆ set_position()
template<Endianness E = Endianness::kLittle>
Sets the read position within the file.
- Parameters
-
| pos | The new position to set. |
- Exceptions
-
| std::runtime_error | If seeking fails. |
- Note
- Position is clamped to file size if it exceeds the file boundary.
◆ size()
template<Endianness E = Endianness::kLittle>
Returns the total size of the file.
- Returns
- The size of the file in bytes.
◆ skip()
template<Endianness E = Endianness::kLittle>
Advances the read position by the specified number of bytes.
- Parameters
-
| bytes | The number of bytes to skip. |
- Exceptions
-
| std::runtime_error | If seeking fails. |
The documentation for this class was generated from the following file: