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/ToString.h>
39 #include <utils/geom/Boundary.h>
40 #include "BinaryFormatter.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // member method definitions
49 // ===========================================================================
51 }
52 
53 
54 void
55 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
57  FileHelpers::writeInt(into, list.size());
58  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
60  FileHelpers::writeString(into, *it);
61  }
62 }
63 
64 bool
66  const std::string& rootElement,
67  const std::string xmlParams,
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  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
80  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(rootElement)));
81  return true;
82  }
83  }
84  return false;
85 }
86 
87 
88 void
89 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
90  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
91  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
92  }
93 }
94 
95 
96 void
97 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
98  myXMLStack.push_back(xmlElement);
100  FileHelpers::writeInt(into, xmlElement);
101 }
102 
103 
104 void
105 BinaryFormatter::closeOpener(std::ostream& into) {
106 }
107 
108 
109 bool
110 BinaryFormatter::closeTag(std::ostream& into, bool abbreviated) {
111  if (!myXMLStack.empty()) {
113  FileHelpers::writeInt(into, myXMLStack.back());
114  myXMLStack.pop_back();
115  return true;
116  }
117  return false;
118 }
119 
120 
121 void
122 BinaryFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val) {
123  if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
124  writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
125  }
126 }
127 
128 
129 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
131  FileHelpers::writeFloat(into, val);
132 }
133 
134 
135 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
137  FileHelpers::writeInt(into, val);
138 }
139 
140 
141 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
143  FileHelpers::writeByte(into, val);
144 }
145 
146 
147 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
149  FileHelpers::writeByte(into, val);
150 }
151 
152 
153 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
154  if (val.z() != 0.) {
156  FileHelpers::writeFloat(into, val.x());
157  FileHelpers::writeFloat(into, val.y());
158  FileHelpers::writeFloat(into, val.z());
159  } else {
161  FileHelpers::writeFloat(into, val.x());
162  FileHelpers::writeFloat(into, val.y());
163  }
164 }
165 
166 
167 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
169  FileHelpers::writeInt(into, val.size());
170  for (PositionVector::ContType::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
171  if (pos->z() != 0.) {
173  FileHelpers::writeFloat(into, pos->x());
174  FileHelpers::writeFloat(into, pos->y());
175  FileHelpers::writeFloat(into, pos->z());
176  } else {
178  FileHelpers::writeFloat(into, pos->x());
179  FileHelpers::writeFloat(into, pos->y());
180  }
181  }
182 }
183 
184 
185 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
187  FileHelpers::writeInt(into, 2);
189  FileHelpers::writeFloat(into, val.xmin());
190  FileHelpers::writeFloat(into, val.ymin());
192  FileHelpers::writeFloat(into, val.xmax());
193  FileHelpers::writeFloat(into, val.ymax());
194 }
195 
196 /****************************************************************************/
197