MeteoIODoc  2.10.0
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>
22 #include <meteoio/IOExceptions.h>
23 
24 #include <string>
25 #include <sstream>
26 #include <map>
27 #include <vector>
28 #include <typeinfo> //for typeid()
29 
30 namespace mio {
31 
77 class ConfigProxy;
78 
79 class Config {
80  public:
84  Config();
85 
90  Config(const std::string& filename_in);
91 
96  void write(const std::string& filename) const;
97 
102  void addFile(const std::string& filename_in);
103 
111  void addKey(std::string key, std::string section, const std::string& value);
112 
118  void deleteKey(std::string key, std::string section);
119 
130  void deleteKeys(std::string keymatch, std::string section, const bool& anywhere=false);
131 
136  std::string getSourceName() const {return sourcename;}
137 
142  std::string getConfigRootDir() const {return configRootDir;}
143 
150  bool keyExists(std::string key, std::string section) const;
151 
157  bool sectionExists(std::string section) const;
158 
163  const std::string toString() const;
164 
165  friend std::ostream& operator<<(std::ostream& os, const Config& cfg);
166  friend std::istream& operator>>(std::istream& is, Config& cfg);
167 
168  template <typename T> std::vector<T> getValue(const std::string& key, std::string& section,
169  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
170  {
171  std::vector<T> tmp;
172  getValue(key, section, tmp, opt);
173  return tmp;
174  }
175 
186  template <typename T> void getValue(const std::string& key,
187  std::vector<T>& vecT,
188  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
189  {
190  getValue(key, "GENERAL", vecT, opt);
191  }
192 
204  template <typename T> void getValue(std::string key, std::string section,
205  std::vector<T>& vecT, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
206  {
207  vecT.clear();
208  IOUtils::toUpper(key);
209  IOUtils::toUpper(section);
210 
211  try {
212  IOUtils::getValueForKey<T>(properties, section + "::" + key, vecT, opt);
213  } catch(const std::exception&){
214  throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
215  }
216  }
217 
232  const ConfigProxy get(const std::string& key, const std::string& section) const;
233 
248  template <typename T> T get(const std::string& key, const std::string& section, const T& dflt) const;
249 
266  std::string get(const std::string& key, const std::string& section, const std::string& dflt) const;
267  std::string get(const std::string& key, const std::string& section, const char dflt[]) const;
268  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...
269  bool get(const std::string& key, const std::string& section, const bool& dflt) const;
270 
271 
278  template <typename T> void getValue(const std::string& key, T& t, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
279  {
280  getValue(key, "GENERAL", t, opt);
281  }
282 
290  template <typename T> void getValue(std::string key, std::string section, T& t,
291  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
292  {
293  IOUtils::toUpper(key);
294  IOUtils::toUpper(section);
295 
296  try {
297  IOUtils::getValueForKey<T>(properties, section + "::" + key, t, opt);
298  } catch(const std::exception&){
299  throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
300  }
301  }
302 
311  void getValue(std::string key, std::string section, Date& t, const double& time_zone,
312  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
313  {
314  t.setUndef(true);
315  IOUtils::toUpper(key);
316  IOUtils::toUpper(section);
317  std::string tmp;
318 
319  try {
320  IOUtils::getValueForKey<std::string>(properties, section + "::" + key, tmp, opt);
321  } catch(const std::exception&){
322  throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+section+"::"+key, AT);
323  }
324 
325  bool parse_ok = false;
326  try {
327  parse_ok = IOUtils::convertString(t, tmp, time_zone);
328  } catch(const std::exception&){
329  parse_ok = false;
330  }
331  if (!parse_ok && opt==IOUtils::dothrow)
332  throw InvalidFormatException("Could not parse date '"+tmp+"' in "+sourcename+"for key "+section+"::"+key, AT);
333  }
334 
344  template <typename T> void getValues(std::string keymatch, std::string section, std::vector<T>& vecT) const
345  {
346  vecT.clear();
347  IOUtils::toUpper(keymatch);
348  IOUtils::toUpper(section);
349  const std::vector< std::string > vecKeys( getKeys(keymatch, section) );
350 
351  for (const std::string& key : vecKeys) {
352  const std::string full_key( section + "::" + key );
353  T tmp;
354  try {
355  IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
356  } catch(const std::exception&){
357  throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
358  }
359  vecT.push_back( tmp );
360  }
361  }
362 
363  template <typename T> void getValues(std::string keymatch, std::string section, std::vector<T>& vecT, std::vector<std::string>& vecKeys) const
364  {
365  vecT.clear();
366  IOUtils::toUpper(keymatch);
367  IOUtils::toUpper(section);
368  vecKeys = getKeys(keymatch, section);
369 
370  for (const std::string& key : vecKeys) {
371  const std::string full_key = section + "::" + key;
372  T tmp;
373  try {
374  IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
375  } catch(const std::exception&){
376  throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
377  }
378  vecT.push_back( tmp );
379  }
380  }
381 
382  std::vector< std::pair<std::string, std::string> > getValuesRegex(std::string regex_str, std::string section) const;
383 
384  std::vector< std::pair<std::string, std::string> > getValues(std::string keymatch, std::string section, const bool& anywhere=false) const;
385 
397  std::vector<std::string> getKeys(std::string keymatch, std::string section, const bool& anywhere=false) const;
398 
403  std::set<std::string> getSections() const {return sections;}
404 
411  void moveSection(std::string org, std::string dest, const bool& overwrite);
412 
425  static unsigned int getCommandNr(const std::string& section, const std::string& cmd_pattern, const std::string& cmd_key);
426 
442  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;
443 
452  std::vector< std::pair<std::string, std::string> > getArgumentsForAlgorithm(const std::string& parname, const std::string& algorithm,
453  const std::string& section = "Interpolations1d") const;
454 
455 
456 
457  private:
458  std::map<std::string, std::string> properties;
459  std::set<std::string> sections;
460  std::string sourcename;
461  std::string configRootDir;
462 }; //end class definition Config
463 
464 class ConfigProxy {
465  public:
466  const Config& proxycfg;
467  const std::string& key;
468  const std::string& section;
469 
470  ConfigProxy(const Config& i_cfg, const std::string& i_key,
471  const std::string& i_section)
472  : proxycfg(i_cfg), key(i_key),section(i_section) { }
473 
474  template<typename T> operator T() const {
475  T tmp;
476  proxycfg.getValue(key, section, tmp, IOUtils::dothrow);
477  return tmp;
478  }
479 };
480 
482  public:
483  ConfigParser(const std::string& filename, std::map<std::string, std::string> &i_properties, std::set<std::string> &i_sections);
484 
485  static std::string extract_section(std::string key);
486  private:
487  void parseFile(const std::string& filename);
488  void parseLine(const unsigned int& linenr, std::vector<std::string> &import_after, bool &accept_import_before, std::string &line, std::string &section);
489  static void processEnvVars(std::string& value);
490  static void processExpr(std::string& value);
491  bool processVars(std::string& value, const std::string& section);
492  bool processSectionHeader(const std::string& line, std::string &section, const unsigned int& linenr);
493  std::string clean_import_path(const std::string& in_path) const;
494  bool processImports(const std::string& key, const std::string& value, std::vector<std::string> &import_after, const bool &accept_import_before);
495  void handleNonKeyValue(const std::string& line_backup, const std::string& section, const unsigned int& linenr, bool &accept_import_before);
496 
497  std::map<std::string, std::string> properties;
498  std::set<std::string> imported;
499  std::set<std::string> sections;
500  std::set<std::string> deferred_vars;
501  std::string sourcename;
502 };
503 
504 } //end namespace mio
505 
506 #endif
#define AT
Definition: IOExceptions.h:28
Definition: Config.h:481
ConfigParser(const std::string &filename, std::map< std::string, std::string > &i_properties, std::set< std::string > &i_sections)
Definition: Config.cc:551
static std::string extract_section(std::string key)
Definition: Config.cc:840
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition: Config.h:79
std::string getSourceName() const
Returns the filename that the Config object was constructed with.
Definition: Config.h:136
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:278
void getValues(std::string keymatch, std::string section, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const
Definition: Config.h:363
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:344
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:118
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:43
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:408
Config()
Empty constructor. The user MUST later one fill the internal key/value map object.
Definition: Config.cc:36
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:353
friend std::ostream & operator<<(std::ostream &os, const Config &cfg)
Definition: Config.cc:419
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:498
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:186
void addFile(const std::string &filename_in)
Add the content of a file to the internal key/value map object.
Definition: Config.cc:104
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:311
friend std::istream & operator>>(std::istream &is, Config &cfg)
Definition: Config.cc:454
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:516
std::set< std::string > getSections() const
Returns all the sections that are present in the config object.
Definition: Config.h:403
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:111
void write(const std::string &filename) const
Write the Config object to a file.
Definition: Config.cc:367
bool keyExists(std::string key, std::string section) const
Return if a given key exists in a given section.
Definition: Config.cc:155
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:290
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:204
bool sectionExists(std::string section) const
Return if a given section exists in the Config object.
Definition: Config.cc:164
std::string getConfigRootDir() const
Returns the directory where the root configuration file is (needed to resolv relative paths).
Definition: Config.h:142
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:176
std::vector< T > getValue(const std::string &key, std::string &section, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Definition: Config.h:168
std::vector< std::pair< std::string, std::string > > getArgumentsForAlgorithm(const std::string &parname, const std::string &algorithm, const std::string &section="Interpolations1d") const
retrieve the resampling algorithm to be used for the 1D interpolation of meteo parameters....
Definition: Config.cc:532
std::vector< std::pair< std::string, std::string > > getValuesRegex(std::string regex_str, std::string section) const
Definition: Config.cc:215
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:125
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
void setUndef(const bool &flag=true)
Definition: Date.cc:192
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
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:597
void toUpper(std::string &str)
Definition: IOUtils.cc:254
ThrowOptions
Definition: IOUtils.h:74
@ dothrow
Definition: IOUtils.h:74
Definition: Config.cc:30