SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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.dlr.de/
12 // Copyright (C) 2002-2015 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 int
180  const std::map<int, int>::const_iterator i = myIntValues.find(id);
181  if (i == myIntValues.end()) {
182  throw EmptyData();
183  }
184  return i->second;
185 }
186 
187 
188 SUMOLong
190  throw NumberFormatException();
191 }
192 
193 
194 std::string
196  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
197  if (i == myStringValues.end()) {
198  throw EmptyData();
199  }
200  return i->second;
201 }
202 
203 
204 std::string
206  const std::string& str) const {
207  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
208  if (i == myStringValues.end()) {
209  return str;
210  }
211  return i->second;
212 }
213 
214 
215 SUMOReal
217  const std::map<int, SUMOReal>::const_iterator i = myFloatValues.find(id);
218  if (i == myFloatValues.end()) {
219  return TplConvert::_2SUMOReal(getString(id).c_str());
220  }
221  return i->second;
222 }
223 
224 
225 SUMOReal
226 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
227  throw ProcessError("not implemented for binary data");
228 }
229 
230 
231 bool
232 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
233  throw ProcessError("not implemented for binary data");
234 }
235 
236 
237 std::string
239  const std::string& /* str */) const {
240  throw ProcessError("not implemented for binary data");
241 }
242 
243 
246  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
247  if (i != myCharValues.end()) {
248  const char func = i->second;
249  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
250  return (SumoXMLEdgeFunc)func;
251  }
252  ok = false;
253  }
254  return EDGEFUNC_NORMAL;
255 }
256 
257 
260  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
261  if (i != myCharValues.end()) {
262  const char type = i->second;
263  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
264  return (SumoXMLNodeType)type;
265  }
266  ok = false;
267  }
268  return NODETYPE_UNKNOWN;
269 }
270 
271 
272 RGBColor
274  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
275  if (i == myIntValues.end()) {
276  throw EmptyData();
277  }
278  const int val = i->second;
279  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
280 }
281 
282 
285  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
286  if (i == myPositionVectors.end() || i->second.size() == 0) {
287  throw EmptyData();
288  }
289  return i->second;
290 }
291 
292 
293 Boundary
295  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
296  if (i == myPositionVectors.end() || i->second.size() == 0) {
297  throw EmptyData();
298  }
299  if (i->second.size() != 2) {
300  throw FormatException("boundary format");
301  }
302  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
303 }
304 
305 
306 std::vector<std::string>
308  std::string def = getString(attr);
309  std::vector<std::string> ret;
310  parseStringVector(def, ret);
311  return ret;
312 }
313 
314 
315 std::string
317  if (myAttrIds.find(attr) == myAttrIds.end()) {
318  return "?";
319  }
320  return myAttrIds.find(attr)->second;
321 }
322 
323 
324 void
326  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
327  os << " " << getName(*i);
328  os << "=\"" << getStringSecure(*i, "?") << "\"";
329  }
330 }
331 
332 
333 /****************************************************************************/
334 
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.
SUMOLong getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
static StringBijection< SumoXMLNodeType > NodeTypes
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
std::map< int, char > myCharValues
Map of attribute ids to char.
void putback(char c)
Returns the next character to be read by an 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.
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
SUMOReal getFloat(int id) const
Returns the SUMOReal-value of the named (by its enum-value) attribute.
size_t size() const
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.
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.
#define SUMOLong
Definition: config.h:215
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.
SUMOSAXAttributesImpl_Binary(const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType, BinaryInputDevice *in)
Constructor.
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 ...
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:218
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.
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
A color information.
Encapsulates binary reading operations on a file.