last update 20 Sep 2009 |
#include <cvrEndianness.h>
Public Member Functions | |
endianness () | |
~endianness () | |
Static Public Member Functions | |
static bool | read (std::istream &in, byte &data) |
static bool | read (std::istream &in, ubyte &data) |
static bool | read (std::istream &in, int16 &data) |
static bool | read (std::istream &in, uint16 &data) |
static bool | read (std::istream &in, int32 &data) |
static bool | read (std::istream &in, uint32 &data) |
static bool | write (std::ostream &out, const byte &data) |
static bool | write (std::ostream &out, const ubyte &data) |
static bool | write (std::ostream &out, const int16 &data) |
static bool | write (std::ostream &out, const uint16 &data) |
static bool | write (std::ostream &out, const int32 &data) |
static bool | write (std::ostream &out, const uint32 &data) |
This template class is used to read from or write data to a file that was created with a known endianness. For example, Windows BMP image files have a header with some integers written with little endianness.
To be more specific, the problem this class tries to solve is the following:
Let us assume we have a binary file (and not an ASCII file) with following byte stream:
0x01 0x02 0x03 0x04 ...
and we use following code to read an 32 bit integer (i.e. 4 bytes long):
cvr::uint32 integer; // hold the values been readed std::istream in("theFile.dat"); // the input stream ... in.read(&integer,4); // reads 4 bytes on "integer"
With a little endian system (for example Intel Pentium), the contents of integer
will be 0x04030201.
With a big endian system (for example Motorola/IBM PowerPC), the contents of integer
will be 0x01020304.
We therefore need a class, which allow us to load data from a file with the correct endiannes!
The detection of the system endianness will be done at configuration time of the library.:
WORDS_BIGENDIAN
is defined as 1.
WORDS_BIGENDIAN
is defined only in big endian systems.Template behaviour.
The class cvr::endianness is a template class of an enumerate that can be LittleEndian or BigEndian, defined in the lti namespace.
cvr::endianness<cvr::LittleEndian> cvr::endianness<cvr::BigEndian>
You can check for the endianness used as default by your system with systemEndianness().
Please note that all methods are static, so that you don't need an instance of the class to use them.
cvr::endianness< E >::endianness | ( | ) |
Default constructor.
cvr::endianness< E >::~endianness | ( | ) |
Destructor.
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
uint32 & | data | |||
) | [static] |
Read a uint32.
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
int32 & | data | |||
) | [static] |
Read a int32.
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
uint16 & | data | |||
) | [static] |
Read a uint16 (2 bytes).
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
int16 & | data | |||
) | [static] |
Read a int16 (2 bytes).
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
ubyte & | data | |||
) | [static] |
Read an unsigned byte from the stream.
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::read | ( | std::istream & | in, | |
byte & | data | |||
) | [static] |
Read a byte from the stream.
in | input stream | |
data | variable where the data should be stored |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const uint32 & | data | |||
) | [static] |
Write a uint32.
out | output stream | |
data | variable with the data to be stored on the file |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const int32 & | data | |||
) | [static] |
Write a int32.
out | output stream | |
data | variable with the data to be stored on the file |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const uint16 & | data | |||
) | [static] |
Write a uint16 (2 bytes).
out | output stream | |
data | variable with the data to be stored on the file |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const int16 & | data | |||
) | [static] |
Write a int16 (2 bytes).
out | output stream | |
data | variable with the data to be stored on the file |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const ubyte & | data | |||
) | [static] |
Write a ubyte.
out | output stream | |
data | variable with the data to be stored on the file |
static bool cvr::endianness< E >::write | ( | std::ostream & | out, | |
const byte & | data | |||
) | [static] |
Write a byte.
out | output stream | |
data | variable with the data to be stored on the file |