BTK  0.3dev.0
Open-source library to visualize/process biomechanical data
List of all members | Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes
btk::BinaryFileStream Class Referenceabstract

An abstract class which read/write binary file in VAX and IEEE floating format with the corresponding byte order. More...

#include <btkBinaryFileStream.h>

Inheritance diagram for btk::BinaryFileStream:
Inheritance graph
[legend]

Public Types

typedef std::ios_base::iostate IOState
 
typedef std::ios_base::openmode OpenMode
 
typedef std::ios_base::seekdir SeekDir
 
typedef std::streamoff StreamOffset
 
typedef std::streampos StreamPosition
 

Public Member Functions

bool Bad () const
 
void Clear (IOState flags=GoodBit)
 
void Close ()
 
bool EndFile () const
 
bool Fail () const
 
size_t Fill (size_t nb)
 
IOState GetExceptions ()
 
const RawFileStreamGetStream () const
 
bool Good () const
 
bool IsOpen () const
 
void Open (const std::string &filename, OpenMode mode)
 
char ReadChar ()
 
void ReadChar (size_t nb, char *values)
 
void ReadChar (std::vector< char > &values)
 
std::vector< char > ReadChar (size_t nb)
 
virtual double ReadDouble ()=0
 
void ReadDouble (size_t nb, double *values)
 
void ReadDouble (std::vector< double > &values)
 
std::vector< double > ReadDouble (size_t nb)
 
virtual float ReadFloat ()=0
 
void ReadFloat (size_t nb, float *values)
 
void ReadFloat (std::vector< float > &values)
 
std::vector< float > ReadFloat (size_t nb)
 
virtual int16_t ReadI16 ()=0
 
void ReadI16 (size_t nb, int16_t *values)
 
void ReadI16 (std::vector< int16_t > &values)
 
std::vector< int16_t > ReadI16 (size_t nb)
 
virtual int32_t ReadI32 ()=0
 
void ReadI32 (size_t nb, int32_t *values)
 
void ReadI32 (std::vector< int32_t > &values)
 
std::vector< int32_t > ReadI32 (size_t nb)
 
int8_t ReadI8 ()
 
void ReadI8 (size_t nb, int8_t *values)
 
void ReadI8 (std::vector< int8_t > &values)
 
std::vector< int8_t > ReadI8 (size_t nb)
 
std::string ReadString (size_t nbChar)
 
void ReadString (size_t nb, size_t nbChar, std::string *values)
 
void ReadString (size_t nbChar, std::vector< std::string > &values)
 
std::vector< std::string > ReadString (size_t nb, size_t nbChar)
 
virtual uint16_t ReadU16 ()=0
 
void ReadU16 (size_t nb, uint16_t *values)
 
void ReadU16 (std::vector< uint16_t > &values)
 
std::vector< uint16_t > ReadU16 (size_t nb)
 
virtual uint32_t ReadU32 ()=0
 
void ReadU32 (size_t nb, uint32_t *values)
 
void ReadU32 (std::vector< uint32_t > &values)
 
std::vector< uint32_t > ReadU32 (size_t nb)
 
uint8_t ReadU8 ()
 
void ReadU8 (size_t nb, uint8_t *values)
 
void ReadU8 (std::vector< uint8_t > &values)
 
std::vector< uint8_t > ReadU8 (size_t nb)
 
void SeekRead (StreamOffset offset, SeekDir dir)
 
void SeekWrite (StreamOffset offset, SeekDir dir)
 
void SetExceptions (IOState except)
 
void SwapStream (BinaryFileStream *toSwap)
 
StreamPosition TellRead () const
 
size_t Write (int8_t i8)
 
size_t Write (const std::vector< int8_t > &rVectorI8)
 
size_t Write (uint8_t u8)
 
size_t Write (const std::vector< uint8_t > &rVectorU8)
 
virtual size_t Write (int16_t i16)=0
 
size_t Write (const std::vector< int16_t > &rVectorI16)
 
virtual size_t Write (uint16_t u16)=0
 
size_t Write (const std::vector< uint16_t > &rVectorU16)
 
virtual size_t Write (int32_t i32)=0
 
size_t Write (const std::vector< int32_t > &rVectorI32)
 
virtual size_t Write (uint32_t u32)=0
 
size_t Write (const std::vector< uint32_t > &rVectorU32)
 
virtual size_t Write (float f)=0
 
size_t Write (const std::vector< float > &rVectorFloat)
 
size_t Write (const std::string &rString)
 
