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-FileCopyrightText: 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 |
1/2✓ Branch 1 taken 126 times.
✗ Branch 2 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 |
1/2✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
|
152 | 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 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
378 | Container readStreamToContainer(std::istream& stream) |
56 | { | ||
57 |
1/2✓ Branch 1 taken 241 times.
✗ Branch 2 not taken.
|
378 | Container v; |
58 |
1/2✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
|
378 | std::istream_iterator<typename Container::value_type> it(stream); |
59 |
2/5✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
386 | std::copy(it, std::istream_iterator<typename Container::value_type>(), std::back_inserter(v)); |
60 | 378 | return v; | |
61 | 366 | } | |
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 | 66 | Container readFileToContainer(const std::string& filename) | |
73 | { | ||
74 | 66 | std::ifstream infile(filename, std::ios::in); | |
75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
66 | if (!infile) |
76 | ✗ | DUNE_THROW(Dune::IOError, "Could not open file: " << filename); | |
77 |
1/2✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
|
132 | return readStreamToContainer<Container>(infile); |
78 | 66 | } | |
79 | |||
80 | } // end namespace Dumux | ||
81 | |||
82 | #endif | ||
83 |