nlib
nn::nlib::BinaryReader Class Referencefinal

The class for reading binary from streams (from InputStream). More...

#include "nn/nlib/BinaryReader.h"

Public Types

enum  EndianSetting {
  DEFAULT = 0,
  ENDIAN_LITTLE,
  ENDIAN_BIG
}
 Specifies the endianness. More...
 

Public Member Functions

bool Init (InputStream *stream, EndianSetting endian=DEFAULT) noexcept
 Initializes a binary reader for the specified stream and specified endianness. More...
 
template<class T >
bool Read (T *x) noexcept
 Calls binary_reader::Read().
 
int Peek () noexcept
 References the first byte of the stream. More...
 
bool Skip (size_t n) noexcept
 Skips over n bytes when reading from stream. More...
 
template<class T >
size_t ReadArray (T *x, size_t n) noexcept
 Calls binary_reader::ReadArray().
 
template<class T , size_t N>
size_t ReadArray (T(&a)[N]) noexcept
 Reads an array from a stream. More...
 
bool Close () noexcept
 Closes the binary reader. More...
 
void SetError (errno_t e) noexcept
 Sets an error. If an error has been set already, this function does not set an error. More...
 
errno_t GetErrorValue () const noexcept
 This function can get the cause of the error when writing has failed. More...
 
 operator bool () const
 Returns true if no internal error has occurred.
 
Basic Member Functions
 BinaryReader () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~BinaryReader () noexcept
 Destructor. This function does not close the stream.
 
Reading Basic Data Types
bool Read (char *x) noexcept
 Reads char-type data as binary from a stream. Returns true if successful.
 
bool Read (signed char *x) noexcept
 Reads signed char-type data as binary from a stream. Returns true if successful.
 
bool Read (unsigned char *x) noexcept
 Reads unsigned char-type data as binary from a stream. Returns true if successful.
 
bool Read (short *x) noexcept
 Reads short-type data as binary from a stream. Returns true if successful.
 
bool Read (unsigned short *x) noexcept
 Reads unsigned short-type data as binary from a stream. Returns true if successful.
 
bool Read (int *x) noexcept
 Reads int-type data as binary from a stream. Returns true if successful.
 
bool Read (unsigned int *x) noexcept
 Reads unsigned int-type data as binary from a stream. Returns true if successful.
 
bool Read (long *x) noexcept
 Reads long-type data as binary from a stream. Returns true if successful.
 
bool Read (unsigned long *x) noexcept
 Reads unsigned long-type data as binary from a stream. Returns true if successful.
 
bool Read (long long *x) noexcept
 Reads long long-type data as binary from a stream. Returns true if successful.
 
bool Read (unsigned long long *x) noexcept
 Reads unsigned long long-type data as binary from a stream. Returns true if successful.
 
bool Read (float *x) noexcept
 Reads float-type data as binary from a stream. Returns true if successful.
 
bool Read (double *x) noexcept
 Reads double-type data as binary from a stream. Returns true if successful.
 
Reading Basic Data Types Arrays
size_t ReadArray (unsigned char *x, size_t n) noexcept
 Reads an unsigned char-type data string as binary from a stream.
 
size_t ReadArray (unsigned short *x, size_t n) noexcept
 Reads an unsigned short-type data string as binary from a stream.
 
size_t ReadArray (unsigned int *x, size_t n) noexcept
 Reads an unsigned int-type data string as binary from a stream.
 
size_t ReadArray (unsigned long long *x, size_t n) noexcept
 Reads an unsigned long long-type data string as binary from a stream.
 
size_t ReadArray (unsigned long *x, size_t n) noexcept
 Reads an unsigned long-type data string as binary from a stream.
 
size_t ReadArray (float *x, size_t n) noexcept
 Reads a float-type data string as binary from a stream.
 
size_t ReadArray (double *x, size_t n) noexcept
 Reads a double-type data string as binary from a stream.
 
size_t ReadArray (signed char *x, size_t n) noexcept
 Reads a signed char-type data string as binary from a stream.
 
size_t ReadArray (char *x, size_t n) noexcept
 Reads a char-type data string as binary from a stream.
 
