Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::BinaryInputStream Class Reference

Binary input file. More...

#include <geogram/basic/b_stream.h>

Inheritance diagram for GEO::BinaryInputStream:
GEO::BinaryStream

Public Member Functions

 BinaryInputStream (const std::string &file_name, int stream_endian=GEO_BIG_ENDIAN)
 Creates a new binary input stream. More...
 
 BinaryInputStream (std::istream &input, int stream_endian=GEO_BIG_ENDIAN)
 Creates a new binary input stream. More...
 
 ~BinaryInputStream ()
 Deletes the input stream. More...
 
bool OK () const
 Gets the status of the stream. More...
 
bool more () const
 Checks if there are more bytes to read. More...
 
template<class T >
BinaryInputStreamoperator>> (T &x)
 Reads a single element. More...
 
BinaryInputStreamread_opaque_data (void *ptr, size_t n)
 Reads opaque data. More...
 
BinaryInputStreamread_opaque_data (void *ptr, size_t size, size_t n)
 Reads opaque data. More...
 
template<class T >
BinaryInputStreamread_array (T *data, size_t n)
 Reads an array of elements. More...
 
void begin_record ()
 Starts reading a data record. More...
 
void end_record ()
 Stops reading a data record. More...
 
template<class T >
BinaryInputStreamread_record (T *data, size_t n)
 Reads an array of elements in a record. More...
 
std::streamoff tell () const
 Gets the position in the input sequence. More...
 
void seek (std::streamoff pos)
 Sets the position in input sequence. More...
 
void seek (std::streamoff off, std::ios_base::seekdir dir)
 Sets the position in input sequence. More...
 
- Public Member Functions inherited from GEO::BinaryStream
void set_stream_endian (int stream_endian)
 Sets the stream endianness. More...
 
int stream_endian () const
 Gets the stream endianness. More...
 
int machine_endian () const
 Gets the current architecture's endianness. More...
 
bool has_record_markers () const
 Checks support for record markers. More...
 
void set_has_record_markers (bool b)
 Enables/disables support for record markers. More...
 

Protected Member Functions

BinaryInputStreamread (char *data, size_t n, ItemSize< 1 >)
 Reads an array of elements of size 1. More...
 
BinaryInputStreamread (char *data, size_t n, ItemSize< 2 >)
 Reads an array of elements of size 2. More...
 
BinaryInputStreamread (char *data, size_t n, ItemSize< 4 >)
 Reads an array of elements of size 4. More...
 
BinaryInputStreamread (char *data, size_t n, ItemSize< 8 >)
 Reads an array of elements of size 8. More...
 
- Protected Member Functions inherited from GEO::BinaryStream
 BinaryStream (int stream_endian=GEO_BIG_ENDIAN)
 Base constructor. More...
 
void detect_machine_endian ()
 Detects the current architecture's endianness.
 

Additional Inherited Members

- Static Public Attributes inherited from GEO::BinaryStream
static const int GEO_LITTLE_ENDIAN = 0
 
static const int GEO_BIG_ENDIAN = 1
 
- Protected Attributes inherited from GEO::BinaryStream
bool swapped_
 

Detailed Description

Binary input file.

This class enables binary files to be read, while taking into account Endian problems (see BinaryStream).

Definition at line 175 of file b_stream.h.

Constructor & Destructor Documentation

◆ BinaryInputStream() [1/2]

GEO::BinaryInputStream::BinaryInputStream ( const std::string &  file_name,
int  stream_endian = GEO_BIG_ENDIAN 
)

Creates a new binary input stream.

This prepares the stream to read data from file file_name. Data is supposed to be stored with the given endianness stream_endian.

Parameters
[in]file_namepath to the file to read
[in]stream_endianthe endianness of the stream:
  • GEO_LITTLE_ENDIAN makes the stream little-endian
  • GEO_BIG_ENDIAN makes the stream big-endian (the default)

◆ BinaryInputStream() [2/2]

GEO::BinaryInputStream::BinaryInputStream ( std::istream &  input,
int  stream_endian = GEO_BIG_ENDIAN 
)

Creates a new binary input stream.

This prepares the BinaryInputStream to read data from std::istream input. Data will be stored with the given endianness stream_endian.

Parameters
[in]inputthe input stream to read from
[in]stream_endianthe endianness of the stream:
  • GEO_LITTLE_ENDIAN makes the stream little-endian
  • GEO_BIG_ENDIAN makes the stream big-endian (the default)

◆ ~BinaryInputStream()

GEO::BinaryInputStream::~BinaryInputStream ( )

Deletes the input stream.

This closes any associated std::istream and closes any associated file.

Member Function Documentation

◆ begin_record()

void GEO::BinaryInputStream::begin_record ( )

Starts reading a data record.

FORTRAN data files are structured into records, bounded by two integers indicating the size of the record. These two functions enable these integers to be read, and to use them as a validity check. If they differ, subsequent calls to OK() return false. Note that if set_had_record_markers() has been called with false, the records are supposed to be continuously written in the file (without markers).

◆ end_record()

void GEO::BinaryInputStream::end_record ( )

Stops reading a data record.

