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 #ifdef CHECK_MEMORY_LEAKS
39 #include <foreign/nvwa/debug_new.h>
40 #endif // CHECK_MEMORY_LEAKS
41 
42 // ===========================================================================
43 // constants definitions
44 // ===========================================================================
45 #define BUF_MAX 10000
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 BinaryInputDevice::BinaryInputDevice(const std::string& name,
52  const bool isTyped, const bool doValidate)
53  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
54  myAmTyped(isTyped), myEnableValidation(doValidate) {}
55 
56 
58 
59 
60 bool
62  return myStream.good();
63 }
64 
65 
66 int
68  return myStream.peek();
69 }
70 
71 
72 std::string
73 BinaryInputDevice::read(int numBytes) {
74  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
75  return std::string(myBuffer, numBytes);
76 }
77 
78 
79 void
81  myStream.putback(c);
82 }
83 
84 
85 int
87  if (myAmTyped) {
88  char c;
89  myStream.read(&c, sizeof(char));
90  if (myEnableValidation && c != t) {
91  throw ProcessError("Unexpected type.");
92  }
93  return c;
94  }
95  return -1;
96 }
97 
98 
102  os.myStream.read(&c, sizeof(char));
103  return os;
104 }
105 
106 
108 operator>>(BinaryInputDevice& os, unsigned char& c) {
110  os.myStream.read((char*) &c, sizeof(unsigned char));
111  return os;
112 }
113 
114 
118  os.myStream.read((char*) &i, sizeof(int));
119  return os;
120 }
121 
122 
127  int v;
128  os.myStream.read((char*) &v, sizeof(int));
129  f = v / 100.;
130  } else {
131  os.myStream.read((char*) &f, sizeof(SUMOReal));
132  }
133  return os;
134 }
135 
136 
140  b = false;
141  os.myStream.read((char*) &b, sizeof(char));
142  return os;
143 }
144 
145 
147 operator>>(BinaryInputDevice& os, std::string& s) {
149  int size;
150  os.myStream.read((char*) &size, sizeof(int));
151  int done = 0;
152  while (done < size) {
153  const int toRead = MIN2((int)size - done, (int)BUF_MAX - 1);
154  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
155  os.myBuffer[toRead] = 0;
156  s += std::string(os.myBuffer);
157  done += toRead;
158  }
159  return os;
160 }
161 
162 
164 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
166  int size;
167  os.myStream.read((char*) &size, sizeof(int));
168  while (size > 0) {
169  std::string s;
170  os >> s;
171  v.push_back(s);
172  size--;
173  }
174  return os;
175 }
176 
177 
179 operator>>(BinaryInputDevice& os, std::vector<int>& v) {
181  int size;
182  os.myStream.read((char*) &size, sizeof(int));
183  while (size > 0) {
184  int i;
185  os >> i;
186  v.push_back(i);
187  size--;
188  }
189  return os;
190 }
191 
192 
194 operator>>(BinaryInputDevice& os, std::vector< std::vector<int> >& v) {
196  int size;
197  os.myStream.read((char*) &size, sizeof(int));
198  while (size > 0) {
199  std::vector<int> nested;
200  os >> nested;
201  v.push_back(nested);
202  size--;
203  }
204  return os;
205 }
206 
207 
211  SUMOReal x, y, z = 0;
213  int v;
214  os.myStream.read((char*) &v, sizeof(int));
215  x = v / 100.;
216  os.myStream.read((char*) &v, sizeof(int));
217  y = v / 100.;
219  os.myStream.read((char*) &v, sizeof(int));
220  z = v / 100.;
221  }
222  } else {
223  os.myStream.read((char*) &x, sizeof(SUMOReal));
224  os.myStream.read((char*) &y, sizeof(SUMOReal));
226  os.myStream.read((char*) &z, sizeof(SUMOReal));
227  }
228  }
229  p.set(x, y, z);
230  return os;
231 }
232 
233 
234 
235 /****************************************************************************/
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
bool good() const
Returns whether the underlying file stream can be used (is good())
~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.
void set(SUMOReal x, SUMOReal y)
set positions x and y
Definition: Position.h:93
#define SUMOReal
Definition: config.h:208
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.