Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

ConfigReader Class Reference

Allows reading of values from configuration files This class allows a module to read from either the main configuration file (inspircd.conf) or from a module-specified configuration file. More...

#include <modules.h>

Inherits classbase.

List of all members.

Public Member Functions

 ConfigReader ()
 Default constructor.

 ConfigReader (std::string filename)
 Overloaded constructor.

 ~ConfigReader ()
 Default destructor.

std::string ReadValue (std::string tag, std::string name, int index)
 Retrieves a value from the config file.

bool ReadFlag (std::string tag, std::string name, int index)
 Retrieves a boolean value from the config file.

long ReadInteger (std::string tag, std::string name, int index, bool needs_unsigned)
 Retrieves an integer value from the config file.

long GetError ()
 Returns the last error to occur.

int Enumerate (std::string tag)
 Counts the number of times a given tag appears in the config file.

bool Verify ()
 Returns true if a config file is valid.

void DumpErrors (bool bail, userrec *user)
 Dumps the list of errors in a config file to an output location.

int EnumerateValues (std::string tag, int index)
 Returns the number of items within a tag.


Protected Attributes

std::stringstream * cache
 The contents of the configuration file This protected member should never be accessed by a module (and cannot be accessed unless the core is changed).

std::stringstream * errorlog
bool readerror
 Used to store errors.

long error


Detailed Description

Allows reading of values from configuration files This class allows a module to read from either the main configuration file (inspircd.conf) or from a module-specified configuration file.

It may either be instantiated with one parameter or none. Constructing the class using one parameter allows you to specify a path to your own configuration file, otherwise, inspircd.conf is read.

Definition at line 780 of file modules.h.


Constructor & Destructor Documentation

ConfigReader::ConfigReader  ) 
 

Default constructor.

This constructor initialises the ConfigReader class to read the inspircd.conf file as specified when running ./configure.

Definition at line 674 of file modules.cpp.

References cache, CONF_FILE_NOT_FOUND, error, errorlog, and readerror.

00675 {
00676         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
00677         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
00678         this->readerror = LoadConf(CONFIG_FILE,this->cache,this->errorlog);
00679         if (!this->readerror)
00680                 this->error = CONF_FILE_NOT_FOUND;
00681 }

ConfigReader::ConfigReader std::string  filename  ) 
 

Overloaded constructor.

This constructor initialises the ConfigReader class to read a user-specified config file

Definition at line 693 of file modules.cpp.

References cache, CONF_FILE_NOT_FOUND, error, errorlog, and readerror.

00694 {
00695         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
00696         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
00697         this->readerror = LoadConf(filename.c_str(),this->cache,this->errorlog);
00698         if (!this->readerror)
00699                 this->error = CONF_FILE_NOT_FOUND;
00700 };

ConfigReader::~ConfigReader  ) 
 

Default destructor.

This method destroys the ConfigReader class.

Definition at line 684 of file modules.cpp.

References cache, and errorlog.

00685 {
00686         if (this->cache)
00687                 delete this->cache;
00688         if (this->errorlog)
00689                 delete this->errorlog;
00690 }


Member Function Documentation

void ConfigReader::DumpErrors bool  bail,
userrec user
 

Dumps the list of errors in a config file to an output location.

If bail is true, then the program will abort. If bail is false and user points to a valid user record, the error report will be spooled to the given user by means of NOTICE. if bool is false AND user is false, the error report will be spooled to all opers by means of a NOTICE to all opers.

Definition at line 771 of file modules.cpp.

References errorlog, connection::fd, and userrec::nick.

00772 {
00773         if (bail)
00774         {
00775                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
00776                 exit(0);
00777         }
00778         else
00779         {
00780                 char dataline[1024];
00781                 if (user)
00782                 {
00783                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
00784                         while (!errorlog->eof())
00785                         {
00786                                 errorlog->getline(dataline,1024);
00787                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
00788                         }
00789                 }
00790                 else
00791                 {
00792                         WriteOpers("There were errors in the configuration file:",user->nick);
00793                         while (!errorlog->eof())
00794                         {
00795                                 errorlog->getline(dataline,1024);
00796                                 WriteOpers(dataline);
00797                         }
00798                 }
00799                 return;
00800         }
00801 }

int ConfigReader::Enumerate std::string  tag  ) 
 

Counts the number of times a given tag appears in the config file.

This method counts the number of times a tag appears in a config file, for use where there are several tags of the same kind, e.g. with opers and connect types. It can be used with the index value of ConfigReader::ReadValue to loop through all copies of a multiple instance tag.

Definition at line 804 of file modules.cpp.

References cache.

00805 {
00806         return EnumConf(cache,tag.c_str());
00807 }

int ConfigReader::EnumerateValues std::string  tag,
int  index
 

Returns the number of items within a tag.

For example if the tag was <test tag="blah" data="foo"> then this function would return 2. Spaces and newlines both qualify as valid seperators between values.

Definition at line 809 of file modules.cpp.

References cache.

00810 {
00811         return EnumValues(cache, tag.c_str(), index);
00812 }

long ConfigReader::GetError  ) 
 

Returns the last error to occur.

