From a0dda8c8b25e61cdab7e6bb8ae62d97f538a2052 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 21 May 2015 20:50:22 +0200 Subject: Make C++11 fix. --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 88c72e6..282062d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -40,7 +40,7 @@ int main(int argc, char* argv[]) runner.addTest( suite ); std::ofstream myfile; - myfile.open("result_"OUTPUT".xml"); + myfile.open("result_" OUTPUT ".xml"); runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), myfile)); // Run the tests. -- cgit v1.2.3 From 16b59fe4f96d35cf2468365ae185fba389b23020 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 21 May 2015 20:51:21 +0200 Subject: Force value of 'initialized' before construction. --- plugingui/plugingui.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc index 60a75fc..170f4f9 100644 --- a/plugingui/plugingui.cc +++ b/plugingui/plugingui.cc @@ -169,10 +169,10 @@ void closeClick(void *ptr) */ PluginGUI::PluginGUI() - : MessageReceiver(MSGRCV_UI), sem("plugingui") + : MessageReceiver(MSGRCV_UI) + , initialised(false) + , sem("plugingui") { - initialised = false; - windowClosedHandler = NULL; changeMidimapHandler = NULL; -- cgit v1.2.3 From 560c26d33c76fee45b04e92ffd36ce885c357db6 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 21 May 2015 20:52:08 +0200 Subject: New configfile parser with unit test. --- src/configfile.cc | 137 +++++++++++++++++++++++++++++++++++---- src/configfile.h | 2 +- test/Makefile.am | 11 +++- test/configtest.cc | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+), 13 deletions(-) create mode 100644 test/configtest.cc diff --git a/src/configfile.cc b/src/configfile.cc index bb7155f..e659a51 100644 --- a/src/configfile.cc +++ b/src/configfile.cc @@ -46,12 +46,12 @@ #ifdef WIN32 #define SEP "\\" - #define CONFIGDIRNAME ".drumgizmo" #else #define SEP "/" - #define CONFIGDIRNAME ".drumgizmo" #endif +#define CONFIGDIRNAME ".drumgizmo" + /** * Return the path containing the config files. */ @@ -89,7 +89,7 @@ static bool createConfigPath() #else if(mkdir(configpath.c_str(), 0755) < 0) { #endif - DEBUG(pluginconfig, "Could not create config directory\n"); + DEBUG(configfile, "Could not create config directory\n"); } return false; @@ -110,7 +110,7 @@ ConfigFile::~ConfigFile() void ConfigFile::load() { - DEBUG(pluginconfig, "Loading config file...\n"); + DEBUG(configfile, "Loading config file...\n"); if(!open("r")) return; values.clear(); @@ -125,14 +125,129 @@ void ConfigFile::load() line = line.substr(0, line.size() - 1); // strip ending newline. } - std::size_t colon = line.find(':'); + std::string key; + std::string value; + enum { + before_key, + in_key, + after_key, + before_value, + in_value, + in_value_single_quoted, + in_value_double_quoted, + after_value, + } state = before_key; + + for(std::size_t p = 0; p < line.size(); ++p) { + switch(state) { + case before_key: + if(line[p] == '#') { + // Comment: Ignore line. + p = line.size(); + continue; + } + if(std::isspace(line[p])) { + continue; + } + key += line[p]; + state = in_key; + break; + + case in_key: + if(std::isspace(line[p])) { + state = after_key; + continue; + } + if(line[p] == ':' || line[p] == '=') { + state = before_value; + continue; + } + key += line[p]; + break; + + case after_key: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == ':' || line[p] == '=') { + state = before_value; + continue; + } + // ERROR: Bad symbol, expecting only whitespace or key/value seperator + break; + + case before_value: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == '\'') { + state = in_value_single_quoted; + continue; + } + if(line[p] == '"') { + state = in_value_double_quoted; + continue; + } + value += line[p]; + state = in_value; + break; + + case in_value: + if(std::isspace(line[p])) { + state = after_value; + continue; + } + if(line[p] == '#') { + // Comment: Ignore the rest of the line. + p = line.size(); + state = after_value; + continue; + } + value += line[p]; + break; + + case in_value_single_quoted: + if(line[p] == '\'') { + state = after_value; + continue; + } + value += line[p]; + break; + + case in_value_double_quoted: + if(line[p] == '"') { + state = after_value; + continue; + } + value += line[p]; + break; + + case after_value: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == '#') { + // Comment: Ignore the rest of the line. + p = line.size(); + continue; + } + // ERROR: Bad symbol, expecting only whitespace or key/value seperator + break; + } + } - if(colon == std::string::npos) break; // malformed line + if(state == before_key) { + // Line did not contain any data (empty or comment) + continue; + } - std::string key = line.substr(0, colon); - std::string value = line.substr(colon + 1); + // If state == in_value_XXX_quoted here, the string was not terminated. + if(state != after_value && state != in_value) { + // ERROR: Malformed line. + break; + } - printf("key['%s'] value['%s']\n", key.c_str(), value.c_str()); + DEBUG(configfile, "key['%s'] value['%s']\n", key.c_str(), value.c_str()); if(key != "") { values[key] = value; @@ -144,7 +259,7 @@ void ConfigFile::load() void ConfigFile::save() { - DEBUG(pluginconfig, "Saving configuration...\n"); + DEBUG(configfile, "Saving configuration...\n"); createConfigPath(); @@ -182,7 +297,7 @@ bool ConfigFile::open(std::string mode) configfile += SEP; configfile += filename; - DEBUG(pluginconfig, "Opening config file '%s'\n", configfile.c_str()); + DEBUG(configfile, "Opening config file '%s'\n", configfile.c_str()); fp = fopen(configfile.c_str(), mode.c_str()); if(!fp) return false; diff --git a/src/configfile.h b/src/configfile.h index 21a6b88..a3fd588 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -46,7 +46,7 @@ protected: std::map values; std::string filename; - bool open(std::string mode); + virtual bool open(std::string mode); void close(); std::string readLine(); diff --git a/test/Makefile.am b/test/Makefile.am index 5aaf33f..90373e1 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,7 @@ # Rules for the test code (use `make check` to execute) include $(top_srcdir)/src/Makefile.am.drumgizmo -TESTS = engine gui resampler lv2 +TESTS = engine gui resampler lv2 configfile check_PROGRAMS = $(TESTS) @@ -40,3 +40,12 @@ lv2_SOURCES = \ test.cc \ lv2_test_host.cc \ lv2.cc + +configfile_CXXFLAGS = -DOUTPUT=\"configfile\" $(CPPUNIT_CFLAGS) \ + -I$(top_srcdir)/hugin +configfile_LDFLAGS = $(CPPUNIT_LIBS) +configfile_SOURCES = \ + $(top_srcdir)/src/configfile.cc \ + $(top_srcdir)/hugin/hugin.c \ + test.cc \ + configtest.cc diff --git a/test/configtest.cc b/test/configtest.cc new file mode 100644 index 0000000..c05299f --- /dev/null +++ b/test/configtest.cc @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * configtest.cc + * + * Thu May 14 20:58:29 CEST 2015 + * Copyright 2015 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of DrumGizmo. + * + * DrumGizmo is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * DrumGizmo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with DrumGizmo; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include + +#include +#include + +#include "../src/configfile.h" + +class TestConfigFile : public ConfigFile { +public: + TestConfigFile() : ConfigFile("") {} + +protected: + // Overload the built-in open method to use local file instead of homedir. + virtual bool open(std::string mode) override + { + fp = fopen("test.conf", mode.c_str()); + return fp != nullptr; + } +}; + +class test_configtest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(test_configtest); + CPPUNIT_TEST(loading_no_newline); + CPPUNIT_TEST(loading_equal_sign); + CPPUNIT_TEST(loading_newline); + CPPUNIT_TEST(loading_padding_space); + CPPUNIT_TEST(loading_padding_space_newline); + CPPUNIT_TEST(loading_padding_tab); + CPPUNIT_TEST(loading_padding_tab_newline); + CPPUNIT_TEST(loading_comment); + CPPUNIT_TEST(loading_inline_comment); + CPPUNIT_TEST(loading_single_quoted_string); + CPPUNIT_TEST(loading_double_quoted_string); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp() + { + } + + void tearDown() + { + unlink("test.conf"); + } + + void writeFile(const char* str) + { + FILE* fp = fopen("test.conf", "w"); + fprintf(fp, "%s", str); + fclose(fp); + } + + void loading_no_newline() + { + writeFile("a:b"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_equal_sign() + { + writeFile(" a =\tb\t\n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_newline() + { + writeFile("a:b\n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_padding_space() + { + writeFile(" a : b "); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_padding_tab() + { + writeFile("\ta\t:\tb\t"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_padding_space_newline() + { + writeFile(" a : b \n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_padding_tab_newline() + { + writeFile("\ta\t:\tb\t\n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_comment() + { + writeFile("# comment\na:b\n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_inline_comment() + { + writeFile("a:b #comment\n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); + } + + void loading_single_quoted_string() + { + writeFile("a: '#\"b\" ' \n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("#\"b\" "), cf.getValue("a")); + } + + void loading_double_quoted_string() + { + writeFile("a: \"#'b' \" \n"); + + TestConfigFile cf; + cf.load(); + CPPUNIT_ASSERT_EQUAL(std::string("#'b' "), cf.getValue("a")); + } +}; + +// Registers the fixture into the 'registry' +CPPUNIT_TEST_SUITE_REGISTRATION(test_configtest); + + -- cgit v1.2.3 From 28776a862e6a00f4209c95531592c1abe08448f2 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 21 May 2015 20:56:56 +0200 Subject: override not supported by our build servers, so better not use it yet. --- test/configtest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/configtest.cc b/test/configtest.cc index c05299f..e12123f 100644 --- a/test/configtest.cc +++ b/test/configtest.cc @@ -37,7 +37,7 @@ public: protected: // Overload the built-in open method to use local file instead of homedir. - virtual bool open(std::string mode) override + virtual bool open(std::string mode) { fp = fopen("test.conf", mode.c_str()); return fp != nullptr; -- cgit v1.2.3 From d7e31edc42e075567059c2ddcda6bb19b4c7bd69 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 21 May 2015 21:00:41 +0200 Subject: nullptr not supported by our build servers, so better not use it yet. --- test/configtest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/configtest.cc b/test/configtest.cc index e12123f..6adcd39 100644 --- a/test/configtest.cc +++ b/test/configtest.cc @@ -40,7 +40,7 @@ protected: virtual bool open(std::string mode) { fp = fopen("test.conf", mode.c_str()); - return fp != nullptr; + return fp != NULL; } }; -- cgit v1.2.3 From 6e3b53cd1b0fc6dcd721fac3beb699cb722e1e23 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 22 May 2015 08:30:24 +0200 Subject: Add error handling in ConfigFile parser. --- src/configfile.cc | 287 ++++++++++++++++++++++++++++------------------------- src/configfile.h | 5 +- test/configtest.cc | 58 +++++++++-- 3 files changed, 202 insertions(+), 148 deletions(-) diff --git a/src/configfile.cc b/src/configfile.cc index e659a51..6b0d14f 100644 --- a/src/configfile.cc +++ b/src/configfile.cc @@ -108,10 +108,12 @@ ConfigFile::~ConfigFile() { } -void ConfigFile::load() +bool ConfigFile::load() { DEBUG(configfile, "Loading config file...\n"); - if(!open("r")) return; + if(!open("r")) { + return false; + } values.clear(); @@ -120,150 +122,26 @@ void ConfigFile::load() line = readLine(); if(line == "") break; - - if(line[line.size() - 1] == '\n') { - line = line.substr(0, line.size() - 1); // strip ending newline. - } - - std::string key; - std::string value; - enum { - before_key, - in_key, - after_key, - before_value, - in_value, - in_value_single_quoted, - in_value_double_quoted, - after_value, - } state = before_key; - - for(std::size_t p = 0; p < line.size(); ++p) { - switch(state) { - case before_key: - if(line[p] == '#') { - // Comment: Ignore line. - p = line.size(); - continue; - } - if(std::isspace(line[p])) { - continue; - } - key += line[p]; - state = in_key; - break; - - case in_key: - if(std::isspace(line[p])) { - state = after_key; - continue; - } - if(line[p] == ':' || line[p] == '=') { - state = before_value; - continue; - } - key += line[p]; - break; - - case after_key: - if(std::isspace(line[p])) { - continue; - } - if(line[p] == ':' || line[p] == '=') { - state = before_value; - continue; - } - // ERROR: Bad symbol, expecting only whitespace or key/value seperator - break; - - case before_value: - if(std::isspace(line[p])) { - continue; - } - if(line[p] == '\'') { - state = in_value_single_quoted; - continue; - } - if(line[p] == '"') { - state = in_value_double_quoted; - continue; - } - value += line[p]; - state = in_value; - break; - - case in_value: - if(std::isspace(line[p])) { - state = after_value; - continue; - } - if(line[p] == '#') { - // Comment: Ignore the rest of the line. - p = line.size(); - state = after_value; - continue; - } - value += line[p]; - break; - - case in_value_single_quoted: - if(line[p] == '\'') { - state = after_value; - continue; - } - value += line[p]; - break; - - case in_value_double_quoted: - if(line[p] == '"') { - state = after_value; - continue; - } - value += line[p]; - break; - - case after_value: - if(std::isspace(line[p])) { - continue; - } - if(line[p] == '#') { - // Comment: Ignore the rest of the line. - p = line.size(); - continue; - } - // ERROR: Bad symbol, expecting only whitespace or key/value seperator - break; - } - } - - if(state == before_key) { - // Line did not contain any data (empty or comment) - continue; - } - - // If state == in_value_XXX_quoted here, the string was not terminated. - if(state != after_value && state != in_value) { - // ERROR: Malformed line. - break; - } - - DEBUG(configfile, "key['%s'] value['%s']\n", key.c_str(), value.c_str()); - - if(key != "") { - values[key] = value; + + if(!parseLine(line)) { + return false; } } close(); + + return true; } -void ConfigFile::save() +bool ConfigFile::save() { DEBUG(configfile, "Saving configuration...\n"); createConfigPath(); - if(!open("w")) return; + if(!open("w")) { + return false; + } std::map::iterator v = values.begin(); for(; v != values.end(); ++v) { @@ -271,6 +149,8 @@ void ConfigFile::save() } close(); + + return true; } std::string ConfigFile::getValue(const std::string& key) @@ -328,3 +208,140 @@ std::string ConfigFile::readLine() return line; } + +bool ConfigFile::parseLine(const std::string& line) +{ + std::string key; + std::string value; + enum { + before_key, + in_key, + after_key, + before_value, + in_value, + in_value_single_quoted, + in_value_double_quoted, + after_value, + } state = before_key; + + for(std::size_t p = 0; p < line.size(); ++p) { + switch(state) { + case before_key: + if(line[p] == '#') { + // Comment: Ignore line. + p = line.size(); + continue; + } + if(std::isspace(line[p])) { + continue; + } + key += line[p]; + state = in_key; + break; + + case in_key: + if(std::isspace(line[p])) { + state = after_key; + continue; + } + if(line[p] == ':' || line[p] == '=') { + state = before_value; + continue; + } + key += line[p]; + break; + + case after_key: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == ':' || line[p] == '=') { + state = before_value; + continue; + } + ERR(configfile, "Bad symbol." + " Expecting only whitespace or key/value seperator: '%s'", + line.c_str()); + return false; + + case before_value: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == '\'') { + state = in_value_single_quoted; + continue; + } + if(line[p] == '"') { + state = in_value_double_quoted; + continue; + } + value += line[p]; + state = in_value; + break; + + case in_value: + if(std::isspace(line[p])) { + state = after_value; + continue; + } + if(line[p] == '#') { + // Comment: Ignore the rest of the line. + p = line.size(); + state = after_value; + continue; + } + value += line[p]; + break; + + case in_value_single_quoted: + if(line[p] == '\'') { + state = after_value; + continue; + } + value += line[p]; + break; + + case in_value_double_quoted: + if(line[p] == '"') { + state = after_value; + continue; + } + value += line[p]; + break; + + case after_value: + if(std::isspace(line[p])) { + continue; + } + if(line[p] == '#') { + // Comment: Ignore the rest of the line. + p = line.size(); + continue; + } + ERR(configfile, "Bad symbol." + " Expecting only whitespace or key/value seperator: '%s'", + line.c_str()); + return false; + } + } + + if(state == before_key) { + // Line did not contain any data (empty or comment) + return true; + } + + // If state == in_value_XXX_quoted here, the string was not terminated. + if(state != after_value && state != in_value) { + ERR(configfile,"Malformed line: '%s'", line.c_str()); + return false; + } + + DEBUG(configfile, "key['%s'] value['%s']\n", key.c_str(), value.c_str()); + + if(key != "") { + values[key] = value; + } + + return true; +} diff --git a/src/configfile.h b/src/configfile.h index a3fd588..a6c50bd 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -36,8 +36,8 @@ public: ConfigFile(std::string filename); virtual ~ConfigFile(); - virtual void load(); - virtual void save(); + virtual bool load(); + virtual bool save(); virtual std::string getValue(const std::string& key); virtual void setValue(const std::string& key, const std::string& value); @@ -49,6 +49,7 @@ protected: virtual bool open(std::string mode); void close(); std::string readLine(); + bool parseLine(const std::string& line); FILE* fp; }; diff --git a/test/configtest.cc b/test/configtest.cc index 6adcd39..24a7048 100644 --- a/test/configtest.cc +++ b/test/configtest.cc @@ -58,6 +58,10 @@ class test_configtest : public CppUnit::TestFixture CPPUNIT_TEST(loading_inline_comment); CPPUNIT_TEST(loading_single_quoted_string); CPPUNIT_TEST(loading_double_quoted_string); + CPPUNIT_TEST(loading_error_no_key); + CPPUNIT_TEST(loading_error_no_value); + CPPUNIT_TEST(loading_error_string_not_terminated_single); + CPPUNIT_TEST(loading_error_string_not_terminated_double); CPPUNIT_TEST_SUITE_END(); public: @@ -82,7 +86,7 @@ public: writeFile("a:b"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -91,7 +95,7 @@ public: writeFile(" a =\tb\t\n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -100,7 +104,7 @@ public: writeFile("a:b\n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -109,7 +113,7 @@ public: writeFile(" a : b "); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -118,7 +122,7 @@ public: writeFile("\ta\t:\tb\t"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -127,7 +131,7 @@ public: writeFile(" a : b \n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -136,7 +140,7 @@ public: writeFile("\ta\t:\tb\t\n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -145,7 +149,7 @@ public: writeFile("# comment\na:b\n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -154,7 +158,7 @@ public: writeFile("a:b #comment\n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); } @@ -163,7 +167,7 @@ public: writeFile("a: '#\"b\" ' \n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("#\"b\" "), cf.getValue("a")); } @@ -172,9 +176,41 @@ public: writeFile("a: \"#'b' \" \n"); TestConfigFile cf; - cf.load(); + CPPUNIT_ASSERT_EQUAL(true, cf.load()); CPPUNIT_ASSERT_EQUAL(std::string("#'b' "), cf.getValue("a")); } + + void loading_error_no_key() + { + writeFile(":foo"); + + TestConfigFile cf; + CPPUNIT_ASSERT_EQUAL(false, cf.load()); + } + + void loading_error_no_value() + { + writeFile("key"); + + TestConfigFile cf; + CPPUNIT_ASSERT_EQUAL(false, cf.load()); + } + + void loading_error_string_not_terminated_single() + { + writeFile("a:'b\n"); + + TestConfigFile cf; + CPPUNIT_ASSERT_EQUAL(false, cf.load()); + } + + void loading_error_string_not_terminated_double() + { + writeFile("a:\"b\n"); + + TestConfigFile cf; + CPPUNIT_ASSERT_EQUAL(false, cf.load()); + } }; // Registers the fixture into the 'registry' -- cgit v1.2.3 From 2699cb7d324ffb1d2adc62e1f62434397aa39e2d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 22 May 2015 08:35:53 +0200 Subject: Use new load return value from ConfigFile. --- plugingui/pluginconfig.cc | 12 ++++++++---- plugingui/pluginconfig.h | 4 ++-- src/audioinputenginemidi.cc | 10 +++++++--- src/drumkitparser.cc | 10 ++++++---- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/plugingui/pluginconfig.cc b/plugingui/pluginconfig.cc index 0beef6e..d4c4477 100644 --- a/plugingui/pluginconfig.cc +++ b/plugingui/pluginconfig.cc @@ -39,21 +39,25 @@ Config::~Config() { } -void Config::load() +bool Config::load() { lastkit.clear(); lastmidimap.clear(); - ConfigFile::load(); + if(!ConfigFile::load()) { + return false; + } lastkit = getValue("lastkit"); lastmidimap = getValue("lastmidimap"); + + return true; } -void Config::save() +bool Config::save() { setValue("lastkit", lastkit); setValue("lastmidimap", lastmidimap); - ConfigFile::save(); + return ConfigFile::save(); } diff --git a/plugingui/pluginconfig.h b/plugingui/pluginconfig.h index 29d2ef5..9bef1f0 100644 --- a/plugingui/pluginconfig.h +++ b/plugingui/pluginconfig.h @@ -34,8 +34,8 @@ public: Config(); ~Config(); - void load(); - void save(); + bool load(); + bool save(); std::string lastkit; std::string lastmidimap; diff --git a/src/audioinputenginemidi.cc b/src/audioinputenginemidi.cc index 5494462..7c1e13d 100644 --- a/src/audioinputenginemidi.cc +++ b/src/audioinputenginemidi.cc @@ -35,15 +35,19 @@ AudioInputEngineMidi::AudioInputEngineMidi() : refs(REFSFILE) { - refs.load(); is_valid = false; } bool AudioInputEngineMidi::loadMidiMap(std::string _f, Instruments &instruments) { std::string f = _f; - if(_f.size() > 1 && _f[0] == '@') { - f = refs.getValue(_f.substr(1)); + + if(refs.load()) { + if(_f.size() > 1 && _f[0] == '@') { + f = refs.getValue(_f.substr(1)); + } + } else { + ERR(drumkitparser, "Error reading refs.conf"); } file = ""; diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index 00232b2..f7bfe40 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -38,12 +38,14 @@ DrumKitParser::DrumKitParser(const std::string &file, DrumKit &k) : kit(k) , refs(REFSFILE) { - refs.load(); - std::string kitfile = file; - if(file.size() > 1 && file[0] == '@') { - kitfile = refs.getValue(file.substr(1)); + if(refs.load()) { + if(file.size() > 1 && file[0] == '@') { + kitfile = refs.getValue(file.substr(1)); + } + } else { + ERR(drumkitparser, "Error reading refs.conf"); } // instr = NULL; -- cgit v1.2.3