18 #ifndef CONFIGREADER_H
19 #define CONFIGREADER_H
71 Config(
const std::string& filename_in);
77 void write(
const std::string& filename)
const;
83 void addFile(
const std::string& filename_in);
98 void addKey(
const std::string& key,
const std::string& section,
const std::string& value);
105 void deleteKey(
const std::string& key,
const std::string& section);
117 void deleteKeys(
const std::string& keymatch,
const std::string& section,
const bool& anywhere=
false);
137 bool keyExists(
const std::string& key,
const std::string& section)
const;
148 template <
typename T> std::vector<T>
getValue(
const std::string& key,
const std::string& section,
166 template <
typename T>
void getValue(
const std::string& key,
167 std::vector<T>& vecT,
170 getValue(key,
"GENERAL", vecT, opt);
184 template <
typename T>
void getValue(
const std::string& key,
const std::string& section,
192 IOUtils::getValueForKey<T>(properties, new_section +
"::" + new_key, vecT, opt);
193 }
catch(
const std::exception&){
240 template <
typename T>
void getValue(
const std::string& key,
const std::string& section, T& t,
247 IOUtils::getValueForKey<T>(properties, new_section +
"::" + new_key, t, opt);
248 }
catch(
const std::exception&){
259 template <
typename T>
void getValues(
const std::string& keystart,
const std::string& section, std::vector<T>& vecT)
const
262 std::vector< std::string > vecKeys;
264 const size_t nr_keys =
findKeys(vecKeys, keystart, new_section);
266 for (
size_t ii=0; ii<nr_keys; ++ii) {
267 const std::string full_key = new_section +
"::" + vecKeys[ii];
271 }
catch(
const std::exception&){
274 vecT.push_back( tmp );
278 template <
typename T>
void getValues(
const std::string& keystart,
const std::string& section, std::vector<T>& vecT, std::vector<std::string>& vecKeys)
const
282 const size_t nr_keys =
findKeys(vecKeys, keystart, new_section);
284 for (
size_t ii=0; ii<nr_keys; ++ii) {
285 const std::string full_key = new_section +
"::" + vecKeys[ii];
289 }
catch(
const std::exception&){
292 vecT.push_back( tmp );
309 size_t findKeys(std::vector<std::string>& vecResult,
310 const std::string& keymatch, std::string section,
const bool& anywhere=
false)
const;
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 §ion);
316 std::string extract_section(std::string key)
const;
317 std::string clean_import_path(
const std::string& in_path)
const;
319 std::map<std::string, std::string> properties;
320 std::vector<std::string> imported;
321 std::string sourcename;
322 std::string configRootDir;
323 static const char* defaultSection;
328 const Config& proxycfg;
329 const std::string& key;
330 const std::string& section;
333 ConfigProxy(
const Config& i_cfg,
const std::string& i_key,
335 : proxycfg(i_cfg), key(i_key),section(i_section), opt(i_opt) { }
337 template<
typename T>
operator T() {
339 proxycfg.getValue(key, section, tmp, opt);
343 ConfigProxy& operator =(
const ConfigProxy& ) {
return *
this;}
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 §ion, 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 §ion, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const
Definition: Config.h:278
void getValue(const std::string &key, const std::string §ion, 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 §ion, 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 §ion, 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
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 §ion)
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 §ion) 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 §ion, 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 §ion, 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