---
name: "Keydata"
section: "3cc"
platform: "linux"
package: "libcommoncpp2-dev"
version: "1.0.13-5"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/Keydata"
---

# Keydata(3cc)

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

## NAME

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

## SYNOPSIS

`#include <misc.h>`

Inherits **MemPager**.

### Public Methods

**CCXX\_MEMBER\_EXPORT** (CCXX\_EMPTY) Keydata()\
 *Create an empty key data object.* **CCXX\_MEMBER\_EXPORT** (CCXX\_EMPTY) Keydata(const char \***keypath**\
 *Create a new key data object and use 'Load' method to load an initial config file section into it.* **CCXX\_MEMBER\_EXPORT** (virtual)~Keydata()\
 *Destroy the keydata object and all allocated memory.* **CCXX\_MEMBER\_EXPORT** (void) unlink(void)\
 *Unlink the keydata object from the cache file stream.* **CCXX\_MEMBER\_EXPORT** (int) getCount(const char \*sym)\
 *Get a count of the number of data 'values' that is associated with a specific keyword.* **CCXX\_MEMBER\_EXPORT** (const char \*) getFirst(const char \*sym)\
 *Get the first data value for a given keyword.* **CCXX\_MEMBER\_EXPORT** (const char \*) getLast(const char \*sym)\
 *Get the last (most recently set) value for a given keyword.* **CCXX\_MEMBER\_EXPORT** (unsigned) getIndex(char \*\***data**\
 *Get an index array of ALL keywords that are stored by the current keydata object.* **CCXX\_MEMBER\_EXPORT** (unsigned) getCount(void)\
 *Get the count of keyword indexes that are actually available so one can allocate a table to receive getIndex.* **CCXX\_MEMBER\_EXPORT** (void) setValue(const char \*sym\
 *Set (replace) the value of a given keyword.* **CCXX\_MEMBER\_EXPORT** (const char \*const \*) getList(const char \*sym)\
 *Return a list of all values set for the given keyword returned in order.* **CCXX\_MEMBER\_EXPORT** (void) clrValue(const char \*sym)\
 *Clear all values associated with a given keyword.* const char \* **operator\[\]** (const char \*keyword)\
 *A convient notation for accessing the keydata as an associative array of keyword/value pairs through the \[\] operator.* friend **CCXX\_EXPORT** (void) endKeydata(void)\
 *Shutdown the file stream cache.*

### Public Attributes

const char \* **environment** = 'CONFIG\_KEYDATA')\
 unsigned **max**\
 const char \* **data**\

### Protected Methods

**CCXX\_MEMBER\_EXPORT** (**Keysym** \*) getSymbol(const char \*sym\
 **CCXX\_MEMBER\_EXPORT** (void) load(const char \***keypath**\
 *Load additional key values into the currrent object from the specfied config source (a config file/section pair).* **CCXX\_MEMBER\_EXPORT** (void) loadPrefix(const char \*prefix\
 *Load additional key values into the currrent object from the specfied config source (a config file/section pair).* **CCXX\_MEMBER\_EXPORT** (void) load(**Define** \*pairs)\
 *Load default keywords into the current object.*

### Protected Attributes

bool **create**\
 const char \* **environment** = 'CONFIG\_KEYDATA')\
 const char \* **keypath**\
 const char const char \* **environment** = 'CONFIG\_KEYDATA')\

## DETAILED DESCRIPTION

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

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]`'. The syntax is:

`.nf`

` [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 '`/`' path refers to '`/etc`' prefixed (on UNIX) directories and this is processed within the constructor. It could refer to the `/config` prefix on QNX, or even, gasp, a '`C:\WINDOWS`'. Hence, a keypath of '`/bayonne.d/vmhost/smtp`' actually resolves to a '`/etc/bayonne.d/vmhost.conf`' and loads key value pairs from the `[smtp]` section of that `.conf` file.

Similarly, something like '`~bayonne/smtp`' path refers to a '`~/.bayonnerc`' 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 '`/etc/server.conf?`' file, I might define something like:

`.nf`

` 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@ostel.com\>**

## MEMBER FUNCTION DOCUMENTATION

### Keydata::CCXX\_EXPORT (void)

**Shutdown the file stream cache.**

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

### Keydata::CCXX\_MEMBER\_EXPORT (void) const

**Clear all values associated with a given keyword.**

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

**Returns:**  **keyword name to clear.**

### Keydata::CCXX\_MEMBER\_EXPORT (const char \*const \*) const

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

**Returns:**  **list pointer of array holding all keyword values.**

**Parameters:**

- *sym*

  *keyword name to fetch.*

### Keydata::CCXX\_MEMBER\_EXPORT (void) const

*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:**

- *sym*

  *keyword name to set.*

- *data*

  *string to store for the keyword.*

### Keydata::CCXX\_MEMBER\_EXPORT (unsigned)

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

**Returns:**  *number of keywords found.*

### Keydata::CCXX\_MEMBER\_EXPORT (unsigned)

*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.*

### Keydata::CCXX\_MEMBER\_EXPORT (const char \*) const

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

*This is typically the value actually used.*

**Parameters:**

- *sym*

  *keywork symbol name.*

**Returns:**  *last set value for this symbol.*

### Keydata::CCXX\_MEMBER\_EXPORT (const char \*) const

*Get the first data value for a given keyword.*

*This will typically be the* `/etc` *set global default.*

**Parameters:**

- *sym*

  *keyword symbol name.*

**Returns:**  *first set value for this symbol.*

### Keydata::CCXX\_MEMBER\_EXPORT (int) const

*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:**

- *sym*

  *keyword symbol name.*

**Returns:**  *count of values associated with keyword.*

### Keydata::CCXX\_MEMBER\_EXPORT (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().*

### Keydata::CCXX\_MEMBER\_EXPORT (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.*

### Keydata::CCXX\_MEMBER\_EXPORT (CCXX\_EMPTY) const

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

**Parameters:**

- *keypath*

  *(filepath/section)*

- *environment*

  *optional environment variable that specifies the home path.*

### Keydata::CCXX\_MEMBER\_EXPORT (CCXX\_EMPTY)

*Create an empty key data object.*

### Keydata::CCXX\_MEMBER\_EXPORT (void)` [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:**

- *pairs*

  *list of NULL terminated default keyword/value pairs.*

### Keydata::CCXX\_MEMBER\_EXPORT (void) const` [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. This varient puts a prefix in front of the key name.*

**Parameters:**

- *prefix*

  - *keypath*

  *(filepath/section)*

- *environment*

  *optional environment variable that specifies the home path.*

### Keydata::CCXX\_MEMBER\_EXPORT (void) const` [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)*

- *environment*

  *string*

### Keydata::CCXX\_MEMBER\_EXPORT (Keysym \*) const` [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.*

## MEMBER DATA DOCUMENTATION

### bool Keydata::create` [protected]`

### const char\* Keydata::data

### const char\* Keydata::environment = 'CONFIG\_KEYDATA')

### const char const char\* Keydata::environment = 'CONFIG\_KEYDATA')` [protected]`

### const char\* Keydata::environment = 'CONFIG\_KEYDATA')` [protected]`

### const char\* Keydata::keypath` [protected]`

### unsigned Keydata::max

## AUTHOR

*Generated automatically by Doxygen for CommonC++ from the source code.*
