Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends
Keydata Class Reference

Keydata objects are used to load and hold "configuration" data for a given application. More...

#include <misc.h>

Inheritance diagram for Keydata:
Inheritance graph
[legend]
Collaboration diagram for Keydata:
Collaboration graph
[legend]

Data Structures

struct  Define
 
struct  Keysym
 
struct  Keyval
 

Public Member Functions

void load (const char *keypath)
 Load additional key values into the currrent object from the specfied config source (a config file/section pair). More...
 
void loadPrefix (const char *prefix, const char *keypath)
 Load additional key values into the currrent object from the specfied config source (a config file/section pair). More...
 
void loadFile (const char *filepath, const char *keys=NULL, const char *pre=NULL)
 Load additional keys into the current object using a real filename that is directly passed rather than a computed key path. More...
 
void load (Define *pairs)
 Load default keywords into the current object. More...
 
 Keydata ()
 Create an empty key data object. More...
 
 Keydata (const char *keypath)
 Create a new key data object and use "Load" method to load an initial config file section into it. More...
 
 Keydata (Define *pairs, const char *keypath=NULL)
 Alternate constructor can take a define list and an optional pathfile to parse. 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...
 
bool isKey (const char *sym)
 Find if a given key exists. More...
 
const char * getString (const char *sym, const char *def=NULL)
 Get a string value, with an optional default if missing. More...
 
long getLong (const char *sym, long def=0)
 Get a long value, with an optional default if missing. More...
 
bool getBool (const char *key)
 Get a bool value. More...
 
double getDouble (const char *key, double def=0.)
 Get a floating value. More...
 
unsigned getIndex (char **data, unsigned max)
 Get an index array of ALL keywords that are stored by the current keydata object. More...
 
unsigned getCount (void)
 Get the count of keyword indexes that are actually available so one can allocate a table to receive getIndex. 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. More...
 

Static Public Member Functions

static void end (void)
 static member to end keydata i/o allocations. More...
 

Protected Member Functions

KeysymgetSymbol (const char *sym, bool create)
 
virtual void * first (size_t size)
 Allocate first workspace from paged memory. More...
 
char * first (char *str)
 Allocate a string from the memory pager pool and copy the string into it's new memory area. More...
 
virtual void * alloc (size_t size)
 Allocate memory from either the currently active page, or allocate a new page for the object. More...
 
char * alloc (const char *str)
 Allocate a string from the memory pager pool and copy the string inti it's new memory area. More...
 
void purge (void)
 purge the current memory pool. More...
 
void clean (void)
 Clean for memory cleanup before exiting. More...
 
int getPages (void)
 Return the total number of pages that have been allocated for this memory pool. More...
 

Private Member Functions

unsigned getIndex (const char *sym)
 Compute a hash key signature id for a symbol name. More...
 

Private Attributes

int link
 
Keysymkeys [97]
 

Static Private Attributes

static std::ifstream * cfgFile
 
static char lastpath [256+1]
 
static int count
 
static int sequence
 

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.

This class is used to load and then hold "<code>keyword = value</code>" pairs parsed from a text based "config" file that has been divided into "<code>[sections]</code>". The syntax is:

[section_name]
key1=value1
key2=value2

Essentially, the "path" is a "keypath" into a theoretical namespace of key pairs, hence one does not use "real" filepaths that may be OS dependent. The "<code>/</code>" path refers to "<code>/etc</code>" prefixed (on UNIX) directories and this is processed within the constructor. It could refer to the /config prefix on QNX, or even, gasp, a "<code>C:\WINDOWS</code>". Hence, a keypath of "<code>/bayonne.d/vmhost/smtp</code>" actually resolves to a "<code>/etc/bayonne.d/vmhost.conf</code>" and loads key value pairs from the [smtp] section of that .conf file.

Similarly, something like "<code>~bayonne/smtp</code>" path refers to a "<code>~/.bayonnerc</code>" and loads key pairs from the [smtp] section. This coercion occurs before the name is passed to the open call.

I actually use derived keydata based classes as global initialized objects, and they hence automatically load and parse config file entries even before "main" has started.

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.

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 "<code>/etc/server.conf?</code>" file, I might define something like:

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

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

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

KeyPaths keypaths;

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m load text configuration files into keyword pairs.

Definition at line 352 of file misc.h.

Constructor & Destructor Documentation

Keydata::Keydata ( )

Create an empty key data object.

Keydata::Keydata ( const char *  keypath)

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

Parameters
keypath(filepath/section) specifies the home path.
Keydata::Keydata ( Define pairs,
const char *  keypath = NULL 
)

Alternate constructor can take a define list and an optional pathfile to parse.

Parameters
pairsof keyword values from a define list
keypathof optional file and section to load from
virtual 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

