hlswmaster-ng/config.cpp

198 lines
3.6 KiB
C++
Raw Permalink Normal View History

2006-02-02 16:55:44 +01:00
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "logging.h"
/* ------ */
Config::Tupel::Tupel(const char* name_, const char* value_)
{
name = strdup(name_);
value = strdup(value_);
}
Config::Tupel::~Tupel()
{
free(name);
free(value);
}
/* ------ */
Config::Section::Section(const char* name_)
{
name = strdup(name_);
}
Config::Section::~Section()
{
while (!tupelList.isEmpty())
delete tupelList.get();
free(name);
}
bool Config::Section::addTupel(const char* name, const char* value)
{
if (!name || !value)
return false;
Tupel* t = new Tupel(name, value);
tupelList.addTail(t);
return true;
}
void Config::Section::show() const
{
Iterator<Tupel>* it = tupelList.createIterator();
LogSystem::log(LOG_DEBUG, "[%s]", name);
while (it->hasNext()) {
Tupel* t = it->next();
LogSystem::log(LOG_DEBUG, " %s = %s", t->name, t->value);
}
delete it;
}
char* Config::Section::getTupelValue(const char* name) const
{
char* retval = 0;
Iterator<Tupel>* it = this->tupelList.createIterator();
while (it->hasNext()) {
Tupel* t = it->next();
if (!strcmp(t->name, name)) {
retval = t->value;
break;
}
}
delete it;
return retval;
}
/* ------ */
Config::~Config()
{
while (!sectionList.isEmpty())
delete sectionList.get();
}
Config::Section* Config::addSection(const char* name)
{
Section* s = new Section(name);
sectionList.addTail(s);
return s;
}
bool Config::parseFile(const char* name)
{
Config::Section* section = 0;
FILE *fz;
int i = 0;
char *row, *tok, *tok2;
bool ret = true;
if (!(row = (char*)malloc(1024))) {
LogSystem::log(LOG_CRIT, "config_parse(): malloc()");
return false;
}
if (!(fz = fopen(name, "r"))) {
LogSystem::log(LOG_ERROR, "config_parse(): can not open %s", name);
return false;
}
while (fgets(row, 1024, fz)) {
i++;
/* kommentar oder leere zeile */
if (row[0] == '#' || row[0] <= ' ') {
continue;
/* neue section */
} else if (row[0] == '[') {
tok = strtok(row +1, " ]\n");
section = addSection(tok);
if (!section) {
LogSystem::log(LOG_WARNING, "config_parse(): invalid section in row %d", i);
ret = false;
break;
}
continue;
/* option, aber es gab noch keine section */
} else if (!section) {
LogSystem::log(LOG_WARNING, "config_parse(): missing section in row %d", i);
ret = false;
break;
}
/* option */
if ((tok = strtok(row, " \n")) && (tok2 = strtok(NULL, " \n"))) {
if (!section->addTupel(tok, tok2))
LogSystem::log(LOG_WARNING, "config_parse(): invalid row %d", i);
}
}
fclose(fz);
free(row);
return ret;
}
void Config::show() const
{
LogSystem::log(LOG_DEBUG, "Config Dump:");
Iterator<Section>* it = sectionList.createIterator();
while (it->hasNext())
it->next()->show();
delete it;
}
char* Config::getParameter(const char* section, const char* option) const
{
char* retval = 0;
Iterator<Section>* it = sectionList.createIterator();
while (it->hasNext()) {
Section* s = it->next();
if (!strcmp(s->name, section)) {
retval = s->getTupelValue(option);
break;
}
}
delete it;
return retval;
}
char* Config::getString(const char* section, const char* option, char* def) const
{
char* retval = getParameter(section, option);
if (!retval) {
LogSystem::log(LOG_NOTICE,
"Config: [%s:%s] not found => using '%s'",
section, option, def);
return def;
}
return retval;
}
int Config::getInteger(const char* section, const char* option, int def) const
{
char* retval = getParameter(section, option);
if (!retval) {
LogSystem::log(LOG_NOTICE,
"Config: [%s:%s] not found => using '%d'",
section, option, def);
return def;
}
return atoi(retval);
}