MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BinaryReader.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 
28 #ifndef BINARY_READER_H
29 #define BINARY_READER_H
30 
31 
32 #include<fstream>
33 #include<iostream>
34 #include<Logger.h>
35 #include<BinaryReader.h>
36 
41 BinaryReader::BinaryReader(std::string fileName)
42 {
43  openFile(fileName);
44 }
45 
50 {
51  closeFile();
52 }
53 
58 {
59  binaryFile_.close();
60 }
61 
66 void BinaryReader::openFile(std::string fileName)
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 }
74 
80 std::string BinaryReader::readString(unsigned int numChar)
81 {
82  char tempRead[numChar];
83  binaryFile_.read(tempRead, numChar);
84  std::string returnString(tempRead);
85  return returnString;
86 
87 }
88 
94 double BinaryReader::readDouble(unsigned int size)
95 {
96  char tempRead[size];
97  binaryFile_.read(tempRead, 4);
98  double* returnDoublePtr = reinterpret_cast<double*>(tempRead);
99  return (*returnDoublePtr);
100 }
101 
108 double BinaryReader::readFloat(unsigned int size)
109 {
110  char tempRead[size];
111  binaryFile_.read(tempRead, 4);
112  float* returnFloatPtr = reinterpret_cast<float*>(tempRead);
113  return static_cast<double>(*returnFloatPtr);
114 }
115 
121 unsigned int BinaryReader::readUnsignedInt(unsigned int size)
122 {
123  char tempRead[size];
124  binaryFile_.read(tempRead, size);
125  unsigned int* returnUnsignedInt = reinterpret_cast<unsigned int*>(tempRead);
126  return (*returnUnsignedInt);
127 }
128 
133 void BinaryReader::ignoreChar(unsigned int size)
134 {
135  char tempRead[size];
136  binaryFile_.read(tempRead, size);
137 }
138 
139 #endif
std::ifstream binaryFile_
The pointer for the binary file.
Definition: BinaryReader.h:88
unsigned int readUnsignedInt(unsigned int size)
read the next so many bytes as a unsined int
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
BinaryReader(std::string)
Default constuction, requires to users to prove the name of the file that will be opened...
Definition: BinaryReader.cc:41
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) ...
double readDouble(unsigned int size)
read the next so many bytes as a double
Definition: BinaryReader.cc:94
void ignoreChar(unsigned int size)
read and ignore the next number of characters
~BinaryReader()
Destructor, simple closes the file.
Definition: BinaryReader.cc:49
void closeFile()
close the file with fileName
Definition: BinaryReader.cc:57
void openFile(std::string fileName)
opens the file with fileName
Definition: BinaryReader.cc:66
std::string readString(unsigned int numChar)
reads the next so many Characters (bytes) as a std::string
Definition: BinaryReader.cc:80