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 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
49  const std::map<int, std::string>& predefinedTagsMML,
50  const std::string& objectType,
51  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
52  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
53  int attr;
54  unsigned char attrByte;
55  *in >> attrByte;
56  attr = attrByte;
57  if (version > 1) {
59  *in >> attrByte;
60  attr += 256 * attrByte;
61  }
62  int type = in->peek();
63  switch (type) {
65  *in >> myCharValues[attr];
66  break;
68  *in >> myIntValues[attr];
69  break;
72  *in >> myFloatValues[attr];
73  break;
75  *in >> myStringValues[attr];
76  break;
78  int size;
79  *in >> size;
80  while (size > 0) {
81  const int type = in->peek();
85  throw ProcessError("Invalid binary file, only supporting position vectors.");
86  }
87  size--;
88  Position p;
89  *in >> p;
90  myPositionVectors[attr].push_back(p);
91  }
92  break;
93  }
95  *in >> myIntValues[attr];
96  break;
98  *in >> myIntValues[attr];
100  *in >> myCharValues[attr];
101  break;
106  Position p;
107  *in >> p;
108  myPositionVectors[attr].push_back(p);
109  break;
110  }
112  Position p;
113  *in >> p;
114  myPositionVectors[attr].push_back(p);
116  *in >> p;
117  myPositionVectors[attr].push_back(p);
118  break;
119  }
121  *in >> myIntValues[attr];
122  break;
124  *in >> myCharValues[attr];
125  break;
127  *in >> myCharValues[attr];
128  break;
130  std::ostringstream into(std::ios::binary);
131  int size;
132  *in >> size;
134  FileHelpers::writeInt(into, size);
135  if (size > 0) {
136  int intsToRead = size - 1;
137  int bitsOrEntry;
139  *in >> bitsOrEntry;
140  FileHelpers::writeInt(into, bitsOrEntry);
141  if (bitsOrEntry < 0) {
142  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
143  }
144  while (intsToRead > 0) {
146  *in >> bitsOrEntry;
147  FileHelpers::writeInt(into, bitsOrEntry);
148  intsToRead--;
149  }
150  }
151  myStringValues[attr] = into.str();
152  break;
153  }
154  default:
155  throw ProcessError("Binary file is invalid, attribute type is unknown.");
156  }
157  myAttrs.insert(attr);
158  }
159 }
160 
161 
163 }
164 
165 
166 bool
168  return myAttrs.find(id) != myAttrs.end();
169 }
170 
171 
172 bool
174  const std::map<int, char>::const_iterator i = myCharValues.find(id);
175  if (i == myCharValues.end()) {
176  throw EmptyData();
177  }
178  return i->second != 0;
179 }
180 
181 
182 int
184  const std::map<int, int>::const_iterator i = myIntValues.find(id);
185  if (i == myIntValues.end()) {
186  throw EmptyData();
187  }
188  return i->second;
189 }
190 
191 
192 long long int
194  throw NumberFormatException();
195 }
196 
197 
198 std::string
200  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
201  if (i == myStringValues.end()) {
202  throw EmptyData();
203  }
204  return i->second;
205 }
206 
207 
208 std::string
210  const std::string& str) const {
211  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
212  if (i == myStringValues.end()) {
213  return str;
214  }
215  return i->second;
216 }
217 
218 
219 double
221  const std::map<int, double>::const_iterator i = myFloatValues.find(id);
222  if (i == myFloatValues.end()) {
223  return TplConvert::_2double(getString(id).c_str());
224  }
225  return i->second;
226 }
227 
228 
229 double
230 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
231  throw ProcessError("not implemented for binary data");
232 }
233 
234 
235 bool
236 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
237  throw ProcessError("not implemented for binary data");
238 }
239 
240 
241 std::string
243  const std::string& /* str */) const {
244  throw ProcessError("not implemented for binary data");
245 }
246 
247 
250  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
251  if (i != myCharValues.end()) {
252  const char func = i->second;
253  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
254  return (SumoXMLEdgeFunc)func;
255  }
256  ok = false;
257  }
258  return EDGEFUNC_NORMAL;
259 }
260 
261 
264  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
265  if (i != myCharValues.end()) {
266  const char type = i->second;
267  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
268  return (SumoXMLNodeType)type;
269  }
270  ok = false;
271  }
272  return NODETYPE_UNKNOWN;
273 }
274 
275 
276 RGBColor
278  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
279  if (i == myIntValues.end()) {
280  throw EmptyData();
281  }
282  const int val = i->second;
283  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
284 }
285 
286 
289  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
290  if (i == myPositionVectors.end() || i->second.size() == 0) {
291  throw EmptyData();
292  }
293  return i->second;
294 }
295 
296 
297 Boundary
299  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
300  if (i == myPositionVectors.end() || i->second.size() == 0) {
301  throw EmptyData();
302  }
303  if (i->second.size() != 2) {
304  throw FormatException("boundary format");
305  }
306  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
307 }
308 
309 
310 std::vector<std::string>
312  std::string def = getString(attr);
313  std::vector<std::string> ret;
314  parseStringVector(def, ret);
315  return ret;
316 }
317 
318 
319 std::string
321  if (myAttrIds.find(attr) == myAttrIds.end()) {
322  return "?";
323  }
324  return myAttrIds.find(attr)->second;
325 }
326 
327 
328 void
330  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
331  os << " " << getName(*i);
332  os << "=\"" << getStringSecure(*i, "?") << "\"";
333  }
334 }
335 
336 
339  std::map<std::string, std::string> attrs;
340  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
341  const std::string attrName = myAttrIds.find(it->first)->second;
342  attrs[attrName] = toString(it->second);
343  }
344  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
345  const std::string attrName = myAttrIds.find(it->first)->second;
346  attrs[attrName] = toString(it->second);
347  }
348  for (std::map<int, double>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
349  const std::string attrName = myAttrIds.find(it->first)->second;
350  attrs[attrName] = toString(it->second);
351  }
352  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
353  const std::string attrName = myAttrIds.find(it->first)->second;
354  attrs[attrName] = it->second;
355  }
356  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
357  const std::string attrName = myAttrIds.find(it->first)->second;
358  attrs[attrName] = toString(it->second);
359  }
361 }
362 
363 /****************************************************************************/
364 
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
int peek()
Returns the next character to be read by an actual parse.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
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.
std::map< int, double > myFloatValues
Map of attribute ids to floats.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...
const std::string & getObjectType() const
return the objecttype to which these attributes belong
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
Encapsulated Xerces-SAX-attributes.
std::map< int, std::string > myStringValues
Map of attribute ids to string.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
int size() const
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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.
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.
RGBColor getColor() const
Returns the value of the named attribute.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) 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.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
A color information.
Encapsulates binary reading operations on a file.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.