MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BinaryReader Class Reference

This gives functionality to read information from binary formats like STL etc. This class is complete stand-alone and is tested with one any reference to other MecuryDPM code except Vections and Logger. More...

#include <BinaryReader.h>

Public Member Functions

 BinaryReader (std::string)
 Default constuction, requires to users to prove the name of the file that will be opened. More...
 
 ~BinaryReader ()
 Destructor, simple closes the file. More...
 
std::string readString (unsigned int numChar)
 reads the next so many Characters (bytes) as a std::string More...
 
double readDouble (unsigned int size)
 read the next so many bytes as a double More...
 
unsigned int readUnsignedInt (unsigned int size)
 read the next so many bytes as a unsined int More...
 
double readFloat (unsigned int size)
 read the next so many bytes as a double (not in this case they were saves as a float orgainlly) More...
 
void ignoreChar (unsigned int size)
 read and ignore the next number of characters More...
 

Private Member Functions

void openFile (std::string fileName)
 opens the file with fileName More...
 
void closeFile ()
 close the file with fileName More...
 

Private Attributes

std::ifstream binaryFile_
 The pointer for the binary file. More...
 

Detailed Description

This gives functionality to read information from binary formats like STL etc. This class is complete stand-alone and is tested with one any reference to other MecuryDPM code except Vections and Logger.

Definition at line 36 of file BinaryReader.h.

Constructor & Destructor Documentation

BinaryReader::BinaryReader ( std::string  fileName)
explicit

Default constuction, requires to users to prove the name of the file that will be opened.

Parameters
in[fileName fileName of the binary file which is to be opened This is the default and only constuctor and it calls the private method openFile.

Definition at line 41 of file BinaryReader.cc.

References openFile().

42 {
43  openFile(fileName);
44 }
void openFile(std::string fileName)
opens the file with fileName
Definition: BinaryReader.cc:66
BinaryReader::~BinaryReader ( )

Destructor, simple closes the file.

This is the destructor it simple closes the file as this is the only memory that needs freeing up

Definition at line 49 of file BinaryReader.cc.

References closeFile().

50 {
51  closeFile();
52 }
void closeFile()
close the file with fileName
Definition: BinaryReader.cc:57

Member Function Documentation

void BinaryReader::closeFile ( )
private

close the file with fileName

Simple function that closes the file; this is private and can only be called by the destructor

Definition at line 57 of file BinaryReader.cc.

References binaryFile_.

Referenced by ~BinaryReader().

58 {
59  binaryFile_.close();
60 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
void BinaryReader::ignoreChar ( unsigned int  size)

read and ignore the next number of characters

Parameters
[in]sizenumber of bytpes to be ignored

Reads and disgards the next size characters size*8 bytes from the binary file

Definition at line 133 of file BinaryReader.cc.

References binaryFile_.

Referenced by WallHandler::readTriangleWall().

134 {
135  char tempRead[size];
136  binaryFile_.read(tempRead, size);
137 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
void BinaryReader::openFile ( std::string  fileName)
private

opens the file with fileName

Parameters
[in]fileNamefileName of the binary file which is to be opened

This opens the file associated with fileName, note the file is points is stored in the private varibles binaryFile_

Definition at line 66 of file BinaryReader.cc.

References binaryFile_, ERROR, and logger.

Referenced by BinaryReader().

67 {
68  binaryFile_.open(fileName, std::ios::binary);
69  if (!binaryFile_)
70  {
71  logger(ERROR, "BinaryReader::openFile Could not open file; file % not found", fileName);
72  }
73 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here...
double BinaryReader::readDouble ( unsigned int  size)

read the next so many bytes as a double

Parameters
[in]sizeThis is the size in bytes of the double stored in the file
Returns
a double that is the double read from the file

Usage myDouble = readDouble(4) would read a 8*4 = 32 bit double from the file; or myDouble = readDouble(8) would read a 64 bit double from the file.

Definition at line 94 of file BinaryReader.cc.

References binaryFile_.

95 {
96  char tempRead[size];
97  binaryFile_.read(tempRead, 4);
98  double* returnDoublePtr = reinterpret_cast<double*>(tempRead);
99  return (*returnDoublePtr);
100 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
double BinaryReader::readFloat ( unsigned int  size)

read the next so many bytes as a double (not in this case they were saves as a float orgainlly)

Parameters
[in]sizeThis is the size in bytes of the float stored in the file
Returns
a double (not a float) that is the upcast of the float that is read in.

Usage myDouble = readFloat(4) would read 32 bit float from file covert it to a double and then return this double.

See also
BinaryReader::readDouble Note, this is different to readDouble as the binary represation of a double and a float is different.

Definition at line 108 of file BinaryReader.cc.

References binaryFile_.

Referenced by WallHandler::readTriangleWall().

109 {
110  char tempRead[size];
111  binaryFile_.read(tempRead, 4);
112  float* returnFloatPtr = reinterpret_cast<float*>(tempRead);
113  return static_cast<double>(*returnFloatPtr);
114 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
std::string BinaryReader::readString ( unsigned int  numChar)

reads the next so many Characters (bytes) as a std::string

Parameters
[in]numCharThe number of characters to be read in from the binary file.
Returns
The function returns a std::string which is as long as the numChar requested.

The usages is myString = readString(6) will read in the next 6 characters from the binaryfile as a string.

Definition at line 80 of file BinaryReader.cc.

References binaryFile_.

Referenced by WallHandler::readTriangleWall().

81 {
82  char tempRead[numChar];
83  binaryFile_.read(tempRead, numChar);
84  std::string returnString(tempRead);
85  return returnString;
86 
87 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
unsigned int BinaryReader::readUnsignedInt ( unsigned int  size)

read the next so many bytes as a unsined int

Parameters
[in]sizeThis is the size in bytes of the unsigned int that is stored in the file.
Returns
a unsigned int this is the unisgned int that is read in

Usage myUnsignedInt = readUnsignedInt(4) woudl read a 32 bit (4*8) unsigned int from the file.

Definition at line 121 of file BinaryReader.cc.

References binaryFile_.

Referenced by WallHandler::readTriangleWall().

122 {
123  char tempRead[size];
124  binaryFile_.read(tempRead, size);
125  unsigned int* returnUnsignedInt = reinterpret_cast<unsigned int*>(tempRead);
126  return (*returnUnsignedInt);
127 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88

Member Data Documentation

std::ifstream BinaryReader::binaryFile_
private

The pointer for the binary file.

Definition at line 88 of file BinaryReader.h.

Referenced by closeFile(), ignoreChar(), openFile(), readDouble(), readFloat(), readString(), and readUnsignedInt().


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