Elektra
0.9.5
|
Key is an essential class that encapsulates key name , value and metainfo . More...
Modules | |
Meta Info Manipulation Methods | |
Methods to do various operations on Key metadata. | |
Methods for Making Tests | |
Methods to do various tests on Keys. | |
Name Manipulation Methods | |
Methods to do various operations on Key names. | |
Value Manipulation Methods | |
Methods to do various operations on Key values. | |
Macros | |
#define | KDB_PATH_SEPARATOR '/' |
/ is used to separate key names. More... | |
#define | KDB_PATH_ESCAPE '\\' |
\ is used as escape character in the key name. More... | |
Enumerations | |
enum | elektraKeyFlags { KEY_NAME = 1, KEY_VALUE = 1 << 1, KEY_FLAGS = 3, KEY_COMMENT = 1 << 3, KEY_BINARY = 1 << 4, KEY_SIZE = 1 << 11, KEY_META = 1 << 15, KEY_NULL = 1 << 16, KEY_END = 0 } |
Allows keyNew() to determine which information comes next. More... | |
enum | elektraCopyFlags { KEY_CP_NAME = 1 << 0, KEY_CP_STRING = 1 << 1, KEY_CP_VALUE = 1 << 2, KEY_CP_META = 1 << 3, KEY_CP_ALL = KEY_CP_NAME | KEY_CP_VALUE | KEY_CP_META } |
Copy options. More... | |
enum | elektraLockFlags { KEY_LOCK_NAME = 1 << 17, KEY_LOCK_VALUE = 1 << 18, KEY_LOCK_META = 1 << 19 } |
Lock options. More... | |
enum | elektraNamespace { KEY_NS_NONE = 0, KEY_NS_CASCADING = 1, KEY_NS_META = 2, KEY_NS_SPEC = 3, KEY_NS_PROC = 4, KEY_NS_DIR = 5, KEY_NS_USER = 6, KEY_NS_SYSTEM = 7, KEY_NS_DEFAULT = 8 } |
Elektra currently supported Key namespaces. More... | |
Functions | |
Key * | keyNew (const char *name,...) |
A practical way to fully create a Key object in one step. More... | |
Key * | keyCopy (Key *dest, const Key *source, elektraCopyFlags flags) |
Copy or clear a key. More... | |
int | keyDel (Key *key) |
A destructor for Key objects. More... | |
int | keyClear (Key *key) |
Key Object Cleaner. More... | |
ssize_t | keyIncRef (Key *key) |
Increment the viability of a key object. More... | |
ssize_t | keyDecRef (Key *key) |
Decrement the viability of a key object. More... | |
ssize_t | keyGetRef (const Key *key) |
Return how many references the key has. More... | |
int | keyLock (Key *key, elektraLockFlags what) |
Permanently locks a part of the key. More... | |
int | keyIsLocked (const Key *key, elektraLockFlags what) |
Tests if a part of a key is locked. More... | |
Key is an essential class that encapsulates key name , value and metainfo .
To use it include:
Key properties are:
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.#define KDB_PATH_ESCAPE '\\' |
\
is used as escape character in the key name.
#define KDB_PATH_SEPARATOR '/' |
/
is used to separate key names.
enum elektraCopyFlags |
Copy options.
enum elektraKeyFlags |
Allows keyNew() to determine which information comes next.
Enumerator | |
---|---|
KEY_NAME | Flag for the key name |
KEY_VALUE | Flag for the key data |
KEY_FLAGS | Allows to define multiple flags at once. |
KEY_COMMENT | Flag for the key comment |
KEY_BINARY | Flag if the key is binary |
KEY_SIZE | Flag for maximum size to limit value |
KEY_META | Flag for metadata |
KEY_NULL | Is not a flag, only as return value
|
KEY_END | Used as a parameter terminator to keyNew() |
enum elektraLockFlags |
Lock options.
Enumerator | |
---|---|
KEY_LOCK_NAME | lock the name of a key |
KEY_LOCK_VALUE | lock the value of a key |
KEY_LOCK_META | lock the meta data of a key |
enum elektraNamespace |
Elektra currently supported Key namespaces.
Enumerator | |
---|---|
KEY_NS_NONE | no key given as parameter to keyGetNamespace() |
KEY_NS_CASCADING | cascading key, starts with /, abstract name for any of the namespaces below |
KEY_NS_META | metakey, i.e. any key name not under other categories |
KEY_NS_SPEC | spec contains the specification of the other namespaces |
KEY_NS_PROC | proc contains process-specific configuration |
KEY_NS_DIR | dir contains configuration from a specific directory |
KEY_NS_USER | user key in the home directory of the current user |
KEY_NS_SYSTEM | system key is shared for a computer system |
KEY_NS_DEFAULT | default key used as a fallback if no other key is found |
int keyClear | ( | Key * | key | ) |
Key Object Cleaner.
Will reset all internal data.
After this call you will receive a fresh key.
The reference counter will stay unmodified.
returns | 0 on success |
-1 | on null pointer |
key | the key object to work with |
Key* keyCopy | ( | Key * | dest, |
const Key * | source, | ||
elektraCopyFlags | flags | ||
) |
Copy or clear a key.
Depending on the chosen flags
keyCopy() only copies certain parts of source
into dest
.
source
to dest
.source
to dest
.source
to dest
. Additionally, if source
is a binary key (keyIsBinary()), dest
will also be marked as binary. This means that even if KEY_CP_META is not set, the binary
meta key will be copied with KEY_CP_VALUE.source
to dest
, but only, if source
is not a binary key (keyIsBinary()). If source
is binary, keyCopy() fails. If dest
is binary, it will still be marked as binary after the copy. This cannot be used together with KEY_CP_VALUE. The main purpose of KEY_CP_STRING is for copying into known string keys. It ensure that you don't accidentally convert string keys into binary keys.There is also the shorthand KEY_CP_ALL. It is equivalent to KEY_CP_NAME | KEY_CP_VALUE | KEY_CP_META
, i.e. all key data supported by keyCopy() will be copied from source
to dest
.
Use this function 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:
Most often you will want to duplicate an existing key. For this purpose the alias keyDup() exists. Calling
is equivalent to
The reference counter will not be changed for both keys. Affiliation to keysets are also not affected.
Since metadata uses copy-on-write semantics there is only a constant memory cost to copying metadata.
When you pass a NULL-pointer as source
the pieces of dest
specified by flags
will be cleared.
Calling keyCopy (dest, NULL, KEY_CP_ALL)
is different from calling keyClear(). The key will not be fully reset, the reference counter and internal flags will remain unchanged. Additionally, keyCopy() respects keyLock() state, while keyClear() always works.
dest | the key which will be written to |
source | the key which should be copied or NULL to clear the data of dest |
flags | specifies which parts of the key should be copied |
dest
NULL | on memory allocation problems |
NULL | when a part of dest that should be modified (e.g. name, value) was marked read-only, e.g. the name of dest will be read-only if dest is part of a KeySet |
NULL | when dest is NULL |
NULL | when both KEY_CP_VALUE and KEY_CP_STRING are set in flags |
NULL | when both KEY_CP_STRING is set in flags and source is a binary key (keyIsBinary()) |
ssize_t keyDecRef | ( | Key * | key | ) |
Decrement the viability of a key object.
The references will be decremented for ksPop() or successful calls of ksLookup() with the option KDB_O_POP. It will also be decremented with an following keyDel() in the case that an old key is replaced with another key with the same name.
The reference counter can't be decremented once it reached 0. In that situation nothing will happen and 0 will be returned.
-1 | on null pointer |
0 | when the key is ready to be freed |
key | the key object to work with |
int keyDel | ( | Key * | key | ) |
A destructor for Key objects.
Every key created by keyNew() must be deleted with keyDel().
It is safe 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.
It is also save to delete a multiple referenced key, nothing will happen then and the reference counter will be returned.
key | the key object to delete |
0 | when the key was freed |
-1 | on null pointers |
ssize_t keyGetRef | ( | const Key * | key | ) |
Return how many references the key has.
The reference counting is the essential property of keys to make sure that they can be put safely into data structures. E.g. if you put a Key into a KeySet:
You can even add the key to more KeySets:
If you increment only by one with keyIncRef() the same as said above is valid:
or use keyIncRef() more than once:
The key won't be deleted by a keyDel() as long refcounter is not 0.
The references will be incremented on successful calls to ksAppendKey() or ksAppend().
For your own applications you can use keyIncRef() and keyDecRef() for reference counting, too.
key | the key object to work with |
-1 | on null pointer |
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().
The reference counter can't be incremented once it reached SSIZE_MAX. In that situation nothing will happen and SSIZE_MAX will be returned.
-1 | on null pointer |
SSIZE_MAX | when maximum exceeded |
key | the key object to work with |
int keyIsLocked | ( | const Key * | key, |
elektraLockFlags | what | ||
) |
Tests if a part of a key is locked.
>0 | the bits that are locked |
0 | if everything is unlocked |
-1 | on error (nullpointer) |
int keyLock | ( | Key * | key, |
elektraLockFlags | what | ||
) |
Permanently locks a part of the key.
This can be:
To unlock the key, duplicate it.
It is also possible to lock when the key is created with keyNew().
Some data structures need to lock the key (most likely its name), so that the ordering does not get confused.
key | which name should be locked |
>0 | the bits that were successfully locked |
0 | if everything was locked before |
-1 | if it could not be locked (nullpointer) |
Key* keyNew | ( | const char * | name, |
... | |||
) |
A practical way to fully create a Key object in one step.
To just get a key object, simple do:
keyNew() allocates memory for a key object and keyDel() cleans everything up.
We can also give an empty key name and a KEY_END tag with the same effect as before:
But we can also give the key a proper name right from the start:
If you want the key object to contain a name, value, comment and other meta info read on.
You can call keyNew() in many different ways depending on the attribute tags you pass as parameters. Tags are represented as elektraKeyFlags values, and tell keyNew() which Key attribute comes next. The Key attribute tags are the following:
keyName
is 0.name | a valid name to the key, or NULL to get a simple initialized, but really empty, object |
NULL | on allocation error or if an invalid name was passed (see keySetName()). |