elektra-key (3) Linux Manual Page
NAME
Key :: Basic Methods –
Key construction and initialization methods.
Functions
Key * keyNew (const char *keyName,…)
Key * keyDup (const Key *source)
int keyCopy (Key *dest, const Key *source)
int keyDel (Key *key)
ssize_t keyIncRef (Key *key)
ssize_t keyDecRef (Key *key)
ssize_t keyGetRef (const Key *key)
Detailed Description
Key construction and initialization methods.
To use them:
#include <kdb.h>
A Key is the essential class that encapsulates key name , value and metainfo . Key properties are:
- *
-
Key name - *
-
Key value - *
-
Data type - *
-
Key comment - *
-
Key owner - *
-
UID, GID and filesystem-like mode permissions - *
-
Mode, change and modification times
Described here the methods to allocate and free the key.
Function Documentation
int keyCopy (Key * dest, const Key * source)Copy or Clear a key.
Most often you may prefer keyDup() which allocates a new key and returns a duplication of another key.
But when you need to copy into an existing key, e.g. because it was passed by a pointer in a function you can do so:
int h(Key *k)
{
// receive key c
keyCopy(k, c);
// the caller will see the changed key k
}
The reference counter will not change for the destination key. Affiliation to keysets are also not affected.
When you pass a NULL-pointer as source the data of dest will be cleaned completely and you get a fresh dest key.
int g(Key *k)
{
keyCopy(k, 0);
// k is now an empty and fresh key
}
Parameters:
- dest the key which will be written to
source the key which should be copied or NULL to clean the destination key
Returns:
- -1 on failure when a NULL pointer was passed for dest or a dynamic property could not be written.
0 when dest was cleaned
1 when source was successfully copied
See also:
-
keyDup()to get a duplication of aKey :: Basic Methods
ssize_t keyDecRef (Key * key)Decrement the viability of a key object.
The reference counter can’t be decremented once it reached 0. In that situation nothing will happen and 0 will be returned.
Returns:
- the value of the new reference counter
-1 on null pointer
0 when the key is ready to be freed
Parameters:
- key the key object to work with
See also:
-
keyGetRef(),keyDel(),keyIncRef()
int keyDel (Key * key)A destructor for Key objects.
Every key created by keyNew() must be deleted with keyDel().
It is save to delete keys which are in a keyset, the number of references will be returned then.
It is save to delete a nullpointer, -1 will be returned then.
Parameters:
- key the key object to delete
See also:
-
keyNew(), keyInc(),keyGetRef()
Returns:
- the value of the reference counter if the key is within keyset(s)
0 when the key was freed
-1 on null pointers
Key* keyDup (const Key * source)Return a duplicate of a key.
Memory will be allocated as needed for dynamic properties.
The new key will not be member of any KeySet and will start with a new reference counter at 0. A subsequent keyDel() will delete the key.
int f(const Key *source)
{
Key *dup = keyDup(source);
// work with duplicate
keyDel(dup);
// everything related to dup is freed
// and source is unchanged
}
Like for a new key after keyNew() a subsequent ksAppend() makes a KeySet to take care of the lifecycle of the key.
int g(const Key *source, KeySet *ks)
{
Key *dup = keyDup(source);
// work with duplicate
ksAppendKey(ks, dup);
// ksDel(ks) will also free the duplicate
// source remains unchanged.
}
Duplication of keys should be preferred to keyNew(), because data like owner can be filled with a copy of the key instead of asking the environment. It can also be optimized in the checks, because the keyname is known to be valid.
Parameters:
- source has to be an initializised source Key
Returns:
- 0 failure or on NULL pointer
a fully copy of source on success
See also:
-
ksAppend(),keyDel()keyClear(),
keyNew()
ssize_t keyGetRef (const Key * key)Return how many references the key has.
The references will be incremented when ksAppendKey() or ksAppend() uses the key and will be decremented when ksPop() is used.
keyDup() will reset the references for dupped key.
For your own applications you can use keyIncRef() and keyDelRef() for reference counting. Keys with zero references will be deleted when using keyDel().
Parameters:
- key the key object to work with
Returns:
- the number of references
-1 on null pointer
See also:
-
keyIncRef()andkeyDecRef()
ssize_t keyIncRef (Key * key)Increment the viability of a key object.
This function is intended for applications using their own reference counter for key objects. With it you can increment the reference and thus avoid destruction of the object in a subsequent keyDel().
Key *k;
keyInc(k);
function_that_keyDec(k);
// work with k
keyDel(k); // now really free it
The reference counter can’t be incremented once it reached SSIZE_MAX. In that situation nothing will happen and SSIZE_MAX will be returned.
Returns:
- the value of the new reference counter
-1 on null pointer
SSIZE_MAX when maximum exceeded
Parameters:
- key the key object to work with
See also:
-
keyGetRef(),keyDecRef(),keyDel()
Key* keyNew (const char * keyName, …)A practical way to fully create a Key object in one step.
This function tries to mimic the C++ way for constructors.
To just get a key object, simple do:
Key *k = keyNew(0);
// work with it
keyDel(k);
If you want the key object to contain a name, value, comment and other meta info read on.
Note:
- When you already have a key with similar properties its easier and cheaper to
keyDup()the key.
Due to ABI compatibility, the Key structure is not defined in kdb.h, only declared. So you can only declare pointers to Keys in your program, and allocate and free memory for them with keyNew() and keyDel() respectively. See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135
You can call it in many different ways depending on the attribute tags you pass as parameters. Tags are represented as the keyswitch_t values, and tell keyNew() which Key attribute comes next.
The simplest and minimum way to use it is with no tags, only a key name:
Key *nullKey, *emptyNamedKey;
// Create a key that has no name, is completely empty, but is initialized
nullKey = keyNew(0);
keyDel(nullKey);
// Is the same as above
nullKey = keyNew(”, KEY_END);
keyDel(nullKey);
// Create and initialize a key with a name and nothing else
emptyNamedKey = keyNew(‘user/some/example’, KEY_END);
keyDel(emptyNamedKey);
keyNew() allocates memory for a key object and cleans everything up. After that, it processes the given argument list.
The Key attribute tags are the following:
- *
- keyswitch_t::KEY_TYPE
Next parameter is a type of the value. Default assumed is KEY_TYPE_UNDEFINED. Set this attribute so that a subsequent KEY_VALUE can toggle to keySetString()or keySetBinary()regarding to keyIsString()or keyIsBinary(). If you don’t use KEY_TYPE but a KEY_VALUE follows afterwards, KEY_TYPE_STRING will be used. - *
- keyswitch_t::KEY_SIZE
Define a maximum length of the value. This is especially useful for setting a binary key. So make sure you use that before you KEY_VALUE for binary keys. - *
- keyswitch_t::KEY_VALUE
Next parameter is a pointer to the value that will be set to the key If no keyswitch_t::KEY_TYPE was used before, keyswitch_t::KEY_TYPE_STRING is assumed. If KEY_TYPE was previously passed with a KEY_TYPE_BINARY, you should have passed KEY_SIZE before! Otherwise it will be cut of with first
