SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OutputDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 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 <map>
34 #include <fstream>
35 #include <sstream>
36 #include <string>
37 #include <iomanip>
38 #include "OutputDevice.h"
39 #include "OutputDevice_File.h"
40 #include "OutputDevice_COUT.h"
41 #include "OutputDevice_CERR.h"
42 #include "OutputDevice_Network.h"
43 #include "PlainXMLFormatter.h"
47 #include <utils/common/ToString.h>
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 
55 // ===========================================================================
56 // static member definitions
57 // ===========================================================================
58 std::map<std::string, OutputDevice*> OutputDevice::myOutputDevices;
59 
60 
61 // ===========================================================================
62 // static method definitions
63 // ===========================================================================
65 OutputDevice::getDevice(const std::string& name) {
66  // check whether the device has already been aqcuired
67  if (myOutputDevices.find(name) != myOutputDevices.end()) {
68  return *myOutputDevices[name];
69  }
70  // build the device
71  OutputDevice* dev = 0;
72  // check whether the device shall print to stdout
73  if (name == "stdout") {
75  } else if (name == "stderr") {
77  } else if (FileHelpers::isSocket(name)) {
78  try {
79  int port = TplConvert::_2int(name.substr(name.find(":") + 1).c_str());
80  dev = new OutputDevice_Network(name.substr(0, name.find(":")), port);
81  } catch (NumberFormatException&) {
82  throw IOError("Given port number '" + name.substr(name.find(":") + 1) + "' is not numeric.");
83  } catch (EmptyData&) {
84  throw IOError("No port number given.");
85  }
86  } else {
87  const size_t len = name.length();
88  dev = new OutputDevice_File(name, len > 4 && name.substr(len - 4) == ".sbx");
89  }
90  dev->setPrecision();
91  dev->getOStream() << std::setiosflags(std::ios::fixed);
92  myOutputDevices[name] = dev;
93  return *dev;
94 }
95 
96 
97 bool
98 OutputDevice::createDeviceByOption(const std::string& optionName,
99  const std::string& rootElement,
100  const std::string& schemaFile) {
101  if (!OptionsCont::getOptions().isSet(optionName)) {
102  return false;
103  }
104  OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName));
105  if (rootElement != "") {
106  if (schemaFile != "") {
107  dev.writeXMLHeader(rootElement, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo-sim.org/xsd/" + schemaFile + "\"");
108  } else {
109  dev.writeXMLHeader(rootElement);
110  }
111  }
112  return true;
113 }
114 
115 
117 OutputDevice::getDeviceByOption(const std::string& optionName) {
118  std::string devName = OptionsCont::getOptions().getString(optionName);
119  if (myOutputDevices.find(devName) == myOutputDevices.end()) {
120  throw InvalidArgument("Device '" + devName + "' has not been created.");
121  }
122  return OutputDevice::getDevice(devName);
123 }
124 
125 
126 void
128  while (myOutputDevices.size() != 0) {
129  myOutputDevices.begin()->second->close();
130  }
131  myOutputDevices.clear();
132 }
133 
134 
135 std::string
136 OutputDevice::realString(const SUMOReal v, const int precision) {
137  std::ostringstream oss;
138  if (v == 0) {
139  return "0";
140  }
141  if (v < pow(10., -precision)) {
142  oss.setf(std::ios::scientific, std::ios::floatfield);
143  } else {
144  oss.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
145  oss.setf(std::ios::showpoint); // print decimal point
146  oss << std::setprecision(precision);
147  }
148  oss << v;
149  return oss.str();
150 }
151 
152 
153 // ===========================================================================
154 // member method definitions
155 // ===========================================================================
156 OutputDevice::OutputDevice(const bool binary, const unsigned int defaultIndentation)
157  : myAmBinary(binary) {
158  if (binary) {
160  } else {
161  myFormatter = new PlainXMLFormatter(defaultIndentation);
162  }
163 }
164 
165 
167  delete myFormatter;
168 }
169 
170 
171 bool
173  return getOStream().good();
174 }
175 
176 
177 void
179  while (closeTag()) {}
180  for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
181  if (i->second == this) {
182  myOutputDevices.erase(i);
183  break;
184  }
185  }
186  delete this;
187 }
188 
189 
190 void
191 OutputDevice::setPrecision(unsigned int precision) {
192  getOStream() << std::setprecision(precision);
193 }
194 
195 
196 bool
197 OutputDevice::writeXMLHeader(const std::string& rootElement,
198  const std::string& attrs, const std::string& comment) {
199  return myFormatter->writeXMLHeader(getOStream(), rootElement, attrs, comment);
200 }
201 
202 
204 OutputDevice::openTag(const std::string& xmlElement) {
205  myFormatter->openTag(getOStream(), xmlElement);
206  return *this;
207 }
208 
209 
211 OutputDevice::openTag(const SumoXMLTag& xmlElement) {
212  myFormatter->openTag(getOStream(), xmlElement);
213  return *this;
214 }
215 
216 
217 bool
219  if (myFormatter->closeTag(getOStream())) {
220  postWriteHook();
221  return true;
222  }
223  return false;
224 }
225 
226 
227 void
229 
230 
231 void
232 OutputDevice::inform(const std::string& msg, const char progress) {
233  if (progress != 0) {
234  getOStream() << msg << progress;
235  } else {
236  getOStream() << msg << '\n';
237  }
238  postWriteHook();
239 }
240 
241 
242 /****************************************************************************/
243 
void close()
Closes the device and removes it from the dictionary.
SumoXMLTag
Numbers representing SUMO-XML - element names.
virtual bool closeTag(std::ostream &into)=0
Closes the most recently opened tag.
static std::map< std::string, OutputDevice * > myOutputDevices
map from names to output devices
Definition: OutputDevice.h:330
static bool isSocket(const std::string &name)
Returns the information whether the given name represents a socket.
Definition: FileHelpers.cpp:93
OutputDevice(const bool binary=false, const unsigned int defaultIndentation=0)
Constructor.
An output device for TCP/IP network connections.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
void inform(const std::string &msg, const char progress=0)
Retrieves a message to this device.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void setPrecision(unsigned int precision=OUTPUT_ACCURACY)
Sets the precison or resets it to default.
Output formatter for plain XML output.
static void closeAll()
static OutputDevice * getDevice()
Returns the single cout instance.
An output device that encapsulates an ofstream.
virtual void openTag(std::ostream &into, const std::string &xmlElement)=0
Opens an XML tag.
virtual bool ok()
returns the information whether one can write into the device
Output formatter for plain XML output.
static int _2int(const E *const data)
Definition: TplConvert.h:114
virtual ~OutputDevice()
Destructor.
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static std::string realString(const SUMOReal v, const int precision=OUTPUT_ACCURACY)
Helper method for string formatting.
OutputFormatter * myFormatter
The formatter for XML.
Definition: OutputDevice.h:335
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
virtual bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::string &attrs="", const std::string &comment="")=0
Writes an XML header with optional configuration.
static OutputDevice * getDevice()
Returns the single cerr instance.
virtual std::ostream & getOStream()=0
Returns the associated ostream.
virtual void postWriteHook()
Called after every write access.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.