SUMO - Simulation of Urban MObility
BinaryFormatter.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Static storage of an output device and its base (abstract) implementation
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This file is part of SUMO.
00014 //   SUMO is free software: you can redistribute it and/or modify
00015 //   it under the terms of the GNU General Public License as published by
00016 //   the Free Software Foundation, either version 3 of the License, or
00017 //   (at your option) any later version.
00018 //
00019 /****************************************************************************/
00020 
00021 
00022 // ===========================================================================
00023 // included modules
00024 // ===========================================================================
00025 #ifdef _MSC_VER
00026 #include <windows_config.h>
00027 #else
00028 #include <config.h>
00029 #endif
00030 
00031 #ifdef HAVE_VERSION_H
00032 #include <version.h>
00033 #endif
00034 
00035 #include <utils/common/ToString.h>
00036 #include <utils/common/FileHelpers.h>
00037 #include <utils/xml/SUMOXMLDefinitions.h>
00038 #include <utils/geom/PositionVector.h>
00039 #include <utils/geom/Boundary.h>
00040 #include "BinaryFormatter.h"
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // member method definitions
00049 // ===========================================================================
00050 BinaryFormatter::BinaryFormatter() {
00051 }
00052 
00053 
00054 void
00055 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
00056     FileHelpers::writeByte(into, BF_LIST);
00057     FileHelpers::writeInt(into, list.size());
00058     for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
00059         FileHelpers::writeByte(into, BF_STRING);
00060         FileHelpers::writeString(into, *it);
00061     }
00062 }
00063 
00064 bool
00065 BinaryFormatter::writeXMLHeader(std::ostream& into,
00066                                 const std::string& rootElement,
00067                                 const std::string xmlParams,
00068                                 const std::string& attrs,
00069                                 const std::string& comment) {
00070     if (myXMLStack.empty()) {
00071         FileHelpers::writeByte(into, BF_BYTE);
00072         FileHelpers::writeByte(into, 1);
00073         FileHelpers::writeByte(into, BF_STRING);
00074         FileHelpers::writeString(into, VERSION_STRING);
00075         writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
00076         writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
00077         writeStringList(into, SUMOXMLDefinitions::NodeTypes.getStrings());
00078         writeStringList(into, SUMOXMLDefinitions::EdgeFunctions.getStrings());
00079         if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
00080             openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(rootElement)));
00081             return true;
00082         }
00083     }
00084     return false;
00085 }
00086 
00087 
00088 void
00089 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
00090     if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
00091         openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
00092     }
00093 }
00094 
00095 
00096 void
00097 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
00098     myXMLStack.push_back(xmlElement);
00099     FileHelpers::writeByte(into, BF_XML_TAG_START);
00100     FileHelpers::writeInt(into, xmlElement);
00101 }
00102 
00103 
00104 void
00105 BinaryFormatter::closeOpener(std::ostream& into) {
00106 }
00107 
00108 
00109 bool
00110 BinaryFormatter::closeTag(std::ostream& into, bool abbreviated) {
00111     if (!myXMLStack.empty()) {
00112         FileHelpers::writeByte(into, BF_XML_TAG_END);
00113         FileHelpers::writeInt(into, myXMLStack.back());
00114         myXMLStack.pop_back();
00115         return true;
00116     }
00117     return false;
00118 }
00119 
00120 
00121 void
00122 BinaryFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val) {
00123     if (SUMOXMLDefinitions::Attrs.hasString(attr)) {
00124         writeAttr(into, (const SumoXMLAttr)(SUMOXMLDefinitions::Attrs.get(attr)), val);
00125     }
00126 }
00127 
00128 
00129 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
00130     BinaryFormatter::writeAttrHeader(into, attr, BF_FLOAT);
00131     FileHelpers::writeFloat(into, val);
00132 }
00133 
00134 
00135 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
00136     BinaryFormatter::writeAttrHeader(into, attr, BF_INTEGER);
00137     FileHelpers::writeInt(into, val);
00138 }
00139 
00140 
00141 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
00142     BinaryFormatter::writeAttrHeader(into, attr, BF_NODE_TYPE);
00143     FileHelpers::writeByte(into, val);
00144 }
00145 
00146 
00147 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
00148     BinaryFormatter::writeAttrHeader(into, attr, BF_EDGE_FUNCTION);
00149     FileHelpers::writeByte(into, val);
00150 }
00151 
00152 
00153 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
00154     if (val.z() != 0.) {
00155         BinaryFormatter::writeAttrHeader(into, attr, BF_POSITION_3D);
00156         FileHelpers::writeFloat(into, val.x());
00157         FileHelpers::writeFloat(into, val.y());
00158         FileHelpers::writeFloat(into, val.z());
00159     } else {
00160         BinaryFormatter::writeAttrHeader(into, attr, BF_POSITION_2D);
00161         FileHelpers::writeFloat(into, val.x());
00162         FileHelpers::writeFloat(into, val.y());
00163     }
00164 }
00165 
00166 
00167 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
00168     BinaryFormatter::writeAttrHeader(into, attr, BF_LIST);
00169     FileHelpers::writeInt(into, val.size());
00170     for (PositionVector::ContType::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
00171         if (pos->z() != 0.) {
00172             FileHelpers::writeByte(into, BF_POSITION_3D);
00173             FileHelpers::writeFloat(into, pos->x());
00174             FileHelpers::writeFloat(into, pos->y());
00175             FileHelpers::writeFloat(into, pos->z());
00176         } else {
00177             FileHelpers::writeByte(into, BF_POSITION_2D);
00178             FileHelpers::writeFloat(into, pos->x());
00179             FileHelpers::writeFloat(into, pos->y());
00180         }
00181     }
00182 }
00183 
00184 
00185 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
00186     BinaryFormatter::writeAttrHeader(into, attr, BF_LIST);
00187     FileHelpers::writeInt(into, 2);
00188     FileHelpers::writeByte(into, BF_POSITION_2D);
00189     FileHelpers::writeFloat(into, val.xmin());
00190     FileHelpers::writeFloat(into, val.ymin());
00191     FileHelpers::writeByte(into, BF_POSITION_2D);
00192     FileHelpers::writeFloat(into, val.xmax());
00193     FileHelpers::writeFloat(into, val.ymax());
00194 }
00195 
00196 /****************************************************************************/
00197 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines