Path: blob/main/src/utils/iodevices/OutputDevice_File.cpp
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2004-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file OutputDevice_File.cpp14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @author Jakob Erdmann17/// @date 200418///19// An output device that encapsulates an ofstream20/****************************************************************************/21#include <config.h>2223#include <iostream>24#include <cstring>25#include <cerrno>26#ifdef HAVE_ZLIB27#include <foreign/zstr/zstr.hpp>28#endif29#include <utils/common/StringUtils.h>30#include <utils/common/UtilExceptions.h>31#include "OutputDevice_File.h"323334// ===========================================================================35// method definitions36// ===========================================================================37OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)38: OutputDevice(0, fullName) {39if (fullName == "/dev/null") {40myAmNull = true;41#ifdef WIN3242myFileStream = new std::ofstream("NUL");43if (!myFileStream->good()) {44delete myFileStream;45throw IOError(TLF("Could not redirect to NUL device (%).", std::string(std::strerror(errno))));46}47return;48#endif49}50const std::string& localName = StringUtils::transcodeToLocal(fullName);51std::ios_base::openmode mode = std::ios_base::out;52if (binary) {53mode |= std::ios_base::binary;54}55#ifdef HAVE_ZLIB56if (fullName.length() > 3 && fullName.substr(fullName.length() - 3) == ".gz") {57try {58myFileStream = new zstr::ofstream(localName.c_str(), mode);59} catch (strict_fstream::Exception& e) {60throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");61} catch (zstr::Exception& e) {62throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");63}64}65#else66UNUSED_PARAMETER(compressed);67#endif68if (myFileStream == nullptr) {69myFileStream = new std::ofstream(localName.c_str(), mode);70}71if (!myFileStream->good()) {72delete myFileStream;73throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");74}75}767778OutputDevice_File::~OutputDevice_File() {79// we need to cleanup the formatter first, because it still might have cached data80delete myFormatter;81myFormatter = nullptr;82delete myFileStream;83}848586/****************************************************************************/878889