WSL/SLF GitLab Repository

Skip to content
Snippets Groups Projects
Commit cdd370e1 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The "nothrow" option was not properly forwarded in Config, leading to...

The "nothrow" option was not properly forwarded in Config, leading to unnecessary throw/catch. The sun.cc example was set to executable.
parent f10de73d
No related branches found
No related tags found
No related merge requests found
File mode changed from 100755 to 100644
......@@ -120,12 +120,13 @@ void Config::parseFile(const std::string& filename)
throw FileAccessException(filename, AT);
}
std::string line, section=defaultSection;
std::string section=defaultSection;
const char eoln = IOUtils::getEoln(fin); //get the end of line character for the file
unsigned int linenr = 0;
try {
do {
std::string line;
getline(fin, line, eoln); //read complete line
parseLine(linenr++, line, section);
} while(!fin.eof());
......@@ -146,10 +147,10 @@ void Config::parseLine(const unsigned int& linenr, std::string& line, std::strin
if (line.empty()) //ignore empty lines
return;
stringstream tmp; //stringstream to convert the unsigned int linenr into a string
if (line[0] == '['){
const size_t endpos = line.find_last_of(']');
if ((endpos == string::npos) || (endpos < 2) || (endpos != (line.length()-1))) {
stringstream tmp;
tmp << linenr;
throw IOException("Section header corrupt in line " + tmp.str(), AT);
} else {
......@@ -161,6 +162,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+"::", true)) {
stringstream tmp;
tmp << linenr;
throw InvalidFormatException("Error reading key value pair in \"" + sourcename + "\" at line " + tmp.str(), AT);
}
......@@ -207,7 +209,7 @@ std::string Config::extract_section(std::string key) const
return defaultSection;
}
void Config::write(const std::string& filename)
void Config::write(const std::string& filename) const
{
std::ofstream fout;
......
......@@ -77,7 +77,7 @@ class Config {
* @brief Write the Config object to a file
* @param filename The filename including the path, e.g. "/tmp/test.ini"
*/
void write(const std::string& filename);
void write(const std::string& filename) const;
/**
* @brief Add the content of a file to the internal key/value map object
......@@ -183,19 +183,15 @@ class Config {
{
try {
vecT.clear();
std::string new_key(key);
std::string new_section(section);
IOUtils::toUpper(new_key);
IOUtils::toUpper(new_section);
IOUtils::getValueForKey<T>(properties, new_section + "::" + new_key, vecT);
const std::string new_key( IOUtils::strToUpper(key) );
const std::string new_section( IOUtils::strToUpper(section) );
IOUtils::getValueForKey<T>(properties, new_section + "::" + new_key, vecT, opt);
} catch(const std::exception& e){
if (opt != Config::nothrow) {
std::stringstream ss;
ss << "[E] Error for Config of " << sourcename << ": " << e.what();
throw UnknownValueException(ss.str(), AT);
}
}
}
/**
* @ brief A function that allows to retrieve a value for a key as return parameter (vectors of values too)
......@@ -243,19 +239,15 @@ class Config {
const Options& opt=Config::dothrow) const
{
try {
std::string tmp_key(key);
std::string tmp_section(section);
IOUtils::toUpper(tmp_key);
IOUtils::toUpper(tmp_section);
IOUtils::getValueForKey<T>(properties, tmp_section + "::" + tmp_key, t);
const std::string new_key( IOUtils::strToUpper(key) );
const std::string new_section( IOUtils::strToUpper(section) );
IOUtils::getValueForKey<T>(properties, new_section + "::" + new_key, t, opt);
} catch(const std::exception& e){
if (opt != Config::nothrow) {
std::stringstream ss;
ss << "[E] Error for Config of " << sourcename << ": " << e.what();
throw UnknownValueException(ss.str(), AT);
}
}
}
/**
......
......@@ -117,8 +117,8 @@ IOException::IOException(const std::string& message, const std::string& position
#endif
}
backtrace_info += "\033[0m"; //back to normal color
free(symbols);
full_output = backtrace_info + "[" + where + "] \033[31;1m" + message + "\033[0m";
free(symbols);
#else
full_output = msg;
#endif
......
......@@ -255,9 +255,8 @@ namespace IOUtils {
*/
template <class T> void getValueForKey(const std::map<std::string,std::string>& properties,
const std::string& key, T& t, const unsigned int& options=IOUtils::dothrow){
if (key.empty()) {
if (key.empty() && options!=IOUtils::nothrow)
throw InvalidArgumentException("Empty key", AT);
}
//const std::string value = (const_cast<std::map<std::string,std::string>&>(properties))[key];
//if (value == ""){} //The alternative way
......@@ -270,9 +269,9 @@ namespace IOUtils {
else
throw UnknownValueException("No value for key " + key, AT);
}
const std::string value = it->second;
const std::string& value = it->second;
if(!convertString<T>(t, value, std::dec)) {
if(!convertString<T>(t, value, std::dec) && options!=IOUtils::nothrow) {
std::cerr << "[E] When reading \"" << key << "\" = \"" << t << "\"\n";
throw ConversionFailedException(value, AT);
}
......@@ -289,7 +288,8 @@ namespace IOUtils {
template <class T> void getValueForKey(const std::map<std::string,std::string>& properties,
const std::string& key, std::vector<T>& vecT, const unsigned int& options=IOUtils::dothrow)
{
if (key.empty()) throw InvalidArgumentException("Empty key", AT);
if (key.empty() && options!=IOUtils::nothrow)
throw InvalidArgumentException("Empty key", AT);
const std::map<std::string, std::string>::const_iterator it = properties.find(key);
if (it == properties.end()) {
......@@ -299,14 +299,14 @@ namespace IOUtils {
throw UnknownValueException("No value for key " + key, AT);
}
}
const std::string value = it->second;
const std::string& value = it->second;
//split value string
std::vector<std::string> vecUnconvertedValues;
const size_t counter = readLineToVec(value, vecUnconvertedValues);
for (size_t ii=0; ii<counter; ii++){
T myvar;
if(!convertString<T>(myvar, vecUnconvertedValues.at(ii), std::dec)){
if(!convertString<T>(myvar, vecUnconvertedValues.at(ii), std::dec) && options!=IOUtils::nothrow){
std::cerr << "[E] When reading \"" << key << "\" = \"" << myvar << "\"\n";
throw ConversionFailedException(vecUnconvertedValues.at(ii), AT);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment