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>

+ Inheritance diagram for BinaryReader:

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.

Constructor & Destructor Documentation

◆ BinaryReader()

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 \deatails This is the default and only constuctor and it calls the private method openFile.
42 {
43  openFile(fileName);
44 }
void openFile(std::string fileName)
opens the file with fileName
Definition: BinaryReader.cc:66

References openFile().

◆ ~BinaryReader()

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

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

References closeFile().

Member Function Documentation

◆ closeFile()

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

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

References binaryFile_.

Referenced by ~BinaryReader().

◆ ignoreChar()

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

134 {
135  char tempRead[size];
136  binaryFile_.read(tempRead, size);
137 }

References binaryFile_.

Referenced by main(), WallHandler::readTriangleWall(), and STLReader::STLReader().

◆ openFile()

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_

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 }
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ ERROR

References binaryFile_, ERROR, and logger.

Referenced by BinaryReader().

◆ readDouble()

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.

95 {
96  char tempRead[size];
97  binaryFile_.read(tempRead, 4);
98  double* returnDoublePtr = reinterpret_cast<double*>(tempRead);
99  return (*returnDoublePtr);
100 }

References binaryFile_.

◆ readFloat()

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.
109 {
110  char tempRead[size];
111  binaryFile_.read(tempRead, 4);
112  float* returnFloatPtr = reinterpret_cast<float*>(tempRead);
113  return static_cast<double>(*returnFloatPtr);
114 }

References binaryFile_.

Referenced by main(), WallHandler::readTriangleWall(), and STLReader::STLReader().

◆ readString()

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.

81 {
82  char tempRead[numChar];
83  binaryFile_.read(tempRead, numChar);
84  std::string returnString(tempRead);
85  return returnString;
86 
87 }

References binaryFile_.

Referenced by main(), WallHandler::readTriangleWall(), and STLReader::STLReader().

◆ readUnsignedInt()

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.

122 {
123  char tempRead[size];
124  binaryFile_.read(tempRead, size);
125  unsigned int* returnUnsignedInt = reinterpret_cast<unsigned int*>(tempRead);
126  return (*returnUnsignedInt);
127 }

References binaryFile_.

Referenced by main(), WallHandler::readTriangleWall(), and STLReader::STLReader().

Member Data Documentation

◆ binaryFile_

std::ifstream BinaryReader::binaryFile_
private

The pointer for the binary file.

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


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