Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
OutputDevice_File.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2004-2025 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/****************************************************************************/
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
32#include "OutputDevice_File.h"
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
38OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
39 : OutputDevice(0, fullName) {
40 if (fullName == "/dev/null") {
41 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 const std::string& localName = StringUtils::transcodeToLocal(fullName);
52 std::ios_base::openmode mode = std::ios_base::out;
53 if (binary) {
54 mode |= std::ios_base::binary;
55 }
56#ifdef HAVE_ZLIB
57 if (fullName.length() > 3 && fullName.substr(fullName.length() - 3) == ".gz") {
58 try {
59 myFileStream = new zstr::ofstream(localName.c_str(), mode);
60 } catch (strict_fstream::Exception& e) {
61 throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
62 } catch (zstr::Exception& e) {
63 throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
64 }
65 }
66#else
67 UNUSED_PARAMETER(compressed);
68#endif
69 if (myFileStream == nullptr) {
70 myFileStream = new std::ofstream(localName.c_str(), mode);
71 }
72 if (!myFileStream->good()) {
73 delete myFileStream;
74 throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
75 }
76}
77
78
80 // we need to cleanup the formatter first, because it still might have cached data
81 delete myFormatter;
82 myFormatter = nullptr;
83 delete myFileStream;
84}
85
86
87/****************************************************************************/
#define TLF(string,...)
Definition MsgHandler.h:307
bool myAmNull
am I redirecting to /dev/null
std::ostream * myFileStream
The wrapped ofstream.
OutputDevice_File(const std::string &fullName, const bool binary=false)
Constructor.
~OutputDevice_File()
Destructor.
Static storage of an output device and its base (abstract) implementation.
OutputFormatter * myFormatter
The formatter for XML, CSV or Parquet.
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage
Exception class thrown by failed operations.
const char * what() const noexcept
Exception class thrown by failed zlib operations.
Definition zstr.hpp:36
#define UNUSED_PARAMETER(x)