Try to reuse the config code, but I was not able to

I create a Conf::Config to replace it...
This commit is contained in:
yanmorin
2005-09-28 19:55:19 +00:00
parent 1274ba869f
commit 9a5618a112
15 changed files with 309 additions and 57 deletions

View File

@ -193,6 +193,7 @@ src/audio/pacpp/include/Makefile \
src/audio/pacpp/include/portaudiocpp/Makefile \
src/audio/pacpp/source/Makefile \
src/audio/pacpp/source/portaudiocpp/Makefile \
src/config/Makefile \
src/gui/Makefile \
src/gui/qt/Makefile \
src/gui/official/Makefile \

View File

@ -5,11 +5,11 @@ maintener_source =
endif
if USE_ZEROCONF
SUBDIRS = audio gui zeroconf
SUBDIRS = audio gui zeroconf config
ZEROCONFLIB = zeroconf/libzeroconf.la
ZEROCONFFLAGS = -DUSE_ZEROCONF
else
SUBDIRS = audio gui
SUBDIRS = audio gui config
ZEROCONFLIB =
ZEROCONFFLAGS =
endif

6
src/config/Makefile.am Normal file
View File

@ -0,0 +1,6 @@
SUBDIRS =
noinst_LTLIBRARIES = libconfig.la
libconfig_la_SOURCES = \
config.cpp config.h

132
src/config/config.cpp Normal file
View File

@ -0,0 +1,132 @@
/**
* Copyright (C) 2005 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
*
* 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 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
namespace Conf {
// ctor
Config::Config()
{
}
// dtor
Config::~Config()
{
// erase every new ItemMap (by CreateSection)
SectionMap::iterator iter = _sections.begin();
while(iter != _sections.end()) {
delete iter->second;
iter->second = NULL;
iter++;
}
}
/**
* Create the section only if it doesn't exists
*/
void
Config::createSection(const std::string& section) {
// if we doesn't find the item, create it
if (_sections.find(section) == _sections.end()) {
_sections[section] = new ItemMap;
}
}
/**
* Add the config item only if it exists..
* If the section doesn't exists, create it
*/
void
Config::addConfigItem(const std::string& section, const ConfigItem& item)
{
// if we doesn't find the item, create it
SectionMap::iterator iter = _sections.find(section);
if ( iter == _sections.end()) {
_sections[section] = new ItemMap;
iter = _sections.find(section);
}
// be prudent here
if (iter!=NULL && iter != _sections.end()) {
std::string name = item.getName();
if ( iter->second->find(name) == iter->second->end()) {
(*(iter->second))[name] = item;
}
}
}
// throw a ConfigItemException if not found
std::string
Config::getConfigItemValue(const std::string& section, const std::string& itemName)
{
ConfigItem* item = getConfigItem(section, itemName);
if (item!=NULL) {
return item->getValue();
} else {
throw new ConfigItemException();
}
return "";
}
// throw a ConfigItemException if not found
int
Config::getConfigItemIntValue(const std::string& section, const std::string& itemName)
{
ConfigItem* item = getConfigItem(section, itemName);
if (item!=NULL && item->getType() == "int") {
return atoi(item->getValue().data());
} else {
throw new ConfigItemException();
}
return 0;
}
/**
* Return a ConfigItem or NULL if not found
*/
ConfigItem*
Config::getConfigItem(const std::string& section, const std::string& itemName) {
SectionMap::iterator iter = _sections.find(section);
if ( iter == _sections.end()) {
return NULL;
}
ItemMap::iterator iterItem = iter->second->find(itemName);
if ( iterItem == iter->second->end()) {
return NULL;
}
return &(iterItem->second);
}
/**
* Set the configItem if found, else do nothing
*/
void
Config::setConfigItem(const std::string& section, const std::string& itemName, const std::string& value) {
SectionMap::iterator iter = _sections.find(section);
if ( iter == _sections.end()) {
return;
}
ItemMap::iterator iterItem = iter->second->find(itemName);
if ( iterItem == iter->second->end()) {
return;
}
iterItem->second.setValue(value);
}
} // end namespace Config

81
src/config/config.h Normal file
View File

