BinaryInputDevice Class Reference

#include <BinaryInputDevice.h>


Detailed Description

Encapsulates binary reading operations on a file.

This class opens a binary file stream for reading and offers read access functions on it.

Please note that the byte order is undefined. Also the length of each type is not defined on a global scale and may differ across compilers or platforms.

Todo:
Recheck whether this class could be replaced by a "normal InputDevice"
Todo:
Maybe some kind of a well-defined types should be used (so that they have a well-defined length)
Todo:
Maybe the byte-order should be defined

Definition at line 54 of file BinaryInputDevice.h.


Public Member Functions

 BinaryInputDevice (const std::string &name) throw ()
 Constructor.
bool good () const throw ()
 Returns whether the file can be used (is good()).
 ~BinaryInputDevice () throw ()
 Destructor.

Private Attributes

char myBuffer [1000]
 The buffer used for string parsing.
std::ifstream myStream
 The encapsulated stream.

Friends

BinaryInputDeviceoperator>> (BinaryInputDevice &os, long &l) throw ()
 Reads a long from the file (input operator).
BinaryInputDeviceoperator>> (BinaryInputDevice &os, std::string &s) throw ()
 Reads a string from the file (input operator).
BinaryInputDeviceoperator>> (BinaryInputDevice &os, bool &b) throw ()
 Reads a bool from the file (input operator).
BinaryInputDeviceoperator>> (BinaryInputDevice &os, SUMOReal &f) throw ()
 Reads a SUMOReal from the file (input operator).
BinaryInputDeviceoperator>> (BinaryInputDevice &os, unsigned int &i) throw ()
 Reads an unsigned int from the file (input operator).
BinaryInputDeviceoperator>> (BinaryInputDevice &os, int &i) throw ()
 Reads an int from the file (input operator).

Constructor & Destructor Documentation

BinaryInputDevice::BinaryInputDevice ( const std::string &  name  )  throw ()

Constructor.

Parameters:
[in] name The name of the file to open for reading

Definition at line 46 of file BinaryInputDevice.cpp.

00047         : myStream(name.c_str(), std::fstream::in|std::fstream::binary) {}

BinaryInputDevice::~BinaryInputDevice (  )  throw ()

Destructor.

Definition at line 50 of file BinaryInputDevice.cpp.

00050 {}


Member Function Documentation

bool BinaryInputDevice::good (  )  const throw ()

Returns whether the file can be used (is good()).

Returns:
Whether the file is good

Definition at line 54 of file BinaryInputDevice.cpp.

References myStream.

00054                                       {
00055     return myStream.good();
00056 }


Friends And Related Function Documentation

BinaryInputDevice& operator>> ( BinaryInputDevice os,
long &  l 
) throw () [friend]

Reads a long from the file (input operator).

Parameters:
[in,out] os The BinaryInputDevice to read the long from
[in] i The int to store the read value into
Returns:
The used BinaryInputDevice for further processing

Definition at line 103 of file BinaryInputDevice.cpp.

00103                                                    {
00104     os.myStream.read((char*) &l, sizeof(long));
00105     return os;
00106 }

BinaryInputDevice& operator>> ( BinaryInputDevice os,
std::string &  s 
) throw () [friend]

Reads a string from the file (input operator).

Reads the length of the string as an unsigned int, first. Reads then the specified number of chars into "myBuffer". Please note that the buffer has a fixed size - longer strings will cause an error.

Parameters:
[in,out] os The BinaryInputDevice to read the string from
[in] i The string to store the read value into
Returns:
The used BinaryInputDevice for further processing
Todo:
Use either a buffer with a flexible size or report an error if the buffer is too small!

Definition at line 89 of file BinaryInputDevice.cpp.

00089                                                         {
00090     unsigned int size;
00091     os >> size;
00092     if (size<BUF_MAX) {
00093         os.myStream.read((char*) &os.myBuffer, sizeof(char)*size);
00094         os.myBuffer[size] = 0;
00095         s = std::string(os.myBuffer);
00096         return os;
00097     }
00098     return os;
00099 }

BinaryInputDevice& operator>> ( BinaryInputDevice os,
bool b 
) throw () [friend]

Reads a bool from the file (input operator).

Parameters:
[in,out] os The BinaryInputDevice to read the bool from
[in] i The bool to store the read value into
Returns:
The used BinaryInputDevice for further processing

Definition at line 81 of file BinaryInputDevice.cpp.

00081                                                    {
00082     b = 0;
00083     os.myStream.read((char*) &b, sizeof(char));
00084     return os;
00085 }

BinaryInputDevice& operator>> ( BinaryInputDevice os,
SUMOReal &  f 
) throw () [friend]

Reads a SUMOReal from the file (input operator).

Parameters:
[in,out] os The BinaryInputDevice to read the SUMOReal from
[in] i The SUMOReal to store the read value into
Returns:
The used BinaryInputDevice for further processing

Definition at line 74 of file BinaryInputDevice.cpp.

00074                                                        {
00075     os.myStream.read((char*) &f, sizeof(SUMOReal));
00076     return os;
00077 }

BinaryInputDevice& operator>> ( BinaryInputDevice os,
unsigned int &  i 
) throw () [friend]

Reads an unsigned int from the file (input operator).

Parameters:
[in,out] os The BinaryInputDevice to read the unsigned int from
[in] i The unsigned int to store the read value into
Returns:
The used BinaryInputDevice for further processing

Definition at line 67 of file BinaryInputDevice.cpp.

00067                                                            {
00068     os.myStream.read((char*) &i, sizeof(unsigned int));
00069     return os;
00070 }

BinaryInputDevice& operator>> ( BinaryInputDevice os,
int &  i 
) throw () [friend]

Reads an int from the file (input operator).

Parameters:
[in,out] os The BinaryInputDevice to read the int from
[in] i The int to store the read value into
Returns:
The used BinaryInputDevice for further processing

Definition at line 60 of file BinaryInputDevice.cpp.

00060                                                   {
00061     os.myStream.read((char*) &i, sizeof(int));
00062     return os;
00063 }


Field Documentation

char BinaryInputDevice::myBuffer[1000] [private]

The buffer used for string parsing.

Definition at line 139 of file BinaryInputDevice.h.

std::ifstream BinaryInputDevice::myStream [private]

The encapsulated stream.

Definition at line 136 of file BinaryInputDevice.h.

Referenced by good(), and operator>>().


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

Generated on Wed May 5 00:06:40 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6