From: Christian P. <cp...@us...> - 2005-06-22 11:35:35
|
Update of /cvsroot/pclasses/pclasses2/src/App In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13726/src/App Modified Files: Makefile.am Added Files: ConfigStore.cpp Config.cpp Log Message: - Added ConfigStore, ConfigValue, ConfigSection, Config --- NEW FILE: Config.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow, SecuLogiX GmbH * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 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 Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/Factory.h" #include "pclasses/App/Config.h" #include "pclasses/App/ConfigStore.h" namespace P { namespace App { ConfigError::ConfigError(const char* what, const SourceInfo& si) throw() : RuntimeError(what, si) { } ConfigError::~ConfigError() throw() { } ConfigValue::ConfigValue() { } ConfigValue::ConfigValue(const std::string& value) : _value(value) { } ConfigValue::ConfigValue(const Unicode::String& value) : _value(value) { } ConfigValue::ConfigValue(double value) { //@todo } ConfigValue::~ConfigValue() throw() { } const Unicode::String& ConfigValue::value() const throw() { return _value; } ConfigSection::ConfigSection(Config* cfg, const std::string& name) : _cfg(cfg), _name(name) { } ConfigSection::~ConfigSection() throw() { section_list::iterator i = _sections.begin(); while(i != _sections.end()) { delete *i; i = _sections.erase(i); } } const std::string& ConfigSection::name() const throw() { return _name; } ConfigSection::section_iterator ConfigSection::sectionsBegin() throw() { return _sections.begin(); } ConfigSection::section_const_iterator ConfigSection::sectionsBegin() const throw() { return _sections.begin(); } ConfigSection::section_iterator ConfigSection::sectionsEnd() throw() { return _sections.end(); } ConfigSection::section_const_iterator ConfigSection::sectionsEnd() const throw() { return _sections.end(); } ConfigSection::section_iterator ConfigSection::findSection(const std::string& name) throw() { section_iterator i = _sections.begin(); while(i != _sections.end()) { if((*i)->name() == name) break; ++i; } return i; } ConfigSection::section_const_iterator ConfigSection::findSection(const std::string& name) const throw() { section_const_iterator i = _sections.begin(); while(i != _sections.end()) { if((*i)->name() == name) break; ++i; } return i; } ConfigSection& ConfigSection::section(const std::string& name, bool add) throw(ConfigError) { section_iterator i = findSection(name); if(i != _sections.end()) return *(*i); if(!add) throw ConfigError("Section not found", P_SOURCEINFO); ConfigSection& sec = *(*_sections.insert(_sections.begin(), new ConfigSection(_cfg, name))); _cfg->setModified(); return sec; } bool ConfigSection::removeSection(const std::string& name) throw() { section_iterator i = findSection(name); if(i != _sections.end()) { delete *i; _sections.erase(i); _cfg->setModified(); return true; } return false; } ConfigSection::value_iterator ConfigSection::valuesBegin() throw() { return _values.begin(); } ConfigSection::value_const_iterator ConfigSection::valuesBegin() const throw() { return _values.begin(); } ConfigSection::value_iterator ConfigSection::valuesEnd() throw() { return _values.end(); } ConfigSection::value_const_iterator ConfigSection::valuesEnd() const throw() { return _values.end(); } ConfigSection::value_iterator ConfigSection::findValue(const std::string& name) throw() { return _values.find(name); } ConfigSection::value_const_iterator ConfigSection::findValue(const std::string& name) const throw() { return _values.find(name); } const ConfigValue& ConfigSection::value(const std::string& name, bool add) throw(ConfigError) { value_iterator i = findValue(name); if(i != _values.end()) return i->second; if(!add) throw ConfigError("Value not found", P_SOURCEINFO); const ConfigValue& val = _values.insert(_values.begin(), std::make_pair(name,ConfigValue()))->second; _cfg->setModified(); return val; } const ConfigValue& ConfigSection::value(const std::string& name, const ConfigValue& defaultVal, bool add) { value_iterator i = findValue(name); if(i != _values.end()) return i->second; const ConfigValue* v = &defaultVal; if(add) { v = &_values.insert(_values.begin(), std::make_pair(name,defaultVal))->second; _cfg->setModified(); } return *v; } void ConfigSection::setValue(const std::string& name, const ConfigValue& val) { value_iterator i = findValue(name); if(i != _values.end()) { i->second = val; _cfg->setModified(); } else { _values.insert(std::make_pair(name, val)); } } bool ConfigSection::removeValue(const std::string& name) throw() { value_iterator i = findValue(name); if(i != _values.end()) { _values.erase(i); _cfg->setModified(); return true; } return false; } bool ConfigSection::hasValue(const std::string& name) const throw() { value_const_iterator i = findValue(name); return i != _values.end() ? true : false; } Config::Config(const IO::URL& url, const std::string& cfgStoreType) throw(ConfigError, IO::IOError) : _store(0), _modified(false), _root(this, "") { ConfigStore* store = createConfigStore(cfgStoreType); store->init(this, url); _store = store; reload(); } Config::~Config() throw() { try { save(); } catch(...) { } delete _store; } void Config::reload() throw(IO::IOError) { _store->load(); _modified = false; } void Config::save() throw(IO::IOError) { if(_modified) { _store->save(); _modified = false; } } void Config::saveAs(const IO::URL& url, const std::string& cfgStoreType, bool clearModified) throw(ConfigError, IO::IOError) { ConfigStore* store = createConfigStore(cfgStoreType); try { store->init(this, url); store->save(); } catch(...) { delete store; throw; } if(clearModified) _modified = false; } ConfigSection& Config::root() throw() { return _root; } void Config::setModified() throw() { if(!_modified) _modified = true; } ConfigStore* Config::createConfigStore(const std::string& cfgStoreType) throw(ConfigError) { ConfigStore* store = Factory<ConfigStore>::instance().create("ConfigStore_" + cfgStoreType); if(!store) throw ConfigError("Unknown config storage type", P_SOURCEINFO); return store; } } // !namespace App } // !namespace P Index: Makefile.am =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/App/Makefile.am,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Makefile.am 20 Jan 2005 11:05:57 -0000 1.6 +++ Makefile.am 22 Jun 2005 11:35:19 -0000 1.7 @@ -5,7 +5,8 @@ CPPFLAGS = -DPAPP_BUILD libpclasses_app_la_SOURCES = AppDetails.cpp LogMessage.cpp LogTarget.cpp \ - LogChannel.cpp LogManager.cpp SimpleApp.cpp BackgroundApp.cpp CmdLine.cpp + LogChannel.cpp LogManager.cpp SimpleApp.cpp BackgroundApp.cpp CmdLine.cpp \ + ConfigStore.cpp Config.cpp libpclasses_app_la_LDFLAGS = -no-undefined --- NEW FILE: ConfigStore.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow, SecuLogiX GmbH * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 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 Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/App/ConfigStore.h" namespace P { namespace App { ConfigStore::ConfigStore() throw() : _config(0) { } ConfigStore::~ConfigStore() throw() { } void ConfigStore::init(Config* cfg, const IO::URL& url) throw() { _config = cfg; _url = url; } Config* ConfigStore::config() const throw() { return _config; } const IO::URL& ConfigStore::url() const throw() { return _url; } } // !namespace App } // !namespace P |