Elektra
0.9.6
|
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) |
Will clear all internal data of a Key. More... | |
ssize_t | keyIncRef (Key *key) |
Increment the reference counter of a Key object. More... | |
ssize_t | keyDecRef (Key *key) |
Decrement the reference counter 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 lock parts of a Key. More... | |
int | keyIsLocked (const Key *key, elektraLockFlags what) |
Checks which parts of a Key are 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 | ) |
Will clear all internal data of a Key.
After this call you will receive a fresh Key - with no value, metadata or name.
The reference counter will stay unmodified.
key | the Key that should be cleared |
0 | on success |
-1 | on NULL pointer |
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 reference counter 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 reaches 0. In that situation nothing will happen and 0 will be returned.
key | the Key object whose reference counter should get decreased |
-1 | on NULL pointer |
0 | when the Key is ready to be freed |
int keyDel | ( | Key * | key | ) |
A destructor for Key objects.
Every Key created by keyNew() must be deleted with keyDel().
Keys contained in a KeySet will not be deleted and the number of references will be returned instead.
It is safe to delete a NULL pointer, -1 will be returned then.
It is also safe 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 whose reference counter to retrieve |
key's
reference counter -1 | on NULL pointer |
ssize_t keyIncRef | ( | Key * | key | ) |
Increment the reference counter 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.
key | the Key object whose reference counter should get increased |
-1 | on NULL pointer |
SSIZE_MAX | when reference counter reached SSIZE_MAX |
int keyIsLocked | ( | const Key * | key, |
elektraLockFlags | what | ||
) |
Checks which parts of a Key are locked.
key | the Key that should be checked for locks |
what | the parts of the Key that should checked for locks |
0 | if nothing is locked |
-1 | on error (NULL pointer) |
int keyLock | ( | Key * | key, |
elektraLockFlags | what | ||
) |
Permanently lock parts of a Key.
This can be:
To unlock the Key, duplicate it.
It is also possible to lock the Key when it 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 | the Key that should be locked |
what | the parts of the Key that should be locked (see above) |
0 | if everything was locked before |
-1 | if it could not be locked (NULL pointer) |
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.KEY_BINARY
Allows one to change the key to a binary key. Make sure that you also pass KEY_SIZE before you set the value. Otherwise it will be cut off with first \0 in the string. So this flag toggle from keySetString() to keySetBinary(). If no value (nor size) is given, it will be a NULL key.
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()). |