WSL/SLF GitLab Repository

Commit 6e7e1378 authored by Thomas Egger's avatar Thomas Egger
Browse files

The keys and sections within a Config object are now case insensitive, the...

The keys and sections within a Config object are now case insensitive, the values remain case sensitive.
parent a9e0da8c
......@@ -22,6 +22,7 @@ using namespace std;
namespace mio {
const unsigned int Config::nothrow = 666;
const std::string Config::defaultSection = "GENERAL";
//Constructors
Config::Config()
......@@ -29,17 +30,17 @@ Config::Config()
//nothing is even put in the property map, the user will have to fill it by himself
}
Config::Config(const std::string& filename_in)
Config::Config(const std::string& i_filename)
{
addFile(filename_in);
addFile(i_filename);
}
//Populating the property map
void Config::addFile(const std::string& filename_in)
void Config::addFile(const std::string& i_filename)
{
sourcename = filename_in;
parseFile(filename_in);
sourcename = i_filename;
parseFile(i_filename);
}
void Config::addCmdLine(const std::string& cmd_line)
......@@ -50,15 +51,15 @@ void Config::addCmdLine(const std::string& cmd_line)
void Config::addKey(const std::string& key, const std::string& value)
{
std::string section="GENERAL";
std::string section=defaultSection;
addKey(key, section, value);
}
void Config::addKey(const std::string& key, const std::string& section, const std::string& value)
void Config::addKey(std::string key, std::string section, const std::string& value)
{
std::string _section = section;
IOUtils::toUpper(_section);
properties[_section + "::" + key] = value;
IOUtils::toUpper(key);
IOUtils::toUpper(section);
properties[section + "::" + key] = value;
}
std::ostream& operator<<(std::ostream &os, const Config& cfg)
......@@ -83,7 +84,7 @@ void Config::parseFile(const std::string& filename)
{
std::ifstream fin; //Input file streams
unsigned int linenr = 0;
std::string line="", section="GENERAL";
std::string line="", section=defaultSection;
if (!IOUtils::validFileName(filename)) {
throw InvalidFileNameException(filename,AT);
......@@ -139,7 +140,7 @@ void Config::parseLine(const unsigned int& linenr, std::string& line, std::strin
}
//At this point line can only be a key value pair
if (!IOUtils::readKeyValuePair(line, "=", properties, section+"::")){
if (!IOUtils::readKeyValuePair(line, "=", properties, section+"::", true)){
tmp << linenr;
throw InvalidFormatException("Error reading key value pair in " + sourcename + " line:" + tmp.str(), AT);
}
......@@ -151,29 +152,29 @@ std::string Config::getSourceName()
return sourcename;
}
unsigned int Config::findKeys(std::vector<std::string>& vecResult,
const std::string keystart,
const std::string section) const
unsigned int Config::findKeys(std::vector<std::string>& vecResult, std::string keystart,
std::string section) const
{
vecResult.clear();
string _section = section;
if (_section.length() == 0) //enforce the default section if user tries to give empty section string
_section = "GENERAL";
IOUtils::toUpper(_section);
string _keystart = _section + "::" + keystart;
if (section.length() == 0) //enforce the default section if user tries to give empty section string
section = defaultSection;
IOUtils::toUpper(section);
IOUtils::toUpper(keystart);
string _keystart = section + "::" + keystart;
//Loop through keys, look for substring match - push it into vecResult
map<string,string>::const_iterator it;
for (it=properties.begin(); it != properties.end(); it++){
string tmp = (*it).first;
tmp = tmp.substr(0,_keystart.length());
tmp = tmp.substr(0, _keystart.length());
int matchcount = _keystart.compare(tmp);
if (matchcount == 0){ //perfect match
string tmp2 = it->first;
tmp2 = tmp2.substr(_section.length() + 2);
tmp2 = tmp2.substr(section.length() + 2);
vecResult.push_back(tmp2);
}
}
......
......@@ -87,19 +87,21 @@ class Config {
void addCmdLine(const std::string& cmd_line);
/**
* @brief Add a specific key/value pair to the internal key/value map object
* @brief Add a specific key/value pair to the internal key/value map object.
* key is case insensitive
* @param[in] key string representing the key to be added
* @param[in] value string representing the matching value to be added
*/
void addKey(const std::string& key, const std::string& value);
/**
* @brief Add a specific key/value pair to the internal key/value map object
* @brief Add a specific key/value pair to the internal key/value map object.
* key and section are case insensitive
* @param[in] key string representing the key to be added
* @param[in] section std::string representing a section name; the key has to be part of this section
* @param[in] value string representing the matching value to be added
*/
void addKey(const std::string& key, const std::string& section, const std::string& value);
void addKey(std::string key, std::string section, const std::string& value);
/**
* @brief Returns the filename that the Config object was constructed with.
......@@ -146,9 +148,11 @@ class Config {
const unsigned int& options=0) const {
try {
vecT.clear();
std::string _section = section;
std::string _key(key);
std::string _section(section);
IOUtils::toUpper(_key);
IOUtils::toUpper(_section);
IOUtils::getValueForKey<T>(properties, _section + "::" + key, vecT);
IOUtils::getValueForKey<T>(properties, _section + "::" + _key, vecT);
} catch(std::exception& e){
if (options != Config::nothrow) {
std::stringstream ss;
......@@ -179,9 +183,11 @@ class Config {
T& t,
const unsigned int& options=0) const {
try {
std::string _section = section;
std::string _key(key);
std::string _section(section);
IOUtils::toUpper(_key);
IOUtils::toUpper(_section);
IOUtils::getValueForKey<T>(properties, _section + "::" + key, t);
IOUtils::getValueForKey<T>(properties, _section + "::" + _key, t);
} catch(std::exception& e){
if (options != Config::nothrow) {
std::stringstream ss;
......@@ -205,9 +211,10 @@ class Config {
* @endcode
*/
unsigned int findKeys(std::vector<std::string>& vecResult,
const std::string keystart, const std::string section="GENERAL") const;
std::string keystart, std::string section="GENERAL") const;
static const unsigned int nothrow;
static const std::string defaultSection;
private:
void parseCmdLine(const std::string& cmd_line);
......
......@@ -82,7 +82,7 @@ void IOUtils::toUpper(std::string& str){
}
bool IOUtils::readKeyValuePair(const std::string& in_line, const std::string& delimiter,
std::map<std::string,std::string>& out_map, const std::string& keyprefix)
std::map<std::string,std::string>& out_map, const std::string& keyprefix, const bool& setToUpperCase)
{
//size_t pos = in_line.find(delimiter); //first occurence of '='
......@@ -106,6 +106,10 @@ bool IOUtils::readKeyValuePair(const std::string& in_line, const std::string& de
return false;
}
if (setToUpperCase)
IOUtils::toUpper(key);
out_map[keyprefix + key] = value;
} else {
return false;
......
......@@ -130,12 +130,13 @@ namespace IOUtils {
* @param delimiter (const string&) delimiter to use for the parsing
* @param out_map (map\<string,string\>&) map after parsing
* @param keyprefix this string is prefixed before the key, defaults to no prefix: ""
* @param setToUpperCase If set to true the key will be put into upper case (for case insensitivity)
* @return (bool) true when line is empty
*/
bool readKeyValuePair(const std::string& in_line,
const std::string& delimiter,
std::map<std::string,std::string>& out_map,
const std::string& keyprefix="");
const std::string& keyprefix="", const bool& setToUpperCase=false);
void toUpper(std::string& str);
unsigned int readLineToVec(const std::string& line_in, std::vector<std::string>& vecString);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment