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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // An output device that encapsulates an ofstream
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <iostream>
26 #include <cstring>
27 #include <cerrno>
28 #ifdef HAVE_ZLIB
29 #include <foreign/zstr/zstr.hpp>
30 #endif
32 #include "OutputDevice_File.h"
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary, const bool compressed)
39  : OutputDevice(binary, 0, fullName), myFileStream(nullptr) {
40 #ifdef WIN32
41  if (fullName == "/dev/null") {
42  myFileStream = new std::ofstream("NUL");
43  if (!myFileStream->good()) {
44  delete myFileStream;
45  throw IOError("Could not redirect to NUL device (" + std::string(std::strerror(errno)) + ").");
46  }
47  return;
48  }
49 #endif
50 #ifdef HAVE_ZLIB
51  if (compressed) {
52  try {
53  myFileStream = new zstr::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
54  } catch (zstr::Exception& e) {
55  throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
56  }
57  } else {
58  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
59  }
60 #else
61  UNUSED_PARAMETER(compressed);
62  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
63 #endif
64  if (!myFileStream->good()) {
65  delete myFileStream;
66  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
67  }
68 }
69 
70 
72  delete myFileStream;
73 }
74 
75 
76 std::ostream&
78  return *myFileStream;
79 }
80 
81 
82 /****************************************************************************/
83 
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
strict_fstream::strerror
static std::string strerror()
Definition: strict_fstream.hpp:29
zstr::Exception::what
const char * what() const NOEXCEPT
Definition: zstr.hpp:55
zstr::Exception
Exception class thrown by failed zlib operations.
Definition: zstr.hpp:24
OutputDevice_File::myFileStream
std::ostream * myFileStream
The wrapped ofstream.
Definition: OutputDevice_File.h:66
OutputDevice_File::getOStream
std::ostream & getOStream()
Returns the associated ostream.
Definition: OutputDevice_File.cpp:77
UtilExceptions.h
OutputDevice_File::~OutputDevice_File
~OutputDevice_File()
Destructor.
Definition: OutputDevice_File.cpp:71
OutputDevice_File.h
OutputDevice_File::OutputDevice_File
OutputDevice_File(const std::string &fullName, const bool binary, const bool compressed=false)
Constructor.
Definition: OutputDevice_File.cpp:38
IOError
Definition: UtilExceptions.h:161
zstr::ofstream
Definition: zstr.hpp:392
config.h
zstr.hpp