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-2016 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 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
48  : OutputDevice(binary), myFileStream(0) {
49 #ifdef WIN32
50  if (fullName == "/dev/null") {
51  myFileStream = new std::ofstream("NUL");
52  } else
53 #endif
54  myFileStream = new std::ofstream(fullName.c_str(), binary ? std::ios::binary : std::ios_base::out);
55  if (!myFileStream->good()) {
56  delete myFileStream;
57  throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
58  }
59 }
60 
61 
63  myFileStream->close();
64  delete myFileStream;
65 }
66 
67 
68 std::ostream&
70  return *myFileStream;
71 }
72 
73 
74 /****************************************************************************/
75 
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