Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2004-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file OutputDevice_File.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @date 2004
19 : ///
20 : // An output device that encapsulates an ofstream
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <iostream>
25 : #include <cstring>
26 : #include <cerrno>
27 : #ifdef HAVE_ZLIB
28 : #include <foreign/zstr/zstr.hpp>
29 : #endif
30 : #include <utils/common/StringUtils.h>
31 : #include <utils/common/UtilExceptions.h>
32 : #include "OutputDevice_File.h"
33 :
34 :
35 : // ===========================================================================
36 : // method definitions
37 : // ===========================================================================
38 66041 : OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool compressed)
39 66041 : : OutputDevice(0, fullName) {
40 66041 : if (fullName == "/dev/null") {
41 684 : myAmNull = true;
42 : #ifdef WIN32
43 : myFileStream = new std::ofstream("NUL");
44 : if (!myFileStream->good()) {
45 : delete myFileStream;
46 : throw IOError(TLF("Could not redirect to NUL device (%).", std::string(std::strerror(errno))));
47 : }
48 : return;
49 : #endif
50 : }
51 66041 : const std::string& localName = StringUtils::transcodeToLocal(fullName);
52 : #ifdef HAVE_ZLIB
53 66041 : if (compressed) {
54 : try {
55 35 : myFileStream = new zstr::ofstream(localName.c_str(), std::ios_base::out);
56 1 : } catch (strict_fstream::Exception& e) {
57 2 : throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
58 1 : } catch (zstr::Exception& e) {
59 0 : throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
60 0 : }
61 : } else {
62 66007 : myFileStream = new std::ofstream(localName.c_str(), std::ios_base::out);
63 : }
64 : #else
65 : UNUSED_PARAMETER(compressed);
66 : myFileStream = new std::ofstream(localName.c_str(), std::ios_base::out);
67 : #endif
68 66040 : if (!myFileStream->good()) {
69 89 : delete myFileStream;
70 178 : throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
71 : }
72 66041 : }
73 :
74 :
75 131874 : OutputDevice_File::~OutputDevice_File() {
76 65937 : delete myFileStream;
77 131874 : }
78 :
79 :
80 : std::ostream&
81 151050637 : OutputDevice_File::getOStream() {
82 151050637 : return *myFileStream;
83 : }
84 :
85 :
86 : /****************************************************************************/
|