ledger-core
Public Types | Public Member Functions | List of all members
ledger::core::BytesReader Class Reference

#include <BytesReader.h>

Public Types

enum  Seek { SET, CUR, END }
 

Public Member Functions

 BytesReader (const std::vector< uint8_t > &data, unsigned long offset, unsigned long length)
 
 BytesReader (const std::vector< uint8_t > &data)
 
void seek (long offset, Seek origin)
 
uint8_t peek () const
 
std::vector< uint8_t > read (unsigned long length)
 
void read (unsigned long length, std::vector< uint8_t > &out)
 
uint8_t readNextByte ()
 
std::string readString (unsigned long length)
 
std::string readNextString ()
 
uint32_t readNextBeUint ()
 
uint32_t readNextLeUint ()
 
uint64_t readNextBeUlong ()
 
uint64_t readNextLeUlong ()
 
ledger::core::BigInt readNextBeBigInt (size_t bytes)
 
ledger::core::BigInt readNextLeBigInt (size_t bytes)
 
uint64_t readNextVarInt ()
 
std::string readNextVarString ()
 
std::vector< uint8_t > readUntilEnd ()
 
void reset ()
 
unsigned long getCursor () const
 
bool hasNext () const
 
unsigned long available () const
 

Detailed Description

A helper class used to symplify parsing of data. Beware that every reading or seeking method can throw a std::out_of_range exception.

Constructor & Destructor Documentation

◆ BytesReader() [1/2]

ledger::core::BytesReader::BytesReader ( const std::vector< uint8_t > &  data,
unsigned long  offset,
unsigned long  length 
)

Creates a new bytes reader starting at the given offset and able to read up to length bytes.

Parameters
dataThe data to read.
offsetThe reader will start reading at this index in the vector.
lengthThe maximum size on which the reader can read data.

◆ BytesReader() [2/2]

ledger::core::BytesReader::BytesReader ( const std::vector< uint8_t > &  data)
inline

Creates a new bytes reader starting at byte 0 and able to read bytes until the end of data.

Parameters
dataThe data to read.
Returns

Member Function Documentation

◆ available()

unsigned long ledger::core::BytesReader::available ( ) const

Gets the number of bytes before the end.

Returns
The number of bytes before the end.

◆ getCursor()

unsigned long ledger::core::BytesReader::getCursor ( ) const

Gets current cursor position

Returns
Current byte offset.

◆ hasNext()

bool ledger::core::BytesReader::hasNext ( ) const

Checks if the reader can read further.

Returns
True if it remains bytes to read, false otherwise.

◆ read()

std::vector< uint8_t > ledger::core::BytesReader::read ( unsigned long  length)

Reads length bytes and advance the cursor in the reader.

Parameters
lengthNumber of bytes to read.
Returns
A vector of read bytes.

◆ readNextBeUint()

uint32_t ledger::core::BytesReader::readNextBeUint ( )

Reads the next big endian unsigned int.

Returns

◆ readNextBeUlong()

uint64_t ledger::core::BytesReader::readNextBeUlong ( )

Reads the next little endian int.

Returns

◆ readNextByte()

uint8_t ledger::core::BytesReader::readNextByte ( )

Reads a single byte.

Returns

◆ readNextLeUint()

uint32_t ledger::core::BytesReader::readNextLeUint ( )

Reads the next little endian unsigned int.

Returns

◆ readNextLeUlong()

uint64_t ledger::core::BytesReader::readNextLeUlong ( )

Reads the next little endian unsigned long.

Returns

◆ readNextString()

std::string ledger::core::BytesReader::readNextString ( )

Reads characters until it reach a '\0'

Returns

◆ readString()

std::string ledger::core::BytesReader::readString ( unsigned long  length)

Reads the next string with the given length.

Parameters
lengthThe length of the string.
Returns

◆ reset()

void ledger::core::BytesReader::reset ( )

Reset cursor position and offset to 0

◆ seek()

void ledger::core::BytesReader::seek ( long  offset,
BytesReader::Seek  origin 
)

Sets the position indicator associated with the BytesReader to a new position.

Parameters
offsetNumber of bytes to offset from origin.
originPosition used as reference for the offset.

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