SUMO - Simulation of Urban MObility
OutputDevice_File.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An output device that encapsulates an ofstream
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2004-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <cstring>
35 #include <cerrno>
37 #include "OutputDevice_File.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
44  : OutputDevice(binary), myFileStream(0) {
45 #ifdef WIN32
46  if (fullName == "/dev/null") {
47  myFileStream = new std::ofstream("NUL");
48  } else
49 #endif
50  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
51  if (!myFileStream->good()) {
52  delete myFileStream;
53  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
54  }
55 }
56 
57 
59  myFileStream->close();
60  delete myFileStream;
61 }
62 
63 
64 std::ostream&
66  return *myFileStream;
67 }
68 
69 
70 /****************************************************************************/
71 
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:71