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.sourceforge.net/
12 // Copyright (C) 2001-2013 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  if (!OptionsCont::getOptions().isSet(optionName)) {
101  return false;
102  }
103  OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName));
104  if (rootElement != "") {
105  if (rootElement == "routes" || rootElement == "netstate") {
106  dev.writeXMLHeader(rootElement, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.sf.net/xsd/" + rootElement + "_file.xsd\"");
107  } else {
108  dev.writeXMLHeader(rootElement);
109  }
110  }
111  return true;
112 }
113 
114 
116 OutputDevice::getDeviceByOption(const std::string& optionName) throw(IOError, InvalidArgument) {
117  std::string devName = OptionsCont::getOptions().getString(optionName);
118  if (myOutputDevices.find(devName) == myOutputDevices.end()) {
119  throw InvalidArgument("Device '" + devName + "' has not been created.");
120  }
121  return OutputDevice::getDevice(devName);
122 }
123 
124 
125 void
127  while (myOutputDevices.size() != 0) {
128  myOutputDevices.begin()->second->close();
129  }
130  myOutputDevices.clear();
131 }
132 
133 
134 std::string
135 OutputDevice::realString(const SUMOReal v, const int precision) {
136  std::ostringstream oss;
137  if (v == 0) {
138  return "0";
139  }
140  if (v < pow(10., -precision)) {
141  oss.setf(std::ios::scientific, std::ios::floatfield);
142  } else {
143  oss.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
144  oss.setf(std::ios::showpoint); // print decimal point
145  oss << std::setprecision(precision);
146  }
147  oss << v;
148  return oss.str();
149 }
150 
151 
152 // ===========================================================================
153 // member method definitions
154 // ===========================================================================
155 OutputDevice::OutputDevice(const bool binary, const unsigned int defaultIndentation)
156  : myAmBinary(binary) {
157  if (binary) {
159  } else {
160  myFormatter = new PlainXMLFormatter(defaultIndentation);
161  }
162 }
163 
164 
166  delete myFormatter;
167 }
168 
169 
170 bool
172  return getOStream().good();
173 }
174 
175 
176 void
178  while (closeTag()) {}
179  for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
180  if (i->second == this) {
181  myOutputDevices.erase(i);
182  break;
183  }
184  }
185  delete this;
186 }
187 
188 
189 void
190 OutputDevice::setPrecision(unsigned int precision) {
191  getOStream() << std::setprecision(precision);
192 }
193 
194 
195 bool
196 OutputDevice::writeXMLHeader(const std::string& rootElement,
197  const std::string& attrs, const std::string& comment) {
198  return myFormatter->writeXMLHeader(getOStream(), rootElement, attrs, comment);
199 }
200 
201 
203 OutputDevice::openTag(const std::string& xmlElement) {
204  myFormatter->openTag(getOStream(), xmlElement);
205  return *this;
206 }
207 
208 
210 OutputDevice::openTag(const SumoXMLTag& xmlElement) {
211  myFormatter->openTag(getOStream(), xmlElement);
212  return *this;
213 }
214 
215 
216 bool
218  if (myFormatter->closeTag(getOStream())) {
219  postWriteHook();
220  return true;
221  }
222  return false;
223 }
224 
225 
226 void
228 
229 
230 void
231 OutputDevice::inform(const std::string& msg, const char progress) {
232  if (progress != 0) {
233  getOStream() << msg << progress;
234  } else {
235  getOStream() << msg << '\n';
236  }
237  postWriteHook();
238 }
239 
240 
242 OutputDevice::writeAttr(std::string attr, std::string val) {
243  myFormatter->writeAttr(getOStream(), attr, val);
244  return *this;
245 }
246 
247 /****************************************************************************/
248