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-2018 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>
30 #include "OutputDevice_File.h"
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
36 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
37  : OutputDevice(binary, 0, fullName), myFileStream(nullptr) {
38 #ifdef WIN32
39  if (fullName == "/dev/null") {
40  myFileStream = new std::ofstream("NUL");
41  } else
42 #endif
43  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
44  if (!myFileStream->good()) {
45  delete myFileStream;
46  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
47  }
48 }
49 
50 
52  myFileStream->close();
53  delete myFileStream;
54 }
55 
56 
57 std::ostream&
59  return *myFileStream;
60 }
61 
62 
63 /****************************************************************************/
64 
std::ostream & getOStream()
Returns the associated ostream.
std::ofstream * myFileStream
The wrapped ofstream.
~OutputDevice_File()
Destructor.
OutputDevice_File(const std::string &fullName, const bool binary)
Constructor.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64