This verifies that the record data is properly enclosed with the same marker, that is: the marker at the current stream position matches the marker found at the beginning of the record (see begin_record()).

◆ more()

bool GEO::BinaryInputStream::more ( ) const

Checks if there are more bytes to read.

Return values
trueif the stream is not at end-of-file
falseotherwise

◆ OK()

bool GEO::BinaryInputStream::OK ( ) const

Gets the status of the stream.

Return values
trueif the stream is valid
falseif an error occurred

◆ operator>>()

template<class T >
BinaryInputStream& GEO::BinaryInputStream::operator>> ( T &  x)
inline

Reads a single element.

Reads an element of type T from the stream and stores it in x. Type T must be a numeric type, other types are not supported.

Parameters
[in]xa reference to a element of type T
Template Parameters
Tthe type of the element to read. This must be a numeric type
Returns
a reference to this stream

Definition at line 237 of file b_stream.h.

◆ read() [1/4]

BinaryInputStream& GEO::BinaryInputStream::read ( char *  data,
size_t  n,
ItemSize< 1 >   
)
inlineprotected

Reads an array of elements of size 1.

Parameters
[in]dataan array of elements of size 1
[in]nthe number of elements to read
Returns
a reference to this stream

Definition at line 372 of file b_stream.h.

◆ read() [2/4]

BinaryInputStream& GEO::BinaryInputStream::read ( char *  data,
size_t  n,
ItemSize< 2 >   
)
protected

Reads an array of elements of size 2.

Parameters
[in]dataan array of elements of size 2
[in]nthe number of elements to read
Returns
a reference to this stream

◆ read() [3/4]

BinaryInputStream& GEO::BinaryInputStream::read ( char *  data,
size_t  n,
ItemSize< 4 >   
)
protected

Reads an array of elements of size 4.

Parameters
[in]dataan array of elements of size 4
[in]nthe number of elements to read
Returns
a reference to this stream

◆ read() [4/4]

BinaryInputStream& GEO::BinaryInputStream::read ( char *  data,
size_t  n,
ItemSize< 8 >   
)
protected

Reads an array of elements of size 8.

Parameters
[in]dataan array of elements of size 8
[in]nthe number of elements to read
Returns
a reference to this stream

◆ read_array()

template<class T >
BinaryInputStream& GEO::BinaryInputStream::read_array ( T *  data,
size_t  n 
)
inline

Reads an array of elements.

Reads n elements of type T from the stream and stores them in array pointed to by data. The array must be large enough to receive n elements. Type T must be a numeric type, other types are not supported.

Parameters
[in]dataan array of at least n elements of type T.
[in]nnumber of items to read
Template Parameters
Tthe type of the elements to read
Returns
a reference to this stream

Definition at line 286 of file b_stream.h.

◆ read_opaque_data() [1/2]

BinaryInputStream& GEO::BinaryInputStream::read_opaque_data ( void *  ptr,
size_t  n 
)
inline

Reads opaque data.

Reads n bytes from the stream and stores them in the byte array ptr. Bytes read from the stream are stored without conversion in the array ptr.

Parameters
[in]ptran array of bytes
[in]nnumber of bytes to read
Returns
a reference to this stream

Definition at line 253 of file b_stream.h.

◆ read_opaque_data() [2/2]

BinaryInputStream& GEO::BinaryInputStream::read_opaque_data ( void *  ptr,
size_t  size,
size_t  n 
)
inline

Reads opaque data.

Reads n elements of size bytes from the stream and stores them in the byte array ptr. Bytes read from the stream are stored without conversion in the array ptr.

Parameters
[in]ptran array of bytes
[in]sizesize of the elements to read
[in]nnumber of elements to read
Returns
a reference to this stream

Definition at line 268 of file b_stream.h.

◆ read_record()

template<class T >
BinaryInputStream& GEO::BinaryInputStream::read_record ( T *  data,
size_t  n 
)
inline

Reads an array of elements in a record.

Reads n elements of type T enclosed in a data record from the stream and stores them in array pointed to by data. The array must be large enough to receive n elements. Type T must be a numeric type, other types are not supported.

Parameters
[in]dataan array of at least n elements of type T.
[in]nnumber of items to read
Returns
a reference to this stream

Definition at line 327 of file b_stream.h.

◆ seek() [1/2]

void GEO::BinaryInputStream::seek ( std::streamoff  off,
std::ios_base::seekdir  dir 
)
inline

Sets the position in input sequence.

Parameters
[in]offoffset value, relative to the dir parameter
[in]dirthe direction in which to seek:
  • ios_base::beg - beginning of the stream
  • ios_base::cur - current position in the stream
  • ios_base::end - end of the stream

Definition at line 361 of file b_stream.h.

◆ seek() [2/2]

void GEO::BinaryInputStream::seek ( std::streamoff  pos)
inline

Sets the position in input sequence.

Parameters
[in]posthe new absolute position in the stream

Definition at line 349 of file b_stream.h.

◆ tell()

std::streamoff GEO::BinaryInputStream::tell ( ) const
inline

Gets the position in the input sequence.

Returns
The absolute position in the stream

Definition at line 341 of file b_stream.h.


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