virtual void* MemPager::alloc ( size_t  size)
protectedvirtualinherited

Allocate memory from either the currently active page, or allocate a new page for the object.

Parameters
sizesize of memory to allocate.
Returns
pointer to allocated memory.

Reimplemented in SharedMemPager.

char* MemPager::alloc ( const char *  str)
protectedinherited

Allocate a string from the memory pager pool and copy the string inti it's new memory area.

This checks only the last active page for available space before allocating a new page.

Parameters
strstring to allocate and copy into paged memory pool.
Returns
copy of string from allocated memory.
void MemPager::clean ( void  )
protectedinherited

Clean for memory cleanup before exiting.

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.
static void Keydata::end ( void  )
static

static member to end keydata i/o allocations.

virtual void* MemPager::first ( size_t  size)
protectedvirtualinherited

Allocate first workspace from paged memory.

This method scans all currently allocated blocks for available space before adding new pages and hence is both slower and more efficient.

Parameters
sizesize of memory to allocate.
Returns
pointer to allocated memory.

Reimplemented in SharedMemPager.

char* MemPager::first ( char *  str)
protectedinherited

Allocate a string from the memory pager pool and copy the string into it's new memory area.

This method allocates memory by first searching for an available page, and then allocating a new page if no space is found.

Parameters
strstring to allocate and copy into paged memory pool.
Returns
copy of string from allocated memory.
bool Keydata::getBool ( const char *  key)

Get a bool value.

Parameters
symkeyword name.
Returns
true or false.
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 "<code>load()</code>" requests.

Parameters
symkeyword symbol name.
Returns
count of values associated with keyword.
unsigned Keydata::getCount ( void  )

Get the count of keyword indexes that are actually available so one can allocate a table to receive getIndex.

Returns
number of keywords found.
double Keydata::getDouble ( const char *  key,
double  def = 0. 
)

Get a floating value.

Parameters
symkeyword name.
defaultif not set.
Returns
value of key.
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
symkeyword symbol name.
Returns
first set value for this symbol.
unsigned Keydata::getIndex ( const char *  sym)
private

Compute a hash key signature id for a symbol name.

Returns
key signature index path.
Parameters
symsymbol name.
unsigned Keydata::getIndex ( char **  data,
unsigned  max 
)

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

Returns
number of keywords found.
Parameters
datapointer of array to hold keyword strings.
maxnumber 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
symkeyword 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
symkeyword name to fetch.
long Keydata::getLong ( const char *  sym,
long  def = 0 
)

Get a long value, with an optional default if missing.

Parameters
symkeyword name.
defaultif not present.
Returns
long value of key.
int MemPager::getPages ( void  )
inlineinherited

Return the total number of pages that have been allocated for this memory pool.

Returns
number of pages allocated.

Definition at line 181 of file misc.h.

const char* Keydata::getString ( const char *  sym,
const char *  def = NULL 
)

Get a string value, with an optional default if missing.

Parameters
symkeyword name.
defaultif not present.
Returns
string value of key.
Keysym* Keydata::getSymbol ( const char *  sym,
bool  create 
)
protected
bool Keydata::isKey ( const char *  sym)

Find if a given key exists.

Parameters
symkeyword to find.
Returns
true if exists.
void Keydata::load ( const char *  keypath)

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::load ( Define pairs)

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
pairslist of NULL terminated default keyword/value pairs.
void Keydata::loadFile ( const char *  filepath,
const char *  keys = NULL,
const char *  pre = NULL 
)

Load additional keys into the current object using a real filename that is directly passed rather than a computed key path.

This also uses a [keys] section as passed to the object.

Parameters
filepathto load from
keyssection to parse from, or NULL to parse from head
preoptional key prefix
void Keydata::loadPrefix ( const char *  prefix,
const char *  keypath 
)

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. This varient puts a prefix in front of the key name.

Parameters
prefix
keypath(filepath/section)
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.

Definition at line 611 of file misc.h.

void MemPager::purge ( void  )
protectedinherited

purge the current memory pool.

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
symkeyword name to set.
datastring to store for the keyword.
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().

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.

Definition at line 623 of file misc.h.

Field Documentation

std::ifstream* Keydata::cfgFile
staticprivate

Definition at line 382 of file misc.h.

int Keydata::count
staticprivate

Definition at line 384 of file misc.h.

Keysym* Keydata::keys[97]
private

Definition at line 389 of file misc.h.

char Keydata::lastpath[256+1]
staticprivate

Definition at line 383 of file misc.h.

int Keydata::link
private

Definition at line 387 of file misc.h.

int Keydata::sequence
staticprivate

Definition at line 385 of file misc.h.


The documentation for this class was generated from the following file: