WSL/SLF GitLab Repository

Commit e54bbc2c authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Add the possibility to delete keys based on string match (from the begining or...

Add the possibility to delete keys based on string match (from the begining or anywhere in the key). This is useful for example to remove all stations declared from the user in the ini file.
parent d5477d40
......@@ -77,6 +77,38 @@ void Config::deleteKey(const std::string& key, const std::string& section)
properties.erase( IOUtils::strToUpper(section) + "::" + IOUtils::strToUpper(key) );
}
void Config::deleteKeys(const std::string& keymatch, const std::string& section, const bool& anywhere)
{
const size_t section_len = section.length();
//Loop through keys, look for match - delete matches
if (anywhere) {
const string key_pattern = IOUtils::strToUpper(keymatch);
const string section_pattern = IOUtils::strToUpper(section);
map<string,string>::iterator it=properties.begin();
while (it != properties.end()) {
const size_t found_section = (it->first).find(section_pattern, 0);
if ( found_section!=string::npos && (it->first).find(key_pattern, section_len)!=string::npos )
properties.erase( it++ ); // advance before iterator become invalid
else //wrong section or no match
++it;
}
} else {
const string key_pattern = IOUtils::strToUpper(section) + "::" + IOUtils::strToUpper(keymatch);
map<string,string>::iterator it=properties.begin();
while (it != properties.end()) {
if ( (it->first).find(key_pattern, 0)==0 ) //match found at start
properties.erase( it++ ); // advance before iterator become invalid
else
++it;
}
}
}
bool Config::keyExists(const std::string& key, const std::string& section) const
{
const string full_key = IOUtils::strToUpper(section) + "::" + IOUtils::strToUpper(key);
......@@ -265,10 +297,6 @@ size_t Config::findKeys(std::vector<std::string>& vecResult, const std::string&
std::string section, const bool& anywhere) const
{
vecResult.clear();
if (section.empty()) //enforce the default section if user tries to give empty section string
section = defaultSection;
const size_t section_len = section.length();
//Loop through keys, look for match - push it into vecResult
......@@ -282,8 +310,8 @@ size_t Config::findKeys(std::vector<std::string>& vecResult, const std::string&
const size_t found_pos = (it->first).find(key_pattern, section_len);
if (found_pos!=string::npos) { //found it!
const string tmp2 = (it->first).substr(section_len + 2); //from pos to the end
vecResult.push_back(tmp2);
const string key = (it->first).substr(section_len + 2); //from pos to the end
vecResult.push_back(key);
}
}
} else {
......@@ -292,8 +320,8 @@ size_t Config::findKeys(std::vector<std::string>& vecResult, const std::string&
for (map<string,string>::const_iterator it=properties.begin(); it != properties.end(); ++it) {
const size_t found_pos = (it->first).find(key_pattern, 0);
if (found_pos==0) { //found it!
const string tmp2 = (it->first).substr(section_len + 2); //from pos to the end
vecResult.push_back(tmp2);
const string key = (it->first).substr(section_len + 2); //from pos to the end
vecResult.push_back(key);
}
}
}
......
......@@ -105,6 +105,18 @@ class Config {
*/
void deleteKey(const std::string& key, const std::string& section=Config::defaultSection);
/**
* @brief Delete keys matching a specific pattern from the internal map object, key/section are case insensitive
* @param[in] keymatch A string representing the beginning of a key to search for
* @param[in] section A string defining which section to search through (default: GENERAL)
* @param[in] anywhere Match substring anywhere in the key string (default=false, ie at the begining only)
* @code
* Config cfg("io.ini");
* cfg.deleteKeys("STATION", "Input");
* @endcode
*/
void deleteKeys(const std::string& keymatch, const std::string& section=Config::defaultSection, const bool& anywhere=false);
/**
* @brief Add a specific key/value pair to the internal key/value map object.
* key and section are case insensitive
......@@ -290,11 +302,11 @@ class Config {
* @param[in] anywhere Match substring anywhere in the key string (default=false, ie at the begining only)
* @code
* vector<string> myVec;
* size_t nrOfMatches = findKeys(myVec, "TA::", "Filters");
* size_t nrOfMatches = cfg.findKeys(myVec, "TA::", "Filters");
* @endcode
*/
size_t findKeys(std::vector<std::string>& vecResult,
const std::string& keymatch, std::string section="GENERAL", const bool& anywhere=false) const;
const std::string& keymatch, std::string section=Config::defaultSection, const bool& anywhere=false) const;
static const std::string defaultSection;
......
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