size_t Write (const std::vector< std::string > &rVectorString)
 
virtual ~BinaryFileStream ()
 

Static Public Attributes

static const IOState BadBit = std::ios_base::badbit
 
static const SeekDir Begin = std::ios_base::beg
 
static const SeekDir Current = std::ios_base::cur
 
static const SeekDir End = std::ios_base::end
 
static const IOState EndFileBit = std::ios_base::eofbit
 
static const IOState FailBit = std::ios_base::failbit
 
static const IOState GoodBit = std::ios_base::goodbit
 
static const OpenMode In = std::ios_base::in
 
static const OpenMode Out = std::ios_base::out
 
static const OpenMode Truncate = std::ios_base::trunc
 

Protected Member Functions

 BinaryFileStream ()
 
 BinaryFileStream (const std::string &filename, OpenMode mode)
 

Protected Attributes

RawFileStreammp_Stream
 

Detailed Description

An abstract class which read/write binary file in VAX and IEEE floating format with the corresponding byte order.

Especially, this file stream can:

The simplest way to use this binary file stream is to use the class btk::NativeBinaryFileStream. It creates a stream to read/write binary file encoded in the format of the porcessor used to compile the code.

As the file stream classes in the standard library you can indicates if the file is to read or write data. The following code presents the definion of input and output streams in binary format.

* btk::NativeBinaryFileStreamfstream ifs, ofs;
* ifs.Open(filename, btk::BinaryFileStream::In); // read
* ofs.Open(filename, btk::BinaryFileStream::Out); // write
*

This class has also exceptions. To use them, you have to set the exception mask. For example:

* btk::NativeBinaryFileStreamfstream ifs;
*
See Also
VAXLittleEndianBinaryFileStream, IEEELittleEndianBinaryFileStream, IEEEBigEndianBinaryFileStream, NativeBinaryFileStream

Member Typedef Documentation

Bitmask type to represent stream error state flags.

Flags describing the requested I/O mode for the file.

Flags representing the seeking direction of a stream seeking operation.

Type to represent position offsets in a stream.

Type to represent positions in a stream.

Constructor & Destructor Documentation

btk::BinaryFileStream::~BinaryFileStream ( )
inlinevirtual

Destroys the abstract binary file stream.

btk::BinaryFileStream::BinaryFileStream ( )
inlineprotected

Default abstract constructor.

btk::BinaryFileStream::BinaryFileStream ( const std::string &  filename,
OpenMode  mode 
)
inlineprotected

Associates the file with the filename filename using the option mode to this object.

If the opening is not successfull, then the FailBit is set. You can check its state by using the method Fail().

Member Function Documentation

bool btk::BinaryFileStream::Bad ( ) const
inline

Checks if badbit is set.

void btk::BinaryFileStream::Clear ( IOState  flags = GoodBit)
inline

Sets error state flags.

void btk::BinaryFileStream::Close ( )
inline

Closes file.

bool btk::BinaryFileStream::EndFile ( ) const
inline

Checks if eofbit is set.

bool btk::BinaryFileStream::Fail ( ) const
inline

Checks if either failbit or badbit is set.

size_t btk::BinaryFileStream::Fill ( size_t  nb)

Fills nb bytes with 0x00 in the stream.

IOState btk::BinaryFileStream::GetExceptions ( )
inline

Gets exception mask.

const RawFileStream * btk::BinaryFileStream::GetStream ( ) const
inline

Return the raw stream associated with this binary file stream.

bool btk::BinaryFileStream::Good ( ) const
inline

Checks if the state of the stream is good for I/O operations.

bool btk::BinaryFileStream::IsOpen ( ) const
inline

Checks if a file is open.

void btk::BinaryFileStream::Open ( const std::string &  filename,
OpenMode  mode 
)
inline

Opens file.

char btk::BinaryFileStream::ReadChar ( )

Extracts one character from the stream.

void btk::BinaryFileStream::ReadChar ( size_t  nb,
char *  values 
)

Extracts nb chars and set them in the array values.

void btk::BinaryFileStream::ReadChar ( std::vector< char > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<char> val(45,0);
* bfs.ReadChar(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadChar(5, &(val[40])); // assign value #41-45
*
std::vector< char > btk::BinaryFileStream::ReadChar ( size_t  nb)
inline

Extracts nb characters and return them as a vector.

float btk::BinaryFileStream::ReadDouble ( )
pure virtual
void btk::BinaryFileStream::ReadDouble ( size_t  nb,
double *  values 
)

Extracts nb doubles and set them in the array values.

void btk::BinaryFileStream::ReadDouble ( std::vector< double > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<double> val(45,0);
* bfs.ReadDouble(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadDouble(5, &(val[40])); // assign value #41-45
*
std::vector< double > btk::BinaryFileStream::ReadDouble ( size_t  nb)
inline

Extracts nb doubles and return them as a vector.

float btk::BinaryFileStream::ReadFloat ( )
pure virtual
void btk::BinaryFileStream::ReadFloat ( size_t  nb,
float *  values 
)

Extracts nb floats and set them in the array values.

void btk::BinaryFileStream::ReadFloat ( std::vector< float > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<float> val(45,0);
* bfs.ReadFloat(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadFloat(5, &(val[40])); // assign value #41-45
*
std::vector< float > btk::BinaryFileStream::ReadFloat ( size_t  nb)
inline

Extracts nb floats and return them as a vector.

int16_t btk::BinaryFileStream::ReadI16 ( )
pure virtual
void btk::BinaryFileStream::ReadI16 ( size_t  nb,
int16_t *  values 
)

Extracts nb signed 16-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadI16 ( std::vector< int16_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<int16_t> val(45,0);
* bfs.ReadI16(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadI16(5, &(val[40])); // assign value #41-45
*
std::vector< int16_t > btk::BinaryFileStream::ReadI16 ( size_t  nb)
inline

Extracts nb signed 16-bit integers and return them as a vector.

int32_t btk::BinaryFileStream::ReadI32 ( )
pure virtual
void btk::BinaryFileStream::ReadI32 ( size_t  nb,
int32_t *  values 
)

Extracts nb signed 32-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadI32 ( std::vector< int32_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<int32_t> val(45,0);
* bfs.ReadI32(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadI32(5, &(val[40])); // assign value #41-45
*
std::vector< int32_t > btk::BinaryFileStream::ReadI32 ( size_t  nb)
inline

Extracts nb signed 32-bit integers and return them as a vector.

int8_t btk::BinaryFileStream::ReadI8 ( )

Extracts one signed 8-bit integer.

void btk::BinaryFileStream::ReadI8 ( size_t  nb,
int8_t *  values 
)

Extracts nb signed 8-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadI8 ( std::vector< int8_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<int8_t> val(45,0);
* bfs.ReadI8(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadI8(5, &(val[40])); // assign value #41-45
*
std::vector< int8_t > btk::BinaryFileStream::ReadI8 ( size_t  nb)
inline

Extracts nb signed 8-bit integers and return them as a vector.

std::string btk::BinaryFileStream::ReadString ( size_t  nbChar)

Extracts one string with nbChar characters.

void btk::BinaryFileStream::ReadString ( size_t  nb,
size_t  nbChar,
std::string *  values 
)

Extracts nb unsigned 8-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadString ( size_t  nbChar,
std::vector< std::string > &  values 
)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<uint8_t> val(45,0);
* bfs.ReadU8(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadU8(5, &(val[40])); // assign value #41-45
*
std::vector< std::string > btk::BinaryFileStream::ReadString ( size_t  nb,
size_t  nbChar 
)
inline

Extracts nb strings with nb Charcharacters and return them as a vector.

uint16_t btk::BinaryFileStream::ReadU16 ( )
pure virtual
void btk::BinaryFileStream::ReadU16 ( size_t  nb,
uint16_t *  values 
)

Extracts nb unsigned 16-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadU16 ( std::vector< uint16_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<uint16_t> val(45,0);
* bfs.ReadU16(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadU16(5, &(val[40])); // assign value #41-45
*
std::vector< uint16_t > btk::BinaryFileStream::ReadU16 ( size_t  nb)
inline

Extracts nb unsigned 16-bit integers and return them as a vector.

uint32_t btk::BinaryFileStream::ReadU32 ( )
pure virtual
void btk::BinaryFileStream::ReadU32 ( size_t  nb,
uint32_t *  values 
)

Extracts nb unsigned 32-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadU32 ( std::vector< uint32_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<uint32_t> val(45,0);
* bfs.ReadU32(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadU32(5, &(val[40])); // assign value #41-45
*
std::vector< uint32_t > btk::BinaryFileStream::ReadU32 ( size_t  nb)
inline

Extracts nb unsigned 32-bit integers and return them as a vector.

uint8_t btk::BinaryFileStream::ReadU8 ( )

Extracts one unsigned 8-bit integer.

void btk::BinaryFileStream::ReadU8 ( size_t  nb,
uint8_t *  values 
)

Extracts nb unsigned 8-bit integers and set them in the array values.

void btk::BinaryFileStream::ReadU8 ( std::vector< uint8_t > &  values)
inline

Extracts exactly the number of elements set in the vector values

Note
In case you want to assign only a part of the vector, you can use the method using an array. For example;
* std::vector<uint8_t> val(45,0);
* bfs.ReadU8(10, &(val[0])); // assign value #0-9
* // ...
* bfs.ReadU8(5, &(val[40])); // assign value #41-45
*
std::vector< uint8_t > btk::BinaryFileStream::ReadU8 ( size_t  nb)
inline

Extracts nb unsigned 8-bit integers and return them as a vector.

void btk::BinaryFileStream::SeekRead ( StreamOffset  offset,
SeekDir  dir 
)
inline

Moves the get pointer by nb bytes in the seeking direction dir.

void btk::BinaryFileStream::SeekWrite ( StreamOffset  offset,
SeekDir  dir 
)
inline

Moves the set pointer by nb bytes in the seeking direction dir.

void btk::BinaryFileStream::SetExceptions ( IOState  except)
inline

Sets exception mask.

void btk::BinaryFileStream::SwapStream ( BinaryFileStream toSwap)

Swap streams.

Warning
The exceptions set are embedded with the stream.
StreamPosition btk::BinaryFileStream::TellRead ( ) const
inline

Get position of the get pointer.

size_t btk::BinaryFileStream::Write ( int8_t  i8)

Writes the character c in the stream an return its size. Writes the signed 8-bit integer i8 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( const std::vector< int8_t > &  rVectorI8)

Writes the vector of signed 8-bit integers rVectorI8 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( uint8_t  u8)

Writes the unsigned 8-bit integer u8 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( const std::vector< uint8_t > &  rVectorU8)

Writes the vector of unsigned 8-bit integers rVectorU8 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( int16_t  i16)
pure virtual
size_t btk::BinaryFileStream::Write ( const std::vector< int16_t > &  rVectorI16)

Writes the vector of signed 16-bit integers rVectorI16 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( uint16_t  u16)
pure virtual
size_t btk::BinaryFileStream::Write ( const std::vector< uint16_t > &  rVectorU16)

Writes the vector of unsigned 16-bit integers rVectorU16 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( int32_t  i32)
pure virtual
size_t btk::BinaryFileStream::Write ( const std::vector< int32_t > &  rVectorI32)

Writes the vector of signed 32-bit integers rVectorI32 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( uint32_t  u32)
pure virtual
size_t btk::BinaryFileStream::Write ( const std::vector< uint32_t > &  rVectorU32)

Writes the vector of unsigned 32-bit integers rVectorU32 in the stream an return its size.

size_t btk::BinaryFileStream::Write ( float  f)
pure virtual
size_t btk::BinaryFileStream::Write ( const std::vector< float > &  rVectorFloat)

Writes the vector of floats rVectorFloat in the stream an return its size.

size_t btk::BinaryFileStream::Write ( const std::string &  rString)

Writes the string rString in the stream an return its size.

size_t btk::BinaryFileStream::Write ( const std::vector< std::string > &  rVectorString)

Writes the vector of strings rVectorString in the stream an return its size.

Member Data Documentation

btk::BinaryFileStream::BadBit = std::ios_base::badbit
static

Error due to the failure of an input/output operation on the stream buffer.

btk::BinaryFileStream::Begin = std::ios_base::beg
static

Beginning of the stream buffer.

btk::BinaryFileStream::Current = std::ios_base::cur
static

Current position in the stream buffer.

btk::BinaryFileStream::End = std::ios_base::end
static

End of the stream buffer.

btk::BinaryFileStream::EndFileBit = std::ios_base::eofbit
static

End-of-File reached while performing an extracting operation on an input stream.

btk::BinaryFileStream::FailBit = std::ios_base::failbit
static

The last input operation failed because of an error related to the internal logic of the operation itself.

btk::BinaryFileStream::GoodBit = std::ios_base::goodbit
static

No error. Represents the absence of all the above (the value zero).

btk::BinaryFileStream::In = std::ios_base::in
static

Allows input operations on the stream.

btk::BinaryFileStream::mp_Stream
protected

Binary stream which read/write data.

btk::BinaryFileStream::Out = std::ios_base::out
static

Allows output operations on the stream.

btk::BinaryFileStream::Truncate = std::ios_base::trunc
static

Any content is erased.The file is assumed to be zero-length.