GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/io/container.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 15 16 93.8%
Functions: 38 39 97.4%
Branches: 18 60 30.0%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup InputOutput
10 * \brief Free functions to write and read a sequence container to and from a file
11 * \note Reading should work for all sequence containers providing begin, end, and push_back
12 * (e.g. std::vector, std::deque, std::list), so not for e.g. std::array.
13 * Writing only needs begin and end member functions returning iterators.
14 */
15 #ifndef DUMUX_IO_CONTAINER_HH
16 #define DUMUX_IO_CONTAINER_HH
17
18 #include <iostream>
19 #include <ios>
20 #include <iomanip>
21 #include <fstream>
22 #include <iterator>
23
24 #include <dune/common/exceptions.hh>
25
26 namespace Dumux {
27
28 /*!
29 * \ingroup InputOutput
30 * \brief Writes a container to file
31 * \param v The container, requires begin() and end() method
32 * \param filename The filename to write to
33 * \param floatPrecision The total number of digits stored, including decimal
34 *
35 * usage: std::vector<double> v(5, 0.0); writeContainerToFile(v, "myvector.txt");
36 */
37 template<typename Container>
38 152 void writeContainerToFile(const Container& v,
39 const std::string& filename,
40 int floatPrecision = 6)
41 {
42 152 std::ofstream outfile(filename, std::ios::out);
43
2/4
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 126 times.
✗ Branch 6 not taken.
152 outfile << std::scientific << std::setprecision(floatPrecision);
44
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
152 std::ostream_iterator<typename Container::value_type> it(outfile, "\n");
45
3/6
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 126 times.
✗ Branch 8 not taken.
466 std::copy(v.begin(),v.end(), it);
46 152 }
47
48 /*!
49 * \brief Read an input stream into a container
50 * \param stream A standard input stream
51 * \tparam Container The container type requires begin(), end(), push_back() functions
52 * and Container::value_type requires operator>>.
53 */
54 template<typename Container>
55 81 Container readStreamToContainer(std::istream& stream)
56 {
57
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
81 Container v;
58
1/2
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
81 std::istream_iterator<typename Container::value_type> it(stream);
59
7/20
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 41 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 41 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 4 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
316 std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v));
60 81 return v;
61 }
62
63 /*!
64 * \brief Read a simple text file into a container
65 * \param filename The filename to write to
66 * \tparam Container The container type requires begin(), end(), push_back() functions
67 * and Container::value_type requires operator>>.
68 *
69 * usage: auto v = readFileToContainer<std::vector<double>>("myvector.txt");
70 */
71 template<typename Container>
72 39 Container readFileToContainer(const std::string& filename)
73 {
74 78 std::ifstream infile(filename, std::ios::in);
75
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
78 if (!infile)
76 DUNE_THROW(Dune::IOError, "Could not open file: " << filename);
77
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
78 return readStreamToContainer<Container>(infile);
78 }
79
80 } // end namespace Dumux
81
82 #endif
83