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-2013 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& /* attrs */,
69  const std::string& /* comment */) {
70  if (myXMLStack.empty()) {
72  FileHelpers::writeByte(into, 1);
75  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
76  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
79  writeStringList(into, std::vector<std::string>());
80  writeStringList(into, std::vector<std::string>());
81 
82  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
83  openTag(into, rootElement);
84  return true;
85  }
86  }
87  return false;
88 }
89 
90 
91 void
92 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
93  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
94  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
95  }
96 }
97 
98 
99 void
100 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
101  myXMLStack.push_back(xmlElement);
103  FileHelpers::writeByte(into, static_cast<unsigned char>(xmlElement));
104 }
105 
106 
107 bool
108 BinaryFormatter::closeTag(std::ostream& into) {
109  if (!myXMLStack.empty()) {
111  FileHelpers::writeByte(into, static_cast<unsigned char>(myXMLStack.back()));
112  myXMLStack.pop_back();
113  return true;
114  }
115  return false;
116 }
117 
118 
119 void
120 BinaryFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val) {
121  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
122  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
123  }
124 }
125 
126 
127 template<>
128 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
130  FileHelpers::writeByte(into, val);
131 }
132 
133 
134 template<>
135 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
136  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
138  FileHelpers::writeInt(into, int(val * 100. + .5));
139  } else {
141  FileHelpers::writeFloat(into, val);
142  }
143 }
144 
145 
146 template<>
147 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
149  FileHelpers::writeInt(into, val);
150 }
151 
152 
153 template<>
154 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val) {
156  FileHelpers::writeInt(into, val);
157 }
158 
159 
160 template<>
161 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
163  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
164 }
165 
166 
167 template<>
168 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
170  FileHelpers::writeByte(into, static_cast<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 template<>
204 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
206  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
207  writePosition(into, val);
208 }
209 
210 
211 template<>
212 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
214  FileHelpers::writeInt(into, static_cast<int>(val.size()));
215  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
216  writePosition(into, *pos);
217  }
218 }
219 
220 
221 template<>
222 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
224  FileHelpers::writeFloat(into, val.xmin());
225  FileHelpers::writeFloat(into, val.ymin());
226  FileHelpers::writeFloat(into, val.xmax());
227  FileHelpers::writeFloat(into, val.ymax());
228 }
229 
230 
231 template<>
232 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
234  FileHelpers::writeByte(into, val.red());
235  FileHelpers::writeByte(into, val.green());
236  FileHelpers::writeByte(into, val.blue());
237  FileHelpers::writeByte(into, val.alpha());
238 }
239 
240 
241 template<>
242 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::vector<int>& val) {
244  FileHelpers::writeInt(into, (int)val.size());
245  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
247  FileHelpers::writeInt(into, *it);
248  }
249 }
250 
251 
252 /****************************************************************************/