diff options
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/ConfigEntry.cpp | 64 | ||||
-rw-r--r-- | src/Common/ConfigEntry.h | 96 | ||||
-rw-r--r-- | src/Common/ConfigManager.cpp | 27 | ||||
-rw-r--r-- | src/Common/ConfigManager.h | 4 | ||||
-rw-r--r-- | src/Common/Configurable.h | 6 | ||||
-rw-r--r-- | src/Common/Logger.cpp | 8 | ||||
-rw-r--r-- | src/Common/Logger.h | 2 | ||||
-rw-r--r-- | src/Common/Makefile.am | 4 | ||||
-rw-r--r-- | src/Common/Makefile.in | 11 | ||||
-rw-r--r-- | src/Common/RemoteLogger.cpp | 19 | ||||
-rw-r--r-- | src/Common/RemoteLogger.h | 13 | ||||
-rw-r--r-- | src/Common/Util.cpp | 9 | ||||
-rw-r--r-- | src/Common/Util.h | 1 |
13 files changed, 218 insertions, 46 deletions
diff --git a/src/Common/ConfigEntry.cpp b/src/Common/ConfigEntry.cpp new file mode 100644 index 0000000..f47cf7a --- /dev/null +++ b/src/Common/ConfigEntry.cpp @@ -0,0 +1,64 @@ +/* + * ConfigEntry.cpp + * + * 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/>. + */ + +#include "ConfigEntry.h" + +namespace Mad { +namespace Common { + +ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) { + try { + return value.at(i); + } + catch(std::out_of_range &e) { + zero = String(); + return zero; + } +} + +const ConfigEntry::String& ConfigEntry::Entry::operator[] (size_t i) const { + try { + return value.at(i); + } + catch(std::out_of_range &e) { + return constZero; + } +} + +ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) { + try { + return entries.at(i); + } + catch(std::out_of_range &e) { + zero = Entry(); + return zero; + } +} + +const ConfigEntry::Entry& ConfigEntry::operator[] (size_t i) const { + try { + return entries.at(i); + } + catch(std::out_of_range &e) { + return constZero; + } +} + +} +} diff --git a/src/Common/ConfigEntry.h b/src/Common/ConfigEntry.h new file mode 100644 index 0000000..8358726 --- /dev/null +++ b/src/Common/ConfigEntry.h @@ -0,0 +1,96 @@ +/* + * ConfigEntry.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_CONFIGENTRY_H_ +#define MAD_COMMON_CONFIGENTRY_H_ + +#include <stdexcept> +#include <string> +#include <string.h> +#include <vector> + +namespace Mad { +namespace Common { + +class ConfigEntry { + public: + class String : public std::string { + public: + String() {} + String(const std::string &str) : std::string(str) {} + + bool matches(const std::string &str) const { + return (strcasecmp(c_str(), str.c_str()) == 0); + } + }; + + class Entry { + private: + String key; + std::vector<String> value; + + String zero, constZero; + + public: + Entry() {} + Entry(const std::vector<std::string> &args) { + if(args.empty()) + return; + + key = args.front(); + + value.assign(args.begin()+1, args.end()); + } + + bool empty() const { + return key.empty(); + } + + String &getKey() {return key;} + const String &getKey() const {return key;} + + size_t getSize() const {return value.size();} + + String& operator[] (size_t i); + const String& operator[] (size_t i) const; + }; + + private: + std::vector<Entry> entries; + Entry zero, constZero; + + public: + size_t getSize() const {return entries.size();} + + Entry& operator[] (size_t i); + const Entry& operator[] (size_t i) const; + + void push(const Entry &entry) { + entries.push_back(entry); + } + + void pop() { + entries.pop_back(); + } +}; + +} +} + +#endif /* MAD_COMMON_CONFIGENTRY_H_ */ diff --git a/src/Common/ConfigManager.cpp b/src/Common/ConfigManager.cpp index a7270e2..bfb38a0 100644 --- a/src/Common/ConfigManager.cpp +++ b/src/Common/ConfigManager.cpp @@ -18,6 +18,7 @@ */ #include "ConfigManager.h" +#include "ConfigEntry.h" #include "Configurable.h" #include "Logger.h" #include "Util.h" @@ -39,7 +40,7 @@ bool ConfigManager::Compare::operator() (const Configurable *c1, const Configura } -void ConfigManager::handleConfigEntry(const std::vector<std::vector<std::string> > &entry) { +void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { bool handled = false; for(std::set<Configurable*>::iterator c = configurables.begin(); c != configurables.end(); ++c) { @@ -48,13 +49,13 @@ void ConfigManager::handleConfigEntry(const std::vector<std::vector<std::string> } if(!handled) - Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry.back().front().c_str()); + Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[0].getKey().c_str()); } bool ConfigManager::loadFile(const std::string &filename, bool finish) { std::ifstream file(filename.c_str()); - std::vector<std::vector<std::string> > section; - std::vector<std::string> entry; + ConfigEntry entry; + std::vector<std::string> subEntry; std::string line; if(!file.good()) @@ -91,27 +92,27 @@ bool ConfigManager::loadFile(const std::string &filename, bool finish) { if(!line.empty()) { pos = line.find_first_of(" \t"); - entry.clear(); + subEntry.clear(); if(pos == std::string::npos) { - entry.push_back(line); + subEntry.push_back(line); } else { - entry.push_back(line.substr(0, pos)); - entry.push_back(Util::trim(line.substr(pos))); + subEntry.push_back(line.substr(0, pos)); + subEntry.push_back(Util::trim(line.substr(pos))); } - section.push_back(entry); - handleConfigEntry(section); - section.pop_back(); + entry.push(subEntry); + handleConfigEntry(entry); + entry.pop(); } switch(bracket) { case '{': - section.push_back(entry); + entry.push(subEntry); break; case '}': - section.pop_back(); + entry.pop(); } line = nextLine; diff --git a/src/Common/ConfigManager.h b/src/Common/ConfigManager.h index b447fc4..45a78ec 100644 --- a/src/Common/ConfigManager.h +++ b/src/Common/ConfigManager.h @@ -22,11 +22,11 @@ #include <set> #include <string> -#include <vector> namespace Mad { namespace Common { +class ConfigEntry; class Configurable; class ConfigManager { @@ -43,7 +43,7 @@ class ConfigManager { ConfigManager() : finished(false) {} - void handleConfigEntry(const std::vector<std::vector<std::string> > &entry); + void handleConfigEntry(const ConfigEntry &entry); public: bool loadFile(const std::string &filename, bool finish = true); diff --git a/src/Common/Configurable.h b/src/Common/Configurable.h index 8c2c083..0f16058 100644 --- a/src/Common/Configurable.h +++ b/src/Common/Configurable.h @@ -20,12 +20,10 @@ #ifndef MAD_COMMON_CONFIGURABLE_H_ #define MAD_COMMON_CONFIGURABLE_H_ -#include <string> -#include <vector> - namespace Mad { namespace Common { +class ConfigEntry; class ConfigManager; class Configurable { @@ -38,7 +36,7 @@ class Configurable { protected: friend class ConfigManager; - virtual bool handleConfigEntry(const std::vector<std::vector<std::string> >&, bool) {return false;} + virtual bool handleConfigEntry(const ConfigEntry&, bool) {return false;} virtual void configFinished() {} }; diff --git a/src/Common/Logger.cpp b/src/Common/Logger.cpp index f99d31f..27c98ba 100644 --- a/src/Common/Logger.cpp +++ b/src/Common/Logger.cpp @@ -18,8 +18,8 @@ */ #include "Logger.h" +#include "ConfigEntry.h" #include "ConfigManager.h" -#include "Util.h" #include <cstdlib> @@ -30,12 +30,12 @@ std::auto_ptr<Logger::ConfigHelper> Logger::configHelper; std::list<Logger*> Logger::loggers; -bool Logger::ConfigHelper::handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled) { +bool Logger::ConfigHelper::handleConfigEntry(const ConfigEntry &entry, bool handled) { if(handled) return false; - if(entry.size() == 1 && entry.front().size() == 2 && Util::tolower(entry.front().front()) == "logger") { - logf(WARNING, "Invalid logger '%s'.", entry.back().back().c_str()); + if(entry[0].getKey().matches("Logger") && entry[1].empty()) { + logf(WARNING, "Unknown logger '%s'.", entry[0][0].c_str()); return true; } diff --git a/src/Common/Logger.h b/src/Common/Logger.h index d8f105a..9499792 100644 --- a/src/Common/Logger.h +++ b/src/Common/Logger.h @@ -37,7 +37,7 @@ class Logger : public LoggerBase { private: class ConfigHelper : private Configurable { protected: - virtual bool handleConfigEntry(const std::vector<std::vector<std::string> > &entry, bool handled); + virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); }; static std::auto_ptr<ConfigHelper> configHelper; diff --git a/src/Common/Makefile.am b/src/Common/Makefile.am index 95b3d50..0e82f18 100644 --- a/src/Common/Makefile.am +++ b/src/Common/Makefile.am @@ -1,8 +1,8 @@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = Configurable.cpp RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp +libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Configurable.cpp Exception.cpp Logger.cpp RemoteLogger.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \ +noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Logger.h LoggerBase.h RemoteLogger.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 8b9300c..a030c7e 100644 --- a/src/Common/Makefile.in +++ b/src/Common/Makefile.in @@ -49,9 +49,9 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libcommon_la_DEPENDENCIES = Backends/libbackends.la \ Requests/librequests.la RequestHandlers/librequesthandlers.la -am_libcommon_la_OBJECTS = Configurable.lo RemoteLogger.lo Logger.lo \ - ConfigManager.lo Exception.lo RequestManager.lo \ - SystemBackend.lo Util.lo +am_libcommon_la_OBJECTS = ConfigEntry.lo ConfigManager.lo \ + Configurable.lo Exception.lo Logger.lo RemoteLogger.lo \ + RequestManager.lo SystemBackend.lo Util.lo libcommon_la_OBJECTS = $(am_libcommon_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -201,9 +201,9 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Backends Requests RequestHandlers noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = Configurable.cpp RemoteLogger.cpp Logger.cpp ConfigManager.cpp Exception.cpp RequestManager.cpp SystemBackend.cpp Util.cpp +libcommon_la_SOURCES = ConfigEntry.cpp ConfigManager.cpp Configurable.cpp Exception.cpp Logger.cpp RemoteLogger.cpp RequestManager.cpp SystemBackend.cpp Util.cpp libcommon_la_LIBADD = Backends/libbackends.la Requests/librequests.la RequestHandlers/librequesthandlers.la -noinst_HEADERS = Configurable.h LoggerBase.h RemoteLogger.h Logger.h ConfigManager.h Exception.h HostInfo.h Request.h RequestBase.h \ +noinst_HEADERS = ConfigEntry.h ConfigManager.h Configurable.h Exception.h HostInfo.h Logger.h LoggerBase.h RemoteLogger.h Request.h RequestBase.h \ RequestHandler.h RequestManager.h SystemBackend.h Util.h all: all-recursive @@ -257,6 +257,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigEntry.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConfigManager.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Configurable.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Exception.Plo@am__quote@ diff --git a/src/Common/RemoteLogger.cpp b/src/Common/RemoteLogger.cpp index 7742ee5..a678e92 100644 --- a/src/Common/RemoteLogger.cpp +++ b/src/Common/RemoteLogger.cpp @@ -18,15 +18,28 @@ */ #include "RemoteLogger.h" -#include <list> -#include <string> -#include <ctime> +#include "ConfigEntry.h" +#include "Logger.h" namespace Mad { namespace Common { +std::auto_ptr<RemoteLogger::ConfigHelper> RemoteLogger::configHelper; std::list<RemoteLogger*> RemoteLogger::remoteLoggers; + +bool RemoteLogger::ConfigHelper::handleConfigEntry(const ConfigEntry &entry, bool handled) { + if(handled) + return false; + + if(entry[0].getKey().matches("RemoteLogger") && entry[1].empty()) { + Logger::logf(WARNING, "Unknown remote logger '%s'.", entry[0][0].c_str()); + return true; + } + + return false; +} + void RemoteLogger::log(MessageCategory category, MessageLevel level, time_t messageTimestamp, const std::string &message, const std::string &messageSource) { for(std::list<Common::RemoteLogger*>::iterator remoteLogger = remoteLoggers.begin(); remoteLogger != remoteLoggers.end(); ++remoteLogger) { diff --git a/src/Common/RemoteLogger.h b/src/Common/RemoteLogger.h index 8257d1f..8ed7836 100644 --- a/src/Common/RemoteLogger.h +++ b/src/Common/RemoteLogger.h @@ -21,17 +21,26 @@ #define MAD_COMMON_REMOTELOGGER_H_ #include "LoggerBase.h" +#include "Configurable.h" + #include <algorithm> -#include <list> -#include <string> #include <cstdarg> #include <ctime> +#include <list> +#include <memory> +#include <string> namespace Mad { namespace Common { class RemoteLogger : public LoggerBase { private: + class ConfigHelper : private Configurable { + protected: + virtual bool handleConfigEntry(const ConfigEntry &entry, bool handled); + }; + + static std::auto_ptr<ConfigHelper> configHelper; static std::list<RemoteLogger*> remoteLoggers; protected: diff --git a/src/Common/Util.cpp b/src/Common/Util.cpp index 2bf4d50..f34aa1e 100644 --- a/src/Common/Util.cpp +++ b/src/Common/Util.cpp @@ -22,15 +22,6 @@ namespace Mad { namespace Common { -std::string Util::tolower(const std::string &str) { - std::string ret; - - for(std::string::const_iterator c = str.begin(); c != str.end(); ++c) - ret += std::tolower(*c); - - return ret; -} - std::string Util::trim(const std::string &str) { size_t beg, end; diff --git a/src/Common/Util.h b/src/Common/Util.h index 8df5cac..ec31beb 100644 --- a/src/Common/Util.h +++ b/src/Common/Util.h @@ -30,7 +30,6 @@ class Util { Util(); public: - static std::string tolower(const std::string &str); static std::string trim(const std::string &str); }; |