SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Binary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated xml-attributes that are retrieved from the sumo-binary-xml format (already typed)
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-2017 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>
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
53  const std::map<int, std::string>& predefinedTagsMML,
54  const std::string& objectType,
55  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
56  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
57  int attr;
58  unsigned char attrByte;
59  *in >> attrByte;
60  attr = attrByte;
61  if (version > 1) {
63  *in >> attrByte;
64  attr += 256 * attrByte;
65  }
66  int type = in->peek();
67  switch (type) {
69  *in >> myCharValues[attr];
70  break;
72  *in >> myIntValues[attr];
73  break;
76  *in >> myFloatValues[attr];
77  break;
79  *in >> myStringValues[attr];
80  break;
82  int size;
83  *in >> size;
84  while (size > 0) {
85  const int type = in->peek();
89  throw ProcessError("Invalid binary file, only supporting position vectors.");
90  }
91  size--;
92  Position p;
93  *in >> p;
94  myPositionVectors[attr].push_back(p);
95  }
96  break;
97  }
99  *in >> myIntValues[attr];
100  break;
102  *in >> myIntValues[attr];
104  *in >> myCharValues[attr];
105  break;
110  Position p;
111  *in >> p;
112  myPositionVectors[attr].push_back(p);
113  break;
114  }
116  Position p;
117  *in >> p;
118  myPositionVectors[attr].push_back(p);
120  *in >> p;
121  myPositionVectors[attr].push_back(p);
122  break;
123  }
125  *in >> myIntValues[attr];
126  break;
128  *in >> myCharValues[attr];
129  break;
131  *in >> myCharValues[attr];
132  break;
134  std::ostringstream into(std::ios::binary);
135  int size;
136  *in >> size;
138  FileHelpers::writeInt(into, size);
139  if (size > 0) {
140  int intsToRead = size - 1;
141  int bitsOrEntry;
143  *in >> bitsOrEntry;
144  FileHelpers::writeInt(into, bitsOrEntry);
145  if (bitsOrEntry < 0) {
146  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
147  }
148  while (intsToRead > 0) {
150  *in >> bitsOrEntry;
151  FileHelpers::writeInt(into, bitsOrEntry);
152  intsToRead--;
153  }
154  }
155  myStringValues[attr] = into.str();
156  break;
157  }
158  default:
159  throw ProcessError("Binary file is invalid, attribute type is unknown.");
160  }
161  myAttrs.insert(attr);
162  }
163 }
164 
165 
167 }
168 
169 
170 bool
172  return myAttrs.find(id) != myAttrs.end();
173 }
174 
175 
176 bool
178  const std::map<int, char>::const_iterator i = myCharValues.find(id);
179  if (i == myCharValues.end()) {
180  throw EmptyData();
181  }
182  return i->second != 0;
183 }
184 
185 
186 int
188  const std::map<int, int>::const_iterator i = myIntValues.find(id);
189  if (i == myIntValues.end()) {
190  throw EmptyData();
191  }
192  return i->second;
193 }
194 
195 
196 long long int
198  throw NumberFormatException();
199 }
200 
201 
202 std::string
204  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
205  if (i == myStringValues.end()) {
206  throw EmptyData();
207  }
208  return i->second;
209 }
210 
211 
212 std::string
214  const std::string& str) const {
215  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
216  if (i == myStringValues.end()) {
217  return str;
218  }
219  return i->second;
220 }
221 
222 
223 SUMOReal
225  const std::map<int, SUMOReal>::const_iterator i = myFloatValues.find(id);
226  if (i == myFloatValues.end()) {
227  return TplConvert::_2SUMOReal(getString(id).c_str());
228  }
229  return i->second;
230 }
231 
232 
233 SUMOReal
234 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
235  throw ProcessError("not implemented for binary data");
236 }
237 
238 
239 bool
240 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
241  throw ProcessError("not implemented for binary data");
242 }
243 
244 
245 std::string
247  const std::string& /* str */) const {
248  throw ProcessError("not implemented for binary data");
249 }
250 
251 
254  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
255  if (i != myCharValues.end()) {
256  const char func = i->second;
257  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
258  return (SumoXMLEdgeFunc)func;
259  }
260  ok = false;
261  }
262  return EDGEFUNC_NORMAL;
263 }
264 
265 
268  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
269  if (i != myCharValues.end()) {
270  const char type = i->second;
271  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
272  return (SumoXMLNodeType)type;
273  }
274  ok = false;
275  }
276  return NODETYPE_UNKNOWN;
277 }
278 
279 
280 RGBColor
282  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
283  if (i == myIntValues.end()) {
284  throw EmptyData();
285  }
286  const int val = i->second;
287  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
288 }
289 
290 
293  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
294  if (i == myPositionVectors.end() || i->second.size() == 0) {
295  throw EmptyData();
296  }
297  return i->second;
298 }
299 
300 
301 Boundary
303  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
304  if (i == myPositionVectors.end() || i->second.size() == 0) {
305  throw EmptyData();
306  }
307  if (i->second.size() != 2) {
308  throw FormatException("boundary format");
309  }
310  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
311 }
312 
313 
314 std::vector<std::string>
316  std::string def = getString(attr);
317  std::vector<std::string> ret;
318  parseStringVector(def, ret);
319  return ret;
320 }
321 
322 
323 std::string
325  if (myAttrIds.find(attr) == myAttrIds.end()) {
326  return "?";
327  }
328  return myAttrIds.find(attr)->second;
329 }
330 
331 
332 void
334  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
335  os << " " << getName(*i);
336  os << "=\"" << getStringSecure(*i, "?") << "\"";
337  }
338 }
339 
340 
343  std::map<std::string, std::string> attrs;
344  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
345  const std::string attrName = myAttrIds.find(it->first)->second;
346  attrs[attrName] = toString(it->second);
347  }
348  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
349  const std::string attrName = myAttrIds.find(it->first)->second;
350  attrs[attrName] = toString(it->second);
351  }
352  for (std::map<int, SUMOReal>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
353  const std::string attrName = myAttrIds.find(it->first)->second;
354  attrs[attrName] = toString(it->second);
355  }
356  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
357  const std::string attrName = myAttrIds.find(it->first)->second;
358  attrs[attrName] = it->second;
359  }
360  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
361  const std::string attrName = myAttrIds.find(it->first)->second;
362  attrs[attrName] = toString(it->second);
363  }
365 }
366 
367 /****************************************************************************/
368 
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
int peek()
Returns the next character to be read by an actual parse.
static StringBijection< SumoXMLNodeType > NodeTypes
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:297
std::map< int, char > myCharValues
Map of attribute ids to char.
void putback(char c)
Pushes a character back into the stream to be read by the next actual parse.
RGBColor getColor() const
Returns the value of the named attribute.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
Encapsulated Xerces-SAX-attributes.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
std::map< int, std::string > myStringValues
Map of attribute ids to string.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
const std::string & getObjectType() const
return the objecttype to which these attributes belong
SUMOReal getFloat(int id) const
Returns the SUMOReal-value of the named (by its enum-value) attribute.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
const std::map< int, std::string > & myAttrIds
Map of attribute ids to names.
A list of positions.
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
std::map< int, SUMOReal > myFloatValues
Map of attribute ids to floats.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
std::map< int, int > myIntValues
Map of attribute ids to integers.
std::map< int, PositionVector > myPositionVectors
Map of attribute ids to string.
std::set< int > myAttrs
the attributes which are set
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
SUMOSAXAttributesImpl_Binary(const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType, BinaryInputDevice *in, const char version)
Constructor.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
#define SUMOReal
Definition: config.h:208
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
int size() const
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
A color information.
Encapsulates binary reading operations on a file.