// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: configuration.cc,v 1.20 2001/04/27 05:49:34 jgg Exp $
+// $Id: configuration.cc,v 1.28 2004/04/30 04:00:15 mdz Exp $
/* ######################################################################
Configuration Class
This class provides a configuration file and command line parser
for a tree-oriented configuration environment. All runtime configuration
is stored in here.
+
+ This source is placed in the Public Domain, do with it what you will
+ It was originally written by Jason Gunthorpe <jgg@debian.org>.
##################################################################### */
/*}}}*/
#include <vector>
#include <algorithm>
#include <fstream>
+#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
+
+using namespace std;
/*}}}*/
Configuration *_config = new Configuration;
if (Len != 0)
{
for (; I != 0; Last = &I->Next, I = I->Next)
- if ((Res = stringcasecmp(I->Tag.begin(),I->Tag.end(),S,S + Len)) == 0)
+ if ((Res = stringcasecmp(I->Tag,S,S + Len)) == 0)
break;
}
else
case 'i':
{
char buf[16];
- snprintf(buf, sizeof(buf)-1, "%d", FindI(key, atoi(Default)));
+ snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
return buf;
}
}
char S[300];
snprintf(S,sizeof(S),"%i",Value);
Itm->Value = S;
+}
+ /*}}}*/
+// Configuration::Clear - Clear an single value from a list /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void Configuration::Clear(string Name, int Value)
+{
+ char S[300];
+ snprintf(S,sizeof(S),"%i",Value);
+ Clear(Name, S);
+}
+ /*}}}*/
+// Configuration::Clear - Clear an single value from a list /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void Configuration::Clear(string Name, string Value)
+{
+ Item *Top = Lookup(Name.c_str(),false);
+ if (Top == 0 || Top->Child == 0)
+ return;
+
+ Item *Tmp, *Prev, *I;
+ Prev = I = Top->Child;
+
+ while(I != NULL)
+ {
+ if(I->Value == Value)
+ {
+ Tmp = I;
+ // was first element, point parent to new first element
+ if(Top->Child == Tmp)
+ Top->Child = I->Next;
+ I = I->Next;
+ Prev->Next = I;
+ delete Tmp;
+ } else {
+ Prev = I;
+ I = I->Next;
+ }
+ }
+
}
/*}}}*/
// Configuration::Clear - Clear an entire tree /*{{{*/
void Configuration::Clear(string Name)
{
Item *Top = Lookup(Name.c_str(),false);
- if (Top == 0)
+ if (Top == 0)
return;
-
+
Top->Value = string();
Item *Stop = Top;
Top = Top->Child;
{
string key = Name;
- if (key.size() > 2 && key.end()[-2] == '/' &&
- key.find_first_of("fdbi",key.size()-1) < key.size())
- {
- key.resize(key.size() - 2);
- if (Exists(key.c_str()))
- return true;
- }
+ if (key.size() > 2 && key.end()[-2] == '/')
+ if (key.find_first_of("fdbi",key.size()-1) < key.size())
+ {
+ key.resize(key.size() - 2);
+ if (Exists(key.c_str()))
+ return true;
+ }
+ else
+ {
+ _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
+ }
return Exists(Name);
}
// Configuration::Dump - Dump the config /*{{{*/
// ---------------------------------------------------------------------
/* Dump the entire configuration space */
-void Configuration::Dump()
+void Configuration::Dump(ostream& str)
{
/* Write out all of the configuration directives by walking the
configuration tree */
const Configuration::Item *Top = Tree(0);
for (; Top != 0;)
{
- clog << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
+ str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
if (Top->Child != 0)
{
Sectional config files are like bind's named.conf where there are
sections like 'zone "foo.org" { .. };' This causes each section to be
added in with a tag like "zone::foo.org" instead of being split
- tag/value. */
+ tag/value. AsSectional enables Sectional parsing.*/
bool ReadConfigFile(Configuration &Conf,string FName,bool AsSectional,
unsigned Depth)
{
// Open the stream for reading
- ifstream F(FName.c_str(),ios::in | ios::nocreate);
+ ifstream F(FName.c_str(),ios::in);
if (!F != 0)
return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
- char Buffer[300];
+ char Buffer[1024];
string LineBuffer;
string Stack[100];
unsigned int StackPos = 0;
{
F.getline(Buffer,sizeof(Buffer));
CurLine++;
+ // This should be made to work instead, but this is better than looping
+ if (F.fail() && !F.eof())
+ return _error->Error(_("Line %d too long (max %d)"), CurLine, sizeof(Buffer));
+
_strtabexpand(Buffer,sizeof(Buffer));
_strstrip(Buffer);
string Tag;
const char *Pos = LineBuffer.c_str();
if (ParseQuoteWord(Pos,Tag) == false)
- return _error->Error(_("Syntax error %s:%u: Malformed Tag"),FName.c_str(),CurLine);
+ return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
// Parse off the word
string Word;