summaryrefslogtreecommitdiffstats
path: root/src/Common
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common')
-rw-r--r--src/Common/ConfigManager.cpp52
-rw-r--r--src/Common/ConfigManager.h32
-rw-r--r--src/Common/Configurable.h42
-rw-r--r--src/Common/Makefile.am3
-rw-r--r--src/Common/Makefile.in4
5 files changed, 100 insertions, 33 deletions
diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp
index 6afeeef..fbd73ea 100644
--- a/src/Common/ConfigManager.cpp
+++ b/src/Common/ConfigManager.cpp
@@ -18,9 +18,9 @@
*/
#include "ConfigManager.h"
+#include "Configurable.h"
+#include "Logger.h"
#include "Util.h"
-#include "Backends/SystemBackendProc.h"
-#include "Backends/SystemBackendPosix.h"
#include <fstream>
#include <stdexcept>
@@ -28,13 +28,26 @@
namespace Mad {
namespace Common {
-std::auto_ptr<ConfigManager> ConfigManager::configManager;
+ConfigManager ConfigManager::configManager;
-bool ConfigManager::loadFile(const std::string &filename) {
+void ConfigManager::handleConfigEntry(const std::vector<std::string> &entry, const std::vector<std::vector<std::string> > &section) {
+ bool handled = false;
+
+ for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c) {
+ if((*c)->handleConfigEntry(entry, section))
+ handled = true;
+ }
+
+ if(!handled)
+ Logger::logf(Logger::WARNING, "Unknown config option '%s'.", entry.front().c_str());
+}
+
+bool ConfigManager::loadFile(const std::string &filename, bool finish) {
std::ifstream file(filename.c_str());
- std::vector<std::string> section;
- std::string line, key;
+ std::vector<std::vector<std::string> > section;
+ std::vector<std::string> entry;
+ std::string line;
if(!file.good())
return false;
@@ -48,7 +61,7 @@ bool ConfigManager::loadFile(const std::string &filename) {
size_t pos = line.find_first_of('#');
if(pos != std::string::npos)
- line[pos] = 0;
+ line = line.substr(0, pos);
line = Util::trim(line);
@@ -70,19 +83,24 @@ bool ConfigManager::loadFile(const std::string &filename) {
if(!line.empty()) {
pos = line.find_first_of(" \t");
+ entry.clear();
+
if(pos == std::string::npos) {
- key = line;
- parseLine(section, key);
+ entry.push_back(line);
+
+ handleConfigEntry(entry, section);
}
else {
- key = line.substr(0, pos);
- parseLine(section, key, Util::trim(line.substr(pos)));
+ entry.push_back(line.substr(0, pos));
+ entry.push_back(Util::trim(line.substr(pos)));
+
+ handleConfigEntry(entry, section);
}
}
switch(bracket) {
case '{':
- section.push_back(key);
+ section.push_back(entry);
break;
case '}':
section.pop_back();
@@ -93,12 +111,12 @@ bool ConfigManager::loadFile(const std::string &filename) {
// TODO Depth check
- return true;
-}
+ if(finish) {
+ for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c)
+ (*c)->configFinished();
+ }
-void ConfigManager::initBackends() {
- Backends::SystemBackendProc::registerBackend();
- Backends::SystemBackendPosix::registerBackend();
+ return true;
}
}
diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h
index 8bb4c87..7fa781e 100644
--- a/src/Common/ConfigManager.h
+++ b/src/Common/ConfigManager.h
@@ -20,37 +20,41 @@
#ifndef MAD_COMMON_CONFIGMANAGER_H_
#define MAD_COMMON_CONFIGMANAGER_H_
+#include <set>
#include <string>
#include <vector>
-#include <memory>
namespace Mad {
namespace Common {
+class Configurable;
+
class ConfigManager {
private:
- static std::auto_ptr<ConfigManager> configManager;
+ static ConfigManager configManager;
- protected:
- ConfigManager() {
- initBackends();
- }
+ std::set<Configurable*> configurables;
+ bool finished;
- static void setConfigManager(std::auto_ptr<ConfigManager> configManager0) {
- configManager = configManager0;
- }
+ ConfigManager() : finished(false) {}
+
+ void handleConfigEntry(const std::vector<std::string>&, const std::vector<std::vector<std::string> >&);
- virtual bool parseLine(const std::vector<std::string> &section, const std::string &key, const std::string &value = std::string()) = 0;
+ public:
+ bool loadFile(const std::string &filename, bool finish = true);
- bool loadFile(const std::string &filename);
+ void registerConfigurable(Configurable *c) {
+ configurables.insert(c);
+ }
- void initBackends();
+ void unregisterConfigurable(Configurable *c) {
+ configurables.erase(c);
+ }
- public:
virtual ~ConfigManager() {}
static ConfigManager *getConfigManager() {
- return configManager.get();
+ return &configManager;
}
};
diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h
new file mode 100644
index 0000000..350b444
--- /dev/null
+++ b/src/Common/Configurable.h
@@ -0,0 +1,42 @@
+/*
+ * Configurable.h
+ *
+ * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_COMMON_CONFIGURABLE_H_
+#define MAD_COMMON_CONFIGURABLE_H_
+
+#include <string>
+#include <vector>
+
+namespace Mad {
+namespace Common {
+
+class ConfigManager;
+
+class Configurable {
+ protected:
+ friend class ConfigManager;
+
+ virtual bool handleConfigEntry(const std::vector<std::string>&, const std::vector<std::vector<std::string> >&) {return false;}
+ virtual void configFinished() {}
+};
+
+}
+}
+
+#endif /* MAD_COMMON_CONFIGURABLE_H_ */
diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am
index be2f08e..d104cac 100644
--- a/src/Common/Makefile.am
+++ b/src/Common/Makefile.am
@@ -4,4 +4,5 @@ noinst_LTLIBRARIES = libcommon.la
libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp
libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la
-noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h
+noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \
+ RequestHandler.h RequestManager.h SystemBackend.h Util.h
diff --git a/src/Common/Makefile.in b/src/Common/Makefile.in
index 40cb554..6bcffb7 100644
--- a/src/Common/Makefile.in
+++ b/src/Common/Makefile.in
@@ -202,7 +202,9 @@ SUBDIRS = Backends Requests RequestHandlers
noinst_LTLIBRARIES = libcommon.la
libcommon_la_SOURCES = RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp
libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la
-noinst_HEADERS = LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h RequestHandler.h RequestManager.h SystemBackend.h Util.h
+noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \
+ RequestHandler.h RequestManager.h SystemBackend.h Util.h
+
all: all-recursive
.SUFFIXES: