/*
* configfile class - partially useful :-)
* $Id: rconf.cpp,v 1.2 2001/09/20 21:43:13 rasp Exp $
*/
/*************************************************************************
* Copyright 2009 Ralph Spitzner (rasp@spitzner.org)
*
* This file is part of Yahdr.
*
* Yahdr 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.
*
* Yahdr 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 Yahdr. If not, see .
**************************************************************************/
using namespace std;
#include "rconf.h"
void
ConfigFile::err_exit(char *why)
{
if(why)
cerr << "Config exit: " << why << endl;
else
cerr << "Config exit: unspecified error\n" << endl;
exit(0);
}
int
ConfigFile::iscomment(char *line)
{
while(isspace(*line))
line++;
if(*line == '#' || *line == ';' || *line == '\0')
return 1;
else
return 0;
}
#include
struct ConfigFile::confval *
ConfigFile::getpair(char *line)
{
struct confval *retval,*temp;
struct confval *llist = 0;
char *key;//,*value;
char *dif;
char *start;
char sep;
int tot;
if(!(retval = (struct confval *)malloc(sizeof(struct confval))))
{
printf("mem in %s,%d\n",__FILE__,__LINE__);
exit(86);
}
tot = strlen(line);
start = line;
retval->leaf = 0L;
#ifdef testing
printf("in getpair %d\n",__LINE__);
cout << "working on line:" << line <key = key;
retval->value = strdup(line);
if(sep == ' ' && !strcmp(retval->value,"{"))
{
char buf[0x800];
// int leaf1 = 1;
temp = retval;
#ifdef testing
cout << "Structure start" << endl;
#endif
while(!(cfile->eof()) &&
//temp &&
!(!strcmp(temp->key,"}") && *temp->value == '\0'))
{
memset(&buf,0,0x7ff);
if(!(cfile->getline(buf,0x7ff)))
break;
if(!(iscomment(buf)))
{
#ifdef testing
cout << "calling myself" << endl;
#endif
temp = getpair(buf);
if(llist)
{
llist->next = temp;
temp->prev = llist;
llist = temp;
}
else
llist = retval->leaf = temp;
}
#ifdef testing
else
cerr << "Comment: " << buf << endl;
#endif
}
#ifdef testing
cout << "Structure end" << endl;
#endif
}
#ifdef testing
cout << "Found Value line == " << line << "-> key=" << retval->key << "||" << retval->value << "||"<< endl;
#endif
return retval;
}
ConfigFile::ConfigFile(char *fname)
{
cfile = new ifstream(fname);
char buf[0x800];
struct confval *temp;
tlist = temp = NULL;
//cout << "cname = |" << fname << "|" << endl;
if(!(cfile->is_open()))
err_exit((char*)"unable to open config");
while(!(cfile->eof()))
{
bzero(buf,0x7ff);
if(!(cfile->getline(buf,0x7ff,'\n')))
break;
if(!(iscomment(buf)))
{
temp = getpair((char *)&buf);
if(tlist)
{
tlist->next = temp;
temp->prev = tlist;
tlist = temp;
}
else
tlist = first = pfirst = temp ;
}
#ifdef testing
else
cerr << "Comment: " << buf << endl;
#endif
}
}
#ifdef testing
int
main()
{
static struct ConfigFile::confval *list,*l2,*l3;
ConfigFile *myc = new ConfigFile("/usr/local/etc/yahdr.channels");
cout << "Values I read:" << endl;
list = myc->first;
while(list)
{
cout << "key-> " << list->key << "<- val ->" << list->value << endl;
if(list->leaf)
{
cout << list->key << " has a leaf, following" << endl;
l2 = list->leaf;
while(l2)
{
if(l2->leaf)
{
cout << l2->key << " has a leaf, following" << endl;
l3 = l2->leaf;
while(l3)
{
cout << "key-> " << l3->key << "<- val ->" << l3->value << endl;
l3 = l3->next;
}
}
cout << list->key <<"------->key ->" << l2->key << "<- val -> " << l2->value << endl;
l2 = l2->next;
}
}
list = list->next;
}
cout << "thhhhhats all, folks" << endl;
}
#endif
struct ConfigFile::confval *
ConfigFile::findkey(char *kname,struct ConfigFile::confval *start)
{
struct ConfigFile::confval *temp;
temp = start;
if(!temp)
temp = pfirst;
while(temp)
{
if(!(strcasecmp(temp->key, kname)))
return temp;
//else
//cerr << "no match :" << temp->key << endl;
temp = temp->next;
}
return (struct confval *) NULL;
}
ConfigFile::~ConfigFile()
{
struct confval *tmp;
tmp = pfirst;
while(tmp)
{
if(tmp->key)
free(tmp->key);
if(tmp->value)
free(tmp->value);
if(tmp->next)
{
tmp = tmp->next;
free(tmp->prev);
}
else
{
free(tmp);
return;
}
}
}