1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// *****************************************************************************
/*!
  \file      src/Base/Writer.cpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     Writer base class definition
  \details   Writer base class definition. Writer base serves as a base class
    for various file writers. It does generic low-level I/O, e.g., opening and
    closing a file, and associated error handling.
*/
// *****************************************************************************

#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <exception><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdio><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "Writer.hpp"
#include "Exception.hpp"

using tk::Writer;

Writer::Writer( const std::string& filename, std::ios_base::openmode mode ) :
  m_filename( filename ), m_outFile()
// *****************************************************************************
//  Constructor: Acquire file handle
//! \param[in] filename Name of file to open for writing
//! \param[in] mode Open mode, see
//!   http://en.cppreference.com/w/cpp/io/ios_base/openmode
// *****************************************************************************
{
  // Doing this if-test gives the derived class an option to pass an empty
  // string in case the file does not need to be opened, because e.g., there is
  // no data that needs to be written, without contaminating client-code with
  // this if-test.
  if (!m_filename.empty()) {
    m_outFile.open( m_filename, mode );
    ErrChk( m_outFile.good(), "Failed to open file: " + m_filename );
  }
}

Writer::~Writer() noexcept
// *****************************************************************************
//  Destructor: Release file handle
//! \details Exception safety: no-throw guarantee: never throws exceptions.
//!   Error handling, while done by throwing and catching exceptions, results in
//!   warnings to terminal. We use C-style printf, since that will not throw
//!   exceptions.
// *****************************************************************************
{
  if (!m_filename.empty()) {
    try {

      m_outFile.close();

      if ( m_outFile.fail() )
        printf( ">>> WARNING: Failed to close file: %s\n", m_filename.c_str() );

    } // emit only a warning on error
      catch ( Exception& e ) {
        e.handleException();
      }
      catch ( std::exception& e ) {
        printf( ">>> WARNING: std::exception in Writer destructor: %s\n",
                e.what() );
      }
      catch (...) {
        printf( ">>> WARNING: UNKNOWN EXCEPTION in Writer destructor\n" );
      }
  }
}