dune-fem 2.8.0
Loading...
Searching...
No Matches
parser.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_IO_PARAMETER_PARSER_HH
2#define DUNE_FEM_IO_PARAMETER_PARSER_HH
3
4#include <sstream>
5#include <string>
6#include <type_traits>
7
8#include <dune/common/fmatrix.hh>
9
10namespace Dune
11{
12
13 namespace Fem
14 {
15
16 // ParameterParser
17 // ---------------
18
19 template< class T >
21 {
22 static bool parse ( const std::string &s, T &value )
23 {
24 std::istringstream in( s );
25 in >> value;
26 if( std::is_same< T, std::string >::value && s.empty() )
27 return true;
28 if( in.fail() )
29 return false;
30 char eof;
31 in >> eof;
32 return in.eof();
33 }
34
35 static std::string toString ( const T &value )
36 {
37 std::ostringstream out;
38 out << value;
39 return out.str();
40 }
41 };
42
43 template<>
44 struct ParameterParser< bool >
45 {
46 static bool parse ( const std::string &s, bool &value )
47 {
48 std::string w;
50 {
51 std::transform(w.begin(), w.end(), w.begin(), ::tolower);
52 if( (w == std::string( "false" )) || (w == std::string( "no" )) || (w == std::string( "0" )) )
53 {
54 value = false;
55 return true;
56 }
57
58 if( (w == std::string( "true" )) || (w == std::string( "yes" )) || (w == std::string( "1" )) )
59 {
60 value = true;
61 return true;
62 }
63 }
64 return false;
65 }
66
67 static std::string toString ( const bool &value )
68 {
69 return std::string( value ? "true" : "false" );
70 }
71 };
72
73 template< class F, int m, int n >
74 struct ParameterParser< FieldMatrix< F, m, n > >
75 {
76 static bool parse ( const std::string &s, FieldMatrix< F, m, n > &value )
77 {
78 std::istringstream in( s );
79 char c;
80 for( int i = 0; i < m; ++i )
81 {
82 if( i > 0 )
83 {
84 in >> c;
85 if( c != ',' )
86 return false;
87 }
88
89 for( int j = 0; j < n; ++j )
90 in >> value[ i ][ j ];
91 }
92 in >> c; // read eof
93 return in.eof();
94 }
95
96 static std::string toString ( const FieldMatrix< F, m, n > &value )
97 {
98 std::ostringstream out;
99 for( int i = 0; i < m; ++i )
100 {
101 out << (i > 0 ? "," : "");
102 for( int j = 0; j< n; ++j )
103 out << " " << value[ i ][ j ];
104 }
105 return out.str();
106 }
107 };
108
109 } // namespace Fem
110
111} // namespace Dune
112
113#endif // #ifndef DUNE_FEM_IO_PARAMETER_PARSER_HH
Definition: bindguard.hh:11
Definition: parser.hh:21
static bool parse(const std::string &s, T &value)
Definition: parser.hh:22
static std::string toString(const T &value)
Definition: parser.hh:35
static bool parse(const std::string &s, bool &value)
Definition: parser.hh:46
static std::string toString(const bool &value)
Definition: parser.hh:67
static std::string toString(const FieldMatrix< F, m, n > &value)
Definition: parser.hh:96
static bool parse(const std::string &s, FieldMatrix< F, m, n > &value)
Definition: parser.hh:76