diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/audioinputenginemidi.cc | 10 | ||||
-rw-r--r-- | src/configfile.cc | 184 | ||||
-rw-r--r-- | src/configfile.h | 7 | ||||
-rw-r--r-- | src/drumkitparser.cc | 10 |
4 files changed, 175 insertions, 36 deletions
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/configfile.cc b/src/configfile.cc index bb7155f..6b0d14f 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; @@ -108,10 +108,12 @@ ConfigFile::~ConfigFile() { } -void ConfigFile::load() +bool ConfigFile::load() { - DEBUG(pluginconfig, "Loading config file...\n"); - if(!open("r")) return; + DEBUG(configfile, "Loading config file...\n"); + if(!open("r")) { + return false; + } values.clear(); @@ -120,35 +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::size_t colon = line.find(':'); - - if(colon == std::string::npos) break; // malformed line - - std::string key = line.substr(0, colon); - std::string value = line.substr(colon + 1); - - printf("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(pluginconfig, "Saving configuration...\n"); + DEBUG(configfile, "Saving configuration...\n"); createConfigPath(); - if(!open("w")) return; + if(!open("w")) { + return false; + } std::map<std::string, std::string>::iterator v = values.begin(); for(; v != values.end(); ++v) { @@ -156,6 +149,8 @@ void ConfigFile::save() } close(); + + return true; } std::string ConfigFile::getValue(const std::string& key) @@ -182,7 +177,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; @@ -213,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 21a6b88..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); @@ -46,9 +46,10 @@ protected: std::map<std::string, std::string> values; std::string filename; - bool open(std::string mode); + virtual bool open(std::string mode); void close(); std::string readLine(); + bool parseLine(const std::string& line); FILE* fp; }; 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; |