LCOV - code coverage report
Current view: top level - src/utils/iodevices - OutputDevice_File.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 91.7 % 24 22
Test Date: 2026-06-15 15:46:12 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2004-2026 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/xml/XMLSubSys.h>
      32              : #include <utils/common/UtilExceptions.h>
      33              : #include "OutputDevice_File.h"
      34              : 
      35              : 
      36              : // ===========================================================================
      37              : // method definitions
      38              : // ===========================================================================
      39        87960 : OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool binary)
      40        87960 :     : OutputDevice(0, fullName) {
      41        87960 :     if (fullName == "/dev/null") {
      42         1050 :         myAmNull = true;
      43              : #ifdef WIN32
      44              :         myFileStream = new std::ofstream("NUL");
      45              :         if (!myFileStream->good()) {
      46              :             delete myFileStream;
      47              :             throw IOError(TLF("Could not redirect to NUL device (%).", std::string(std::strerror(errno))));
      48              :         }
      49              :         return;
      50              : #endif
      51              :     }
      52        87960 :     const std::string& localName = XMLSubSys::transcodeToLocal(fullName);
      53              :     std::ios_base::openmode mode = std::ios_base::out;
      54        87960 :     if (binary) {
      55              :         mode |= std::ios_base::binary;
      56              :     }
      57              : #ifdef HAVE_ZLIB
      58       176003 :     if (fullName.length() > 3 && fullName.substr(fullName.length() - 3) == ".gz") {
      59              :         try {
      60          515 :             myFileStream = new zstr::ofstream(localName.c_str(), mode);
      61            1 :         } catch (strict_fstream::Exception& e) {
      62            3 :             throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
      63            1 :         } catch (zstr::Exception& e) {
      64            0 :             throw IOError("Could not build output file '" + fullName + "' (" + e.what() + ").");
      65            0 :         }
      66              :     }
      67              : #else
      68              :     UNUSED_PARAMETER(compressed);
      69              : #endif
      70        87959 :     if (myFileStream == nullptr) {
      71        87446 :         myFileStream = new std::ofstream(localName.c_str(), mode);
      72              :     }
      73        87959 :     if (!myFileStream->good()) {
      74           89 :         delete myFileStream;
      75          267 :         throw IOError("Could not build output file '" + fullName + "' (" + std::strerror(errno) + ").");
      76              :     }
      77        87960 : }
      78              : 
      79              : 
      80       175628 : OutputDevice_File::~OutputDevice_File() {
      81              :     // we need to cleanup the formatter first, because it still might have cached data
      82        87841 :     delete myFormatter;
      83        87841 :     myFormatter = nullptr;
      84        87841 :     delete myFileStream;
      85       175628 : }
      86              : 
      87              : 
      88              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1