SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
11 // Copyright (C) 2005-2014 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 
124 operator>>(BinaryInputDevice& os, unsigned int& i) {
126  os.myStream.read((char*) &i, sizeof(unsigned int));
127  return os;
128 }
129 
130 
135  int v;
136  os.myStream.read((char*) &v, sizeof(int));
137  f = v / 100.;
138  } else {
139  os.myStream.read((char*) &f, sizeof(SUMOReal));
140  }
141  return os;
142 }
143 
144 
148  b = false;
149  os.myStream.read((char*) &b, sizeof(char));
150  return os;
151 }
152 
153 
155 operator>>(BinaryInputDevice& os, std::string& s) {
157  unsigned int size;
158  os.myStream.read((char*) &size, sizeof(unsigned int));
159  unsigned int done = 0;
160  while (done < size) {
161  const unsigned int toRead = MIN2((unsigned int)size - done, (unsigned int)BUF_MAX - 1);
162  os.myStream.read((char*) &os.myBuffer, sizeof(char)*toRead);
163  os.myBuffer[toRead] = 0;
164  s += std::string(os.myBuffer);
165  done += toRead;
166  }
167  return os;
168 }
169 
170 
172 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
174  unsigned int size;
175  os.myStream.read((char*) &size, sizeof(unsigned int));
176  while (size > 0) {
177  std::string s;
178  os >> s;
179  v.push_back(s);
180  size--;
181  }
182  return os;
183 }
184 
185 
187 operator>>(BinaryInputDevice& os, std::vector<unsigned int>& v) {
189  unsigned int size;
190  os.myStream.read((char*) &size, sizeof(unsigned int));
191  while (size > 0) {
192  unsigned int i;
193  os >> i;
194  v.push_back(i);
195  size--;
196  }
197  return os;
198 }
199 
200 
202 operator>>(BinaryInputDevice& os, std::vector< std::vector<unsigned int> >& v) {
204  unsigned int size;
205  os.myStream.read((char*) &size, sizeof(unsigned int));
206  while (size > 0) {
207  std::vector<unsigned int> nested;
208  os >> nested;
209  v.push_back(nested);
210  size--;
211  }
212  return os;
213 }
214 
215 
219  SUMOReal x, y, z = 0;
221  int v;
222  os.myStream.read((char*) &v, sizeof(int));
223  x = v / 100.;
224  os.myStream.read((char*) &v, sizeof(int));
225  y = v / 100.;
227  os.myStream.read((char*) &v, sizeof(int));
228  z = v / 100.;
229  }
230  } else {
231  os.myStream.read((char*) &x, sizeof(SUMOReal));
232  os.myStream.read((char*) &y, sizeof(SUMOReal));
234  os.myStream.read((char*) &z, sizeof(SUMOReal));
235  }
236  }
237  p.set(x, y, z);
238  return os;
239 }
240 
241 
242 
243 /****************************************************************************/