summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2019-02-01 17:57:58 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2019-02-01 17:57:58 +0100
commit5ca1c40c92f074315bbdfe328c00484ff18146f2 (patch)
tree3d801d2ec86d9b155326289bd4e557e8b956a61d
parentf3fa39437c711f75949fbe0056007e343a428062 (diff)
Fix configfile parsing of empty value lines and creation of missing configuration directory.
-rw-r--r--src/configfile.cc9
-rw-r--r--test/configtest.cc10
2 files changed, 15 insertions, 4 deletions
diff --git a/src/configfile.cc b/src/configfile.cc
index 731d3aa..54fb4d1 100644
--- a/src/configfile.cc
+++ b/src/configfile.cc
@@ -84,10 +84,10 @@ static bool createConfigPath()
std::string configpath = getConfigPath();
struct stat st;
- if(stat(configpath.c_str(), &st) == 0)
+ if(stat(configpath.c_str(), &st) != 0)
{
DEBUG(configfile, "No configuration exists, creating directory '%s'\n",
- configpath.c_str());
+ configpath.c_str());
#if DG_PLATFORM == DG_PLATFORM_WINDOWS
if(mkdir(configpath.c_str()) < 0)
{
@@ -221,7 +221,7 @@ std::string ConfigFile::readLine()
{
return "";
}
-
+
std::string line;
char buf[1024];
@@ -383,7 +383,8 @@ bool ConfigFile::parseLine(const std::string& line)
}
// If state == in_value_XXX_quoted here, the string was not terminated.
- if(state != after_value && state != in_value)
+ // If state == before_value it means that the value is empty.
+ if(state != after_value && state != in_value && state != before_value)
{
ERR(configfile, "Malformed line: '%s'", line.c_str());
return false;
diff --git a/test/configtest.cc b/test/configtest.cc
index a4d5228..85a59ca 100644
--- a/test/configtest.cc
+++ b/test/configtest.cc
@@ -69,6 +69,7 @@ public:
DGUNIT_TEST(test_configtest::loading_error_no_value);
DGUNIT_TEST(test_configtest::loading_error_string_not_terminated_single);
DGUNIT_TEST(test_configtest::loading_error_string_not_terminated_double);
+ DGUNIT_TEST(test_configtest::empty_value);
}
void teardown() override
@@ -213,6 +214,15 @@ public:
TestConfigFile cf;
DGUNIT_ASSERT_EQUAL(false, cf.load());
}
+
+ void empty_value()
+ {
+ writeFile("a:\n");
+
+ TestConfigFile cf;
+ DGUNIT_ASSERT_EQUAL(true, cf.load());
+ DGUNIT_ASSERT_EQUAL(std::string(""), cf.getValue("a"));
+ }
};
// Registers the fixture into the 'registry'