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.
19 {
21 }
void openFile(std::string fileName)
opens the file with fileName
Definition: BinaryReader.cc:43
string fileName
Definition: UniformPSDSelfTest.py:10

References UniformPSDSelfTest::fileName, and 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

27 {
28  closeFile();
29 }
void closeFile()
close the file with fileName
Definition: BinaryReader.cc:34

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

35 {
36  binaryFile_.close();
37 }
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:67

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

111 {
112  char tempRead[size];
113  binaryFile_.read(tempRead, size);
114 }
Scalar Scalar int size
Definition: benchVecAdd.cpp:17

References binaryFile_, and size.

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_

44 {
45  binaryFile_.open(fileName, std::ios::binary);
46  if (!binaryFile_)
47  {
48  logger(ERROR, "BinaryReader::openFile Could not open file; file % not found", fileName);
49  }
50 }
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, UniformPSDSelfTest::fileName, 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.

72 {
73  char tempRead[size];
74  binaryFile_.read(tempRead, 4);
75  double* returnDoublePtr = reinterpret_cast<double*>(tempRead);
76  return (*returnDoublePtr);
77 }

References binaryFile_, and size.

◆ 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.
86 {
87  char tempRead[size];
88  binaryFile_.read(tempRead, 4);
89  float* returnFloatPtr = reinterpret_cast<float*>(tempRead);
90  return static_cast<double>(*returnFloatPtr);
91 }

References binaryFile_, and size.

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.

58 {
59  char tempRead[numChar];
60  binaryFile_.read(tempRead, numChar);
61  std::string returnString(tempRead);
62  return returnString;
63 
64 }
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References binaryFile_, and oomph::Global_string_for_annotation::string().

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.

99 {
100  char tempRead[size];
101  binaryFile_.read(tempRead, size);
102  unsigned int* returnUnsignedInt = reinterpret_cast<unsigned int*>(tempRead);
103  return (*returnUnsignedInt);
104 }

References binaryFile_, and size.

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: