MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
29 namespace mio {
30 
56 class ConfigProxy;
57 
58 class Config {
59  public:
63  Config();
64 
65  virtual ~Config() {}
66 
71  Config(const std::string& filename_in);
72 
77  void write(const std::string& filename) const;
78 
83  void addFile(const std::string& filename_in);
84 
89  void addCmdLine(const std::string& cmd_line);
90 
98  void addKey(const std::string& key, const std::string& section, const std::string& value);
99 
105  void deleteKey(const std::string& key, const std::string& section);
106 
117  void deleteKeys(const std::string& keymatch, const std::string& section, const bool& anywhere=false);
118 
123  std::string getSourceName() const;
124 
129  std::string getConfigRootDir() const;
130 
137  bool keyExists(const std::string& key, const std::string& section) const;
138 
143  const std::string toString() const;
144 
145  friend std::iostream& operator<<(std::iostream& os, const Config& cfg);
146  friend std::iostream& operator>>(std::iostream& is, Config& cfg);
147 
148  template <typename T> std::vector<T> getValue(const std::string& key, const std::string& section,
149  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
150  {
151  std::vector<T> tmp;
152  getValue(key, section, tmp, opt);
153  return tmp;
154  }
155 
166  template <typename T> void getValue(const std::string& key,
167  std::vector<T>& vecT,
168  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
169  {
170  getValue(key, "GENERAL", vecT, opt);
171  }
172 
184  template <typename T> void getValue(const std::string& key, const std::string& section,
185  std::vector<T>& vecT, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
186  {
187  vecT.clear();
188  const std::string new_key( IOUtils::strToUpper(key) );
189  const std::string new_section( IOUtils::strToUpper(section) );
190 
191  try {
192  IOUtils::getValueForKey<T>(properties, new_section + "::" + new_key, vecT, opt);
193  } catch(const std::exception&){
194  throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+new_section+"::"+new_key, AT);
195  }
196  }
197 
204  ConfigProxy get(const std::string& key, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const;
205 
220  ConfigProxy get(const std::string& key, const std::string& section, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const;
221 
228  template <typename T> void getValue(const std::string& key, T& t, const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
229  {
230  getValue(key, "GENERAL", t, opt);
231  }
232 
240  template <typename T> void getValue(const std::string& key, const std::string& section, T& t,
241  const IOUtils::ThrowOptions& opt=IOUtils::dothrow) const
242  {
243  const std::string new_key( IOUtils::strToUpper(key) );
244  const std::string new_section( IOUtils::strToUpper(section) );
245 
246  try {
247  IOUtils::getValueForKey<T>(properties, new_section + "::" + new_key, t, opt);
248  } catch(const std::exception&){
249  throw UnknownValueException("[E] Error in "+sourcename+": no value for key "+new_section+"::"+new_key, AT);
250  }
251  }
252 
259  template <typename T> void getValues(const std::string& keystart, const std::string& section, std::vector<T>& vecT) const
260  {
261  vecT.clear();
262  std::vector< std::string > vecKeys;
263  const std::string new_section( IOUtils::strToUpper(section) );
264  const size_t nr_keys = findKeys(vecKeys, keystart, new_section);
265 
266  for (size_t ii=0; ii<nr_keys; ++ii) {
267  const std::string full_key = new_section + "::" + vecKeys[ii];
268  T tmp;
269  try {
270  IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
271  } catch(const std::exception&){
272  throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
273  }
274  vecT.push_back( tmp );
275  }
276  }
277 
278  template <typename T> void getValues(const std::string& keystart, const std::string& section, std::vector<T>& vecT, std::vector<std::string>& vecKeys) const
279  {
280  vecT.clear();
281  const std::string new_section( IOUtils::strToUpper(section) );
282  const size_t nr_keys = findKeys(vecKeys, keystart, new_section);
283 
284  for (size_t ii=0; ii<nr_keys; ++ii) {
285  const std::string full_key = new_section + "::" + vecKeys[ii];
286  T tmp;
287  try {
288  IOUtils::getValueForKey<T>(properties, full_key, tmp, IOUtils::dothrow);
289  } catch(const std::exception&){
290  throw UnknownValueException("[E] Error in "+sourcename+" reading key "+full_key, AT);
291  }
292  vecT.push_back( tmp );
293  }
294  }
295 
309  size_t findKeys(std::vector<std::string>& vecResult,
310  const std::string& keymatch, std::string section, const bool& anywhere=false) const;
311 
312  private:
313  void parseCmdLine(const std::string& cmd_line);
314  void parseFile(const std::string& filename);
315  void parseLine(const unsigned int& linenr, std::vector<std::string> &import_after, bool &accept_import_before, std::string &line, std::string &section);
316  std::string extract_section(std::string key) const;
317  std::string clean_import_path(const std::string& in_path) const;
318 
319  std::map<std::string, std::string> properties; //Save key value pairs
320  std::vector<std::string> imported; //list of files already imported (to avoid circular references)
321  std::string sourcename; //description of the data source for the key/value pair
322  std::string configRootDir; //directory of the root config file
323  static const char* defaultSection;
324 }; //end class definition Config
325 
326 class ConfigProxy {
327  public:
328  const Config& proxycfg;
329  const std::string& key;
330  const std::string& section;
331  const IOUtils::ThrowOptions& opt;
332 
333  ConfigProxy(const Config& i_cfg, const std::string& i_key,
334  const std::string& i_section, const IOUtils::ThrowOptions& i_opt)
335  : proxycfg(i_cfg), key(i_key),section(i_section), opt(i_opt) { }
336 
337  template<typename T> operator T() {
338  T tmp;
339  proxycfg.getValue(key, section, tmp, opt);
340  return tmp;
341  }
342 
343  ConfigProxy& operator =(const ConfigProxy& /*i_cfg*/) {return *this;} //making VC++ happy...
344 };
345 
346 } //end namespace mio
347 
348 #endif
void addCmdLine(const std::string &cmd_line)
Add the content of the given command line to the internal key/value map object.
Definition: Config.cc:52
friend std::iostream & operator<<(std::iostream &os, const Config &cfg)
Definition: Config.cc:119
void addKey(const std::string &key, const 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:59
void write(const std::string &filename) const
Write the Config object to a file.
Definition: Config.cc:352
ThrowOptions
Definition: IOUtils.h:59
thrown when encountered an unexpected value (e.g. unknown name or key)
Definition: IOExceptions.h:144
void addFile(const std::string &filename_in)
Add the content of a file to the internal key/value map object.
Definition: Config.cc:45
void getValues(const std::string &keystart, const std::string &section, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const
Definition: Config.h:278
void getValue(const std::string &key, const 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:240
std::vector< T > getValue(const std::string &key, const std::string &section, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
Definition: Config.h:148
std::string getSourceName() const
Returns the filename that the Config object was constructed with.
Definition: Config.cc:284
void deleteKeys(const std::string &keymatch, const 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:69
Config()
Empty constructor. The user MUST later one fill the internal key/value map object.
Definition: Config.cc:32
Definition: IOUtils.h:59
std::string getConfigRootDir() const
Returns the directory where the root configuration file is (needed to resolv relative paths)...
Definition: Config.cc:289
void deleteKey(const std::string &key, const std::string &section)
Delete a specific key/value pair from the internal map object, key/section are case insensitive...
Definition: Config.cc:64
bool keyExists(const std::string &key, const std::string &section) const
Return if a given key exists in a given section.
Definition: Config.cc:101
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s...
Definition: Config.h:58
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:228
#define AT
Definition: IOExceptions.h:29
friend std::iostream & operator>>(std::iostream &is, Config &cfg)
Definition: Config.cc:141
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:166
void getValue(const std::string &key, const 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:184
std::string strToUpper(std::string str)
Definition: IOUtils.cc:154
size_t findKeys(std::vector< std::string > &vecResult, const std::string &keymatch, std::string section, const bool &anywhere=false) const
Function that searches for a given string within the keys of section (default: GENERAL) it returns th...
Definition: Config.cc:294
void getValues(const std::string &keystart, const 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:259
virtual ~Config()
Definition: Config.h:65
const std::string toString() const
Print the content of the Config object (usefull for debugging) The Config is bound by "<Config>" and ...
Definition: Config.cc:108