From 96b6f07a32cb02ae5d907bacd81f62fc25fdc278 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 19 Oct 2008 20:35:52 +0200 Subject: Neuen ConfigManager angefangen & alten Code daran angepasst --- src/Common/ConfigManager.cpp | 52 +++++++++++++++++++++++++++++--------------- src/Common/ConfigManager.h | 32 +++++++++++++++------------ src/Common/Configurable.h | 42 +++++++++++++++++++++++++++++++++++ src/Common/Makefile.am | 3 ++- src/Common/Makefile.in | 4 +++- 5 files changed, 100 insertions(+), 33 deletions(-) create mode 100644 src/Common/Configurable.h (limited to 'src/Common') 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 #include @@ -28,13 +28,26 @@ namespace Mad { namespace Common { -std::auto_ptr ConfigManager::configManager; +ConfigManager ConfigManager::configManager; -bool ConfigManager::loadFile(const std::string &filename) { +void ConfigManager::handleConfigEntry(const std::vector &entry, const std::vector > §ion) { + bool handled = false; + + for(std::set::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 section; - std::string line, key; + std::vector > section; + std::vector 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::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 #include #include -#include namespace Mad { namespace Common { +class Configurable; + class ConfigManager { private: - static std::auto_ptr configManager; + static ConfigManager configManager; - protected: - ConfigManager() { - initBackends(); - } + std::set configurables; + bool finished; - static void setConfigManager(std::auto_ptr configManager0) { - configManager = configManager0; - } + ConfigManager() : finished(false) {} + + void handleConfigEntry(const std::vector&, const std::vector >&); - virtual bool parseLine(const std::vector §ion, 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 + * + * 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 . + */ + +#ifndef MAD_COMMON_CONFIGURABLE_H_ +#define MAD_COMMON_CONFIGURABLE_H_ + +#include +#include + +namespace Mad { +namespace Common { + +class ConfigManager; + +class Configurable { + protected: + friend class ConfigManager; + + virtual bool handleConfigEntry(const std::vector&, const std::vector >&) {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: -- cgit v1.2.3