Eclipse SUMO - Simulation of Urban MObility
BinaryInputDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Encapsulates binary reading operations on a file
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <utils/common/StdDefs.h>
26 #include <utils/geom/Position.h>
27 #include "BinaryFormatter.h"
28 #include "BinaryInputDevice.h"
29 
30 // ===========================================================================
31 // constants definitions
32 // ===========================================================================
33 #define BUF_MAX 10000
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 BinaryInputDevice::BinaryInputDevice(const std::string& name,
40  const bool isTyped, const bool doValidate)
41  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
42  myAmTyped(isTyped), myEnableValidation(doValidate) {}
43 
44 
46 
47 
48 bool
50  return myStream.good();
51 }
52 
53 
54 int
56  return myStream.peek();
57 }
58 
59 
60 std::string
61 BinaryInputDevice::read(int numBytes) {
62  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
63  return std::string(myBuffer, numBytes);
64 }
65 
66 
67 void
69  myStream.putback(c);
70 }
71 
72 
73 int
75  if (myAmTyped) {
76  char c;
77  myStream.read(&c, sizeof(char));
78  if (myEnableValidation && c != t) {
79  throw ProcessError("Unexpected type.");
80  }
81  return c;
82  }
83  return -1;
84 }
85 
86 
90  os.myStream.read(&c, sizeof(char));
91  return os;
92 }
93 
94 
96 operator>>(BinaryInputDevice& os, unsigned char& c) {
98  os.myStream.read((char*) &c, sizeof(unsigned char));
99  return os;
100 }
101 
102 
106  os.myStream.read((char*) &i, sizeof(int));
107  return os;
108 }
109 
110 
112 operator>>(BinaryInputDevice& os, double& f) {
115  int v;
116  os.myStream.read((char*) &v, sizeof(int));
117  f = v / 100.;
118  } else {
119  os.myStream.read((char*) &f, sizeof(double));
120  }
121  return os;
122 }
123 
124 
128  b = false;
129  os.myStream.read((char*) &b, sizeof(char));
130  return os;
131 }
132 
133 
135 operator>>(BinaryInputDevice& os, std::string& s) {
137  int size;
138  os.myStream.read((char*) &size, sizeof(int));
139  int done = 0;
140  while (done < size) {
141  const int toRead = MIN2((int)size - done, (int)BUF_MAX - 1);
142  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
143  os.myBuffer[toRead] = 0;
144  s += std::string(os.myBuffer);
145  done += toRead;
146  }
147  return os;
148 }
149 
150 
152 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
154  int size;
155  os.myStream.read((char*) &size, sizeof(int));
156  while (size > 0) {
157  std::string s;
158  os >> s;
159  v.push_back(s);
160  size--;
161  }
162  return os;
163 }
164 
165 
167 operator>>(BinaryInputDevice& os, std::vector<int>& v) {
169  int size;
170  os.myStream.read((char*) &size, sizeof(int));
171  while (size > 0) {
172  int i;
173  os >> i;
174  v.push_back(i);
175  size--;
176  }
177  return os;
178 }
179 
180 
182 operator>>(BinaryInputDevice& os, std::vector< std::vector<int> >& v) {
184  int size;
185  os.myStream.read((char*) &size, sizeof(int));
186  while (size > 0) {
187  std::vector<int> nested;
188  os >> nested;
189  v.push_back(nested);
190  size--;
191  }
192  return os;
193 }
194 
195 
199  double x, y, z = 0;
201  int v;
202  os.myStream.read((char*) &v, sizeof(int));
203  x = v / 100.;
204  os.myStream.read((char*) &v, sizeof(int));
205  y = v / 100.;
207  os.myStream.read((char*) &v, sizeof(int));
208  z = v / 100.;
209  }
210  } else {
211  os.myStream.read((char*) &x, sizeof(double));
212  os.myStream.read((char*) &y, sizeof(double));
214  os.myStream.read((char*) &z, sizeof(double));
215  }
216  }
217  p.set(x, y, z);
218  return os;
219 }
220 
221 
222 
223 /****************************************************************************/
operator>>
BinaryInputDevice & operator>>(BinaryInputDevice &os, char &c)
Definition: BinaryInputDevice.cpp:88
BinaryInputDevice::myBuffer
char myBuffer[10000]
The buffer used for string parsing.
Definition: BinaryInputDevice.h:225
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:73
BinaryFormatter::BF_SCALED2INT_POSITION_3D
Definition: BinaryFormatter.h:97
BinaryFormatter::BF_LIST
Definition: BinaryFormatter.h:67
BinaryFormatter::BF_BYTE
Definition: BinaryFormatter.h:59
BinaryInputDevice::myAmTyped
const bool myAmTyped
Definition: BinaryInputDevice.h:219
BinaryInputDevice::putback
void putback(char c)
Pushes a character back into the stream to be read by the next actual parse.
Definition: BinaryInputDevice.cpp:68
BinaryFormatter::DataType
DataType
data types in binary output
Definition: BinaryFormatter.h:57
BinaryInputDevice.h
BinaryFormatter::BF_SCALED2INT_POSITION_2D
Definition: BinaryFormatter.h:95
Position::set
void set(double x, double y)
set positions x and y
Definition: Position.h:86
BinaryFormatter::BF_SCALED2INT
Definition: BinaryFormatter.h:93
BinaryInputDevice::checkType
int checkType(BinaryFormatter::DataType t)
Definition: BinaryInputDevice.cpp:74
ProcessError
Definition: UtilExceptions.h:39
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
BinaryInputDevice::~BinaryInputDevice
~BinaryInputDevice()
Destructor.
Definition: BinaryInputDevice.cpp:45
BUF_MAX
#define BUF_MAX
Definition: BinaryInputDevice.cpp:33
BinaryFormatter::BF_POSITION_2D
Definition: BinaryFormatter.h:79
BinaryInputDevice::good
bool good() const
Returns whether the underlying file stream can be used (is good())
Definition: BinaryInputDevice.cpp:49
BinaryFormatter::BF_INTEGER
Definition: BinaryFormatter.h:61
BinaryFormatter::BF_POSITION_3D
Definition: BinaryFormatter.h:81
Position.h
BinaryInputDevice::myStream
std::ifstream myStream
The encapsulated stream.
Definition: BinaryInputDevice.h:217
BinaryInputDevice::peek
int peek()
Returns the next character to be read by an actual parse.
Definition: BinaryInputDevice.cpp:55
BinaryFormatter::BF_STRING
Definition: BinaryFormatter.h:65
BinaryInputDevice::BinaryInputDevice
BinaryInputDevice(const std::string &name, const bool isTyped=false, const bool doValidate=false)
Constructor.
Definition: BinaryInputDevice.cpp:39
BinaryInputDevice::myEnableValidation
const bool myEnableValidation
Information whether types shall be checked.
Definition: BinaryInputDevice.h:222
config.h
StdDefs.h
BinaryFormatter::BF_FLOAT
Definition: BinaryFormatter.h:63
BinaryInputDevice::read
std::string read(int numBytes)
Reads the defined number of bytes and returns them as a string.
Definition: BinaryInputDevice.cpp:61
BinaryFormatter.h
BinaryInputDevice
Encapsulates binary reading operations on a file.
Definition: BinaryInputDevice.h:57