diff options
author | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-29 11:32:00 +0200 |
---|---|---|
committer | André Nusser <andre.nusser@googlemail.com> | 2016-03-31 17:35:47 +0200 |
commit | cc292a3ee15253d09cb5cb0a24ea143a4423be13 (patch) | |
tree | 4c2195b308baafcf6dfaf4bb49d8ffba8a011fcc | |
parent | 11fee241287c3d26a7dc7a24fe68270a90ee0e73 (diff) |
Made API of class ConfigFile more consistent
-rw-r--r-- | src/configfile.cc | 491 | ||||
-rw-r--r-- | src/configfile.h | 34 |
2 files changed, 287 insertions, 238 deletions
diff --git a/src/configfile.cc b/src/configfile.cc index fc1611e..7fe095a 100644 --- a/src/configfile.cc +++ b/src/configfile.cc @@ -44,11 +44,11 @@ #endif #include <hugin.hpp> - + #ifdef WIN32 - #define SEP "\\" +#define SEP "\\" #else - #define SEP "/" +#define SEP "/" #endif #define CONFIGDIRNAME ".drumgizmo" @@ -56,22 +56,23 @@ /** * Return the path containing the config files. */ -static std::string configPath() +static std::string getConfigPath() { #ifdef WIN32 - std::string configpath; - TCHAR szPath[256]; - if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, - NULL, 0, szPath))) { - configpath = szPath; - } + std::string configpath; + TCHAR szPath[256]; + if(SUCCEEDED(SHGetFolderPath( + NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, szPath))) + { + configpath = szPath; + } #else - std::string configpath = getenv("HOME"); + std::string configpath = getenv("HOME"); #endif - configpath += SEP; - configpath += CONFIGDIRNAME; + configpath += SEP; + configpath += CONFIGDIRNAME; - return configpath; + return configpath; } /** @@ -79,270 +80,320 @@ static std::string configPath() */ static bool createConfigPath() { - std::string configpath = configPath(); + std::string configpath = getConfigPath(); - struct stat st; - if(stat(configpath.c_str(), &st) == 0) { - DEBUG(configfile, "No configuration exists, creating directory '%s'\n", - configpath.c_str()); + struct stat st; + if(stat(configpath.c_str(), &st) == 0) + { + DEBUG(configfile, "No configuration exists, creating directory '%s'\n", + configpath.c_str()); #ifdef WIN32 - if(mkdir(configpath.c_str()) < 0) { + if(mkdir(configpath.c_str()) < 0) + { #else - if(mkdir(configpath.c_str(), 0755) < 0) { + if(mkdir(configpath.c_str(), 0755) < 0) + { #endif - DEBUG(configfile, "Could not create config directory\n"); - } + DEBUG(configfile, "Could not create config directory\n"); + } - return false; - } + return false; + } - return true; + return true; } -ConfigFile::ConfigFile(std::string filename) - : filename(filename) - , fp(NULL) +ConfigFile::ConfigFile(std::string const& filename) + : filename(filename) + , fp(nullptr) { } ConfigFile::~ConfigFile() { + if (fp != nullptr) + { + DEBUG(configfile, "File has not been closed by the client...\n"; + } } bool ConfigFile::load() { - DEBUG(configfile, "Loading config file...\n"); - if(!open("r")) { - return false; - } + DEBUG(configfile, "Loading config file...\n"); + if(!open("r")) + { + return false; + } + + values.clear(); - values.clear(); + std::string line; + while(true) + { + line = readLine(); - std::string line; - while(true) { - line = readLine(); + if(line == "") + break; - if(line == "") break; - - if(!parseLine(line)) { - return false; - } - } + if(!parseLine(line)) + { + return false; + } + } - close(); + close(); - return true; + return true; } bool ConfigFile::save() { - DEBUG(configfile, "Saving configuration...\n"); + DEBUG(configfile, "Saving configuration...\n"); - createConfigPath(); + createConfigPath(); - if(!open("w")) { - return false; - } + if(!open("w")) + { + return false; + } - std::map<std::string, std::string>::iterator v = values.begin(); - for(; v != values.end(); ++v) { - fprintf(fp, "%s:%s\n", v->first.c_str(), v->second.c_str()); - } + std::map<std::string, std::string>::iterator v = values.begin(); + for(; v != values.end(); ++v) + { + fprintf(fp, "%s:%s\n", v->first.c_str(), v->second.c_str()); + } - close(); + close(); - return true; + return true; } -std::string ConfigFile::getValue(const std::string& key) +std::string ConfigFile::getValue(const std::string& key) const { - if(values.find(key) != values.end()) { - return values[key]; - } + auto i = values.find(key); + if (i != values.end()) + { + return i->second; + } - return ""; + return ""; } void ConfigFile::setValue(const std::string& key, const std::string& value) { - values[key] = value; + values[key] = value; } bool ConfigFile::open(std::string mode) { - if(fp) close(); + if(fp) + { + close(); + } - std::string configpath = configPath(); + std::string configpath = getConfigPath(); - std::string configfile = configpath; - configfile += SEP; - configfile += filename; + std::string configfile = configpath; + configfile += SEP; + configfile += filename; - DEBUG(configfile, "Opening config file '%s'\n", configfile.c_str()); - fp = fopen(configfile.c_str(), mode.c_str()); + DEBUG(configfile, "Opening config file '%s'\n", configfile.c_str()); + fp = fopen(configfile.c_str(), mode.c_str()); - if(!fp) return false; + if(!fp) + { + return false; + } - return true; + return true; } void ConfigFile::close() { - fclose(fp); - fp = NULL; + fclose(fp); + fp = nullptr; } std::string ConfigFile::readLine() { - if(!fp) return ""; - - std::string line; - - char buf[1024]; - while(!feof(fp)) { - char *s = fgets(buf, sizeof(buf), fp); - if(s) { - line += buf; - if(buf[strlen(buf) - 1] == '\n') break; - } - } - - return line; + if(!fp) + { + return ""; + } + + std::string line; + + char buf[1024]; + while(!feof(fp)) + { + char* s = fgets(buf, sizeof(buf), fp); + if(s) + { + line += buf; + if(buf[strlen(buf) - 1] == '\n') + break; + } + } + + 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; + 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 3a781ec..47ae80b 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -24,34 +24,32 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_CONFIGFILE_H__ -#define __DRUMGIZMO_CONFIGFILE_H__ +#pragma once #include <string> #include <map> #include <stdio.h> -class ConfigFile { +class ConfigFile +{ public: - ConfigFile(std::string filename); - virtual ~ConfigFile(); + ConfigFile(std::string const& filename); + virtual ~ConfigFile(); - virtual bool load(); - virtual bool 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); + virtual std::string getValue(const std::string& key) const; + virtual void setValue(const std::string& key, const std::string& value); protected: - std::map<std::string, std::string> values; - std::string filename; + std::map<std::string, std::string> values; + std::string filename; - virtual bool open(std::string mode); - void close(); - std::string readLine(); - bool parseLine(const std::string& line); + virtual bool open(std::string mode); + void close(); + std::string readLine(); + bool parseLine(const std::string& line); - FILE* fp; + FILE* fp; }; - -#endif/*__DRUMGIZMO_CONFIGFILE_H__*/ |