Valid errors can be found by looking in modules.h. Any nonzero value indicates an error condition. A call to GetError() resets the error flag back to 0.

Definition at line 764 of file modules.cpp.

References error.

00765 {
00766         long olderr = this->error;
00767         this->error = 0;
00768         return olderr;
00769 }

bool ConfigReader::ReadFlag std::string  tag,
std::string  name,
int  index
 

Retrieves a boolean value from the config file.

This method retrieves a boolean value from the config file. Where multiple copies of the tag exist in the config file, index indicates which of the values to retrieve. The values "1", "yes" and "true" in the config file count as true to ReadFlag, and any other value counts as false.

Definition at line 718 of file modules.cpp.

References cache, CONF_VALUE_NOT_FOUND, and error.

00719 {
00720         char val[MAXBUF];
00721         char t[MAXBUF];
00722         char n[MAXBUF];
00723         strlcpy(t,tag.c_str(),MAXBUF);
00724         strlcpy(n,name.c_str(),MAXBUF);
00725         int res = ReadConf(cache,t,n,index,val);
00726         if (!res)
00727         {
00728                 this->error = CONF_VALUE_NOT_FOUND;
00729                 return false;
00730         }
00731         std::string s = val;
00732         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
00733 }

long ConfigReader::ReadInteger std::string  tag,
std::string  name,
int  index,
bool  needs_unsigned
 

Retrieves an integer value from the config file.

This method retrieves an integer value from the config file. Where multiple copies of the tag exist in the config file, index indicates which of the values to retrieve. Any invalid integer values in the tag will cause the objects error value to be set, and any call to GetError() will return CONF_INVALID_NUMBER to be returned. needs_unsigned is set if the number must be unsigned. If a signed number is placed into a tag which is specified unsigned, 0 will be returned and GetError() will return CONF_NOT_UNSIGNED

Definition at line 735 of file modules.cpp.

References cache, CONF_NOT_A_NUMBER, CONF_NOT_UNSIGNED, CONF_VALUE_NOT_FOUND, and error.

00736 {
00737         char val[MAXBUF];
00738         char t[MAXBUF];
00739         char n[MAXBUF];
00740         strlcpy(t,tag.c_str(),MAXBUF);
00741         strlcpy(n,name.c_str(),MAXBUF);
00742         int res = ReadConf(cache,t,n,index,val);
00743         if (!res)
00744         {
00745                 this->error = CONF_VALUE_NOT_FOUND;
00746                 return 0;
00747         }
00748         for (int i = 0; i < strlen(val); i++)
00749         {
00750                 if (!isdigit(val[i]))
00751                 {
00752                         this->error = CONF_NOT_A_NUMBER;
00753                         return 0;
00754                 }
00755         }
00756         if ((needs_unsigned) && (atoi(val)<0))
00757         {
00758                 this->error = CONF_NOT_UNSIGNED;
00759                 return 0;
00760         }
00761         return atoi(val);
00762 }

std::string ConfigReader::ReadValue std::string  tag,
std::string  name,
int  index
 

Retrieves a value from the config file.

This method retrieves a value from the config file. Where multiple copies of the tag exist in the config file, index indicates which of the values to retrieve.

Definition at line 702 of file modules.cpp.

References cache, CONF_VALUE_NOT_FOUND, and error.

00703 {
00704         char val[MAXBUF];
00705         char t[MAXBUF];
00706         char n[MAXBUF];
00707         strlcpy(t,tag.c_str(),MAXBUF);
00708         strlcpy(n,name.c_str(),MAXBUF);
00709         int res = ReadConf(cache,t,n,index,val);
00710         if (!res)
00711         {
00712                 this->error = CONF_VALUE_NOT_FOUND;
00713                 return "";
00714         }
00715         return std::string(val);
00716 }

bool ConfigReader::Verify  ) 
 

Returns true if a config file is valid.

This method is partially implemented and will only return false if the config file does not exist or could not be opened.

Definition at line 814 of file modules.cpp.

References readerror.

00815 {
00816         return this->readerror;
00817 }


Member Data Documentation

std::stringstream* ConfigReader::cache [protected]
 

The contents of the configuration file This protected member should never be accessed by a module (and cannot be accessed unless the core is changed).

It will contain a pointer to the configuration file data with unneeded data (such as comments) stripped from it.

Definition at line 788 of file modules.h.

Referenced by ConfigReader(), Enumerate(), EnumerateValues(), ReadFlag(), ReadInteger(), ReadValue(), and ~ConfigReader().

long ConfigReader::error [protected]
 

Definition at line 793 of file modules.h.

Referenced by ConfigReader(), GetError(), ReadFlag(), ReadInteger(), and ReadValue().

std::stringstream* ConfigReader::errorlog [protected]
 

Definition at line 789 of file modules.h.

Referenced by ConfigReader(), DumpErrors(), and ~ConfigReader().

bool ConfigReader::readerror [protected]
 

Used to store errors.

Definition at line 792 of file modules.h.

Referenced by ConfigReader(), and Verify().


The documentation for this class was generated from the following files:
Generated on Wed Apr 13 12:32:23 2005 for InspIRCd by doxygen 1.3.3