size_t ReadArray (short *x, size_t n) noexcept
 Reads a short-type data string as binary from a stream.
 
size_t ReadArray (int *x, size_t n) noexcept
 Reads an int-type data string as binary from a stream.
 
size_t ReadArray (long *x, size_t n) noexcept
 Reads a long-type data string as binary from a stream.
 
size_t ReadArray (long long *x, size_t n) noexcept
 Reads a long long-type data string as binary from a stream.
 

Detailed Description

The class for reading binary from streams (from InputStream).

Description
The endianness of the stream data can be specified, in which case the reading of fields is performed accordingly.
// Reads data in little endian and sets to variables.
unsigned char buf[] = {
0x00,
0x00, 0x01,
0x00, 0x01, 0x02, 0x03,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
MemoryInputStream s;
s.Init(buf);
// Initializes in little endian.
if (!r.Init(&s, r.ENDIAN_LITTLE))) { ERROR; }
int8_t v8;
int16_t v16;
int32_t v32;
int64_t v64;
// Each field is read in little endian.
if (!r.Read(&v8))) { ERROR; } // v8 == 0
if (!r.Read(&v16)) { ERROR; } // v16 == 0x100
if (!r.Read(&v32)) { ERROR; } // v32 == 0x03020100
if (!r.Read(&v64)) { ERROR; } // v64 == 0x0706050403020100LL
if (!r.Close()) { ERROR; }
Examples:
succinct/detection/detection.cpp, and succinct/kwlink/kwlink.cpp.

Definition at line 13 of file BinaryReader.h.

Member Enumeration Documentation

Specifies the endianness.

Enumerator
DEFAULT 

Writes data in the same endianness as the machine the program is running on.

ENDIAN_LITTLE 

Writes in little-endian.

ENDIAN_BIG 

Writes in big-endian.

Definition at line 15 of file BinaryReader.h.

Member Function Documentation

nn::nlib::BinaryReader::Close ( )
inlinenoexcept

Closes the binary reader.

Returns
Returns true on success. (Always successful.)
Description
Closes the binary reader and clears the base stream. The base stream is not closed by this operation.

Definition at line 107 of file BinaryReader.h.

nn::nlib::BinaryReader::GetErrorValue ( ) const
inlinenoexcept

This function can get the cause of the error when writing has failed.

Return values
0No error occurred.
EINVALInvalid argument.
EEXISTInitialized redundantly.
EBADFNo stream to read.
EIOFailed to read from the stream for some reason.

Definition at line 115 of file BinaryReader.h.

nn::nlib::BinaryReader::Init ( InputStream stream,
EndianSetting  endian = DEFAULT 
)
noexcept

Initializes a binary reader for the specified stream and specified endianness.

Parameters
[in]streamA stream.
[in]endianSpecifies the endian.
Returns
Returns true when successful.
Description
The function returns false if it is already initialized or if stream is NULL.
nn::nlib::BinaryReader::Peek ( )
inlinenoexcept

References the first byte of the stream.

Return values
-1Failure.
Anyother value The first byte of the stream.

Definition at line 49 of file BinaryReader.h.

template<class T , size_t N>
nn::nlib::BinaryReader::ReadArray ( T(&)  a[N])
inlinenoexcept

Reads an array from a stream.

Template Parameters
TThe numeric type (such as int32_t or float).
Parameters
[out]aThe array storing the data.
Returns
The amount of data that could be read. (If everything was read, the function returns N).

Definition at line 177 of file BinaryReader.h.

nn::nlib::BinaryReader::SetError ( errno_t  e)
inlinenoexcept

Sets an error. If an error has been set already, this function does not set an error.

Parameters
[in]eAn error value.
Description
If you have user code that reads user-defined types and you want to generate errors, you can use this function for that purpose.

Definition at line 112 of file BinaryReader.h.

nn::nlib::BinaryReader::Skip ( size_t  n)
inlinenoexcept

Skips over n bytes when reading from stream.

Parameters
[in]nThe number of bytes to skip.
Returns
Returns true when successful.

Definition at line 56 of file BinaryReader.h.


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