summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2015-05-15 11:53:17 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2015-05-15 11:53:17 +0200
commit9555c6e2734977c82023907f82a3ae82f845720a (patch)
tree75354bb1449b49639bd8171fac30d9afa12380b7
parent6a19e8f182183ad88df91d4a46e418fb4d42f9bd (diff)
New ConfigFile class for generic config file reading/writing in homedir .drumgizmo folder. Use said new class for PluginConfig class.
-rw-r--r--plugingui/Makefile.am1
-rw-r--r--plugingui/pluginconfig.cc122
-rw-r--r--plugingui/pluginconfig.h22
-rw-r--r--src/Makefile.am.drumgizmo1
-rw-r--r--src/configfile.cc215
-rw-r--r--src/configfile.h56
-rw-r--r--vst/Makefile.mingw32.in1
7 files changed, 291 insertions, 127 deletions
diff --git a/plugingui/Makefile.am b/plugingui/Makefile.am
index bdb11cb..00b0881 100644
--- a/plugingui/Makefile.am
+++ b/plugingui/Makefile.am
@@ -14,6 +14,7 @@ plugingui_CFLAGS = $(plugingui_CXXFLAGS)
plugingui_SOURCES = \
$(PLUGIN_GUI_SOURCES) \
+ $(top_srcdir)/src/configfile.cc \
$(top_srcdir)/src/thread.cc \
$(top_srcdir)/src/semaphore.cc \
$(top_srcdir)/src/mutex.cc \
diff --git a/plugingui/pluginconfig.cc b/plugingui/pluginconfig.cc
index d57d9df..0beef6e 100644
--- a/plugingui/pluginconfig.cc
+++ b/plugingui/pluginconfig.cc
@@ -26,140 +26,34 @@
*/
#include "pluginconfig.h"
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include "directory.h"
-
-#ifdef WIN32
-#include <direct.h>
-#include <windows.h>
-#include <Shlobj.h>
-#include <Shlwapi.h>
-#else
-#endif
-
#include <hugin.hpp>
#define CONFIGFILENAME "plugingui.conf"
-#ifdef WIN32
- #define SEP "\\"
- #define CONFIGDIRNAME ".drumgizmo"
-#else
- #define SEP "/"
- #define CONFIGDIRNAME ".drumgizmo"
-#endif
-
Config::Config()
+ : ConfigFile(CONFIGFILENAME)
{
-
}
Config::~Config()
{
-
-}
-
-static std::string configPath() {
- #ifdef WIN32
- std::string configpath;
- TCHAR szPath[256];
- if(SUCCEEDED(SHGetFolderPath(NULL,
- CSIDL_APPDATA | CSIDL_FLAG_CREATE,
- NULL,
- 0,
- szPath))); {
- configpath = szPath;
- }
-#else
- std::string configpath = strdup(getenv("HOME"));
-#endif
- configpath += SEP;
- configpath += CONFIGDIRNAME;
-
- return configpath;
-}
-
-static bool createConfigPath() {
- std::string configpath = configPath();
-
- if(!Directory::exists(configpath)) {
- DEBUG(pluginconfig, "No configuration exists, creating directory '%s'\n", configpath.c_str());
-#ifdef WIN32
- if( (mkdir(configpath.c_str())) < 0) {
-#else
- if( (mkdir(configpath.c_str(), 0755)) < 0) {
-#endif
- DEBUG(pluginconfig, "Could not create config directory\n");
- }
- return false;
- }
-
- return true;
-}
-
-static FILE* openConfigFile(std::string mode) {
-
- std::string configpath = configPath();
-
- FILE *fp;
- std::string configfile = configpath;
- configfile += SEP;
- configfile += CONFIGFILENAME;
-
- DEBUG(pluginconfig, "Opening config file '%s'\n", configfile.c_str());
- if(! (fp = fopen(configfile.c_str(), mode.c_str())) ) {
- return NULL;
- }
-
- return fp;
}
void Config::load()
{
- DEBUG(pluginconfig, "Loading config file...\n");
- FILE *fp = openConfigFile("r");
- if(!fp) return;
-
lastkit.clear();
lastmidimap.clear();
- char buf[4096];
- while( fgets(buf, 4096, fp) ) {
- if(!strncmp(buf, "lastkit:", 8)) {
- DEBUG(pluginconfig, "Loading last kit path\n");
- // Dont copy newline
- if(strlen(buf) > 8 + 1) {
- lastkit.append(buf+8, strlen(buf+8) - 1);
- DEBUG(pluginconfig, "\t path is %s\n", lastkit.c_str());
- }
- }
- if(!strncmp(buf, "lastmidimap:", 12)) {
- DEBUG(pluginconfig, "Loading lastmidimap path\n");
- // Dont copy newline
- if(strlen(buf) > 12+1) lastmidimap.append(buf+12, strlen(buf+12) - 1);
- DEBUG(pluginconfig, "\t path is %s\n", lastmidimap.c_str());
- }
- }
+ ConfigFile::load();
+
+ lastkit = getValue("lastkit");
+ lastmidimap = getValue("lastmidimap");
}
void Config::save()
{
- DEBUG(pluginconfig, "Saving configuration...\n");
-
- createConfigPath();
-
- FILE *fp = openConfigFile("w");
- if(!fp) return;
-
- std::string buf;
- buf.append("lastkit:" + lastkit + '\n');
- buf.append("lastmidimap:" + lastmidimap + '\n');
-
- fputs(buf.c_str(), fp);
+ setValue("lastkit", lastkit);
+ setValue("lastmidimap", lastmidimap);
- fclose(fp);
+ ConfigFile::save();
}
diff --git a/plugingui/pluginconfig.h b/plugingui/pluginconfig.h
index d0e9bcd..29d2ef5 100644
--- a/plugingui/pluginconfig.h
+++ b/plugingui/pluginconfig.h
@@ -27,22 +27,18 @@
#ifndef __DRUMGIZMO_CONFIG_H__
#define __DRUMGIZMO_CONFIG_H__
-#include <string>
-#include <list>
+#include <configfile.h>
-#define DIRECTORY_HIDDEN 1
+class Config : public ConfigFile {
+public:
+ Config();
+ ~Config();
-class Config {
+ void load();
+ void save();
- public:
- Config();
- ~Config();
-
- void load();
- void save();
-
- std::string lastkit;
- std::string lastmidimap;
+ std::string lastkit;
+ std::string lastmidimap;
};
#endif/*__DRUMGIZMO_CONFIG_H__*/
diff --git a/src/Makefile.am.drumgizmo b/src/Makefile.am.drumgizmo
index 26e2a30..1a3c857 100644
--- a/src/Makefile.am.drumgizmo
+++ b/src/Makefile.am.drumgizmo
@@ -4,6 +4,7 @@ DRUMGIZMO_SOURCES = \
$(top_srcdir)/src/channel.cc \
$(top_srcdir)/src/channelmixer.cc \
$(top_srcdir)/src/chresampler.cc \
+ $(top_srcdir)/src/configfile.cc \
$(top_srcdir)/src/configuration.cc \
$(top_srcdir)/src/configparser.cc \
$(top_srcdir)/src/drumgizmo.cc \
diff --git a/src/configfile.cc b/src/configfile.cc
new file mode 100644
index 0000000..bb7155f
--- /dev/null
+++ b/src/configfile.cc
@@ -0,0 +1,215 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * configfile.cc
+ *
+ * Thu May 14 14:51:39 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 "configfile.h"
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#ifdef WIN32
+#include <direct.h>
+#include <windows.h>
+#include <Shlobj.h>
+#include <Shlwapi.h>
+#else
+#endif
+
+#include <hugin.hpp>
+
+#ifdef WIN32
+ #define SEP "\\"
+ #define CONFIGDIRNAME ".drumgizmo"
+#else
+ #define SEP "/"
+ #define CONFIGDIRNAME ".drumgizmo"
+#endif
+
+/**
+ * Return the path containing the config files.
+ */
+static std::string configPath()
+{
+#ifdef WIN32
+ std::string configpath;
+ TCHAR szPath[256];
+ if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
+ NULL, 0, szPath))) {
+ configpath = szPath;
+ }
+#else
+ std::string configpath = strdup(getenv("HOME"));
+#endif
+ configpath += SEP;
+ configpath += CONFIGDIRNAME;
+
+ return configpath;
+}
+
+/**
+ * Calling this makes sure that the config path exists
+ */
+static bool createConfigPath()
+{
+ std::string configpath = configPath();
+
+ 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) {
+#else
+ if(mkdir(configpath.c_str(), 0755) < 0) {
+#endif
+ DEBUG(pluginconfig, "Could not create config directory\n");
+ }
+
+ return false;
+ }
+
+ return true;
+}
+
+ConfigFile::ConfigFile(std::string filename)
+ : filename(filename)
+ , fp(NULL)
+{
+}
+
+ConfigFile::~ConfigFile()
+{
+}
+
+void ConfigFile::load()
+{
+ DEBUG(pluginconfig, "Loading config file...\n");
+ if(!open("r")) return;
+
+ values.clear();
+
+ std::string line;
+ while(true) {
+ 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;
+ }
+ }
+
+ close();
+}
+
+void ConfigFile::save()
+{
+ DEBUG(pluginconfig, "Saving configuration...\n");
+
+ createConfigPath();
+
+ if(!open("w")) return;
+
+ 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();
+}
+
+std::string ConfigFile::getValue(const std::string& key)
+{
+ if(values.find(key) != values.end()) {
+ return values[key];
+ }
+
+ return "";
+}
+
+void ConfigFile::setValue(const std::string& key, const std::string& value)
+{
+ values[key] = value;
+}
+
+bool ConfigFile::open(std::string mode)
+{
+ if(fp) close();
+
+ std::string configpath = configPath();
+
+ std::string configfile = configpath;
+ configfile += SEP;
+ configfile += filename;
+
+ DEBUG(pluginconfig, "Opening config file '%s'\n", configfile.c_str());
+ fp = fopen(configfile.c_str(), mode.c_str());
+
+ if(!fp) return false;
+
+ return true;
+}
+
+void ConfigFile::close()
+{
+ fclose(fp);
+ fp = NULL;
+}
+
+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;
+}
diff --git a/src/configfile.h b/src/configfile.h
new file mode 100644
index 0000000..21a6b88
--- /dev/null
+++ b/src/configfile.h
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * configfile.h
+ *
+ * Thu May 14 14:51:38 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.
+ */
+#ifndef __DRUMGIZMO_CONFIGFILE_H__
+#define __DRUMGIZMO_CONFIGFILE_H__
+
+#include <string>
+#include <map>
+#include <stdio.h>
+
+class ConfigFile {
+public:
+ ConfigFile(std::string filename);
+ virtual ~ConfigFile();
+
+ virtual void load();
+ virtual void save();
+
+ virtual std::string getValue(const std::string& key);
+ virtual void setValue(const std::string& key, const std::string& value);
+
+protected:
+ std::map<std::string, std::string> values;
+ std::string filename;
+
+ bool open(std::string mode);
+ void close();
+ std::string readLine();
+
+ FILE* fp;
+};
+
+#endif/*__DRUMGIZMO_CONFIGFILE_H__*/
diff --git a/vst/Makefile.mingw32.in b/vst/Makefile.mingw32.in
index a5c8c31..54c42e0 100644
--- a/vst/Makefile.mingw32.in
+++ b/vst/Makefile.mingw32.in
@@ -12,6 +12,7 @@ DG_SRC = \
@top_srcdir@/src/channel.cc \
@top_srcdir@/src/channelmixer.cc \
@top_srcdir@/src/chresampler.cc \
+ @top_srcdir@/src/configfile.cc \
@top_srcdir@/src/configuration.cc \
@top_srcdir@/src/configparser.cc \
@top_srcdir@/src/drumgizmo.cc \