Elektra
0.8.22
|
Methods to do various tests on Keys. More...
Functions | |
int | keyCmp (const Key *k1, const Key *k2) |
Compare the name of two keys. More... | |
int | keyNeedSync (const Key *key) |
Test if a key needs to be synced to backend storage. More... | |
int | keyIsBelow (const Key *key, const Key *check) |
Check if the key check is below the key key or not. More... | |
int | keyIsDirectBelow (const Key *key, const Key *check) |
Check if the key check is direct below the key key or not. More... | |
int | keyRel (const Key *key, const Key *check) |
Information about the relation in the hierarchy between two keys. More... | |
int | keyIsInactive (const Key *key) |
Check whether a key is inactive. More... | |
int | keyIsBinary (const Key *key) |
Check if a key is binary type. More... | |
int | keyIsString (const Key *key) |
Check if a key is string type. More... | |
Methods to do various tests on Keys.
To use them:
int keyCmp | ( | const Key * | k1, |
const Key * | k2 | ||
) |
Compare the name of two keys.
The comparison is based on a strcmp of the keynames, and iff they match a strcmp of the owner will be used to distuingish. If even this matches the keys are found to be exactly the same and 0 is returned. These two keys can't be used in the same KeySet.
keyCmp() defines the sorting order for a KeySet.
The following 3 points are the rules for null values:
If the name is equal then:
Often is enough to know if the other key is less then or greater than the other one. But Sometimes you need more precise information, see keyRel().
Given any Keys k1 and k2 constructed with keyNew(), following equation hold true:
Here are some more examples:
And even more:
Do not strcmp the keyName() yourself because the result differs from simple ascii comparison.
k1 | the first key object to compare with |
k2 | the second key object to compare with |
int keyIsBelow | ( | const Key * | key, |
const Key * | check | ||
) |
Check if the key check is below the key key or not.
Example:
key user/sw/app check user/sw/app/key
returns true because check is below key
Example:
key user/sw/app check user/sw/app/folder/key
returns also true because check is indirect below key
Obviously, there is no key above a namespace (e.g. user, system, /):
key * check user
key | the key object to work with |
check | the key to find the relative position of |
1 | if check is below key |
0 | if it is not below or if it is the same key |
-1 | if key or check is null |
int keyIsBinary | ( | const Key * | key | ) |
Check if a key is binary type.
The function checks if the key is a binary. Opposed to string values binary values can have '\0' inside the value and may not be terminated by a null character. Their disadvantage is that you need to pass their size.
Make sure to use this function and don't test the binary type another way to ensure compatibility and to write less error prone programs.
1 | if it is binary |
0 | if it is not |
-1 | on NULL pointer |
key | the key to check |
int keyIsDirectBelow | ( | const Key * | key, |
const Key * | check | ||
) |
Check if the key check is direct below the key key or not.
Example: key user/sw/app check user/sw/app/key returns true because check is below key Example: key user/sw/app check user/sw/app/folder/key does not return true, because there is only a indirect relation
key | the key object to work with |
check | the key to find the relative position of |
1 | if check is below key |
0 | if it is not below or if it is the same key |
-1 | on null pointer |
int keyIsInactive | ( | const Key * | key | ) |
Check whether a key is inactive.
In Elektra terminology a hierarchy of keys is inactive if the rootkey's basename starts with '.'. So a key is also inactive if it is below an inactive key. For example, user/key/.hidden is inactive and so is user/.hidden/below.
Inactive keys should not have any meaning to applications, they are only a convention reserved for users and administrators. To automatically remove all inactive keys for an application, consider to use the hidden plugin.
key | the key object to work with |
1 | if the key is inactive |
0 | if the key is active |
-1 | on NULL pointer or when key has no name |
int keyIsString | ( | const Key * | key | ) |
Check if a key is string type.
String values are null terminated and are not allowed to have any '\0' characters inside the string.
Make sure to use this function and don't test the string type another way to ensure compatibility and to write less error prone programs.
1 | if it is string |
0 | if it is not |
-1 | on NULL pointer |
key | the key to check |
int keyNeedSync | ( | const Key * | key | ) |
Test if a key needs to be synced to backend storage.
If any key modification took place the key will be flagged so that kdbSet() knows which keys were modified and which not.
After keyNew() the flag will normally be set, but after kdbGet() and kdbSet() the flag will be removed. When you modify the key the flag will be set again.
In your application you can make use of that flag to know if you changed something in a key after a kdbGet() or kdbSet().
key | the key object to work with |
1 | if key was changed in memory, 0 otherwise |
-1 | on NULL pointer |
int keyRel | ( | const Key * | key, |
const Key * | check | ||
) |
Information about the relation in the hierarchy between two keys.
Unlike keyCmp() the number gives information about hierarchical information.
user/key user/key
user/key/folder user/key/folder/child
user/key/folder user/key/folder/any/depth/deeper/grand-child
user/key/myself user/key/sibling
TODO Below is an idea how it could be extended: It could continue the search into the other direction if any (grand-)parents are equal.
user/key/myself user/key/sibling
user/key/myself user/key/sibling/nephew
user/key/myself user/key/sibling/any/depth/deeper/grand-nephew
The same holds true for the other direction, but with negative values. For no relation INT_MIN is returned.
2 | if below |
1 | if direct below |
0 | if the same |
-1 | on null or invalid keys |
-2 | if none of any other relation |
-3 | if same hierarchy (none of those below) |
-4 | if sibling (in same hierarchy) |
-5 | if nephew (in same hierarchy) |
key | the key object to work with |
check | the second key object to check the relation with |