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-2012 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  const std::string& base) {
67  std::string internalName = name;
68  if (name == "-") {
69  internalName = "stdout";
70  }
71  // check whether the device has already been aqcuired
72  if (myOutputDevices.find(internalName) != myOutputDevices.end()) {
73  return *myOutputDevices[internalName];
74  }
75  // build the device
76  OutputDevice* dev = 0;
77  // check whether the device shall print to stdout
78  if (internalName == "stdout") {
80  } else if (internalName == "stderr") {
82  } else if (FileHelpers::isSocket(internalName)) {
83  try {
84  int port = TplConvert::_2int(internalName.substr(internalName.find(":") + 1).c_str());
85  dev = new OutputDevice_Network(internalName.substr(0, internalName.find(":")), port);
86  } catch (NumberFormatException&) {
87  throw IOError("Given port number '" + internalName.substr(internalName.find(":") + 1) + "' is not numeric.");
88  } catch (EmptyData&) {
89  throw IOError("No port number given.");
90  }
91  } else {
92  const size_t len = internalName.length();
93  dev = new OutputDevice_File(FileHelpers::checkForRelativity(internalName, base),
94  len > 4 && internalName.substr(len - 4) == ".sbx");
95  }
96  dev->setPrecision();
97  dev->getOStream() << std::setiosflags(std::ios::fixed);
98  myOutputDevices[internalName] = dev;
99  return *dev;
100 }
101 
102 
103 bool
104 OutputDevice::createDeviceByOption(const std::string& optionName,
105  const std::string& rootElement) {
106  if (!OptionsCont::getOptions().isSet(optionName)) {
107  return false;
108  }
109  OutputDevice& dev = OutputDevice::getDevice(OptionsCont::getOptions().getString(optionName));
110  if (rootElement != "") {
111  dev.writeXMLHeader(rootElement);
112  }
113  return true;
114 }
115 
116 
118 OutputDevice::getDeviceByOption(const std::string& optionName) throw(IOError, InvalidArgument) {
119  std::string devName = OptionsCont::getOptions().getString(optionName);
120  if (myOutputDevices.find(devName) == myOutputDevices.end()) {
121  throw InvalidArgument("Device '" + devName + "' has not been created.");
122  }
123  return OutputDevice::getDevice(devName);
124 }
125 
126 
127 void
129  while (myOutputDevices.size() != 0) {
130  myOutputDevices.begin()->second->close();
131  }
132  myOutputDevices.clear();
133 }
134 
135 
136 std::string
137 OutputDevice::realString(const SUMOReal v, const int precision) {
138  std::ostringstream oss;
139  if (v == 0) {
140  return "0";
141  }
142  if (v < pow(10., -precision)) {
143  oss.setf(std::ios::scientific, std::ios::floatfield);
144  } else {
145  oss.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
146  oss.setf(std::ios::showpoint); // print decimal point
147  oss << std::setprecision(precision);
148  }
149  oss << v;
150  return oss.str();
151 }
152 
153 
154 // ===========================================================================
155 // member method definitions
156 // ===========================================================================
157 OutputDevice::OutputDevice(const bool binary, const unsigned int defaultIndentation)
158  : myAmBinary(binary) {
159  if (binary) {
161  } else {
162  myFormatter = new PlainXMLFormatter(defaultIndentation);
163  }
164 }
165 
166 
168  delete myFormatter;
169 }
170 
171 
172 bool
174  return getOStream().good();
175 }
176 
177 
178 void
180  while (closeTag()) {}
181  for (std::map<std::string, OutputDevice*>::iterator i = myOutputDevices.begin(); i != myOutputDevices.end(); ++i) {
182  if (i->second == this) {
183  myOutputDevices.erase(i);
184  break;
185  }
186  }
187  delete this;
188 }
189 
190 
191 void
192 OutputDevice::setPrecision(unsigned int precision) {
193  getOStream() << std::setprecision(precision);
194 }
195 
196 
197 bool
198 OutputDevice::writeXMLHeader(const std::string& rootElement, const std::string xmlParams,
199  const std::string& attrs, const std::string& comment) {
200  return myFormatter->writeXMLHeader(getOStream(), rootElement, xmlParams, attrs, comment);
201 }
202 
203 
205 OutputDevice::openTag(const std::string& xmlElement) {
206  myFormatter->openTag(getOStream(), xmlElement);
207  return *this;
208 }
209 
210 
212 OutputDevice::openTag(const SumoXMLTag& xmlElement) {
213  myFormatter->openTag(getOStream(), xmlElement);
214  return *this;
215 }
216 
217 
218 void
221 }
222 
223 
224 bool
225 OutputDevice::closeTag(bool abbreviated) {
226  if (myFormatter->closeTag(getOStream(), abbreviated)) {
227  postWriteHook();
228  return true;
229  }
230  return false;
231 }
232 
233 
234 void
236 
237 
238 void
239 OutputDevice::inform(const std::string& msg, const char progress) {
240  if (progress != 0) {
241  getOStream() << msg << progress;
242  } else {
243  getOStream() << msg << '\n';
244  }
245  postWriteHook();
246 }
247 
248 
250 OutputDevice::writeAttr(std::string attr, std::string val) {
251  myFormatter->writeAttr(getOStream(), attr, val);
252  return *this;
253 }
254 
255 /****************************************************************************/
256