DUNE-FEM (unstable)

asciiparser.hh
1#ifndef DUNE_FEM_ASCIIPARSER_HH
2#define DUNE_FEM_ASCIIPARSER_HH
3
4#include <iostream>
5#include <fstream>
6#include <string>
7
8namespace Dune
9{
10
11 namespace Fem
12 {
13
17 template< class T >
18 static inline bool readParameter( std::istream& file,
19 const std::string keyword,
20 T & data,
21 bool verbose = true,
22 bool warn = true )
23 {
24 bool readData = false;
25 while (! file.eof() )
26 {
27 std::string keyHelp;
28 file >> keyHelp;
29
30 // % or # means comment
31 if((keyHelp[0] == '%') || (keyHelp[0] == '#'))
32 {
33 std::string tmp;
34 std::getline(file,tmp);
35 }
36 else
37 {
38 // copy only keyword size and compare
39 int pos = keyHelp.find_first_of(':');
40 int pos1 = keyHelp.find_first_of(' ');
41
42 if (pos > 0)
43 {
44 if(pos1 > 0)
45 pos -= pos1;
46 else
47 pos = 1;
48 }
49 else
50 pos = 0;
51
52 std::string key (keyHelp,0,keyHelp.size() - pos);
53 if(key == keyword)
54 {
55 file >> data;
56 readData = true;
57 break;
58 }
59 }
60 }
61
62 // if data was read sucessfully
63 if(readData)
64 {
65 if(verbose)
66 {
67 static const int MAXTAB = 30;
68 int length = MAXTAB - keyword.size();
69 std::cout << "Reading " << keyword;
70 for(int i=0; i<length; i++) std::cout << ".";
71 std::cout << " " << data << std::endl;
72 }
73 }
74 else
75 {
76 if( warn )
77 std::cerr << "WARNING: couldn't read " << keyword << std::endl;
78 }
79
80 return readData;
81 }
82
86 template< class T >
87 static inline bool readParameter( const std::string filename,
88 const std::string keyword,
89 T & data,
90 bool verbose = true,
91 bool warn = true )
92 {
93 std::ifstream file (filename.c_str());
94 if( !file.is_open() )
95 {
96 if( warn )
97 std::cerr << "WARNING: couldn't open file '" << filename << "' in " << __FILE__<< " line: " << __LINE__ << std::endl;
98 return false;
99 }
100 const bool result = readParameter( file, keyword, data, verbose, warn );
101
102 // close file
103 file.close();
104
105 return result;
106 }
107
108 } // namespace Fem
109
110} // namespace Dune
111#endif // #ifndef DUNE_FEM_ASCIIPARSER_HH
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)