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 /****************************************************************************/
17 // An output device that encapsulates an ofstream
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
27 #include <cstring>
28 #include <cerrno>
29 #ifdef HAVE_ZLIB
30 #include <foreign/zstr/zstr.hpp>
31 #endif
33 #include "OutputDevice_File.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary, const bool compressed)
40  : OutputDevice(binary, 0, fullName), myFileStream(nullptr) {
41 #ifdef WIN32
42  if (fullName == "/dev/null") {
43  myFileStream = new std::ofstream("NUL");
44  if (!myFileStream->good()) {
45  delete myFileStream;
46  throw IOError("Could not redirect to NUL device (" + std::string(std::strerror(errno)) + ").");
47  }
48  return;
49  }
50 #endif
51 #ifdef HAVE_ZLIB
52  if (compressed) {
53  try {
54  myFileStream = new zstr::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
55  } catch (zstr::Exception& e) {
56  throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
57  }
58  } else {
59  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
60  }
61 #else
62  UNUSED_PARAMETER(compressed);
63  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
64 #endif
65  if (!myFileStream->good()) {
66  delete myFileStream;
67  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
68  }
69 }
70 
71 
73  delete myFileStream;
74 }
75 
76 
77 std::ostream&
79  return *myFileStream;
80 }
81 
82 
83 /****************************************************************************/
84 
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
strict_fstream::strerror
static std::string strerror()
Definition: strict_fstream.hpp:28
zstr::Exception::what
const char * what() const NOEXCEPT
Definition: zstr.hpp:55
zstr::Exception
Exception class thrown by failed zlib operations.
Definition: zstr.hpp:22
OutputDevice_File::myFileStream
std::ostream * myFileStream
The wrapped ofstream.
Definition: OutputDevice_File.h:67
OutputDevice_File::getOStream
std::ostream & getOStream()
Returns the associated ostream.
Definition: OutputDevice_File.cpp:78
UtilExceptions.h
OutputDevice_File::~OutputDevice_File
~OutputDevice_File()
Destructor.
Definition: OutputDevice_File.cpp:72
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:39
IOError
Definition: UtilExceptions.h:147
zstr::ofstream
Definition: zstr.hpp:389
config.h
zstr.hpp