-
Notifications
You must be signed in to change notification settings - Fork 2
/
Input.cxx
289 lines (243 loc) · 9.75 KB
/
Input.cxx
1
2
3
4
5
6
7
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <iomanip>
#include <boost/algorithm/string.hpp>
#include "Input.h"
#include "Convert.h"
namespace
{
using namespace Convert;
std::string get_item_string(const Input::Itemlist& itemlist,
const std::string& blockname,
const std::string& itemname,
const std::string& subitemname)
{
auto itblock = itemlist.find(blockname);
if (itblock == itemlist.end())
throw std::runtime_error("Block does not exist");
auto ititem = itblock->second.find(itemname);
if (ititem == itblock->second.end())
throw std::runtime_error("Item does not exist");
// If the subitem is empty, it finds the default option "".
auto itsubitem = ititem->second.find(subitemname);
if (itsubitem == ititem->second.end())
{
itsubitem = ititem->second.find(std::string(""));
if (itsubitem == ititem->second.end())
throw std::runtime_error("Subitem does not exist");
}
return itsubitem->second;
}
}
Input::Input(const std::string& file_name)
{
std::string blockname;
// Read file and throw exception on error.
std::ifstream infile;
// infile.exceptions(std::ifstream::failbit);
infile.open(file_name);
if (!infile.good())
throw std::runtime_error("Illegal file name");
std::string line;
while (std::getline(infile, line))
{
// Strip of the comments.
std::vector<std::string> strings;
boost::split(strings, line, boost::is_any_of("#"));
// Keep part that is not comment.
if (strings.size() >= 2)
line = strings[0];
// Strip of all the whitespace.
boost::trim(line);
// Split string on = character.
strings.clear();
boost::split(strings, line, boost::is_any_of("="));
// In case of one element, check whether we have a block header.
if (strings.size() == 1)
{
std::string header = strings[0];
boost::trim(header);
// If only an empty line remains, jump to the next line.
if (header.empty())
continue;
// Store the block name and check for validity of string.
// TODO: use .front() and back() C++11 once Intel supports it.
if (header.at(0) == '[' && header.at(header.size()-1) == ']')
{
blockname = header.substr(1, header.size()-2);
check_item(blockname);
}
else
throw std::runtime_error("Illegal line");
}
// Read item.
else if (strings.size() == 2)
{
if (blockname.empty())
throw std::runtime_error("No block name found");
std::string left = strings[0];
std::string right = strings[1];
boost::trim(left);
boost::trim(right);
// Check if suboptions are defined.
std::string leftsub;
std::size_t openpos = left.find_first_of("[");
std::size_t closepos = left.find_last_of("]");
if (openpos != std::string::npos && closepos != std::string::npos)
{
if (openpos < closepos)
{
// Save the suboption and check string.
leftsub = left.substr(openpos+1, closepos-openpos-1);
check_item(leftsub);
// Strip of suboption.
left = left.substr(0, openpos);
}
}
check_item(left);
// Leave the checking of the right string for later
// when the type is known.
itemlist[blockname][left][leftsub] = right;
}
// Throw an error.
else
throw std::runtime_error("Illegal line");
}
}
void Input::print_itemlist()
{
// Print the list as a test.
for (auto& b : itemlist)
for (auto& i : b.second)
for (auto& is : i.second)
std::cout << b.first << "," << i.first << "," << is.first << "," << is.second << ";" << std::endl;
}
namespace
{
template<typename T>
T convert_value_to_item(const std::string& value)
{
std::istringstream ss(value);
T item = get_item_from_stream<T>(ss);
check_item<T>(item);
return item;
}
}
template<typename T>
T Input::get_item(const std::string& blockname,
const std::string& itemname,
const std::string& subitemname)
{
std::string value = get_item_string(itemlist, blockname, itemname, subitemname);
T item = convert_value_to_item<T>(value);
std::string itemout = "[" + blockname + "][" + itemname + "]";
if (!subitemname.empty())
itemout += "[" + subitemname + "]";
std::cout << std::left << std::setw(30) << itemout << "= "
<< std::right << std::setw(11) << std::setprecision(5) << std::boolalpha << item
<< std::endl;
return item;
}
template<typename T>
T Input::get_item(const std::string& blockname,
const std::string& itemname,
const std::string& subitemname,
const T default_value)
{
T item;
std::string itemqualifier;
try
{
std::string value = get_item_string(itemlist, blockname, itemname, subitemname);
item = convert_value_to_item<T>(value);
}
catch (std::runtime_error& e)
{
item = default_value;
itemqualifier = "(default)";
}
std::string itemout = "[" + blockname + "][" + itemname + "]";
if (!subitemname.empty())
itemout += "[" + subitemname + "]";
std::cout << std::left << std::setw(30) << itemout << "= "
<< std::right << std::setw(11) << std::setprecision(5) << std::boolalpha << item
<< " " << itemqualifier << std::endl;
return item;
}
template<typename T>
std::vector<T> Input::get_list(const std::string& blockname,
const std::string& itemname,
const std::string& subitemname)
{
std::string value = get_item_string(itemlist, blockname, itemname, subitemname);
std::vector<std::string> listitems;
boost::split(listitems, value, boost::is_any_of(","));
std::vector<T> list;
for (std::string itemstring : listitems)
{
std::istringstream ss(itemstring);
T item = get_item_from_stream<T>(ss);
check_item(item);
list.push_back(item);
}
return list;
}
template<typename T>
std::vector<T> Input::get_list(const std::string& blockname,
const std::string& itemname,
const std::string& subitemname,
const std::vector<T> default_value)
{
bool set_default_value = false;
std::string value;
try
{
value = get_item_string(itemlist, blockname, itemname, subitemname);
}
catch (std::runtime_error& e)
{
set_default_value = true;
// itemqualifier = "(default)";
}
if (!set_default_value)
{
std::vector<std::string> listitems;
boost::split(listitems, value, boost::is_any_of(","));
std::vector<T> list;
for (std::string itemstring : listitems)
{
std::istringstream ss(itemstring);
T item = get_item_from_stream<T>(ss);
check_item(item);
list.push_back(item);
}
return list;
}
else
return default_value;
}
// Explicitly instantiate templates.
template bool Input::get_item<bool>(const std::string&, const std::string&, const std::string&);
template int Input::get_item<int>(const std::string&, const std::string&, const std::string&);
template double Input::get_item<double>(const std::string&, const std::string&, const std::string&);
template float Input::get_item<float>(const std::string&, const std::string&, const std::string&);
template std::string Input::get_item<std::string>(const std::string&, const std::string&, const std::string&);
template bool Input::get_item<bool>(const std::string&, const std::string&, const std::string&, const bool);
template int Input::get_item<int>(const std::string&, const std::string&, const std::string&, const int);
template double Input::get_item<double>(const std::string&, const std::string&, const std::string&, const double);
template float Input::get_item<float>(const std::string&, const std::string&, const std::string&, const float);
template std::string Input::get_item<std::string>(const std::string&, const std::string&, const std::string&, const std::string);
template std::vector<int> Input::get_list<int>(const std::string&, const std::string&, const std::string&);
template std::vector<double> Input::get_list<double>(const std::string&, const std::string&, const std::string&);
template std::vector<float> Input::get_list<float>(const std::string&, const std::string&, const std::string&);
template std::vector<std::string> Input::get_list<std::string>(const std::string&, const std::string&, const std::string&);
template std::vector<int> Input::get_list<int>(const std::string&, const std::string&, const std::string&, const std::vector<int>);
template std::vector<double> Input::get_list<double>(const std::string&, const std::string&, const std::string&, const std::vector<double>);
template std::vector<float> Input::get_list<float>(const std::string&, const std::string&, const std::string&, const std::vector<float>);
template std::vector<std::string> Input::get_list<std::string>(const std::string&, const std::string&, const std::string&, const std::vector<std::string>);