Eclipse SUMO - Simulation of Urban MObility
OutputDevice_File.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // An output device that encapsulates an ofstream
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <iostream>
25 #include <cstring>
26 #include <cerrno>
27 #ifdef HAVE_ZLIB
28 #include <foreign/zstr/zstr.hpp>
29 #endif
31 #include "OutputDevice_File.h"
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
37 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool compressed)
38  : OutputDevice(0, fullName), myFileStream(nullptr) {
39 #ifdef WIN32
40  if (fullName == "/dev/null") {
41  myFileStream = new std::ofstream("NUL");
42  if (!myFileStream->good()) {
43  delete myFileStream;
44  throw IOError("Could not redirect to NUL device (" + std::string(std::strerror(errno)) + ").");
45  }
46  return;
47  }
48 #endif
49 #ifdef HAVE_ZLIB
50  if (compressed) {
51  try {
52  myFileStream = new zstr::ofstream(fullName.c_str(), std::ios_base::out);
53  } catch (zstr::Exception& e) {
54  throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
55  }
56  } else {
57  myFileStream = new std::ofstream(fullName.c_str(), std::ios_base::out);
58  }
59 #else
60  UNUSED_PARAMETER(compressed);
61  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
62 #endif
63  if (!myFileStream->good()) {
64  delete myFileStream;
65  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
66  }
67 }
68 
69 
71  delete myFileStream;
72 }
73 
74 
75 std::ostream&
77  return *myFileStream;
78 }
79 
80 
81 /****************************************************************************/
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
std::ostream * myFileStream
The wrapped ofstream.
OutputDevice_File(const std::string &fullName, const bool compressed=false)
Constructor.
std::ostream & getOStream()
Returns the associated ostream.
~OutputDevice_File()
Destructor.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
Exception class thrown by failed zlib operations.
Definition: zstr.hpp:24
const char * what() const NOEXCEPT
Definition: zstr.hpp:55
static std::string strerror()