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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // An output device that encapsulates an ofstream
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <iostream>
33 #include <cstring>
34 #include <cerrno>
36 #include "OutputDevice_File.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
43  : OutputDevice(binary), myFileStream(0) {
44 #ifdef WIN32
45  if (fullName == "/dev/null") {
46  myFileStream = new std::ofstream("NUL");
47  } else
48 #endif
49  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
50  if (!myFileStream->good()) {
51  delete myFileStream;
52  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
53  }
54 }
55 
56 
58  myFileStream->close();
59  delete myFileStream;
60 }
61 
62 
63 std::ostream&
65  return *myFileStream;
66 }
67 
68 
69 /****************************************************************************/
70 
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:70