hlswmaster-ng/config.cpp

277 lines
6.1 KiB
C++
Raw Permalink Normal View History

2006-04-16 21:37:00 +02:00
/***************************************************************************
* Copyright (C) 04/2006 by Olaf Rempel *
* razzor@kopf-tisch.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 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2006-02-02 16:55:44 +01:00
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "logging.h"
2006-02-05 12:00:47 +01:00
#define BUFSIZE 1024
2006-02-02 16:55:44 +01:00
/* ------ */
Config::Tupel::Tupel(const char* name_, const char* value_)
{
name = strdup(name_);
value = strdup(value_);
}
Config::Tupel::~Tupel()
{
2006-02-05 16:44:38 +01:00
free((char*)name);
free((char*)value);
2006-02-02 16:55:44 +01:00
}
/* ------ */
Config::Section::Section(const char* name_)
{
name = strdup(name_);
}
Config::Section::~Section()
{
while (!tupelList.isEmpty())
delete tupelList.get();
2006-02-05 16:44:38 +01:00
free((char*)name);
2006-02-02 16:55:44 +01:00
}
bool Config::Section::addTupel(const char* name, const char* value)
{
if (!name || !value)
return false;
Tupel* t = new Tupel(name, value);
2006-04-15 19:55:07 +02:00
tupelList.add(t);
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
return true;
}
2006-04-15 19:55:07 +02:00
void Config::Section::show()
2006-02-02 16:55:44 +01:00
{
Iterator<Tupel>* it = tupelList.createIterator();
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_INFO, "[%s]", name);
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
while (it->hasNext()) {
Tupel* t = it->next();
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_INFO, " %s = %s", t->name, t->value);
2006-02-02 16:55:44 +01:00
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
delete it;
}
2006-04-15 19:55:07 +02:00
const char* Config::Section::getTupelValue(const char* name)
2006-02-02 16:55:44 +01:00
{
2006-02-05 16:44:38 +01:00
const char* retval = 0;
2006-02-02 16:55:44 +01:00
Iterator<Tupel>* it = this->tupelList.createIterator();
while (it->hasNext()) {
Tupel* t = it->next();
if (!strcmp(t->name, name)) {
retval = t->value;
break;
}
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
delete it;
return retval;
}
/* ------ */
2006-02-05 16:44:38 +01:00
Config::Section::SectionIterator::SectionIterator(Section* section, const char* name)
: nexttup(0), name(name)
{
it = section->tupelList.createIterator();
}
Config::Section::SectionIterator::~SectionIterator()
{
delete it;
}
bool Config::Section::SectionIterator::hasNext()
{
while (it->hasNext()) {
nexttup = it->next();
if (!strcmp(nexttup->name, name))
return true;
}
return false;
}
char* Config::Section::SectionIterator::next()
{
return (char*)nexttup->value;
}
void Config::Section::SectionIterator::reset()
{
it->reset();
nexttup = 0;
}
/* ------ */
2006-02-02 16:55:44 +01:00
Config::~Config()
{
while (!sectionList.isEmpty())
delete sectionList.get();
}
Config::Section* Config::addSection(const char* name)
{
Section* s = new Section(name);
2006-04-15 19:55:07 +02:00
sectionList.add(s);
2006-02-02 16:55:44 +01:00
return s;
}
bool Config::parseFile(const char* name)
{
2006-02-05 16:44:38 +01:00
Section* section = 0;
2006-02-02 16:55:44 +01:00
FILE *fz;
int i = 0;
char *row, *tok, *tok2;
bool ret = true;
2006-03-05 02:28:19 +01:00
2006-02-05 12:00:47 +01:00
row = new char[BUFSIZE];
if (!row) {
LogSystem::log(LOG_ERROR, "config_parse(): out of memory()");
2006-02-02 16:55:44 +01:00
return false;
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
if (!(fz = fopen(name, "r"))) {
LogSystem::log(LOG_ERROR, "config_parse(): can not open %s", name);
delete [] row;
2006-02-02 16:55:44 +01:00
return false;
}
2006-03-05 02:28:19 +01:00
2006-02-05 12:00:47 +01:00
while (fgets(row, BUFSIZE, fz)) {
2006-02-02 16:55:44 +01:00
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) {
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_WARN, "config_parse(): invalid section in row %d", i);
2006-02-02 16:55:44 +01:00
ret = false;
break;
}
continue;
/* option, aber es gab noch keine section */
} else if (!section) {
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_WARN, "config_parse(): missing section in row %d", i);
2006-02-02 16:55:44 +01:00
ret = false;
break;
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
/* option */
2006-02-05 16:44:38 +01:00
if ((tok = strtok(row, " \n"))) {
while ((tok2 = strtok(NULL, " \n"))) {
if (!section->addTupel(tok, tok2))
LogSystem::log(LOG_WARN, "config_parse(): invalid row %d", i);
}
2006-02-02 16:55:44 +01:00
}
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
fclose(fz);
delete [] row;
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
return ret;
}
2006-04-15 19:55:07 +02:00
void Config::show()
2006-02-02 16:55:44 +01:00
{
2006-02-05 12:00:47 +01:00
LogSystem::log(LOG_INFO, "Config Dump:");
2006-02-02 16:55:44 +01:00
Iterator<Section>* it = sectionList.createIterator();
while (it->hasNext())
it->next()->show();
delete it;
}
2006-04-15 19:55:07 +02:00
Config::Section* Config::getSection(const char* name)
2006-02-02 16:55:44 +01:00
{
2006-02-05 16:44:38 +01:00
Section* retval = 0;
2006-02-02 16:55:44 +01:00
Iterator<Section>* it = sectionList.createIterator();
while (it->hasNext()) {
Section* s = it->next();
2006-02-05 16:44:38 +01:00
if (!strcmp(s->name, name)) {
retval = s;
2006-02-02 16:55:44 +01:00
break;
}
}
2006-03-05 02:28:19 +01:00
2006-02-02 16:55:44 +01:00
delete it;
return retval;
}
2006-04-15 19:55:07 +02:00
const char* Config::getParameter(const char* section, const char* option)
2006-02-02 16:55:44 +01:00
{
2006-02-05 16:44:38 +01:00
Section* s = getSection(section);
return s ? s->getTupelValue(option) : 0;
}
2006-04-15 19:55:07 +02:00
const char* Config::getString(const char* section, const char* option, char* def)
2006-02-05 16:44:38 +01:00
{
const char* retval = getParameter(section, option);
2006-02-02 16:55:44 +01:00
if (!retval) {
LogSystem::log(LOG_NOTICE,
"Config: [%s:%s] not found => using '%s'",
section, option, def);
return def;
}
return retval;
}
2006-04-15 19:55:07 +02:00
int Config::getInteger(const char* section, const char* option, int def)
2006-02-02 16:55:44 +01:00
{
2006-02-05 16:44:38 +01:00
const char* retval = getParameter(section, option);
2006-02-02 16:55:44 +01:00
if (!retval) {
LogSystem::log(LOG_NOTICE,
"Config: [%s:%s] not found => using '%d'",
section, option, def);
return def;
}
return atoi(retval);
}
2006-02-05 16:44:38 +01:00
Iterator<char>* Config::createIterator(const char* section, const char* name)
{
Section* s = getSection(section);
if (!s) {
LogSystem::log(LOG_NOTICE,"Config: [%s:%s] not found",
section, name);
return new NullIterator<char>();
2006-03-05 02:28:19 +01:00
} else {
2006-02-05 16:44:38 +01:00
return new Config::Section::SectionIterator(s, name);
}
}