SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOSAXAttributesImpl_Binary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated Xerces-SAX-attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <sstream>
35 #include <utils/common/RGBColor.h>
37 #include <utils/geom/Boundary.h>
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
52  const std::map<int, std::string>& predefinedTagsMML,
53  const std::string& objectType,
54  BinaryInputDevice* in) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
55  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
56  unsigned char attr;
57  *in >> attr;
58  int type = in->peek();
59  switch (type) {
61  *in >> myCharValues[attr];
62  break;
64  *in >> myIntValues[attr];
65  break;
68  *in >> myFloatValues[attr];
69  break;
71  *in >> myStringValues[attr];
72  break;
74  int size;
75  *in >> size;
76  while (size > 0) {
77  const int type = in->peek();
81  throw ProcessError("Invalid binary file, only supporting position vectors.");
82  }
83  size--;
84  Position p;
85  *in >> p;
86  myPositionVectors[attr].push_back(p);
87  }
88  break;
89  }
91  *in >> myIntValues[attr];
92  break;
94  *in >> myIntValues[attr];
96  *in >> myCharValues[attr];
97  break;
102  Position p;
103  *in >> p;
104  myPositionVectors[attr].push_back(p);
105  break;
106  }
108  Position p;
109  *in >> p;
110  myPositionVectors[attr].push_back(p);
112  *in >> p;
113  myPositionVectors[attr].push_back(p);
114  break;
115  }
117  *in >> myIntValues[attr];
118  break;
120  *in >> myCharValues[attr];
121  break;
123  *in >> myCharValues[attr];
124  break;
126  std::ostringstream into(std::ios::binary);
127  int size;
128  *in >> size;
130  FileHelpers::writeInt(into, size);
131  if (size > 0) {
132  int intsToRead = size - 1;
133  int bitsOrEntry;
135  *in >> bitsOrEntry;
136  FileHelpers::writeInt(into, bitsOrEntry);
137  if (bitsOrEntry < 0) {
138  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
139  }
140  while (intsToRead > 0) {
142  *in >> bitsOrEntry;
143  FileHelpers::writeInt(into, bitsOrEntry);
144  intsToRead--;
145  }
146  }
147  myStringValues[attr] = into.str();
148  break;
149  }
150  default:
151  throw ProcessError("Invalid binary file");
152  }
153  myAttrs.insert(attr);
154  }
155 }
156 
157 
159 }
160 
161 
162 bool
164  return myAttrs.find(id) != myAttrs.end();
165 }
166 
167 
168 bool
170  const std::map<int, char>::const_iterator i = myCharValues.find(id);
171  if (i == myCharValues.end()) {
172  throw EmptyData();
173  }
174  return i->second != 0;
175 }
176 
177 
178 bool
180  const std::map<int, char>::const_iterator i = myCharValues.find(id);
181  if (i == myCharValues.end()) {
182  return val;
183  }
184  return i->second != 0;
185 }
186 
187 
188 int
190  const std::map<int, int>::const_iterator i = myIntValues.find(id);
191  if (i == myIntValues.end()) {
192  throw EmptyData();
193  }
194  return i->second;
195 }
196 
197 
198 int
200  int def) const {
201  const std::map<int, int>::const_iterator i = myIntValues.find(id);
202  if (i == myIntValues.end()) {
203  return def;
204  }
205  return i->second;
206 }
207 
208 
209 SUMOLong
211  throw NumberFormatException();
212 }
213 
214 
215 std::string
217  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
218  if (i == myStringValues.end()) {
219  throw EmptyData();
220  }
221  return i->second;
222 }
223 
224 
225 std::string
227  const std::string& str) const throw(EmptyData) {
228  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
229  if (i == myStringValues.end()) {
230  return str;
231  }
232  return i->second;
233 }
234 
235 
236 SUMOReal
238  const std::map<int, SUMOReal>::const_iterator i = myFloatValues.find(id);
239  if (i == myFloatValues.end()) {
240  return TplConvert::_2SUMOReal(getString(id).c_str());
241  }
242  return i->second;
243 }
244 
245 
246 SUMOReal
248  const std::map<int, SUMOReal>::const_iterator i = myFloatValues.find(id);
249  if (i == myFloatValues.end()) {
250  return def;
251  }
252  return i->second;
253 }
254 
255 
256 SUMOReal
257 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
258  throw ProcessError("not implemented for binary data");
259 }
260 
261 
262 bool
263 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
264  throw ProcessError("not implemented for binary data");
265 }
266 
267 
268 std::string
270  const std::string& /* str */) const {
271  throw ProcessError("not implemented for binary data");
272 }
273 
274 
277  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
278  if (i != myCharValues.end()) {
279  const char func = i->second;
280  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
281  return (SumoXMLEdgeFunc)func;
282  }
283  ok = false;
284  }
285  return EDGEFUNC_NORMAL;
286 }
287 
288 
291  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
292  if (i != myCharValues.end()) {
293  const char type = i->second;
294  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
295  return (SumoXMLNodeType)type;
296  }
297  ok = false;
298  }
299  return NODETYPE_UNKNOWN;
300 }
301 
302 
303 RGBColor
304 SUMOSAXAttributesImpl_Binary::getColorReporting(const char* /* objectid */, bool& /* ok */) const {
305  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
306  if (i != myIntValues.end()) {
307  const int val = i->second;
308  return RGBColor((val & 0xff) / 255., ((val >> 8) & 0xff) / 255., ((val >> 16) & 0xff) / 255.);
309  }
310  return RGBColor();
311 }
312 
313 
315 SUMOSAXAttributesImpl_Binary::getShapeReporting(int attr, const char* objectid, bool& ok,
316  bool allowEmpty) const {
317  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
318  if (i == myPositionVectors.end() || i->second.size() == 0) {
319  if (!allowEmpty) {
320  emitEmptyError(getName(attr), objectid);
321  ok = false;
322  }
323  return PositionVector();
324  }
325  return i->second;
326 }
327 
328 
329 Boundary
330 SUMOSAXAttributesImpl_Binary::getBoundaryReporting(int attr, const char* objectid, bool& ok) const {
331  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
332  if (i == myPositionVectors.end() || i->second.size() == 0) {
333  emitEmptyError(getName(attr), objectid);
334  ok = false;
335  return Boundary();
336  }
337  if (i->second.size() != 2) {
338  emitFormatError(getName(attr), "a valid number of entries", objectid);
339  ok = false;
340  return Boundary();
341  }
342  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
343 }
344 
345 
346 std::string
348  if (myAttrIds.find(attr) == myAttrIds.end()) {
349  return "?";
350  }
351  return myAttrIds.find(attr)->second;
352 }
353 
354 
355 void
357  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
358  os << " " << getName(*i);
359  os << "=\"" << getStringSecure(*i, "?") << "\"";
360  }
361 }
362 
363 
364 /****************************************************************************/
365