SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Binary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Encapsulated xml-attributes that are retrieved from the sumo-binary-xml format (already typed)
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <sstream>
28 #include <utils/common/RGBColor.h>
30 #include <utils/geom/Boundary.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
42  const std::map<int, std::string>& predefinedTagsMML,
43  const std::string& objectType,
44  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
45  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
46  int attr;
47  unsigned char attrByte;
48  *in >> attrByte;
49  attr = attrByte;
50  if (version > 1) {
52  *in >> attrByte;
53  attr += 256 * attrByte;
54  }
55  int type = in->peek();
56  switch (type) {
58  *in >> myCharValues[attr];
59  break;
61  *in >> myIntValues[attr];
62  break;
65  *in >> myFloatValues[attr];
66  break;
68  *in >> myStringValues[attr];
69  break;
71  int size;
72  *in >> size;
73  while (size > 0) {
74  const int type = in->peek();
78  throw ProcessError("Invalid binary file, only supporting position vectors.");
79  }
80  size--;
81  Position p;
82  *in >> p;
83  myPositionVectors[attr].push_back(p);
84  }
85  break;
86  }
88  *in >> myIntValues[attr];
89  break;
91  *in >> myIntValues[attr];
93  *in >> myCharValues[attr];
94  break;
99  Position p;
100  *in >> p;
101  myPositionVectors[attr].push_back(p);
102  break;
103  }
105  Position p;
106  *in >> p;
107  myPositionVectors[attr].push_back(p);
109  *in >> p;
110  myPositionVectors[attr].push_back(p);
111  break;
112  }
114  *in >> myIntValues[attr];
115  break;
117  *in >> myCharValues[attr];
118  break;
120  *in >> myCharValues[attr];
121  break;
123  std::ostringstream into(std::ios::binary);
124  int size;
125  *in >> size;
127  FileHelpers::writeInt(into, size);
128  if (size > 0) {
129  int intsToRead = size - 1;
130  int bitsOrEntry;
132  *in >> bitsOrEntry;
133  FileHelpers::writeInt(into, bitsOrEntry);
134  if (bitsOrEntry < 0) {
135  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
136  }
137  while (intsToRead > 0) {
139  *in >> bitsOrEntry;
140  FileHelpers::writeInt(into, bitsOrEntry);
141  intsToRead--;
142  }
143  }
144  myStringValues[attr] = into.str();
145  break;
146  }
147  default:
148  throw ProcessError("Binary file is invalid, attribute type is unknown.");
149  }
150  myAttrs.insert(attr);
151  }
152 }
153 
154 
156 }
157 
158 
159 bool
161  return myAttrs.find(id) != myAttrs.end();
162 }
163 
164 
165 bool
167  const std::map<int, char>::const_iterator i = myCharValues.find(id);
168  if (i == myCharValues.end()) {
169  throw EmptyData();
170  }
171  return i->second != 0;
172 }
173 
174 
175 int
177  const std::map<int, int>::const_iterator i = myIntValues.find(id);
178  if (i == myIntValues.end()) {
179  throw EmptyData();
180  }
181  return i->second;
182 }
183 
184 
185 long long int
187  return TplConvert::_2long(getString(id).c_str());
188 }
189 
190 
191 std::string
193  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
194  if (i == myStringValues.end()) {
195  throw EmptyData();
196  }
197  return i->second;
198 }
199 
200 
201 std::string
203  const std::string& str) const {
204  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
205  if (i == myStringValues.end()) {
206  return str;
207  }
208  return i->second;
209 }
210 
211 
212 double
214  const std::map<int, double>::const_iterator i = myFloatValues.find(id);
215  if (i == myFloatValues.end()) {
216  return TplConvert::_2double(getString(id).c_str());
217  }
218  return i->second;
219 }
220 
221 
222 double
223 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
224  throw ProcessError("not implemented for binary data");
225 }
226 
227 
228 bool
229 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
230  throw ProcessError("not implemented for binary data");
231 }
232 
233 
234 std::string
236  const std::string& /* str */) const {
237  throw ProcessError("not implemented for binary data");
238 }
239 
240 
243  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
244  if (i != myCharValues.end()) {
245  const char func = i->second;
246  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
247  return (SumoXMLEdgeFunc)func;
248  }
249  ok = false;
250  }
251  return EDGEFUNC_NORMAL;
252 }
253 
254 
257  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
258  if (i != myCharValues.end()) {
259  const char type = i->second;
260  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
261  return (SumoXMLNodeType)type;
262  }
263  ok = false;
264  }
265  return NODETYPE_UNKNOWN;
266 }
267 
268 
269 RGBColor
271  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
272  if (i == myIntValues.end()) {
273  throw EmptyData();
274  }
275  const int val = i->second;
276  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
277 }
278 
279 
282  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
283  if (i == myPositionVectors.end() || i->second.size() == 0) {
284  throw EmptyData();
285  }
286  return i->second;
287 }
288 
289 
290 Boundary
292  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
293  if (i == myPositionVectors.end() || i->second.size() == 0) {
294  throw EmptyData();
295  }
296  if (i->second.size() != 2) {
297  throw FormatException("boundary format");
298  }
299  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
300 }
301 
302 
303 std::vector<std::string>
305  std::string def = getString(attr);
306  std::vector<std::string> ret;
307  parseStringVector(def, ret);
308  return ret;
309 }
310 
311 
312 std::string
314  if (myAttrIds.find(attr) == myAttrIds.end()) {
315  return "?";
316  }
317  return myAttrIds.find(attr)->second;
318 }
319 
320 
321 void
323  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
324  os << " " << getName(*i);
325  os << "=\"" << getStringSecure(*i, "?") << "\"";
326  }
327 }
328 
329 
332  std::map<std::string, std::string> attrs;
333  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
334  const std::string attrName = myAttrIds.find(it->first)->second;
335  attrs[attrName] = toString(it->second);
336  }
337  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
338  const std::string attrName = myAttrIds.find(it->first)->second;
339  attrs[attrName] = toString(it->second);
340  }
341  for (std::map<int, double>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
342  const std::string attrName = myAttrIds.find(it->first)->second;
343  attrs[attrName] = toString(it->second);
344  }
345  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
346  const std::string attrName = myAttrIds.find(it->first)->second;
347  attrs[attrName] = it->second;
348  }
349  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
350  const std::string attrName = myAttrIds.find(it->first)->second;
351  attrs[attrName] = toString(it->second);
352  }
354 }
355 
356 /****************************************************************************/
357 
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.
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:204
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:42
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:49
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
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:305
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.