intnslib 0.1
A library to hold common functionality used across multiple projects.
Loading...
Searching...
No Matches
intns::io::FileReader< E > Class Template Reference

A file-based binary reader with buffering and configurable endianness. More...

#include <BinaryReader.hpp>

Public Member Functions

 FileReader (const std::string &filename, size_t buffer_size=8192)
 Constructs a FileReader and opens the specified file.
 
size_t size () const noexcept
 Returns the total size of the file.
 
size_t position () const noexcept
 Returns the current read position in the file.
 
size_t remaining () const noexcept
 Returns the number of bytes remaining to be read.
 
void set_position (size_t pos)
 Sets the read position within the file.
 
void skip (size_t bytes)
 Advances the read position by the specified number of bytes.
 
uint8_t read_u8 ()
 Reads an unsigned 8-bit integer.
 
uint16_t read_u16 ()
 Reads an unsigned 16-bit integer with endianness conversion.
 
uint32_t read_u32 ()
 Reads an unsigned 32-bit integer with endianness conversion.
 
uint64_t read_u64 ()
 Reads an unsigned 64-bit integer with endianness conversion.
 
int8_t read_s8 ()
 Reads a signed 8-bit integer.
 
int16_t read_s16 ()
 Reads a signed 16-bit integer with endianness conversion.
 
int32_t read_s32 ()
 Reads a signed 32-bit integer with endianness conversion.
 
int64_t read_s64 ()
 Reads a signed 64-bit integer with endianness conversion.
 
float read_f32 ()
 Reads a 32-bit floating-point value with endianness conversion.
 
double read_f64 ()
 Reads a 64-bit floating-point value with endianness conversion.
 
void read_bytes (void *dest, size_t bytes)
 Reads raw bytes into a destination buffer.
 
std::string read_string (size_t length)
 Reads a fixed-length string from the file.
 
std::string read_cstring ()
 Reads a null-terminated C-style string from the file.
 

Detailed Description

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
EThe endianness for data interpretation (default: little endian).

Usage

Open a file and read data sequentially:

std::string header = reader.read_string(16);
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.

Constructor & Destructor Documentation

◆ FileReader()

template<Endianness E = Endianness::kLittle>
intns::io::FileReader< E >::FileReader ( const std::string &  filename,
size_t  buffer_size = 8192 
)
inlineexplicit

Constructs a FileReader and opens the specified file.

Parameters
filenamePath to the file to read.
buffer_sizeSize of the internal buffer (default: 8192 bytes).
Exceptions
std::runtime_errorIf the file cannot be opened.
std::invalid_argumentIf buffer_size is 0.

Member Function Documentation

◆ position()

template<Endianness E = Endianness::kLittle>
size_t intns::io::FileReader< E >::position ( ) const
inlinenoexcept

Returns the current read position in the file.

Returns
The current position within the file.

◆ read_bytes()

template<Endianness E = Endianness::kLittle>
void intns::io::FileReader< E >::read_bytes ( void dest,
size_t  bytes 
)
inline

Reads raw bytes into a destination buffer.

Parameters
destPointer to the destination buffer.
bytesNumber of bytes to read.
Exceptions
std::invalid_argumentIf dest is nullptr.
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_cstring()

template<Endianness E = Endianness::kLittle>
std::string intns::io::FileReader< E >::read_cstring ( )
inline

Reads a null-terminated C-style string from the file.

Returns
The string read from the file (without null terminator).
Exceptions
std::out_of_rangeIf 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>
float intns::io::FileReader< E >::read_f32 ( )
inline

Reads a 32-bit floating-point value with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_f64()

template<Endianness E = Endianness::kLittle>
double intns::io::FileReader< E >::read_f64 ( )
inline

Reads a 64-bit floating-point value with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_s16()

template<Endianness E = Endianness::kLittle>
int16_t intns::io::FileReader< E >::read_s16 ( )
inline

Reads a signed 16-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_s32()

template<Endianness E = Endianness::kLittle>
int32_t intns::io::FileReader< E >::read_s32 ( )
inline

Reads a signed 32-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_s64()

template<Endianness E = Endianness::kLittle>
int64_t intns::io::FileReader< E >::read_s64 ( )
inline

Reads a signed 64-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_s8()

template<Endianness E = Endianness::kLittle>
int8_t intns::io::FileReader< E >::read_s8 ( )
inline

Reads a signed 8-bit integer.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf at end of file.

◆ read_string()

template<Endianness E = Endianness::kLittle>
std::string intns::io::FileReader< E >::read_string ( size_t  length)
inline

Reads a fixed-length string from the file.

Parameters
lengthNumber of bytes to read.
Returns
The string read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_u16()

template<Endianness E = Endianness::kLittle>
uint16_t intns::io::FileReader< E >::read_u16 ( )
inline

Reads an unsigned 16-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_u32()

template<Endianness E = Endianness::kLittle>
uint32_t intns::io::FileReader< E >::read_u32 ( )
inline

Reads an unsigned 32-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_u64()

template<Endianness E = Endianness::kLittle>
uint64_t intns::io::FileReader< E >::read_u64 ( )
inline

Reads an unsigned 64-bit integer with endianness conversion.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf insufficient bytes remain in the file.

◆ read_u8()

template<Endianness E = Endianness::kLittle>
uint8_t intns::io::FileReader< E >::read_u8 ( )
inline

Reads an unsigned 8-bit integer.

Returns
The value read from the file.
Exceptions
std::out_of_rangeIf at end of file.

◆ remaining()

template<Endianness E = Endianness::kLittle>
size_t intns::io::FileReader< E >::remaining ( ) const
inlinenoexcept

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>
void intns::io::FileReader< E >::set_position ( size_t  pos)
inline

Sets the read position within the file.

Parameters
posThe new position to set.
Exceptions
std::runtime_errorIf seeking fails.
Note
Position is clamped to file size if it exceeds the file boundary.

◆ size()

template<Endianness E = Endianness::kLittle>
size_t intns::io::FileReader< E >::size ( ) const
inlinenoexcept

Returns the total size of the file.

Returns
The size of the file in bytes.

◆ skip()

template<Endianness E = Endianness::kLittle>
void intns::io::FileReader< E >::skip ( size_t  bytes)
inline

Advances the read position by the specified number of bytes.

Parameters
bytesThe number of bytes to skip.
Exceptions
std::runtime_errorIf seeking fails.

The documentation for this class was generated from the following file: