This source file includes following definitions.
- err_open_file
- err_file_to_small
- file_to_dof
- file_to_dof
- dof_to_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #ifndef IO_DOF_H
18 #define IO_DOF_H
19
20 #include <fstream>
21 #include "esfem_fwd.h"
22
23 namespace Esfem{
24 namespace Io{
25 template<typename It>
26 void file_to_dof(It first, It last,
27 const std::string& filename);
28
29 template<typename It>
30 void file_to_dof(It first, const std::string& filename, std::size_t dof_no);
31 template<typename It>
32 void dof_to_file(It first, It last,
33 const std::string& filename);
34
35
36
37
38
39
40
41 inline void err_open_file(const std::string& fname){
42 throw std::logic_error {"Could not open file " + fname};
43 }
44 inline void err_file_to_small(const std::string& fname){
45 throw std::runtime_error {"File \"" + fname + "\" not big enough."};
46 }
47
48 template<typename It>
49 void file_to_dof(It first, It last,
50 const std::string& filename) try{
51 std::ifstream ifs {filename};
52 if(!ifs) err_open_file(filename);
53 while(first != last){
54 double value {0.};
55 if(ifs >> value) *first = value;
56 else err_file_to_small(filename);
57 ++first;
58 }
59 }
60 catch(const std::exception&){
61 std::throw_with_nested(std::runtime_error
62 {"Error in file_to_dof()."});
63 }
64 template<typename It>
65 void file_to_dof(It first, const std::string& filename,
66 std::size_t dof_no) try{
67 std::ifstream ifs {filename};
68 if(!ifs) err_open_file(filename);
69 for( ; dof_no > 0; --dof_no){
70 double value {0.};
71 if(ifs >> value) *first = value;
72 else err_file_to_small(filename);
73 ++first;
74 }
75 if(!(ifs >> std::ws).eof()) throw std::runtime_error
76 {"Stuff left in file \"" + filename + "\"."};
77 }
78 catch(const std::exception&){
79 std::throw_with_nested(std::runtime_error
80 {"Error in file_to_dof()."});
81 }
82 template<typename It>
83 void dof_to_file(It first, It last,
84 const std::string& filename) try{
85 std::ofstream ofs {filename};
86 if(!ofs) err_open_file(filename);
87 while(first != last){
88 if( !(ofs << *first << std::endl) )
89 err_file_to_small(filename);
90 ++first;
91 }
92 }
93 catch(const std::exception&){
94 std::throw_with_nested(std::runtime_error
95 {"Error in dof_to_file()."});
96 }
97 }
98 }
99
100
101 #endif
102
103
104