Base module

BitReader

Class for reading binary streams.

BitReader

#include <icy/buffer.h>

Class for reading binary streams.

Public Methods

ReturnNameDescription
BitReaderConstructs a [BitReader](#bitreader) over a raw byte array.
BitReaderConstructs a [BitReader](#bitreader) over a Buffer.
BitReaderConstructs a [BitReader](#bitreader) over a [ConstBuffer](icy-ConstBuffer.html#constbuffer-6).
voidgetReads a value from the BitReader. Returns false if there isn't enough data left for the specified type. Throws a std::out_of_range exception if reading past the limit. Reads len raw bytes into val. Throws std::out_of_range if insufficient data remains.
voidgetReads len bytes and appends them to val. Throws std::out_of_range if insufficient data remains.
voidgetU8Reads an unsigned 8-bit integer. Throws std::out_of_range if insufficient data remains.
voidgetU16Reads an unsigned 16-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.
voidgetU24Reads an unsigned 24-bit integer into a 32-bit variable, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.
voidgetU32Reads an unsigned 32-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.
voidgetU64Reads an unsigned 64-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.
charpeekPeeks at the current byte without advancing the position.
uint8_tpeekU8Peeks at the current byte as a uint8_t without advancing the position.
uint16_tpeekU16Peeks at the next two bytes as a uint16_t without advancing the position.
uint32_tpeekU24Peeks at the next three bytes as a uint32_t without advancing the position.
uint32_tpeekU32Peeks at the next four bytes as a uint32_t without advancing the position.
uint64_tpeekU64Peeks data from the BitReader. -1 is returned if reading past boundary.
size_tskipToCharAdvances the position until the given character is found, stopping before it.
size_tskipWhitespaceAdvances the position past any leading space characters.
size_tskipToNextLineAdvances the position past the end of the current line (past the newline).
size_tskipNextWordAdvances the position past the next whitespace-delimited word.
size_treadNextWordReads the next whitespace-delimited word into val.
size_treadNextNumberReads the next whitespace-delimited decimal number into val.
size_treadLineReads bytes up to (but not including) the next newline into val.
size_treadToNextReads bytes up to (but not including) the next occurrence of c into val.
voidseekSet position pointer to absolute position. Throws a std::out_of_range exception if the value exceeds the limit.
voidskipSet position pointer to relative position. Throws a std::out_of_range exception if the value exceeds the limit.
size_tlimit constReturns the read limit.
size_tposition const inlineReturns the current read position.
size_tavailable constReturns the number of elements between the current position and the limit.
const char *begin const inlineReturns a pointer to the start of the buffer.
const char *current const inlineReturns a pointer to the current read position.
ByteOrderorder const inlineReturns the byte order used for multi-byte integer reads.
std::stringtoStringReturns the remaining unread bytes as a std::string.

BitReader

BitReader(const char * bytes, size_t size, ByteOrder order)

Constructs a [BitReader](#bitreader) over a raw byte array.

Parameters

  • bytes Pointer to the start of the data. Must remain valid for the lifetime of the reader.

  • size Number of bytes available for reading.

  • order Byte order used when reading multi-byte integer types.


BitReader

BitReader(const Buffer & buf, ByteOrder order)

Constructs a [BitReader](#bitreader) over a Buffer.

Parameters

  • buf Source buffer. Must remain valid for the lifetime of the reader.

  • order Byte order used when reading multi-byte integer types.


BitReader

BitReader(const ConstBuffer & pod, ByteOrder order)

Constructs a [BitReader](#bitreader) over a [ConstBuffer](icy-ConstBuffer.html#constbuffer-6).

Parameters

  • pod Source const buffer. Must remain valid for the lifetime of the reader.

  • order Byte order used when reading multi-byte integer types.


get

void get(char * val, size_t len)

Reads a value from the BitReader. Returns false if there isn't enough data left for the specified type. Throws a std::out_of_range exception if reading past the limit. Reads len raw bytes into val. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Destination buffer; must have capacity of at least len bytes.

  • len Number of bytes to read.


get

void get(std::string & val, size_t len)

Reads len bytes and appends them to val. Throws std::out_of_range if insufficient data remains.

Parameters

  • val String to append the read bytes to.

  • len Number of bytes to read.


getU8

void getU8(uint8_t & val)

Reads an unsigned 8-bit integer. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Output parameter receiving the read value.

getU16

void getU16(uint16_t & val)

Reads an unsigned 16-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Output parameter receiving the read value.

getU24

void getU24(uint32_t & val)

Reads an unsigned 24-bit integer into a 32-bit variable, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Output parameter receiving the read value.

getU32

void getU32(uint32_t & val)

Reads an unsigned 32-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Output parameter receiving the read value.

getU64

void getU64(uint64_t & val)

Reads an unsigned 64-bit integer, applying byte-order conversion. Throws std::out_of_range if insufficient data remains.

Parameters

  • val Output parameter receiving the read value.

peek

char peek()

Peeks at the current byte without advancing the position.

Returns

Current byte as char, or 0 if at the end of the buffer.


peekU8

uint8_t peekU8()

Peeks at the current byte as a uint8_t without advancing the position.

Returns

Current value, or 0 if at the end of the buffer.


peekU16

uint16_t peekU16()

Peeks at the next two bytes as a uint16_t without advancing the position.

Returns

Current value with byte-order conversion applied, or 0 on failure.


peekU24

uint32_t peekU24()

Peeks at the next three bytes as a uint32_t without advancing the position.

Returns

Current value with byte-order conversion applied, or 0 on failure.


peekU32

uint32_t peekU32()

Peeks at the next four bytes as a uint32_t without advancing the position.

Returns

Current value with byte-order conversion applied, or 0 on failure.


peekU64

uint64_t peekU64()

Peeks data from the BitReader. -1 is returned if reading past boundary.


skipToChar

size_t skipToChar(char c)

Advances the position until the given character is found, stopping before it.

Parameters

  • c Character to search for.

Returns

Number of bytes skipped.


skipWhitespace

size_t skipWhitespace()

Advances the position past any leading space characters.

Returns

Number of bytes skipped.


skipToNextLine

size_t skipToNextLine()

Advances the position past the end of the current line (past the newline).

Returns

Number of bytes skipped including the newline character.


skipNextWord

size_t skipNextWord()

Advances the position past the next whitespace-delimited word.

Returns

Number of bytes skipped.


readNextWord

size_t readNextWord(std::string & val)

Reads the next whitespace-delimited word into val.

Parameters

  • val String to receive the word.

Returns

Number of bytes consumed.


readNextNumber

size_t readNextNumber(unsigned int & val)

Reads the next whitespace-delimited decimal number into val.

Parameters

  • val Output parameter receiving the parsed unsigned integer.

Returns

Number of bytes consumed.


readLine

size_t readLine(std::string & val)

Reads bytes up to (but not including) the next newline into val.

Parameters

  • val String to receive the line content.

Returns

Number of bytes consumed including the newline.


readToNext

size_t readToNext(std::string & val, char c)

Reads bytes up to (but not including) the next occurrence of c into val.

Parameters

  • val String to receive the read bytes.

  • c Delimiter character to stop at.

Returns

Number of bytes consumed.


seek

void seek(size_t val)

Set position pointer to absolute position. Throws a std::out_of_range exception if the value exceeds the limit.


skip

void skip(size_t size)

Set position pointer to relative position. Throws a std::out_of_range exception if the value exceeds the limit.


limit

const

size_t limit() const

Returns the read limit.


position

const inline

inline size_t position() const

Returns the current read position.


available

const

size_t available() const

Returns the number of elements between the current position and the limit.


begin

const inline

inline const char * begin() const

Returns a pointer to the start of the buffer.


current

const inline

inline const char * current() const

Returns a pointer to the current read position.


order

const inline

inline ByteOrder order() const

Returns the byte order used for multi-byte integer reads.


toString

std::string toString()

Returns the remaining unread bytes as a std::string.

Returns

String containing bytes from the current position to the end.

Protected Attributes

ReturnNameDescription
size_t_position
size_t_limit
ByteOrder_order
const char *_bytes

_position

size_t _position

_limit

size_t _limit

_order

ByteOrder _order

_bytes

const char * _bytes

Protected Methods

ReturnNameDescription
voidinit

init

void init(const char * bytes, size_t size, ByteOrder order)