@ -0,0 +1,81 @@
/**
* Copyright (C) 2005 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
*
* 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 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CONFIG_CONFIG_H_
#define __CONFIG_CONFIG_H_
#include <map>
#include <string>
namespace Conf {
class ConfigItem;
typedef std::map<std::string, ConfigItem> ItemMap;
typedef std::map<std::string, ItemMap*> SectionMap;
class ConfigItemException {
public:
ConfigItemException() {}
~ConfigItemException() {}
};
class Config {
public:
Config();
~Config();
void createSection(const std::string& section);
void addConfigItem(const std::string& section, const ConfigItem &item);
void setConfigItem(const std::string& section, const std::string& itemName, const std::string& value);
// throw a ConfigItemException if not found
std::string getConfigItemValue(const std::string& section, const std::string& itemName);
int getConfigItemIntValue(const std::string& section, const std::string& itemName);
private:
ConfigItem* getConfigItem(const std::string& section, const std::string& itemName);
SectionMap _sections;
};
class ConfigItem {
public:
ConfigItem() : _defaultValue(""), _type("string") {}
ConfigItem(const std::string& name, const std::string& value, const std::string& defaultValue, const std::string& type) :
_name(name), _value(value),
_defaultValue(defaultValue), _type(type) {}
~ConfigItem();
void setValue(const std::string& value) { _value = value; }
const std::string getName() const { return _name; }
const std::string getValue() const { return _value; }
const std::string getDefaultValue() const { return _defaultValue; }
const std::string getType() const { return _type; }
private:
std::string _name;
std::string _value;
std::string _defaultValue;
std::string _type;
};
} // end namespace Config
#endif

View File

@ -23,25 +23,27 @@
#include <string>
#include <stdio.h>
using namespace std;
class ConfigItem {
public:
ConfigItem (void);
ConfigItem (const std::string& );
ConfigItem (const std::string& , const std::string& );
ConfigItem (const string& );
ConfigItem (const string& , const string& );
~ConfigItem (void);
std::string* key (void) { return _key; }
std::string* value (void) { return _value; }
string* key (void) { return _key; }
string* value (void) { return _value; }
ConfigItem* head (void) { return _head; }
void setHead (ConfigItem *h) { _head = h; }
std::string* getValueByKey (const std::string& );
ConfigItem* getItemByKey (const std::string& );
void setValue (const std::string& );
void setValueByKey (const std::string& , const std::string& );
void saveToFile (std::fstream*);
string* getValueByKey (const string& );
ConfigItem* getItemByKey (const string& );
void setValue (const string& );
void setValueByKey (const string& , const string& );
void saveToFile (fstream*);
private:
std::string* _key;
std::string* _value;
string* _key;
string* _value;
ConfigItem* _next;
ConfigItem* _head;
void init (void);

View File

@ -41,14 +41,14 @@
class ConfigurationTree {
public:
ConfigurationTree (void);
ConfigurationTree (const std::string&);
ConfigurationTree (const string&);
~ConfigurationTree (void);
ConfigSection* head (void) { return this->_head; }
int populateFromFile(const std::string& );
int saveToFile (const std::string& );
int setValue (const std::string& , const std::string& , int);
int setValue(const std::string& , const std::string& , const std::string& );
std::string getValue (const std::string& , const std::string& );
int populateFromFile(const string& );
int saveToFile (const string& );
int setValue (const string& , const string& , int);
int setValue(const string& , const string& , const string& );
string getValue (const string& , const string& );
private:
ConfigSection *_head;

View File

@ -234,15 +234,15 @@ GuiFramework::getConfigAll(const std::string& sequenceId)
}
bool
GuiFramework::getConfig(const std::string& sequenceId, const std::string& name)
GuiFramework::getConfig(const std::string& section, const std::string& name, TokenList& arg)
{
return Manager::instance().getConfig(sequenceId, name);
return Manager::instance().getConfig(section, name, arg);
}
bool
GuiFramework::setConfig(const std::string& name, const std::string& value)
GuiFramework::setConfig(const std::string& section, const std::string& name, const std::string& value)
{
return Manager::instance().setConfig(name, value);
return Manager::instance().setConfig(section, name, value);
}
bool

View File

@ -85,8 +85,8 @@ public:
bool attachZeroconfEvents(const std::string& sequenceId);
bool getCallStatus(const std::string& sequenceId);
bool getConfigAll(const std::string& sequenceId);
bool getConfig(const std::string& sequenceId, const std::string& name);
bool setConfig(const std::string& name, const std::string& value);
bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
bool setConfig(const std::string& section, const std::string& name, const std::string& value);
bool getConfigList(const std::string& sequenceId, const std::string& name);
// Observer methods

View File

@ -45,6 +45,10 @@ public:
ResponseMessage response(code, _sequenceId, message);
return response;
}
ResponseMessage message(const std::string &code, TokenList& arg) {
ResponseMessage response(code, _sequenceId, arg);
return response;
}
std::string sequenceId () const { return _sequenceId; }
protected:

View File

@ -64,36 +64,13 @@ RequestConfigGetAll::execute()
RequestConfigGet::RequestConfigGet(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList)
{
TokenList::iterator iter = _argList.begin();
if (iter != _argList.end()) {
_name = *iter;
_argList.pop_front();
} else {
throw RequestConstructorException();
}
}
ResponseMessage
RequestConfigGet::execute()
{
if (GUIServer::instance().getConfig(_sequenceId, _name)) {
return message("200", "OK");
} else {
return message("500","Server Error");
}
}
RequestConfigSet::RequestConfigSet(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList)
{
TokenList::iterator iter = _argList.begin();
// get two strings arguments
bool argsAreValid = false;
if (iter != _argList.end()) {
_name = *iter;
_section = *iter;
_argList.pop_front();
iter++;
if (iter != _argList.end()) {
_value = *iter;
_name = *iter;
_argList.pop_front();
argsAreValid = true;
}
@ -103,10 +80,47 @@ RequestConfigSet::RequestConfigSet(const std::string &sequenceId, const TokenLis
}
}
ResponseMessage
RequestConfigGet::execute()
{
TokenList arg;
if (GUIServer::instance().getConfig(_section, _name, arg)) {
return message("200", arg);
} else {
return message("500","Server Error");
}
}
RequestConfigSet::RequestConfigSet(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList)
{
TokenList::iterator iter = _argList.begin();
// get three strings arguments
bool argsAreValid = false;
if (iter != _argList.end()) {
_section = *iter;
_argList.pop_front();
iter++;
if (iter != _argList.end()) {
_name = *iter;
_argList.pop_front();
iter++;
if (iter != _argList.end()) {
_value = *iter;
_argList.pop_front();
argsAreValid = true;
}
}
}
if (!argsAreValid) {
throw RequestConstructorException();
}
}
ResponseMessage
RequestConfigSet::execute()
{
if (GUIServer::instance().setConfig(_name, _value)) {
if (GUIServer::instance().setConfig(_section, _name, _value)) {
return message("200", "OK");
} else {
return message("500","Server Error");

View File

@ -56,6 +56,7 @@ public:
RequestConfigGet(const std::string &sequenceId, const TokenList& argList);
ResponseMessage execute();
private:
std::string _section;
std::string _name;
};
@ -65,6 +66,7 @@ public:
RequestConfigSet(const std::string &sequenceId, const TokenList& argList);
ResponseMessage execute();
private:
std::string _section;
std::string _name;
std::string _value;
};

View File

@ -1139,14 +1139,14 @@ ManagerImpl::getConfigAll(const std::string& sequenceId)
}
bool
ManagerImpl::getConfig(const std::string& sequenceId, const std::string& name)
ManagerImpl::getConfig(const std::string& section, const std::string& name, TokenList& arg)
{
bool returnValue = false;
return returnValue;
}
bool
ManagerImpl::setConfig(const std::string& name, const std::string& value)
ManagerImpl::setConfig(const std::string& section, const std::string& name, const std::string& value)
{
bool returnValue = false;
return returnValue;
@ -1156,6 +1156,18 @@ bool
ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& name)
{
bool returnValue = false;
if (name=="") {
returnValue = true;
} else if (name=="") {
returnValue = true;
} else if (name=="") {
returnValue = true;
} else if (name=="") {
returnValue = true;
} else if (name=="") {
returnValue = true;
}
return returnValue;
}

View File

@ -197,8 +197,8 @@ public:
bool attachZeroconfEvents(const std::string& sequenceId, const Pattern::Observer &observer);
bool getCallStatus(const std::string& sequenceId);
bool getConfigAll(const std::string& sequenceId);
bool getConfig(const std::string& sequenceId, const std::string& name);
bool setConfig(const std::string& name, const std::string& value);
bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
bool setConfig(const std::string& section, const std::string& name, const std::string& value);
bool getConfigList(const std::string& sequenceId, const std::string& name);

View File

@ -21,5 +21,3 @@ pkginclude_HEADERS = \
pkgincludedir=$(includedir)/utilspp/singleton
noinst_HEADERS = guiserversingleton.h