Newer
Older
Oliver Sander
committed
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#if HAVE_CONFIG_H
#include "config.h"
#endif
Oliver Sander
committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "parametertreeparser.hh"
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include <fstream>
#include <set>
#include <dune/common/exceptions.hh>
std::string Dune::ParameterTreeParser::ltrim(const std::string& s)
{
std::size_t firstNonWS = s.find_first_not_of(" \t\n\r");
if (firstNonWS!=std::string::npos)
return s.substr(firstNonWS);
return std::string();
}
std::string Dune::ParameterTreeParser::rtrim(const std::string& s)
{
std::size_t lastNonWS = s.find_last_not_of(" \t\n\r");
if (lastNonWS!=std::string::npos)
return s.substr(0, lastNonWS+1);
return std::string();
}
void Dune::ParameterTreeParser::readINITree(std::string file,
ParameterTree& pt,
bool overwrite)
{
std::ifstream in(file.c_str());
if (!in)
DUNE_THROW(Dune::IOError, "Could not open configuration file " << file);
readINITree(in, pt, "file '" + file + "'", overwrite);
}
void Dune::ParameterTreeParser::readINITree(std::istream& in,
ParameterTree& pt,
bool overwrite)
{
readINITree(in, pt, "stream", overwrite);
}
void Dune::ParameterTreeParser::readINITree(std::istream& in,
ParameterTree& pt,
const std::string srcname,
bool overwrite)
{
std::string prefix;
std::set<std::string> keysInFile;
while(!in.eof())
{
std::string line;
getline(in, line);
line = ltrim(line);
switch (line[0]) {
case '#' :
break;
case '[' :
line = rtrim(line);
if (line[line.length()-1] == ']')
{
prefix = rtrim(ltrim(line.substr(1, line.length()-2)));
if (prefix != "")
prefix += ".";
}
break;
default :
std::string::size_type comment = line.find("#");
line = line.substr(0,comment);
std::string::size_type mid = line.find("=");
if (mid != std::string::npos)
{
std::string key = prefix+rtrim(ltrim(line.substr(0, mid)));
std::string value = ltrim(line.substr(mid+1));
if (value.length()>0)
{
// handle quoted strings
if ((value[0]=='\'')or (value[0]=='"'))
{
char quote = value[0];
value=value.substr(1);
while (*(rtrim(value).rbegin())!=quote)
{
if (not in.eof())
{
std::string l;
getline(in, l);
value = value+"\n"+l;
}
else
value = value+quote;
}
value = rtrim(value);
value = value.substr(0,value.length()-1);
}
else
value = rtrim(value);
}
if (keysInFile.count(key) != 0)
DUNE_THROW(Exception, "Key '" << key <<
"' appears twice in " << srcname << " !");
else
{
if(overwrite || ! pt.hasKey(key))
pt[key] = value;
keysInFile.insert(key);
}
}
break;
}
}
}
void Dune::ParameterTreeParser::readOptions(int argc, char* argv [],
ParameterTree& pt)
{
std::string v = "";
std::string k = "";
for(int i=1; i<argc; i++)
{
std::string s(argv[i]);
if ((argv[i][0]=='-') && (argv[i][1]!='\000'))
{
k = argv[i]+1;
continue;
}
else
{
if (k.size())
pt[k] = argv[i];
k.clear();
}
}
}