-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNamelistReader.hpp
More file actions
406 lines (350 loc) · 12.1 KB
/
Copy pathNamelistReader.hpp
File metadata and controls
406 lines (350 loc) · 12.1 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#ifndef NAMELISTREADER_HPP
#define NAMELISTREADER_HPP
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
namespace NLReader{
using namespace std;
enum Expect {
Ampersand=0,
NLName,
ParaNameOrEnd,
Equals,
Value,
ValueOrEOL
};
enum ReadFrom {
ReadFromFile=0,
ReadFromString
};
enum Create {
CreateManually=0
};
// trim from left
inline string& ltrim(string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
return s;
}
// trim from right
inline string& rtrim(string& s, const char* t = " \t\n\r\f\v")
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
// trim from left & right
inline string& trim(string& s, const char* t = " \t\n\r\f\v")
{
return ltrim(rtrim(s, t), t);
}
struct Param{
string name;
bool used;
vector<string> values;
Param(string name)
: name(name), used(false)
{}
};
struct NameList{
string name;
vector<Param> params;
NameList(string name)
: name(name)
{}
void add_param(string word)
{
params.push_back(Param(word));
}
void add_value(string word)
{
params[params.size()-1].values.push_back(word);
}
};
static inline string get_first_word(string& str, int start){
for (string::size_type i = start; i < str.size(); i++){
if ((str[i]>=48 && str[i]<=57) || (str[i]>=65 && str[i]<=90) ||
(str[i]>=97 && str[i]<=122) || str[i]==95){
// still in word (0-9, A-Z, a-z, _)
} else {
// Not in word
return str.substr(start, i-start);
}
}
return str.substr(start,str.size()-start);
}
static inline string get_first_value(string& str, int start){
bool in_squote=false;
bool in_dquote=false;
for (string::size_type i = start; i < str.size(); i++){
if (in_dquote==true || in_squote==true){
if (str[i]==34) in_dquote=false;
if (str[i]==39) in_squote=false;
} else {
if (str[i]==34) in_dquote=true;
if (str[i]==39) in_squote=true;
if (str[i]=='!' || str[i]==' ' || str[i]=='\t'|| str[i]=='\r'|| str[i]=='\f'|| str[i]=='\v'){
// Not in word
return str.substr(start, i-start);
}
}
}
return str.substr(start,str.size()-start);
}
template<typename T>
static inline T string_to_param(string& param);
template<>
bool string_to_param<bool>(string& param){
// Make lower case
for (int i = 0; i<param.size(); i++)
param[i]=tolower(param[i]);
if (param==".false."){
return false;
} else if (param==".true."){
return true;
} else {
printf("Error, couldn't parse %s \n", param.c_str());
return false;
}
}
static inline void change_D_to_E(string& param){
for (int i = 0; i<param.size(); i++)
if (param[i]=='D' || param[i]=='d') param[i]='e';
}
template<>
int string_to_param<int>(string& param){
change_D_to_E(param);
return stoi(param);
}
template<>
float string_to_param<float>(string& param){
change_D_to_E(param);
return stof(param);
}
template<>
double string_to_param<double>(string& param){
change_D_to_E(param);
return stod(param);
}
template<>
string string_to_param<string>(string& param){
// Remove the quotes
return param.substr(1,param.size()-2);
}
class NamelistReader{
vector<NameList> namelists;
int namelist_index;
bool use_all;
bool modifiable;
template<class T>
void parse_lines(T& newfile, const string& filename){
const bool verbose=false;
string line;
Expect expect = Ampersand;
int i_line=0;
while(getline(newfile, line)){ //read data from file object and put it into string.
i_line++;
trim(line); // Remove leading and trailing whitespace
for (string::size_type i = 0; i < line.size(); i++){
if (line[i]=='!'){
// Comment has begun
break;
}
if (expect==Ampersand){
if (line[i]=='&'){
expect=NLName;
}
} else if (expect==NLName){
string word = get_first_word(line,i);
if (word.size()>0){ // Found namelist name
expect = ParaNameOrEnd;
namelists.push_back(NameList(word));
if (verbose) cout << "Found namelist: " << word << "\n";
break;
}
} else if(expect==ParaNameOrEnd){
if (line[i]=='/'){ // End of namelist
expect=Ampersand;
break;
}
string word = get_first_word(line,i);
if (word.size()>0){
i+=word.size()-1;
expect = Equals;
namelists[namelists.size()-1].add_param(word);
if (verbose) cout << " Found parameter: " << word << "\n";
}
} else if(expect==Equals){
if (line[i]=='='){
expect=Value;
}
} else if(expect==Value || expect == ValueOrEOL){
string word = get_first_value(line,i);
if (word.size()>0){
i+=word.size()-1;
expect = ValueOrEOL;
namelists[namelists.size()-1].add_value(word);
if (verbose) cout << " Found Value: " << word << "\n";
}
}
}
// Got to end of line
if (expect==NLName){
printf("\nParse failure reading %s at Line %d. Failed to find name of namelist.\n", filename.c_str(), i_line);
return;
} else if(expect==Equals){
printf("\nParse failure reading %s at Line %d. Expected parameter assignment.\n", filename.c_str(), i_line);
return;
} else if(expect==Value){
printf("\nParse failure reading %s at Line %d. Couldn't parse value assignment.\n", filename.c_str(), i_line);
return;
}
// Hit EOL, now expecting next parameter
if (expect==ValueOrEOL) expect = ParaNameOrEnd;
}
}
void load(const string& filename){
fstream newfile;
newfile.open(filename,ios::in); //open a file to perform read operation using file object
if (!newfile.is_open()) return; //checking whether the file is open
parse_lines(newfile,filename);
newfile.close(); //close the file object.
}
void load_from_string(const string& str){
istringstream f(str.c_str());
parse_lines(f,"string input");
}
public:
NamelistReader(const string& filename)
: namelist_index(-1), use_all(false), modifiable(false)
{
load(filename);
}
NamelistReader(const string& str_name, ReadFrom read_from)
: namelist_index(-1), use_all(false), modifiable(false)
{
if (read_from==ReadFromFile){
load(str_name);
}else{
load_from_string(str_name);
}
}
NamelistReader(Create create)
: namelist_index(-1), use_all(false), modifiable(false)
{
if (create==CreateManually) modifiable = true;
}
void enable_modifications(){
modifiable = true;
}
void add_namelist(const string& namelist){
if (modifiable){
namelists.push_back(NameList(namelist));
} else {
printf("Warning: 'add_namelist' ignored, only valid if NamelistReader is in CreateManually mode");
}
}
void add_to_namelist(const string& param, const string& val){
if (modifiable){
namelists[namelists.size()-1].add_param(param);
namelists[namelists.size()-1].add_value(val);
} else {
printf("Warning: 'add_to_namelist' ignored, only valid if NamelistReader is in CreateManually mode");
}
}
bool check_all_used(){
int n_unused=0;
for (vector<NameList>::iterator nl = namelists.begin(); nl != namelists.end(); ++nl) {
for (vector<Param>::iterator param = nl->params.begin(); param != nl->params.end(); ++param ) {
if (param->used==false){
printf("Warning: '%s' was present in '%s' but was not used.\n",param->name.c_str(), nl->name.c_str());
n_unused++;
}
}
}
return (n_unused==0);
}
void use_namelist(const string& namelist){
// Locate the requested namelist
vector<NameList>::iterator it = namelists.begin();
while (it!=namelists.end()) {
if ((*it).name==namelist) break;
++it;
}
if (it != namelists.end())
namelist_index = distance(namelists.begin(), it);
else
printf("\nNamelist '%s' not found in the file!",namelist.c_str());
}
bool present(const string& param){
if (namelist_index==-1){
printf("\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
}
// Locate the requested parameter
vector<Param>::iterator it = namelists[namelist_index].params.begin();
while (it!=namelists[namelist_index].params.end()) {
if ((*it).name==param) break;
++it;
}
return (it != namelists[namelist_index].params.end());
}
template <typename T>
T get(const string& param, const T default_val, int val_ind=0){
if (namelist_index==-1){
printf("\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
}
// Locate the requested parameter
int param_index = -1;
vector<Param>::iterator it = namelists[namelist_index].params.begin();
while (it!=namelists[namelist_index].params.end()) {
if ((*it).name==param) break;
++it;
}
if (it != namelists[namelist_index].params.end())
param_index = distance(namelists[namelist_index].params.begin(), it);
else{
return default_val;
}
namelists[namelist_index].params[param_index].used=true;
if (val_ind>=0 && val_ind<namelists[namelist_index].params[param_index].values.size()){
string str_var = namelists[namelist_index].params[param_index].values[val_ind];
return string_to_param<T>(str_var);
} else {
printf("\nParameter '%s' in namelist '%s' didn't have enough values!",param.c_str(),namelists[namelist_index].name.c_str());
exit(1);
return default_val;
}
}
template <typename T>
T get_required(const string& param, int val_ind=0){
if (namelist_index==-1){
printf("\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
}
// Locate the requested parameter
int param_index = -1;
vector<Param>::iterator it = namelists[namelist_index].params.begin();
while (it!=namelists[namelist_index].params.end()) {
if ((*it).name==param) break;
++it;
}
if (it != namelists[namelist_index].params.end())
param_index = distance(namelists[namelist_index].params.begin(), it);
else{
printf("\nParameter '%s' not found in namelist '%s'!",param.c_str(),namelists[namelist_index].name.c_str());
exit(1);
}
namelists[namelist_index].params[param_index].used=true;
if (val_ind>=0 && val_ind<namelists[namelist_index].params[param_index].values.size()){
string str_var = namelists[namelist_index].params[param_index].values[val_ind];
return string_to_param<T>(str_var);
} else {
printf("\nParameter '%s' in namelist '%s' didn't have enough values!",param.c_str(),namelists[namelist_index].name.c_str());
exit(1);
}
}
};
}
#endif