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.sourceforge.net/
11 // Copyright (C) 2001-2012 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/geom/Position.h>
34 #include "BinaryFormatter.h"
35 #include "BinaryInputDevice.h"
36 
37 #ifdef CHECK_MEMORY_LEAKS
38 #include <foreign/nvwa/debug_new.h>
39 #endif // CHECK_MEMORY_LEAKS
40 
41 // ===========================================================================
42 // constants definitions
43 // ===========================================================================
44 #define BUF_MAX 1000
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 BinaryInputDevice::BinaryInputDevice(const std::string& name,
51  const bool isTyped, const bool doValidate)
52  : myStream(name.c_str(), std::fstream::in | std::fstream::binary),
53  myAmTyped(isTyped), myEnableValidation(doValidate) {}
54 
55 
57 
58 
59 bool
61  return myStream.good();
62 }
63 
64 
65 int
67  return myStream.peek();
68 }
69 
70 
71 std::string
72 BinaryInputDevice::read(int numBytes) {
73  if (numBytes > BUF_MAX) {
74  throw ProcessError("Buffer to small.");
75  }
76  myStream.read((char*) &myBuffer, sizeof(char)*numBytes);
77  return std::string(myBuffer, numBytes);
78 }
79 
80 
81 void
83  myStream.putback(c);
84 }
85 
86 
87 int
89  if (myAmTyped) {
90  char c;
91  myStream.read(&c, sizeof(char));
92  if (myEnableValidation && c != t) {
93  throw ProcessError("Unexpected type.");
94  }
95  return c;
96  }
97  return -1;
98 }
99 
100 
104  os.myStream.read(&c, sizeof(char));
105  return os;
106 }
107 
108 
110 operator>>(BinaryInputDevice& os, unsigned char& c) {
112  os.myStream.read((char*) &c, sizeof(unsigned char));
113  return os;
114 }
115 
116 
120  os.myStream.read((char*) &i, sizeof(int));
121  return os;
122 }
123 
124 
126 operator>>(BinaryInputDevice& os, unsigned int& i) {
128  os.myStream.read((char*) &i, sizeof(unsigned int));
129  return os;
130 }
131 
132 
137  int v;
138  os.myStream.read((char*) &v, sizeof(int));
139  f = v / 100.;
140  } else {
141  os.myStream.read((char*) &f, sizeof(SUMOReal));
142  }
143  return os;
144 }
145 
146 
150  b = false;
151  os.myStream.read((char*) &b, sizeof(char));
152  return os;
153 }
154 
155 
157 operator>>(BinaryInputDevice& os, std::string& s) {
159  unsigned int size;
160  os.myStream.read((char*) &size, sizeof(unsigned int));
161  if (size < BUF_MAX) {
162  os.myStream.read((char*) &os.myBuffer, sizeof(char)*size);
163  os.myBuffer[size] = 0;
164  s = std::string(os.myBuffer);
165  }
166  return os;
167 }
168 
169 
171 operator>>(BinaryInputDevice& os, std::vector<std::string>& v) {
173  unsigned int size;
174  os.myStream.read((char*) &size, sizeof(unsigned int));
175  while (size > 0) {
176  std::string s;
177  os >> s;
178  v.push_back(s);
179  size--;
180  }
181  return os;
182 }
183 
184 
186 operator>>(BinaryInputDevice& os, std::vector<unsigned int>& v) {
188  unsigned int size;
189  os.myStream.read((char*) &size, sizeof(unsigned int));
190  while (size > 0) {
191  unsigned int i;
192  os >> i;
193  v.push_back(i);
194  size--;
195  }
196  return os;
197 }
198 
199 
201 operator>>(BinaryInputDevice& os, std::vector< std::vector<unsigned int> >& v) {
203  unsigned int size;
204  os.myStream.read((char*) &size, sizeof(unsigned int));
205  while (size > 0) {
206  std::vector<unsigned int> nested;
207  os >> nested;
208  v.push_back(nested);
209  size--;
210  }
211  return os;
212 }
213 
214 
218  SUMOReal x, y, z = 0;
220  int v;
221  os.myStream.read((char*) &v, sizeof(int));
222  x = v / 100.;
223  os.myStream.read((char*) &v, sizeof(int));
224  y = v / 100.;
226  os.myStream.read((char*) &v, sizeof(int));
227  z = v / 100.;
228  }
229  } else {
230  os.myStream.read((char*) &x, sizeof(SUMOReal));
231  os.myStream.read((char*) &y, sizeof(SUMOReal));
233  os.myStream.read((char*) &z, sizeof(SUMOReal));
234  }
235  }
236  p.set(x, y, z);
237  return os;
238 }
239 
240 
241 
242 /****************************************************************************/