$darkmode
Elektra 0.11.0
|
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_VALUE = 1 << 1 , KEY_FLAGS = 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... | |
uint16_t | keyIncRef (Key *key) |
Increment the reference counter of a Key object. More... | |
uint16_t | keyDecRef (Key *key) |
Decrement the reference counter of a Key object. More... | |
uint16_t | keyGetRef (const Key *key) |
Return the current reference counter value of a Key object. 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_VALUE | Flag for the key data |
KEY_FLAGS | Allows to define multiple flags at once. |
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's
name is "/" key's
metadata is emptykey | 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
must be a valid Key (created with keyNew) dest
must not have read-only flags set source
must be a valid Key or NULL 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()) |
uint16_t keyDecRef | ( | Key * | key | ) |
Decrement the reference counter of a Key object.
As long as the reference counter is non-zero, keyDel()
operations on key
will be a no-op and return an error code.
key's
reference counter is >= 0 key's
reference counter is < SSIZE_MAXkey | the Key object whose reference counter should get decreased |
UINT16_MAX | on NULL pointer |
0 | when the reference counter already was the minimum value 0, the reference counter will not be modified in this case |
int keyDel | ( | Key * | key | ) |
A destructor for Key objects.
Every Key created by keyNew() must be deleted with keyDel().
When the reference counter of key
is non-zero, this function will do nothing and simply return the current value of the reference counter.
It is therefore safe to call keyDel (k)
on any Key * k
.
key
will be freedkey | the Key object to delete |
0 | when the Key was freed |
-1 | on NULL pointers |
uint16_t keyGetRef | ( | const Key * | key | ) |
Return the current reference counter value of a Key object.
key | the Key whose reference counter to retrieve |
key's
reference counter -1 | on NULL pointer |
uint16_t keyIncRef | ( | Key * | key | ) |
Increment the reference counter of a Key object.
As long as the reference counter is non-zero, keyDel()
operations on key
will be a no-op and return an error code.
Elektra's system for reference counting is not based on a concept of shared ownership. It is more similar to a shared lock, where the counter is used to keep track of how many clients hold the lock.
Initially, the reference counter will be 0. This is can be interpreted as the lock being unlocked. When you increment the reference counter, the lock becomes locked and keyDel()
is blocked and fails. Only when the reference counter is fully decremented back down to 0 again, will keyDel()
work again.
UINT16_MAX - 1
. UINT16_MAX
is reserved as an error code.key's
reference counter is > 0 key's
reference counter is <= UINT16_MAX - 1key | the Key object whose reference counter should be increased |
UINT16_MAX | on NULL pointer |
UINT16_MAX | when the reference counter already was the maximum value UINT16_MAX - 1 , the reference counter will not be modified in this case |
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.
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
is a valid Key name name | a valid name to the key (see keySetName()) |
NULL | on allocation error or if an invalid name was passed (see keySetName()). |