Google

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

Keydata Class Reference

Keydata objects are used to load and hold "configuration" data for a given application. load text configuration files into keyword pairs. More...

#include <misc.h>

Inheritance diagram for Keydata:

MemPager List of all members.

Public Methods

 Keydata ()
 Create an empty key data object.

 Keydata (const char *keypath, const char *environment="CONFIG_KEYDATA")
 Create a new key data object and use "Load" method to load an initial config file section into it. More...

virtual ~Keydata ()
 Destroy the keydata object and all allocated memory. More...

void Unlink (void)
 Unlink the keydata object from the cache file stream. More...

int getCount (const char *sym)
 Get a count of the number of data "values" that is associated with a specific keyword. More...

const char* getFirst (const char *sym)
 Get the first data value for a given keyword. More...

const char* getLast (const char *sym)
 Get the last (most recently set) value for a given keyword. More...

int getIndex (char **data, int max)
 Get an index array of ALL keywords that are stored by the current keydata object. More...

void setValue (const char *sym, const char *data)
 Set (replace) the value of a given keyword. More...

const char* const* getList (const char *sym)
 Return a list of all values set for the given keyword returned in order. More...

void clrValue (const char *sym)
 Clear all values associated with a given keyword. More...

const char* operator[] (const char *keyword)
 A convient notation for accessing the keydata as an associative array of keyword/value pairs through the [] operator.


Protected Methods

keysym_tgetSymbol (const char *sym, bool create)
void Load (const char *keypath, const char *environment = "CONFIG_KEYDATA")
 Load additional key values into the currrent object from the specfied config source (a config file/section pair). More...

void Load (KEYDEF *pairs)
 Load default keywords into the current object. More...


Friends

void endKeydata (void)
 Shutdown the file stream cache. More...


Detailed Description

Keydata objects are used to load and hold "configuration" data for a given application. load text configuration files into keyword pairs.

Each "keydata" object holds the key pairs from a [section] of a text readable config file. The /etc directory is presumed to hold the referenced file key set unless a ~ is used.

Keydata can hold multiple values for the same key pair. This can occur either from storing a "list" of data items in a config file, or when overlaying multiple config sources (such as /etc/....conf and ~/.confrc segments) into a single object. The keys are stored as cumulative (read-only/replacable) config values under a hash index system for quick retrieval.

The most useful of the misc. classes is the Keydata class. This class is used to load and then hold "keyword = value" pairs parsed from a text based "config" file that has been divided into "[sections]". Keydata can also load a table of "initialization" values for keyword pairs that were not found in the external file.

One typically derives an application specific keydata class to load a specific portion of a known config file and initialize it's values. One can then declare a global instance of these objects and have configuration data initialized automatically as the executable is loaded.

Hence, if I have a "[paths]" section in a "/etc/server.conf" file, I might define something like:

class KeyPaths : public Keydata { public: KeyPaths() : Keydata("/server/paths") { static KEYDEF *defvalues = { {"datafiles", "/var/server"}, {NULL, NULL}};

// override with [paths] from "~/.serverrc" if avail.

Load("~server/paths"); Load(defvalues); } };

KeyPaths keypaths;

Author(s):
David Sugar <dyfet@ostel.com>


Constructor & Destructor Documentation

Keydata::Keydata ( )
 

Create an empty key data object.

Keydata::Keydata ( const char * keypath,
const char * environment = "CONFIG_KEYDATA" )
 

Create a new key data object and use "Load" method to load an initial config file section into it.

Parameters:
keypath   (filepath/section)

Keydata::~Keydata ( ) [virtual]
 

Destroy the keydata object and all allocated memory.

This may also clear the "cache" file stream if no other keydata objects currently reference it.


Member Function Documentation

void Keydata::Load ( KEYDEF * pairs ) [protected]
 

Load default keywords into the current object.

This only loads keyword entries which have not already been defined to reduce memory usage. This form of Load is also commonly used in the constructor of a derived Keydata class.

Parameters:
list   of NULL terminated default keyword/value pairs.

void Keydata::Load ( const char * keypath,
const char * environment = "CONFIG_KEYDATA" ) [protected]
 

Load additional key values into the currrent object from the specfied config source (a config file/section pair).

These values will overlay the current keywords when matches are found. This can be used typically in a derived config object class constructor to first load a /etc section, and then load a matching user specific entry from ~/. to override default system values with user specific keyword values.

Parameters:
keypath   (filepath/section)

void Keydata::Unlink ( void )
 

Unlink the keydata object from the cache file stream.

This should be used if you plan to keepa Keydata object after it is loaded once all keydata objects have been loaded, otherwise the cfgFile stream will remain open. You can also use endKeydata.

void Keydata::clrValue ( const char * sym )
 

Clear all values associated with a given keyword.

This does not de-allocate the keyword from memory, however.

Returns:
keyword name to clear.

int Keydata::getCount ( const char * sym )
 

Get a count of the number of data "values" that is associated with a specific keyword.

Each value is from an accumulation of "load()" requests.

Parameters:
keyword   symbol name.
Returns:
count of values associated with keyword.

const char * Keydata::getFirst ( const char * sym )
 

Get the first data value for a given keyword.

This will typically be the /etc set global default.

Parameters:
keyword   symbol name.
Returns:
first set value for this symbol.

int Keydata::getIndex ( char ** data,
int max )
 

Get an index array of ALL keywords that are stored by the current keydata object.

Returns:
number of keywords found.
Parameters:
data   pointer of array to hold keyword strings.
max   number of entries the array can hold.

const char * Keydata::getLast ( const char * sym )
 

Get the last (most recently set) value for a given keyword.

This is typically the value actually used.

Parameters:
keywork   symbol name.
Returns:
last set value for this symbol.

const char *const * Keydata::getList ( const char * sym )
 

Return a list of all values set for the given keyword returned in order.

Returns:
list pointer of array holding all keyword values.
Parameters:
keyword   name to fetch.

keysym_t* Keydata::getSymbol ( const char * sym,
bool create ) [protected]
 

const char * Keydata::operator[] ( const char * keyword ) [inline]
 

A convient notation for accessing the keydata as an associative array of keyword/value pairs through the [] operator.

void Keydata::setValue ( const char * sym,
const char * data )
 

Set (replace) the value of a given keyword.

This new value will become the value returned from getLast(), while the prior value will still be stored and found from getList().

Parameters:
keyword   name to set.
data   string to store for the keyword.


Friends And Related Function Documentation

void endKeydata ( void ) [friend]
 

Shutdown the file stream cache.

This should be used before detaching a deamon, exec(), fork(), etc.


The documentation for this class was generated from the following file:
Generated at Fri Mar 23 10:47:55 2001 for CommonC++ by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000