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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Encapsulated xml-attributes that are retrieved from the sumo-binary-xml format (already typed)
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 <cassert>
33 #include <sstream>
34 #include <utils/common/RGBColor.h>
36 #include <utils/geom/Boundary.h>
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
48  const std::map<int, std::string>& predefinedTagsMML,
49  const std::string& objectType,
50  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
51  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
52  int attr;
53  unsigned char attrByte;
54  *in >> attrByte;
55  attr = attrByte;
56  if (version > 1) {
58  *in >> attrByte;
59  attr += 256 * attrByte;
60  }
61  int type = in->peek();
62  switch (type) {
64  *in >> myCharValues[attr];
65  break;
67  *in >> myIntValues[attr];
68  break;
71  *in >> myFloatValues[attr];
72  break;
74  *in >> myStringValues[attr];
75  break;
77  int size;
78  *in >> size;
79  while (size > 0) {
80  const int type = in->peek();
84  throw ProcessError("Invalid binary file, only supporting position vectors.");
85  }
86  size--;
87  Position p;
88  *in >> p;
89  myPositionVectors[attr].push_back(p);
90  }
91  break;
92  }
94  *in >> myIntValues[attr];
95  break;
97  *in >> myIntValues[attr];
99  *in >> myCharValues[attr];
100  break;
105  Position p;
106  *in >> p;
107  myPositionVectors[attr].push_back(p);
108  break;
109  }
111  Position p;
112  *in >> p;
113  myPositionVectors[attr].push_back(p);
115  *in >> p;
116  myPositionVectors[attr].push_back(p);
117  break;
118  }
120  *in >> myIntValues[attr];
121  break;
123  *in >> myCharValues[attr];
124  break;
126  *in >> myCharValues[attr];
127  break;
129  std::ostringstream into(std::ios::binary);
130  int size;
131  *in >> size;
133  FileHelpers::writeInt(into, size);
134  if (size > 0) {
135  int intsToRead = size - 1;
136  int bitsOrEntry;
138  *in >> bitsOrEntry;
139  FileHelpers::writeInt(into, bitsOrEntry);
140  if (bitsOrEntry < 0) {
141  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
142  }
143  while (intsToRead > 0) {
145  *in >> bitsOrEntry;
146  FileHelpers::writeInt(into, bitsOrEntry);
147  intsToRead--;
148  }
149  }
150  myStringValues[attr] = into.str();
151  break;
152  }
153  default:
154  throw ProcessError("Binary file is invalid, attribute type is unknown.");
155  }
156  myAttrs.insert(attr);
157  }
158 }
159 
160 
162 }
163 
164 
165 bool
167  return myAttrs.find(id) != myAttrs.end();
168 }
169 
170 
171 bool
173  const std::map<int, char>::const_iterator i = myCharValues.find(id);
174  if (i == myCharValues.end()) {
175  throw EmptyData();
176  }
177  return i->second != 0;
178 }
179 
180 
181 int
183  const std::map<int, int>::const_iterator i = myIntValues.find(id);
184  if (i == myIntValues.end()) {
185  throw EmptyData();
186  }
187  return i->second;
188 }
189 
190 
191 long long int
193  throw NumberFormatException();
194 }
195 
196 
197 std::string
199  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
200  if (i == myStringValues.end()) {
201  throw EmptyData();
202  }
203  return i->second;
204 }
205 
206 
207 std::string
209  const std::string& str) const {
210  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
211  if (i == myStringValues.end()) {
212  return str;
213  }
214  return i->second;
215 }
216 
217 
218 double
220  const std::map<int, double>::const_iterator i = myFloatValues.find(id);
221  if (i == myFloatValues.end()) {
222  return TplConvert::_2double(getString(id).c_str());
223  }
224  return i->second;
225 }
226 
227 
228 double
229 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
230  throw ProcessError("not implemented for binary data");
231 }
232 
233 
234 bool
235 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
236  throw ProcessError("not implemented for binary data");
237 }
238 
239 
240 std::string
242  const std::string& /* str */) const {
243  throw ProcessError("not implemented for binary data");
244 }
245 
246 
249  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
250  if (i != myCharValues.end()) {
251  const char func = i->second;
252  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
253  return (SumoXMLEdgeFunc)func;
254  }
255  ok = false;
256  }
257  return EDGEFUNC_NORMAL;
258 }
259 
260 
263  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
264  if (i != myCharValues.end()) {
265  const char type = i->second;
266  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
267  return (SumoXMLNodeType)type;
268  }
269  ok = false;
270  }
271  return NODETYPE_UNKNOWN;
272 }
273 
274 
275 RGBColor
277  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
278  if (i == myIntValues.end()) {
279  throw EmptyData();
280  }
281  const int val = i->second;
282  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
283 }
284 
285 
288  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
289  if (i == myPositionVectors.end() || i->second.size() == 0) {
290  throw EmptyData();
291  }
292  return i->second;
293 }
294 
295 
296 Boundary
298  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
299  if (i == myPositionVectors.end() || i->second.size() == 0) {
300  throw EmptyData();
301  }
302  if (i->second.size() != 2) {
303  throw FormatException("boundary format");
304  }
305  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
306 }
307 
308 
309 std::vector<std::string>
311  std::string def = getString(attr);
312  std::vector<std::string> ret;
313  parseStringVector(def, ret);
314  return ret;
315 }
316 
317 
318 std::string
320  if (myAttrIds.find(attr) == myAttrIds.end()) {
321  return "?";
322  }
323  return myAttrIds.find(attr)->second;
324 }
325 
326 
327 void
329  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
330  os << " " << getName(*i);
331  os << "=\"" << getStringSecure(*i, "?") << "\"";
332  }
333 }
334 
335 
338  std::map<std::string, std::string> attrs;
339  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
340  const std::string attrName = myAttrIds.find(it->first)->second;
341  attrs[attrName] = toString(it->second);
342  }
343  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
344  const std::string attrName = myAttrIds.find(it->first)->second;
345  attrs[attrName] = toString(it->second);
346  }
347  for (std::map<int, double>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
348  const std::string attrName = myAttrIds.find(it->first)->second;
349  attrs[attrName] = toString(it->second);
350  }
351  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
352  const std::string attrName = myAttrIds.find(it->first)->second;
353  attrs[attrName] = it->second;
354  }
355  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
356  const std::string attrName = myAttrIds.find(it->first)->second;
357  attrs[attrName] = toString(it->second);
358  }
360 }
361 
362 /****************************************************************************/
363 
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:47
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:55
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
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:311
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.