DUtils::BinaryFile Class Reference

Manages files in binary format. More...

#include <BinaryFile.h>

List of all members.

Public Member Functions

 BinaryFile (void)
 ~BinaryFile (void)
 BinaryFile (const char *filename, const FILE_MODES mode)
 BinaryFile (const string &filename, const FILE_MODES mode)
void OpenForReading (const char *filename)
void OpenForReading (const string &filename)
void OpenForWriting (const char *filename)
void OpenForWriting (const string &filename)
void OpenForAppending (const char *filename)
void OpenForAppending (const string &filename)
bool Eof ()
void Close ()
void DiscardNextByte ()
void DiscardBytes (int count)
unsigned int BytesRead ()
BinaryFileoperator<< (char v)
BinaryFileoperator<< (int v)
BinaryFileoperator<< (float v)
BinaryFileoperator<< (double v)
BinaryFileoperator>> (char &v)
BinaryFileoperator>> (int &v)
BinaryFileoperator>> (float &v)
BinaryFileoperator>> (double &v)

Protected Member Functions

void Init (const char *filename, const FILE_MODES mode)
void setEndianness ()
void hton_f (float v, char buf[8]) const
void hton_d (double d, char buf[8]) const
float ntoh_f (char buf[8]) const
double ntoh_d (char buf[8]) const
bool isLittleEndian () const

Protected Attributes

FILE_MODES m_mode
 Opening mode.
fstream m_f
 File stream.
char m_aux [8]
 Auxiliar buffer.
int m_is_little_endian
 Current machine endianness.

Detailed Description

Manages files in binary format.


Constructor & Destructor Documentation

BinaryFile::BinaryFile ( void   ) 

Creates a binary file with no file

BinaryFile::~BinaryFile ( void   ) 

Closes any opened file

BinaryFile::BinaryFile ( const char *  filename,
const FILE_MODES  mode 
)

Creates a binary file by opening a file

Parameters:
filename 
mode,: READ or WRITE
Exceptions:
DException if cannot open the file
BinaryFile::BinaryFile ( const string &  filename,
const FILE_MODES  mode 
)

Creates a binary file by opening a file

Parameters:
filename 
mode,: READ or WRITE
Exceptions:
DException if cannot open the file

Member Function Documentation

unsigned int BinaryFile::BytesRead (  ) 

Returns the number of bytes read in reading mode

Returns:
number of bytes read
void BinaryFile::Close (  ) 

Closes any opened file. It is not necessary to call this function explicitly

void BinaryFile::DiscardBytes ( int  count  ) 

Reads n bytes and discards them

Parameters:
count number of bytes to discard
Exceptions:
DException if wrong access mode
void DUtils::BinaryFile::DiscardNextByte (  )  [inline]

Reads the next byte and throws it away

Exceptions:
DException if wrong access mode
bool BinaryFile::Eof (  )  [inline]

Says whether the end of the file has been reached. Requires to read the end of the file to return true

Returns:
true iff the end of the file has been already read
Exceptions:
DException if wrong access mode
void BinaryFile::hton_d ( double  d,
char  buf[8] 
) const [protected]

Converts a double into 8 bytes in network order

Parameters:
d double value
buf (out) byte buffer output
void BinaryFile::hton_f ( float  v,
char  buf[8] 
) const [protected]

Converts a float into 4 bytes in network order

Parameters:
v float value
buf (out) byte buffer output. Only buf[0..3] is used
void BinaryFile::Init ( const char *  filename,
const FILE_MODES  mode 
) [protected]

Initializes the object by opening a file

Parameters:
filename file to open
mode opening mode
Exceptions:
DException if cannot open the file
bool DUtils::BinaryFile::isLittleEndian (  )  const [inline, protected]

Returns if this machine uses little endian

Returns:
true iff little endian
double BinaryFile::ntoh_d ( char  buf[8]  )  const [protected]

Converts an array of bytes in network order into a 8 byte double

Parameters:
buf byte array
Returns:
double value
float BinaryFile::ntoh_f ( char  buf[8]  )  const [protected]

Converts an array of bytes in network order into a 4 byte float

Parameters:
buf byte array. only buf[0..3] is used
Returns:
float value
void DUtils::BinaryFile::OpenForAppending ( const string &  filename  )  [inline]

Opens a file for writing at the end. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot open the file
void BinaryFile::OpenForAppending ( const char *  filename  ) 

Opens a file for writing at the end. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot open the file
void DUtils::BinaryFile::OpenForReading ( const string &  filename  )  [inline]

Opens a file for reading. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot open the file
void BinaryFile::OpenForReading ( const char *  filename  ) 

Opens a file for reading. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot open the file
void DUtils::BinaryFile::OpenForWriting ( const string &  filename  )  [inline]

Opens a file for writing. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot create the file
void BinaryFile::OpenForWriting ( const char *  filename  ) 

Opens a file for writing. It closes any other opened file

Parameters:
filename 
Exceptions:
DException if cannot create the file
BinaryFile & BinaryFile::operator<< ( double  v  ) 

Writes a 8 byte float value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator<< ( float  v  ) 

Writes a 4 byte float value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator<< ( int  v  ) 

Writes a 4 byte integer value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator<< ( char  v  ) 

Writes a byte char

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator>> ( double &  v  ) 

Reads a 8 byte float value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator>> ( float &  v  ) 

Reads a 4 byte float value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator>> ( int &  v  ) 

Reads a 4 byte integer value

Exceptions:
DException if wrong access mode
BinaryFile & BinaryFile::operator>> ( char &  v  ) 

Reads a byte char

Exceptions:
DException if wrong access mode
void BinaryFile::setEndianness (  )  [protected]

Checks the endianness of this machine


The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Enumerations Friends
Generated on Thu Feb 7 19:16:00 2013 by  doxygen 1.6.3