Elektra  0.9.2
Functions
Methods for Making Tests

Methods to do various tests on Keys. More...

Collaboration diagram for Methods for Making Tests:

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 keyIsDirectlyBelow (const Key *key, const Key *check)
 Check if the key check is direct below the key key or not. 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...
 

Detailed Description

Methods to do various tests on Keys.

To use them:

#include <kdb.h>

Function Documentation

◆ keyCmp()

int keyCmp ( const Key *  k1,
const Key *  k2 
)

Compare the name of two keys.

Returns
a number less than, equal to or greater than zero if k1 is found, respectively, to be less than, to match, or be greater than k2.

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:

  • A null pointer will be found to be smaller than every other key. If both are null pointers, 0 is returned.
  • A null name will be found to be smaller than every other name. If both are null names, 0 is returned.

If the name is equal then:

  • No owner will be found to be smaller then every other owner. If both don't have an owner, 0 is returned.
Note
the owner will only be used if the names are equal.

Given any Keys k1 and k2 constructed with keyNew(), following equation hold true:

succeed_if (keyCmp (0, 0) == 0, "all null pointers same");
succeed_if (keyCmp (k1, 0) == 1, "null pointer is smaller");
succeed_if (keyCmp (0, k2) == -1, "null pointer is smaller");

Here are some more examples:

Key *k1 = keyNew("user/a", KEY_END);
Key *k2 = keyNew("user/b", KEY_END);
// keyCmp(k1,k2) < 0
// keyCmp(k2,k1) > 0

And even more:

Key *k1 = keyNew("user/a", KEY_OWNER, "markus", KEY_END);
Key *k2 = keyNew("user/a", KEY_OWNER, "max", KEY_END);
// keyCmp(k1,k2) < 0
// keyCmp(k2,k1) > 0

Do not strcmp the keyName() yourself because the result differs from simple ascii comparison.

Parameters
k1the first key object to compare with
k2the second key object to compare with
See also
ksAppendKey(), ksAppend() will compare keys when appending
ksLookup() will compare keys during searching

◆ keyIsBelow()

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
Parameters
keythe key object to work with
checkthe key to find the relative position of
Return values
1if check is below key
0if it is not below or if it is the same key
-1if key or check is null
See also
keySetName(), keyGetName(), keyIsDirectlyBelow()

◆ keyIsBinary()

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.

Return values
1if it is binary
0if it is not
-1on NULL pointer
See also
keyGetBinary(), keySetBinary()
Parameters
keythe key to check

◆ keyIsDirectlyBelow()

int keyIsDirectlyBelow ( 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 an indirect relation
Parameters
keythe key object to work with
checkthe key to find the relative position of
Return values
1if check is below key
0if it is not below or if it is the same key
-1on null pointer
See also
keyIsBelow(), keySetName(), keyGetName()

◆ keyIsInactive()

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.

Parameters
keythe key object to work with
Return values
1if the key is inactive
0if the key is active
-1on NULL pointer or when key has no name

◆ keyIsString()

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.

Return values
1if it is string
0if it is not
-1on NULL pointer
See also
keyGetString(), keySetString()
Parameters
keythe key to check

◆ keyNeedSync()

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().

Note
Note that the sync status will be updated on any change, including metadata.
Deprecated:
The handling of synchronization is done internally and does not need to be checked by neither application nor plugins.
See also
after keyNew(), keyDup() keys need sync
Parameters
keythe key object to work with
Return values
1if key was changed in memory, 0 otherwise
-1on NULL pointer
keyCmp
int keyCmp(const Key *k1, const Key *k2)
Compare the name of two keys.
Definition: keyset.c:645
KEY_OWNER
@ KEY_OWNER
Definition: kdbenum.c:25
KEY_END
@ KEY_END
Definition: kdbenum.c:41
keyNew
Key * keyNew(const char *name,...)
A practical way to fully create a Key object in one step.
Definition: key.c:167