SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Static storage of an output device and its base (abstract) implementation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #ifdef HAVE_VERSION_H
32 #include <version.h>
33 #endif
34 
35 #include <utils/common/RGBColor.h>
36 #include <utils/common/ToString.h>
40 #include <utils/geom/Boundary.h>
41 #include "BinaryFormatter.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52 }
53 
54 
55 void
56 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
58  FileHelpers::writeInt(into, (int)list.size());
59  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
61  FileHelpers::writeString(into, *it);
62  }
63 }
64 
65 bool
67  const std::string& rootElement,
68  const std::string /* xmlParams */,
69  const std::string& /* attrs */,
70  const std::string& /* comment */) {
71  if (myXMLStack.empty()) {
73  FileHelpers::writeByte(into, 1);
76  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
77  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
80  writeStringList(into, std::vector<std::string>());
81  writeStringList(into, std::vector<std::string>());
82 
83  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
84  openTag(into, rootElement);
85  return true;
86  }
87  }
88  return false;
89 }
90 
91 
92 void
93 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
94  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
95  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
96  }
97 }
98 
99 
100 void
101 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
102  myXMLStack.push_back(xmlElement);
104  FileHelpers::writeByte(into, xmlElement);
105 }
106 
107 
108 void
109 BinaryFormatter::closeOpener(std::ostream& /* into */) {
110 }
111 
112 
113 bool
114 BinaryFormatter::closeTag(std::ostream& into, bool /* abbreviated */) {
115  if (!myXMLStack.empty()) {
117  FileHelpers::writeByte(into, myXMLStack.back());
118  myXMLStack.pop_back();
119  return true;
120  }
121  return false;
122 }
123 
124 
125 void
126 BinaryFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val) {
127  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
128  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
129  }
130 }
131 
132 
133 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
135  FileHelpers::writeByte(into, val);
136 }
137 
138 
139 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
140  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
142  FileHelpers::writeInt(into, int(val * 100. + .5));
143  } else {
145  FileHelpers::writeFloat(into, val);
146  }
147 }
148 
149 
150 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
152  FileHelpers::writeInt(into, val);
153 }
154 
155 
156 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val) {
158  FileHelpers::writeInt(into, val);
159 }
160 
161 
162 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
164  FileHelpers::writeByte(into, (unsigned char) val);
165 }
166 
167 
168 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
170  FileHelpers::writeByte(into, (unsigned char) val);
171 }
172 
173 
174 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
175  if (val.z() != 0.) {
176  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
177  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
179  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
180  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
181  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
182  } else {
184  FileHelpers::writeFloat(into, val.x());
185  FileHelpers::writeFloat(into, val.y());
186  FileHelpers::writeFloat(into, val.z());
187  }
188  } else {
189  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
190  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
192  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
193  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
194  } else {
196  FileHelpers::writeFloat(into, val.x());
197  FileHelpers::writeFloat(into, val.y());
198  }
199  }
200 }
201 
202 
203 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
204  FileHelpers::writeByte(into, static_cast<unsigned char>(BF_XML_ATTRIBUTE));
205  FileHelpers::writeByte(into, attr);
206  writePosition(into, val);
207 }
208 
209 
210 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
212  FileHelpers::writeInt(into, (int)val.size());
213  for (PositionVector::ContType::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
214  writePosition(into, *pos);
215  }
216 }
217 
218 
219 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
221  FileHelpers::writeFloat(into, val.xmin());
222  FileHelpers::writeFloat(into, val.ymin());
223  FileHelpers::writeFloat(into, val.xmax());
224  FileHelpers::writeFloat(into, val.ymax());
225 }
226 
227 
228 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
230  FileHelpers::writeByte(into, char(val.red() * 255 + .5));
231  FileHelpers::writeByte(into, char(val.green() * 255 + .5));
232  FileHelpers::writeByte(into, char(val.blue() * 255 + .5));
233  FileHelpers::writeByte(into, 0);
234 }
235 
236 
237 /****************************************************************************/