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 /****************************************************************************/
16 // Encapsulates binary reading operations on a file
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <utils/common/StdDefs.h>
27 #include <utils/geom/Position.h>
28 #include "BinaryFormatter.h"
29 #include "BinaryInputDevice.h"
30 
31 // ===========================================================================
32 // constants definitions
33 // ===========================================================================
34 #define BUF_MAX 10000
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 BinaryInputDevice::BinaryInputDevice(const std::string& name,
41  const bool isTyped, const bool doValidate)
42  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
43  myAmTyped(isTyped), myEnableValidation(doValidate) {}
44 
45 
47 
48 
49 bool
51  return myStream.good();
52 }
53 
54 
55 int
57  return myStream.peek();
58 }
59 
60 
61 std::string
62 BinaryInputDevice::read(int numBytes) {
63  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
64  return std::string(myBuffer, numBytes);
65 }
66 
67 
68 void
70  myStream.putback(c);
71 }
72 
73 
74 int
76  if (myAmTyped) {
77  char c;
78  myStream.read(&c, sizeof(char));
79  if (myEnableValidation && c != t) {
80  throw ProcessError("Unexpected type.");
81  }
82  return c;
83  }
84  return -1;
85 }
86 
87 
91  os.myStream.read(&c, sizeof(char));
92  return os;
93 }
94 
95 
97 operator>>(BinaryInputDevice& os, unsigned char& c) {
99  os.myStream.read((char*) &c, sizeof(unsigned char));
100  return os;
101 }
102 
103 
107  os.myStream.read((char*) &i, sizeof(int));
108  return os;
109 }
110 
111 
113 operator>>(BinaryInputDevice& os, double& f) {
116  int v;
117  os.myStream.read((char*) &v, sizeof(int));
118  f = v / 100.;
119  } else {
120  os.myStream.read((char*) &f, sizeof(double));
121  }
122  return os;
123 }
124 
125 
129  b = false;
130  os.myStream.read((char*) &b, sizeof(char));
131  return os;
132 }
133 
134 
136 operator>>(BinaryInputDevice& os, std::string& s) {
138  int size;
139  os.myStream.read((char*) &size, sizeof(int));
140  int done = 0;
141  while (done < size) {
142  const int toRead = MIN2((int)size - done, (int)BUF_MAX - 1);
143  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
144  os.myBuffer[toRead] = 0;
145  s += std::string(os.myBuffer);
146  done += toRead;
147  }
148  return os;
149 }
150 
151 
153 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
155  int size;
156  os.myStream.read((char*) &size, sizeof(int));
157  while (size > 0) {
158  std::string s;
159  os >> s;
160  v.push_back(s);
161  size--;
162  }
163  return os;
164 }
165 
166 
168 operator>>(BinaryInputDevice& os, std::vector<int>& v) {
170  int size;
171  os.myStream.read((char*) &size, sizeof(int));
172  while (size > 0) {
173  int i;
174  os >> i;
175  v.push_back(i);
176  size--;
177  }
178  return os;
179 }
180 
181 
183 operator>>(BinaryInputDevice& os, std::vector< std::vector<int> >& v) {
185  int size;
186  os.myStream.read((char*) &size, sizeof(int));
187  while (size > 0) {
188  std::vector<int> nested;
189  os >> nested;
190  v.push_back(nested);
191  size--;
192  }
193  return os;
194 }
195 
196 
200  double x, y, z = 0;
202  int v;
203  os.myStream.read((char*) &v, sizeof(int));
204  x = v / 100.;
205  os.myStream.read((char*) &v, sizeof(int));
206  y = v / 100.;
208  os.myStream.read((char*) &v, sizeof(int));
209  z = v / 100.;
210  }
211  } else {
212  os.myStream.read((char*) &x, sizeof(double));
213  os.myStream.read((char*) &y, sizeof(double));
215  os.myStream.read((char*) &z, sizeof(double));
216  }
217  }
218  p.set(x, y, z);
219  return os;
220 }
221 
222 
223 
224 /****************************************************************************/
int peek()
Returns the next character to be read by an actual parse.
void putback(char c)
Pushes a character back into the stream to be read by the next actual parse.
DataType
data types in binary output
#define BUF_MAX
void set(double x, double y)
set positions x and y
Definition: Position.h:87
~BinaryInputDevice()
Destructor.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
T MIN2(T a, T b)
Definition: StdDefs.h:74
std::ifstream myStream
The encapsulated stream.
bool good() const
Returns whether the underlying file stream can be used (is good())
const bool myEnableValidation
Information whether types shall be checked.
char myBuffer[10000]
The buffer used for string parsing.
BinaryInputDevice(const std::string &name, const bool isTyped=false, const bool doValidate=false)
Constructor.
friend BinaryInputDevice & operator>>(BinaryInputDevice &os, char &c)
Reads a char from the file (input operator)
Encapsulates binary reading operations on a file.
int checkType(BinaryFormatter::DataType t)
std::string read(int numBytes)
Reads the defined number of bytes and returns them as a string.