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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Encapsulates binary reading operations on a file
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <utils/common/StdDefs.h>
33 #include <utils/geom/Position.h>
34 #include "BinaryFormatter.h"
35 #include "BinaryInputDevice.h"
36 
37 // ===========================================================================
38 // constants definitions
39 // ===========================================================================
40 #define BUF_MAX 10000
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 BinaryInputDevice::BinaryInputDevice(const std::string& name,
47  const bool isTyped, const bool doValidate)
48  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
49  myAmTyped(isTyped), myEnableValidation(doValidate) {}
50 
51 
53 
54 
55 bool
57  return myStream.good();
58 }
59 
60 
61 int
63  return myStream.peek();
64 }
65 
66 
67 std::string
68 BinaryInputDevice::read(int numBytes) {
69  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
70  return std::string(myBuffer, numBytes);
71 }
72 
73 
74 void
76  myStream.putback(c);
77 }
78 
79 
80 int
82  if (myAmTyped) {
83  char c;
84  myStream.read(&c, sizeof(char));
85  if (myEnableValidation && c != t) {
86  throw ProcessError("Unexpected type.");
87  }
88  return c;
89  }
90  return -1;
91 }
92 
93 
97  os.myStream.read(&c, sizeof(char));
98  return os;
99 }
100 
101 
103 operator>>(BinaryInputDevice& os, unsigned char& c) {
105  os.myStream.read((char*) &c, sizeof(unsigned char));
106  return os;
107 }
108 
109 
113  os.myStream.read((char*) &i, sizeof(int));
114  return os;
115 }
116 
117 
119 operator>>(BinaryInputDevice& os, double& f) {
122  int v;
123  os.myStream.read((char*) &v, sizeof(int));
124  f = v / 100.;
125  } else {
126  os.myStream.read((char*) &f, sizeof(double));
127  }
128  return os;
129 }
130 
131 
135  b = false;
136  os.myStream.read((char*) &b, sizeof(char));
137  return os;
138 }
139 
140 
142 operator>>(BinaryInputDevice& os, std::string& s) {
144  int size;
145  os.myStream.read((char*) &size, sizeof(int));
146  int done = 0;
147  while (done < size) {
148  const int toRead = MIN2((int)size - done, (int)BUF_MAX - 1);
149  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
150  os.myBuffer[toRead] = 0;
151  s += std::string(os.myBuffer);
152  done += toRead;
153  }
154  return os;
155 }
156 
157 
159 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
161  int size;
162  os.myStream.read((char*) &size, sizeof(int));
163  while (size > 0) {
164  std::string s;
165  os >> s;
166  v.push_back(s);
167  size--;
168  }
169  return os;
170 }
171 
172 
174 operator>>(BinaryInputDevice& os, std::vector<int>& v) {
176  int size;
177  os.myStream.read((char*) &size, sizeof(int));
178  while (size > 0) {
179  int i;
180  os >> i;
181  v.push_back(i);
182  size--;
183  }
184  return os;
185 }
186 
187 
189 operator>>(BinaryInputDevice& os, std::vector< std::vector<int> >& v) {
191  int size;
192  os.myStream.read((char*) &size, sizeof(int));
193  while (size > 0) {
194  std::vector<int> nested;
195  os >> nested;
196  v.push_back(nested);
197  size--;
198  }
199  return os;
200 }
201 
202 
206  double x, y, z = 0;
208  int v;
209  os.myStream.read((char*) &v, sizeof(int));
210  x = v / 100.;
211  os.myStream.read((char*) &v, sizeof(int));
212  y = v / 100.;
214  os.myStream.read((char*) &v, sizeof(int));
215  z = v / 100.;
216  }
217  } else {
218  os.myStream.read((char*) &x, sizeof(double));
219  os.myStream.read((char*) &y, sizeof(double));
221  os.myStream.read((char*) &z, sizeof(double));
222  }
223  }
224  p.set(x, y, z);
225  return os;
226 }
227 
228 
229 
230 /****************************************************************************/
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:92
~BinaryInputDevice()
Destructor.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
T MIN2(T a, T b)
Definition: StdDefs.h:67
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.