1 #ifndef NAMELISTREADER_HPP
2 #define NAMELISTREADER_HPP
35 inline string&
ltrim(
string& s,
const char* t =
" \t\n\r\f\v")
37 s.erase(0, s.find_first_not_of(t));
42 inline string&
rtrim(
string& s,
const char* t =
" \t\n\r\f\v")
44 s.erase(s.find_last_not_of(t) + 1);
49 inline string&
trim(
string& s,
const char* t =
" \t\n\r\f\v")
60 : name(name), used(false)
74 params.push_back(
Param(word));
79 params[params.size()-1].values.push_back(word);
85 if ((str[i]>=48 && str[i]<=57) || (str[i]>=65 && str[i]<=90) ||
86 (str[i]>=97 && str[i]<=122) || str[i]==95){
90 return str.substr(start, i-start);
93 return str.substr(start,str.size()-start);
100 if (in_dquote==
true || in_squote==
true){
101 if (str[i]==34) in_dquote=
false;
102 if (str[i]==39) in_squote=
false;
104 if (str[i]==34) in_dquote=
true;
105 if (str[i]==39) in_squote=
true;
106 if (str[i]==
'!' || str[i]==
' ' || str[i]==
'\t'|| str[i]==
'\r'|| str[i]==
'\f'|| str[i]==
'\v'){
108 return str.substr(start, i-start);
112 return str.substr(start,str.size()-start);
120 for (
int i = 0; i<lc_str.size(); i++)
121 lc_str[i]=tolower(lc_str[i]);
134 if (lc_param==
".false."){
136 }
else if (lc_param==
".true."){
139 printf(
"Error, couldn't parse %s \n", param.c_str());
145 for (
int i = 0; i<param.size(); i++)
146 if (param[i]==
'D' || param[i]==
'd') param[i]=
'e';
170 return param.substr(1,param.size()-2);
180 typename vector<T>::iterator it = vec.begin();
181 while (it!=vec.end()) {
187 return distance(vec.begin(), it);
201 const bool verbose=
false;
205 while(getline(newfile, line)){
217 }
else if (expect==
NLName){
221 namelists.push_back(
NameList(word));
222 if (verbose) cout <<
"Found namelist: " << word <<
"\n";
235 namelists[namelists.size()-1].add_param(word);
236 if (verbose) cout <<
" Found parameter: " << word <<
"\n";
238 }
else if(expect==
Equals){
247 namelists[namelists.size()-1].add_value(word);
248 if (verbose) cout <<
" Found Value: " << word <<
"\n";
254 printf(
"\nParse failure reading %s at Line %d. Failed to find name of namelist.\n", filename.c_str(), i_line);
256 }
else if(expect==
Equals){
257 printf(
"\nParse failure reading %s at Line %d. Expected parameter assignment.\n", filename.c_str(), i_line);
259 }
else if(expect==
Value){
260 printf(
"\nParse failure reading %s at Line %d. Couldn't parse value assignment.\n", filename.c_str(), i_line);
268 void load(
const string& filename){
270 newfile.open(filename,ios::in);
271 if (!newfile.is_open())
return;
273 parse_lines(newfile,filename);
279 istringstream f(str.c_str());
281 parse_lines(f,
"string input");
287 : namelist_index(-1), use_all(false), modifiable(false)
293 : namelist_index(-1), use_all(false), modifiable(false)
298 load_from_string(str_name);
303 : namelist_index(-1), use_all(false), modifiable(false)
314 namelists.push_back(
NameList(namelist));
315 namelist_index = namelists.size()-1;
317 printf(
"Warning: 'add_namelist' ignored, only valid if NamelistReader is in CreateManually mode");
323 namelists[namelist_index].add_param(param);
324 namelists[namelist_index].add_value(val);
326 printf(
"Warning: 'add_to_namelist' ignored, only valid if NamelistReader is in CreateManually mode");
332 namelists[namelist_index].add_value(val);
334 printf(
"Warning: 'add_to_param' ignored, only valid if NamelistReader is in CreateManually mode");
340 for (vector<NameList>::iterator nl = namelists.begin(); nl != namelists.end(); ++nl) {
341 for (vector<Param>::iterator param = nl->params.begin(); param != nl->params.end(); ++param ) {
342 if (param->used==
false){
343 printf(
"Warning: '%s' was present in '%s' but was not used.\n",param->name.c_str(), nl->name.c_str());
348 return (n_unused==0);
357 if(namelist_index==-1){
358 printf(
"\nNamelist '%s' not found in the file!",namelist.c_str());
364 if (namelist_index==-1){
365 printf(
"\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
372 template <
typename T>
373 T
get(
const string& param,
const T default_val,
int val_ind=0){
374 if (namelist_index==-1){
375 printf(
"\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
384 namelists[namelist_index].params[param_index].used=
true;
386 if (val_ind>=0 && val_ind<namelists[namelist_index].params[param_index].values.size()){
387 string str_var = namelists[namelist_index].params[param_index].values[val_ind];
388 return string_to_param<T>(str_var);
390 printf(
"\nParameter '%s' in namelist '%s' didn't have enough values!",param.c_str(),namelists[namelist_index].name.c_str());
396 template <
typename T>
398 if (namelist_index==-1){
399 printf(
"\nNeed to choose which namelist to use with use_namelist(const std::string&)!\n");
405 printf(
"\nParameter '%s' not found in namelist '%s'!",param.c_str(),namelists[namelist_index].name.c_str());
409 namelists[namelist_index].params[param_index].used=
true;
411 if (val_ind>=0 && val_ind<namelists[namelist_index].params[param_index].values.size()){
412 string str_var = namelists[namelist_index].params[param_index].values[val_ind];
413 return string_to_param<T>(str_var);
415 printf(
"\nParameter '%s' in namelist '%s' didn't have enough values!",param.c_str(),namelists[namelist_index].name.c_str());
static void change_D_to_E(string ¶m)
Definition: NamelistReader.hpp:144
void add_value(string word)
Definition: NamelistReader.hpp:77
bool modifiable
Definition: NamelistReader.hpp:197
Definition: NamelistReader.hpp:64
void add_to_param(const string &val)
Definition: NamelistReader.hpp:330
void add_namelist(const string &namelist)
Definition: NamelistReader.hpp:312
void enable_modifications()
Definition: NamelistReader.hpp:308
void load_from_string(const string &str)
Definition: NamelistReader.hpp:278
Definition: NamelistReader.hpp:21
Definition: NamelistReader.hpp:31
Definition: NamelistReader.hpp:20
void load(const string &filename)
Definition: NamelistReader.hpp:268
vector< string > values
Definition: NamelistReader.hpp:57
string name
Definition: NamelistReader.hpp:65
int find_case_insensitive_name_match(vector< T > &vec, const string &str)
Definition: NamelistReader.hpp:175
string & rtrim(string &s, const char *t=" \t\n\r\f\v")
Definition: NamelistReader.hpp:42
bool present(const string ¶m)
Definition: NamelistReader.hpp:363
bool check_all_used()
Definition: NamelistReader.hpp:338
bool use_all
Definition: NamelistReader.hpp:196
Definition: NamelistReader.hpp:193
string & ltrim(string &s, const char *t=" \t\n\r\f\v")
Definition: NamelistReader.hpp:35
Definition: NamelistReader.hpp:18
NamelistReader(const string &filename)
Definition: NamelistReader.hpp:286
int size_type
Definition: col_grid_matrix.hpp:19
Create
Definition: NamelistReader.hpp:30
vector< NameList > namelists
Definition: NamelistReader.hpp:194
bool string_to_param< bool >(string ¶m)
Definition: NamelistReader.hpp:130
void use_namelist(const string &namelist)
Definition: NamelistReader.hpp:355
Definition: NamelistReader.hpp:27
bool used
Definition: NamelistReader.hpp:56
Definition: NamelistReader.hpp:17
int namelist_index
Definition: NamelistReader.hpp:195
float string_to_param< float >(string ¶m)
Definition: NamelistReader.hpp:156
NameList(string name)
Definition: NamelistReader.hpp:68
vector< Param > params
Definition: NamelistReader.hpp:66
void parse_lines(T &newfile, const string &filename)
Definition: NamelistReader.hpp:200
NamelistReader(const string &str_name, ReadFrom read_from)
Definition: NamelistReader.hpp:292
void add_to_namelist(const string ¶m, const string &val)
Definition: NamelistReader.hpp:321
int string_to_param< int >(string ¶m)
Definition: NamelistReader.hpp:150
static T string_to_param(string ¶m)
Definition: NamelistReader.hpp:19
string & trim(string &s, const char *t=" \t\n\r\f\v")
Definition: NamelistReader.hpp:49
bool namelist_present(const string &namelist)
Definition: NamelistReader.hpp:351
string name
Definition: NamelistReader.hpp:55
Param(string name)
Definition: NamelistReader.hpp:59
ReadFrom
Definition: NamelistReader.hpp:25
Expect
Definition: NamelistReader.hpp:16
static string lower_case_copy(const string &str)
Definition: NamelistReader.hpp:115
NamelistReader(Create create)
Definition: NamelistReader.hpp:302
double string_to_param< double >(string ¶m)
Definition: NamelistReader.hpp:162
string string_to_param< string >(string ¶m)
Definition: NamelistReader.hpp:168
static string get_first_value(string &str, int start)
Definition: NamelistReader.hpp:96
static string get_first_word(string &str, int start)
Definition: NamelistReader.hpp:83
T get_required(const string ¶m, int val_ind=0)
Definition: NamelistReader.hpp:397
Definition: NamelistReader.hpp:26
Definition: NamelistReader.hpp:54
void add_param(string word)
Definition: NamelistReader.hpp:72
Definition: NamelistReader.hpp:22