SUMO - Simulation of Urban MObility
BinaryInputDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Encapsulates binary reading operations on a file
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <utils/common/StdDefs.h>
34 #include <utils/geom/Position.h>
35 #include "BinaryFormatter.h"
36 #include "BinaryInputDevice.h"
37 
38 // ===========================================================================
39 // constants definitions
40 // ===========================================================================
41 #define BUF_MAX 10000
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 BinaryInputDevice::BinaryInputDevice(const std::string& name,
48  const bool isTyped, const bool doValidate)
49  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
50  myAmTyped(isTyped), myEnableValidation(doValidate) {}
51 
52 
54 
55 
56 bool
58  return myStream.good();
59 }
60 
61 
62 int
64  return myStream.peek();
65 }
66 
67 
68 std::string
69 BinaryInputDevice::read(int numBytes) {
70  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
71  return std::string(myBuffer, numBytes);
72 }
73 
74 
75 void
77  myStream.putback(c);
78 }
79 
80 
81 int
83  if (myAmTyped) {
84  char c;
85  myStream.read(&c, sizeof(char));
86  if (myEnableValidation && c != t) {
87  throw ProcessError("Unexpected type.");
88  }
89  return c;
90  }
91  return -1;
92 }
93 
94 
98  os.myStream.read(&c, sizeof(char));
99  return os;
100 }
101 
102 
104 operator>>(BinaryInputDevice& os, unsigned char& c) {
106  os.myStream.read((char*) &c, sizeof(unsigned char));
107  return os;
108 }
109 
110 
114  os.myStream.read((char*) &i, sizeof(int));
115  return os;
116 }
117 
118 
120 operator>>(BinaryInputDevice& os, double& f) {
123  int v;
124  os.myStream.read((char*) &v, sizeof(int));
125  f = v / 100.;
126  } else {
127  os.myStream.read((char*) &f, sizeof(double));
128  }
129  return os;
130 }
131 
132 
136  b = false;
137  os.myStream.read((char*) &b, sizeof(char));
138  return os;
139 }
140 
141 
143 operator>>(BinaryInputDevice& os, std::string& s) {
145  int size;
146  os.myStream.read((char*) &size, sizeof(int));
147  int done = 0;
148  while (done < size) {
149  const int toRead = MIN2((int)size - done, (int)BUF_MAX - 1);
150  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
151  os.myBuffer[toRead] = 0;
152  s += std::string(os.myBuffer);
153  done += toRead;
154  }
155  return os;
156 }
157 
158 
160 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
162  int size;
163  os.myStream.read((char*) &size, sizeof(int));
164  while (size > 0) {
165  std::string s;
166  os >> s;
167  v.push_back(s);
168  size--;
169  }
170  return os;
171 }
172 
173 
175 operator>>(BinaryInputDevice& os, std::vector<int>& v) {
177  int size;
178  os.myStream.read((char*) &size, sizeof(int));
179  while (size > 0) {
180  int i;
181  os >> i;
182  v.push_back(i);
183  size--;
184  }
185  return os;
186 }
187 
188 
190 operator>>(BinaryInputDevice& os, std::vector< std::vector<int> >& v) {
192  int size;
193  os.myStream.read((char*) &size, sizeof(int));
194  while (size > 0) {
195  std::vector<int> nested;
196  os >> nested;
197  v.push_back(nested);
198  size--;
199  }
200  return os;
201 }
202 
203 
207  double x, y, z = 0;
209  int v;
210  os.myStream.read((char*) &v, sizeof(int));
211  x = v / 100.;
212  os.myStream.read((char*) &v, sizeof(int));
213  y = v / 100.;
215  os.myStream.read((char*) &v, sizeof(int));
216  z = v / 100.;
217  }
218  } else {
219  os.myStream.read((char*) &x, sizeof(double));
220  os.myStream.read((char*) &y, sizeof(double));
222  os.myStream.read((char*) &z, sizeof(double));
223  }
224  }
225  p.set(x, y, z);
226  return os;
227 }
228 
229 
230 
231 /****************************************************************************/
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:93
~BinaryInputDevice()
Destructor.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
T MIN2(T a, T b)
Definition: StdDefs.h:64
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.