MeteoIODoc 20261119.86f8ff36
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
Config.h
Go to the documentation of this file.
1/***********************************************************************************/
2/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3/***********************************************************************************/
4/* This file is part of MeteoIO.
5 MeteoIO is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 MeteoIO is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef CONFIGREADER_H
19#define CONFIGREADER_H
20
21#include <meteoio/IOUtils.h>
23
24#include <string>
25#include <map>
26#include <vector>
27
28namespace mio {
29
84class ConfigProxy;
85
86class Config {
87 public:
91 Config();
92
97 Config(const std::string& filename_in);
98
103 void write(const std::string& filename) const;
104
109 void addFile(const std::string& filename_in);
110
118 void addKey(std::string key, std::string section, const std::string& value);
119
125 void deleteKey(std::string key, std::string section);
126
137 void deleteKeys(std::string keymatch, std::string section, const bool& anywhere=false);
138
143 std::string getSourceName() const {return sourcename;}
144
149 std::string getConfigRootDir() const {return configRootDir;}
150
157 bool keyExists(std::string key, std::string section) const;
158 bool keyExistsRegex(std::string key_pattern, std::string section) const;
159
165 bool sectionExists(std::string section) const;
166
171 const std::string toString() const;
172
173 friend std::ostream& operator<<(std::ostream& os, const Config& cfg);
174 friend std::istream& operator>>(std::istream& is, Config& cfg);
175
176 template <typename T> std::vector<T> getValue(const std::string& key, std::string& section,
178 {
179 std::vector<T> tmp;
180 getValue(key, section, tmp, opt);
181 return tmp;
182 }
183
194 template <typename T> void getValue(const std::string& key,
195 std::vector<T>& vecT,
197 {
198 getValue(key, "GENERAL", vecT, opt);
199 }
200
212 template <typename T> void getValue(std::string key, std::string section,
213 std::vector<T>& vecT, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
214 {
215 vecT.clear();
216 IOUtils::toUpper(key);
217 IOUtils::toUpper(section);
218
219 try {
220 IOUtils::getValueForKey<T>(properties, section + "::" + key, vecT, opt);
221 } catch(const std::exception&){
222 throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
223 }
224 }
225
240 const ConfigProxy get(const std::string& key, const std::string& section) const;
241
256 template <typename T> T get(const std::string& key, const std::string& section, const T& dflt) const;
257
274 std::string get(const std::string& key, const std::string& section, const std::string& dflt) const;
275 std::string get(const std::string& key, const std::string& section, const char dflt[]) const;
276 double get(const std::string& key, const std::string& section, const double& dflt) const; //surprisingly, in c++11 with gcc 9.3.0 this is needed...
277 bool get(const std::string& key, const std::string& section, const bool& dflt) const;
278
279
286 template <typename T> void getValue(const std::string& key, T& t, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
287 {
288 getValue(key, "GENERAL", t, opt);
289 }
290
298 template <typename T> void getValue(std::string key, std::string section, T& t,
300 {
301 IOUtils::toUpper(key);
302 IOUtils::toUpper(section);
303
304 try {
305 IOUtils::getValueForKey<T>(properties, section + "::" + key, t, opt);
306 } catch(const std::exception&){
307 throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
308 }
309 }
310
319 void getValue(std::string key, std::string section, Date& t, const double& time_zone,
321 {
322 t.setUndef(true);
323 IOUtils::toUpper(key);
324 IOUtils::toUpper(section);
325 std::string tmp;
326
327 try {
328 IOUtils::getValueForKey<std::string>(properties, section + "::" + key, tmp, opt);
329 } catch(const std::exception&){
330 throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
331 }
332
333 bool parse_ok = false;
334 try {
335 parse_ok = IOUtils::convertString(t, tmp, time_zone);
336 } catch(const std::exception&){
337 parse_ok = false;
338 }
339 if (!parse_ok && opt==IOUtils::dothrow)
340 throw InvalidFormatException("Could not parse date '"+tmp+"' in "+sourcename+"for key "+section+"::"+key, AT);
341 }
342
352 template <typename T> void getValues(std::string keymatch, std::string section, std::vector<T>& vecT) const
353 {
354 vecT.clear();
355 IOUtils::toUpper(keymatch);
356 IOUtils::toUpper(section);
357 const std::vector< std::string > vecKeys( getKeys(keymatch, section) );
358
359 for (const std::string& key : vecKeys) {
360 const std::string full_key( section + "::" + key );
361 T tmp;
362 try {
363 IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
364 } catch(const std::exception&){
365 throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
366 }
367 vecT.push_back( tmp );
368 }
369 }
370
371 template <typename T> void getValues(std::string keymatch, std::string section, std::vector<T>& vecT, std::vector<std::string>& vecKeys) const
372 {
373 vecT.clear();
374 IOUtils::toUpper(keymatch);
375 IOUtils::toUpper(section);
376 vecKeys = getKeys(keymatch, section);
377
378 for (const std::string& key : vecKeys) {
379 const std::string full_key = section + "::" + key;
380 T tmp;
381 try {
382 IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
383 } catch(const std::exception&){
384 throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
385 }
386 vecT.push_back( tmp );
387 }
388 }
389
401 std::vector< std::pair<std::string, std::string> > getValues(std::string keymatch, std::string section, const bool& anywhere=false) const;
402
403 std::vector< std::pair<std::string, std::string> > getValuesRegex(const std::string& regex_str, std::string section) const;
404
416 std::vector<std::string> getKeys(std::string keymatch, std::string section, const bool& anywhere=false) const;
417 std::vector<std::string> getKeysRegex(const std::string& regex_str, std::string section) const;
418
425 void replaceKeysRegex(const std::string& regex_str, std::string section, const std::string& replacement);
426
431 std::set<std::string> getSections() const {return sections;}
432
445 void resolveOverlay(std::map<std::string, std::string>& overlay) const;
446
453 void moveSection(std::string org, std::string dest, const bool& overwrite);
454
467 static unsigned int getCommandNr(const std::string& section, const std::string& cmd_pattern, const std::string& cmd_key);
468
484 std::vector< std::pair<std::string, std::string> > parseArgs(const std::string& section, const std::string& cmd_id, const unsigned int& cmd_nr, const std::string& arg_pattern) const;
485
486
487//TODO remove these two methods, they don't belong here!!!
496 std::vector< std::pair<std::string, std::string> > getArgumentsForAlgorithm(const std::string& parname, const std::string& algorithm,
497 const std::string& section) const;
498 // overload for indexed algorith
499 std::vector< std::pair<std::string, std::string> > getArgumentsForAlgorithm(const std::string& parname, const std::string& algorithm, const size_t& algo_index, const std::string& section) const;
500
501
502
503 private:
504 std::map<std::string, std::string> properties;
505 std::set<std::string> sections;
506 std::string sourcename;
507 std::string configRootDir;
508}; //end class definition Config
509
510class ConfigProxy {
511 public:
512 const Config& proxycfg;
513 const std::string& key;
514 const std::string& section;
515
516 ConfigProxy(const Config& i_cfg, const std::string& i_key,
517 const std::string& i_section)
518 : proxycfg(i_cfg), key(i_key),section(i_section) { }
519
520 template<typename T> operator T() const {
521 T tmp;
522 proxycfg.getValue(key, section, tmp, IOUtils::dothrow);
523 return tmp;
524 }
525};
526
536 public:
541 VarExpander(const std::map<std::string, std::string> &i_properties, const std::set<std::string> &i_sections);
542
548 void resolveOverlay(std::map<std::string, std::string>& overlay, const std::string& sourcename="");
549
550 private:
551 enum VarType {
552 ROOT,
553 ENV,
554 EXPR,
555 REF
556 };
557
559 typedef struct VARIABLE {
560 VARIABLE() : value(), children_idx(), children_not_ready(0), parent_idx(IOUtils::npos), pos_start(std::string::npos), pos_end(std::string::npos), type(REF), isExpanded(false) {}
561 VARIABLE(const size_t& i_pos_start, const VarType& i_type) : value(), children_idx(), children_not_ready(0), parent_idx(IOUtils::npos), pos_start(i_pos_start), pos_end(std::string::npos), type(i_type), isExpanded(false) {}
562 VARIABLE(const std::string& i_value, const size_t& i_pos_start, const size_t& i_pos_end, const VarType& i_type) : value(i_value), children_idx(), children_not_ready(0), parent_idx(IOUtils::npos), pos_start(i_pos_start), pos_end(i_pos_end), type(i_type), isExpanded(false) {}
563
564 void finalize(const std::string& i_value, const size_t& i_pos_end) {value=i_value; pos_end=i_pos_end;}
565 void setParent(const size_t& idx) {parent_idx=idx;}
566 void setChild(const size_t& idx) {children_idx.insert(idx); children_not_ready++;}
567
568 std::string toString() const {std::ostringstream os; os << "<Variable>" << (type==EXPR?"expr":(type==ENV?"env":"ref")) << " @[" << pos_start << ","; if(pos_end!=IOUtils::npos) os << pos_end; else os << "-"; os << "]" << " is '" << value << "'"; if(parent_idx!=IOUtils::npos) os << " parent: " << parent_idx; if(!children_idx.empty()){ os << " children: "; for(const auto& val : children_idx) os << val << " ";}; os << " children_not_ready=" << children_not_ready << " "; os << "</variable>"; return os.str();}
569
570 std::string value;
571 std::set<size_t> children_idx;
572 size_t children_not_ready;
573 size_t parent_idx;
574 size_t pos_start, pos_end;
575 VarType type;
576 bool isExpanded;
577 } variable;
578
579 static std::vector<variable> parseVariable(const std::string &value);
580 bool expandVar(const variable& var, const std::string& section, std::string &replacement);
581 bool expandVarsForKey(std::vector<variable>& vecVars, const std::string& section, bool& hasSomeSuccesses);
582 bool expandString(std::string& value, const std::string& section);
583
584 const std::map<std::string, std::string>& properties;
585 const std::set<std::string>& sections;
586 std::map<std::string, std::string> resolved;
587 std::map<std::string, std::vector<variable> > pending;
588 std::vector<std::string> env_stack;
589};
590
592 public:
593 ConfigParser(const std::string& filename, std::map<std::string, std::string> &i_properties, std::set<std::string> &i_sections);
594
595 static std::string extract_section(const std::string& key, const bool& provide_default=false);
596
597 private:
598
599
601 typedef struct FILE_PPT {
602 FILE_PPT() : original_name(), restrict_section(), clean_name() {}
603 FILE_PPT(const std::string& filename, const std::string& ini_sourcename);
604
605 std::string toString() const {std::ostringstream os; os << "<INI file>" << original_name << ", cleaned as " << clean_name; if (!restrict_section.empty()) os << ", restricted to [" << restrict_section << "]"; os << "</INI file>"; return os.str();}
606
607 bool operator<(const FILE_PPT& a) const { //needed for "sort"
608 if (original_name<a.original_name) return true;
609 if (original_name>a.original_name) return false;
610 if (clean_name<a.clean_name) return true;
611 if (clean_name>a.clean_name) return false;
612 return restrict_section < a.restrict_section;
613 }
614 bool operator==(const FILE_PPT& a) const { //needed to check for uniqueness
615 return (original_name==a.original_name) && (restrict_section==a.restrict_section) && (clean_name==a.clean_name);
616 }
617
618 std::string original_name;
619 std::string restrict_section;
620 std::string clean_name;
621 } fileProperties;
622
623 void parseFile(const fileProperties& iniFile);
624 void parseLine(const unsigned int& linenr, const std::string& restrict_section, std::vector< fileProperties > &import_after, bool &accept_import_before, std::string line, std::string &section);
625
626 static bool onlyOneEqual(const std::string& str);
627 bool processSectionHeader(const std::string& line, std::string &section, const unsigned int& linenr);
628 bool processImports(const std::string& key, const std::string& value, std::vector< fileProperties > &import_after, const bool &accept_import_before);
629 void handleNonKeyValue(const std::string& line_backup, const std::string& section, const unsigned int& linenr, bool &accept_import_before);
630
631 std::map<std::string, std::string> properties;
632 std::set< fileProperties > imported;
633 std::set<std::string> sections;
634 std::map<std::string, std::string> vars;
635 std::string sourcename;
636};
637
638} //end namespace mio
639
640#endif
#define AT
Definition IOExceptions.h:28
Definition Config.h:591
static std::string extract_section(const std::string &key, const bool &provide_default=false)
Definition Config.cc:841
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition Config.h:86
std::string getSourceName() const
Returns the filename that the Config object was constructed with.
Definition Config.h:143
void getValue(const std::string &key, T &t, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Template function to retrieve a value of class T for a certain key.
Definition Config.h:286
void getValues(std::string keymatch, std::string section, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const
Definition Config.h:371
void getValues(std::string keymatch, std::string section, std::vector< T > &vecT) const
Template function to retrieve a vector of values of class T for a certain key pattern.
Definition Config.h:352
void resolveOverlay(std::map< std::string, std::string > &overlay) const
Expand the variables of extra key/values that are not part of the ini file (an "overlay"),...
Definition Config.cc:120
friend std::ostream & operator<<(std::ostream &os, const Config &cfg)
Definition Config.cc:493
void deleteKey(std::string key, std::string section)
Delete a specific key/value pair from the internal map object, key/section are case insensitive.
Definition Config.cc:134
const ConfigProxy get(const std::string &key, const std::string &section) const
A function that allows to retrieve a value for a key as return parameter (vectors of values too).
Definition Config.cc:52
const std::string toString() const
Print the content of the Config object (useful for debugging) The Config is bound by "<Config>" and "...
Definition Config.cc:482
Config()
Empty constructor. The user MUST later one fill the internal key/value map object.
Definition Config.cc:45
std::vector< std::string > getKeys(std::string keymatch, std::string section, const bool &anywhere=false) const
Function that searches for a given string within the keys of a given section (default: GENERAL) it re...
Definition Config.cc:427
static unsigned int getCommandNr(const std::string &section, const std::string &cmd_pattern, const std::string &cmd_key)
Extract the command number from a given command string, given the command pattern.
Definition Config.cc:572
std::set< std::string > getSections() const
Returns all the sections that are present in the config object.
Definition Config.h:431
void getValue(const std::string &key, std::vector< T > &vecT, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Template function to retrieve a vector of values of class T for a certain key.
Definition Config.h:194
std::vector< std::string > getKeysRegex(const std::string &regex_str, std::string section) const
Definition Config.cc:379
void addFile(const std::string &filename_in)
Add the content of a file to the internal key/value map object.
Definition Config.cc:113
void getValue(std::string key, std::string section, Date &t, const double &time_zone, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Function to retrieve a Date value for a certain key.
Definition Config.h:319
std::vector< std::pair< std::string, std::string > > parseArgs(const std::string &section, const std::string &cmd_id, const unsigned int &cmd_nr, const std::string &arg_pattern) const
Extract the arguments for a given command and store them into a vector of key / value pairs.
Definition Config.cc:590
std::vector< std::pair< std::string, std::string > > getArgumentsForAlgorithm(const std::string &parname, const std::string &algorithm, const std::string &section) const
retrieve the resampling algorithm to be used for the 1D interpolation of meteo parameters....
Definition Config.cc:606
friend std::istream & operator>>(std::istream &is, Config &cfg)
Definition Config.cc:528
void addKey(std::string key, std::string section, const std::string &value)
Add a specific key/value pair to the internal key/value map object. key and section are case insensit...
Definition Config.cc:127
void write(const std::string &filename) const
Write the Config object to a file.
Definition Config.cc:441
bool keyExists(std::string key, std::string section) const
Return if a given key exists in a given section (matching is case insensitive)
Definition Config.cc:171
std::vector< T > getValue(const std::string &key, std::string &section, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Definition Config.h:176
void getValue(std::string key, std::string section, T &t, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Template function to retrieve a value of class T for a certain key.
Definition Config.h:298
void getValue(std::string key, std::string section, std::vector< T > &vecT, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Template function to retrieve a vector of values of class T for a certain key.
Definition Config.h:212
bool sectionExists(std::string section) const
Return if a given section exists in the Config object.
Definition Config.cc:192
bool keyExistsRegex(std::string key_pattern, std::string section) const
Definition Config.cc:181
std::string getConfigRootDir() const
Returns the directory where the root configuration file is (needed to resolv relative paths).
Definition Config.h:149
void replaceKeysRegex(const std::string &regex_str, std::string section, const std::string &replacement)
In a given section, replace key text based on a given regex.
Definition Config.cc:391
void moveSection(std::string org, std::string dest, const bool &overwrite)
Move all keys of the org section to the dest section.
Definition Config.cc:204
std::vector< std::pair< std::string, std::string > > getValuesRegex(const std::string &regex_str, std::string section) const
Definition Config.cc:243
void deleteKeys(std::string keymatch, std::string section, const bool &anywhere=false)
Delete keys matching a specific pattern from the internal map object, key/section are case insensitiv...
Definition Config.cc:141
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition Date.h:86
void setUndef(const bool &flag=true)
Definition Date.cc:195
thrown when parsed data does not reflect an expected format (e.g. premature end of a line,...
Definition IOExceptions.h:94
thrown when encountered an unexpected value (e.g. unknown name or key)
Definition IOExceptions.h:142
Expansion of the variables of key/values (see special values): references to other keys,...
Definition Config.h:535
void resolveOverlay(std::map< std::string, std::string > &overlay, const std::string &sourcename="")
Expand the given key/values, in rounds until all are done (they may refer to each other)
Definition Config.cc:861
bool convertString(Date &t, std::string str, const double &time_zone, std::ios_base &(*f)(std::ios_base &))
Convert a string to a date (template specialization of convertString)
Definition IOUtils.cc:659
void toUpper(std::string &str)
Definition IOUtils.cc:285
ThrowOptions
Definition IOUtils.h:75
@ dothrow
Definition IOUtils.h:75
bool operator==(const geoLocation &lhs, const geoLocation &rhs)
Definition iCSVHelper.cc:686
Definition Config.cc:34