Elektra  0.9.0
Public Member Functions | List of all members
kdb::Key Class Reference

Key is an essential class that encapsulates key name , value and metainfo . More...

#include <key.hpp>

Public Member Functions

 Key ()
 Constructs an empty, invalid key. More...
 
 Key (ckdb::Key *k)
 Constructs a key out of a C key. More...
 
 Key (Key &k)
 Takes a reference of another key. More...
 
 Key (Key const &k)
 Takes a reference of another key. More...
 
 Key (const char *keyName,...)
 A practical way to fully create a Key object in one step. More...
 
 Key (const std::string keyName,...)
 A practical way to fully create a Key object in one step. More...
 
 Key (const char *keyName, va_list ap)
 A practical way to fully create a Key object in one step. More...
 
void operator++ (int) const
 Increment the viability of a key object. More...
 
void operator++ () const
 Increment the viability of a key object. More...
 
void operator-- (int) const
 Decrement the viability of a key object. More...
 
void operator-- () const
 Decrement the viability of a key object. More...
 
ssize_t getReferenceCounter () const
 Return how many references the key has. More...
 
Keyoperator= (ckdb::Key *k)
 Assign a C key. More...
 
Keyoperator= (const Key &k)
 Assign a key. More...
 
void copy (const Key &other)
 Copy or Clear a key. More...
 
void clear ()
 Clears/Invalidates a key. More...
 
Keyoperator-> ()
 
ckdb::Key * getKey () const
 Passes out the raw key pointer. More...
 
ckdb::Key * operator* () const
 Is an abbreviation for getKey. More...
 
ckdb::Key * release ()
 Passes out the raw key pointer and resets internal key handle. More...
 
ckdb::Key * dup () const
 Return a duplicate of a key. More...
 
 ~Key ()
 Destructs the key. More...
 
std::string getName () const
 Returns a pointer to the abbreviated real internal key name. More...
 
ssize_t getNameSize () const
 Bytes needed to store the key name without owner. More...
 
std::string getBaseName () const
 Returns a pointer to the internal unescaped key name where the basename starts. More...
 
ssize_t getBaseNameSize () const
 Calculates number of bytes needed to store basename of key. More...
 
void setName (const std::string &newName)
 Set a new name to a key. More...
 
void setBaseName (const std::string &baseName)
 Sets a base name for a key. More...
 
void addBaseName (const std::string &baseName)
 Adds a base name for a key. More...
 
void delBaseName ()
 Delete the baseName of a key. More...
 
ssize_t getFullNameSize () const
 Bytes needed to store the key name including user domain and ending NULL. More...
 
std::string getFullName () const
 Get key full name, including the user domain name. More...
 
bool operator== (const Key &k) const
 Compare the name of two keys. More...
 
bool operator!= (const Key &k) const
 Compare the name of two keys. More...
 
bool operator< (const Key &other) const
 Compare the name of two keys. More...
 
bool operator<= (const Key &other) const
 Compare the name of two keys. More...
 
bool operator> (const Key &other) const
 Compare the name of two keys. More...
 
bool operator>= (const Key &other) const
 Compare the name of two keys. More...
 
bool isNull () const
 Checks if C++ wrapper has an underlying key. More...
 
 operator bool () const
 This is for loops and lookups only. More...
 
bool needSync () const
 Test if a key needs to be synced to backend storage. More...
 
template<class T >
get () const
 Get a key value. More...
 
template<class T >
void set (T x)
 Set a key value. More...
 
std::string getString () const
 
void setString (std::string newString)
 Set the value for key as newStringValue. More...
 
ssize_t getStringSize () const
 Returns the number of bytes needed to store the key value, including the NULL terminator. More...
 
func_t getFunc () const
 Elektra can store function pointers as binary. More...
 
const void * getValue () const
 Return a pointer to the real internal key value. More...
 
std::string getBinary () const
 Get the value of a key as a binary. More...
 
ssize_t getBinarySize () const
 Returns the number of bytes needed to store the key value, including the NULL terminator. More...
 
ssize_t setBinary (const void *newBinary, size_t dataSize)
 Set the value of a key as a binary. More...
 
bool hasMeta (const std::string &metaName) const
 
template<class T >
getMeta (const std::string &metaName) const
 Returns the value of a meta-information given by name. More...
 
template<class T >
void setMeta (const std::string &metaName, T x)
 Set metadata for key. More...
 
void delMeta (const std::string &metaName)
 Delete metadata for key. More...
 
void copyMeta (const Key &other, const std::string &metaName)
 Do a shallow copy of metadata from source to dest. More...
 
void copyAllMeta (const Key &other)
 Do a shallow copy of all metadata from source to dest. More...
 
void rewindMeta ()
 Rewind the internal iterator to first metadata. More...
 
const Key nextMeta ()
 Iterate to the next meta information. More...
 
const Key currentMeta () const
 Returns the value of a meta-information which is current. More...
 
bool isValid () const
 
std::string getNamespace () const
 
bool isCascading () const
 Determines if the key is in cascading namespace. More...
 
bool isSpec () const
 Determines if the key is in spec namespace. More...
 
bool isProc () const
 Determines if the key is in proc namespace. More...
 
bool isDir () const
 Determines if the key is in dir namespace. More...
 
bool isUser () const
 Determines if the key is in user namespace. More...
 
bool isSystem () const
 Determines if the key is in system namespace. More...
 
bool isString () const
 Check if a key is string type. More...
 
bool isBinary () const
 Check if a key is binary type. More...
 
bool isInactive () const
 Check whether a key is inactive. More...
 
bool isBelow (const Key &k) const
 Check if the key check is below the key key or not. More...
 
bool isBelowOrSame (const Key &k) const
 Check if a key is below or same or not. More...
 
bool isDirectBelow (const Key &k) const
 Check if the key check is direct below the key key or not. More...
 

Detailed Description

Key is an essential class that encapsulates key name , value and metainfo .

To use it include:

#include <kdb.h>

Key properties are:

ABI
Due to ABI compatibility, the 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.
Reference Counting
Every key has its reference counter (see keyGetRef() for longer explanation) that will be initialized with 0, that means a subsequent call of keyDel() will delete the key. If you append the key to a keyset the reference counter will be incremented by one (see keyIncRef()) and the key can't be deleted by a keyDel().
As you can imagine this refcounting allows you to put the Key in your own data structures. It can be a very powerful feature, e.g. if you need your own-defined ordering or different Models of your configuration.

This class is an wrapper for an optional, refcounted ckdb::Key. It is like an shared_ptr<ckdb::Key>, but the shared_ptr functionality is already within the Key and exposed with this wrapper.

optional
A key can be constructed with an null pointer, by using Key (static_cast<ckdb::Key*>(0)); or made empty afterwards by using release() or assign a null key. To check if there is an associated managed object the user can use isNull().
references
Copies of keys are cheap because they are only flat. If you really need a deep copy, you can use copy() or dup(). If you release() an object, the reference counter will stay All other operations operate on references.
documentation
Note that the documentation is typically copied from the underlying function which is wrapped and sometimes extended with C++ specific details. So you might find C examples within the C++ documentation.
Invariant
Key either has a working underlying Elektra Key object or a null pointer. The Key, however, might be invalid (see isValid()) or null (see isNull()).
Note
that the reference counting in the keys is mutable, so that const keys can be passed around by value.

Constructor & Destructor Documentation

◆ Key() [1/7]

kdb::Key::Key ( )
inline

Constructs an empty, invalid key.

Note
That this is not a null key, so the key will evaluate to true.
See also
isValid(), isNull()

◆ Key() [2/7]

kdb::Key::Key ( ckdb::Key *  k)
inline

Constructs a key out of a C key.

Note
If you pass a null pointer here, the key will evaluate to false.
Parameters
kthe key to work with
See also
isValid(), isNull()

◆ Key() [3/7]

kdb::Key::Key ( Key k)
inline

Takes a reference of another key.

The key will not be copied, but the reference counter will be increased.

Parameters
kthe key to work with

◆ Key() [4/7]

kdb::Key::Key ( Key const &  k)
inline

Takes a reference of another key.

The key will not be copied, but the reference counter will be increased.

Parameters
kthe key to work with

◆ Key() [5/7]

kdb::Key::Key ( const char *  keyName,
  ... 
)
inlineexplicit

A practical way to fully create a Key object in one step.

To just get a key object, simple do:

Key *k = keyNew(0);
// work with it
keyDel (k);

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:

Key *k =keyNew("", KEY_END); // Has the same effect as above
// work with it
keyDel (k);

But we can also give the key a proper name right from the start:

// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/some/example", KEY_END);
// work with it
keyDel (k);

If you want the key object to contain a name, value, comment and other meta info read on.

Note
When you already have a key with similar properties its easier to keyDup() the key.

You can call keyNew() in many different ways depending on the attribute tags you pass as parameters. Tags are represented as keyswitch_t values, and tell keyNew() which Key attribute comes next. The Key attribute tags are the following:

  • KEY_VALUE
    Next parameter is a pointer to the value that will be used. If no KEY_BINARY was used before, a string is assumed.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex0",
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_SIZE
    Define a maximum length of the value. This is only used when setting a binary key.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex1",
    KEY_SIZE, 4, // has no effect on strings
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_META
    Next two parameter is a metaname and a metavalue. See keySetMeta().
    Key *k=keyNew("user/tmp/ex3",
    KEY_META, "comment", "a comment", // with a comment
    KEY_META, "owner", "root", // and an owner
    KEY_META, "special", "yes", // and any other metadata
    KEY_END); // end of args
  • KEY_END
    Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.
  • KEY_FLAGS
    Bitwise disjunction of flags, which don't require one or more values. recommended way to set multiple flags. overrides previously defined flags.
    Key *k=keyNew("user/tmp/ex3",
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_END); // end of args
  • 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.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex2",
    KEY_SIZE, 4, // now the size is important
    KEY_VALUE, "some data", // sets the binary value ("some")
    KEY_END); // end of args
  • KEY_CASCADING_NAME allow the name to start with / useful for ksLookup() and kdbGet() parent/lookup keys
  • KEY_META_NAME allow the name to start with arbitrary namespaces useful to compare with metakeys
Deprecated:
The flags below are deprecated and KEY_META should be preferred. They remain some time, however, for compatibility:
Parameters
namea valid name to the key, or NULL to get a simple initialized, but really empty, object
See also
keyDel()
Returns
a pointer to a new allocated and initialized Key object.
Return values
NULLon allocation error or if an invalid name was passed (see keySetName()).
Exceptions
bad_allocif key could not be constructed (allocation problems)
Parameters
keyNamethe name of the new key

◆ Key() [6/7]

kdb::Key::Key ( const std::string  keyName,
  ... 
)
inlineexplicit

A practical way to fully create a Key object in one step.

To just get a key object, simple do:

Key *k = keyNew(0);
// work with it
keyDel (k);

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:

Key *k =keyNew("", KEY_END); // Has the same effect as above
// work with it
keyDel (k);

But we can also give the key a proper name right from the start:

// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/some/example", KEY_END);
// work with it
keyDel (k);

If you want the key object to contain a name, value, comment and other meta info read on.

Note
When you already have a key with similar properties its easier to keyDup() the key.

You can call keyNew() in many different ways depending on the attribute tags you pass as parameters. Tags are represented as keyswitch_t values, and tell keyNew() which Key attribute comes next. The Key attribute tags are the following:

  • KEY_VALUE
    Next parameter is a pointer to the value that will be used. If no KEY_BINARY was used before, a string is assumed.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex0",
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_SIZE
    Define a maximum length of the value. This is only used when setting a binary key.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex1",
    KEY_SIZE, 4, // has no effect on strings
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_META
    Next two parameter is a metaname and a metavalue. See keySetMeta().
    Key *k=keyNew("user/tmp/ex3",
    KEY_META, "comment", "a comment", // with a comment
    KEY_META, "owner", "root", // and an owner
    KEY_META, "special", "yes", // and any other metadata
    KEY_END); // end of args
  • KEY_END
    Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.
  • KEY_FLAGS
    Bitwise disjunction of flags, which don't require one or more values. recommended way to set multiple flags. overrides previously defined flags.
    Key *k=keyNew("user/tmp/ex3",
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_END); // end of args
  • 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.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex2",
    KEY_SIZE, 4, // now the size is important
    KEY_VALUE, "some data", // sets the binary value ("some")
    KEY_END); // end of args
  • KEY_CASCADING_NAME allow the name to start with / useful for ksLookup() and kdbGet() parent/lookup keys
  • KEY_META_NAME allow the name to start with arbitrary namespaces useful to compare with metakeys
Deprecated:
The flags below are deprecated and KEY_META should be preferred. They remain some time, however, for compatibility:
Parameters
namea valid name to the key, or NULL to get a simple initialized, but really empty, object
See also
keyDel()
Returns
a pointer to a new allocated and initialized Key object.
Return values
NULLon allocation error or if an invalid name was passed (see keySetName()).
Exceptions
bad_allocif key could not be constructed (allocation problems)
Warning
Not supported on some compilers, e.g. clang which requires you to only pass non-POD in varg lists.
Parameters
keyNamethe name of the new key

◆ Key() [7/7]

kdb::Key::Key ( const char *  keyName,
va_list  ap 
)
inlineexplicit

A practical way to fully create a Key object in one step.

To just get a key object, simple do:

Key *k = keyNew(0);
// work with it
keyDel (k);

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:

Key *k =keyNew("", KEY_END); // Has the same effect as above
// work with it
keyDel (k);

But we can also give the key a proper name right from the start:

// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/some/example", KEY_END);
// work with it
keyDel (k);

If you want the key object to contain a name, value, comment and other meta info read on.

Note
When you already have a key with similar properties its easier to keyDup() the key.

You can call keyNew() in many different ways depending on the attribute tags you pass as parameters. Tags are represented as keyswitch_t values, and tell keyNew() which Key attribute comes next. The Key attribute tags are the following:

  • KEY_VALUE
    Next parameter is a pointer to the value that will be used. If no KEY_BINARY was used before, a string is assumed.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex0",
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_SIZE
    Define a maximum length of the value. This is only used when setting a binary key.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex1",
    KEY_SIZE, 4, // has no effect on strings
    KEY_VALUE, "some data", // set a string value
    KEY_END); // end of args
  • KEY_META
    Next two parameter is a metaname and a metavalue. See keySetMeta().
    Key *k=keyNew("user/tmp/ex3",
    KEY_META, "comment", "a comment", // with a comment
    KEY_META, "owner", "root", // and an owner
    KEY_META, "special", "yes", // and any other metadata
    KEY_END); // end of args
  • KEY_END
    Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.
  • KEY_FLAGS
    Bitwise disjunction of flags, which don't require one or more values. recommended way to set multiple flags. overrides previously defined flags.
    Key *k=keyNew("user/tmp/ex3",
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_END); // end of args
  • 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.
    // Create and initialize a key with a name and nothing else
    Key *k=keyNew("user/tmp/ex2",
    KEY_SIZE, 4, // now the size is important
    KEY_VALUE, "some data", // sets the binary value ("some")
    KEY_END); // end of args
  • KEY_CASCADING_NAME allow the name to start with / useful for ksLookup() and kdbGet() parent/lookup keys
  • KEY_META_NAME allow the name to start with arbitrary namespaces useful to compare with metakeys
Deprecated:
The flags below are deprecated and KEY_META should be preferred. They remain some time, however, for compatibility:
Parameters
namea valid name to the key, or NULL to get a simple initialized, but really empty, object
See also
keyDel()
Returns
a pointer to a new allocated and initialized Key object.
Return values
NULLon allocation error or if an invalid name was passed (see keySetName()).
Exceptions
bad_allocif key could not be constructed (allocation problems)
Parameters
keyNamethe name of the new key
apthe variable argument list pointer

◆ ~Key()

kdb::Key::~Key ( )
inline

Destructs the key.

See also
del()

Member Function Documentation

◆ addBaseName()

void kdb::Key::addBaseName ( const std::string &  baseName)
inline

Adds a base name for a key.

Adds baseName (that will be escaped) to the current key name. A new baseName will be added, no other part of the key name will be affected.

Assumes that key is a directory and will append baseName to it. The function adds the path separator for concatenating.

So if key has name "system/dir1/dir2" and this method is called with baseName "mykey", the resulting key will have the name "system/dir1/dir2/mykey".

When baseName is 0 nothing will happen and the size of the name is returned.

The escaping rules apply as in above .

A simple example is:

Key * k = keyNew ("user/my/long", KEY_END);
keyAddBaseName (k, "myname");
printf ("%s\n", keyName (k)); // will print user/my/long/myname
keyDel (k);

E.g. if you add . it will be escaped:

keySetName (k, "system/valid");
succeed_if (keyAddBaseName (k, ".") >= 0, "could not add a base name");
succeed_if_same_string (keyName (k), "system/valid/\\.");
succeed_if_same_string (keyBaseName (k), ".");
See also
keySetBaseName() to set a base name
keySetName() to set a new name.
Parameters
keythe key object to work with
baseNamethe string to append to the name
Returns
the size in bytes of the new key name including the ending NULL
Return values
-1if the key had no name
-1on NULL pointers
-1if key was inserted to a keyset before
-1on allocation errors
Exceptions
KeyInvalidNameif the name is not valid

◆ clear()

void kdb::Key::clear ( )
inline

Clears/Invalidates a key.

Afterwards the object is empty again.

Note
This is not a null key, so it will evaluate to true. isValid() will, however, be false.
See also
release()
isValid(), isNull()

Key Object Cleaner. Will reset all internal data.

After this call you will receive a fresh key.

The reference counter will stay unmodified.

Note
that you might also clear() all aliases with this operation.
int f (Key *k)
{
keyClear (k);
// you have a fresh key k here
keySetString (k, "value");
// the caller will get an empty key k with an value
}
Return values
returns0 on success
-1on null pointer
Parameters
keythe key object to work with

◆ copy()

void kdb::Key::copy ( const Key other)
inline

Copy or Clear a key.

Most often you may prefer keyDup() which allocates a new key and returns a duplication of another key.

But 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:

void h (Key * k)
{
// receive key c
keyCopy (k, copy);
// the caller will see the changed key k
}

The reference counter will not be changed for both keys. Affiliation to keysets are also not affected.

The metadata will be duplicated for the destination key. So it will not take much additional space, even with lots of metadata.

When you pass a NULL-pointer as source the data of dest will be cleaned completely (except reference counter, see keyClear()) and you get a fresh dest key:

void g (Key * k)
{
keyCopy (k, 0);
// k is now an empty and fresh key
}

If you want to copy everything, except e.g. the value you can use keyCopy() too:

void j (Key * k)
{
size_t size = keyGetValueSize (k);
char * value = malloc (size);
int bstring = keyIsString (k);
// receive key c
memcpy (value, keyValue (k), size);
keyCopy (k, copy);
if (bstring)
keySetString (k, value);
else
keySetBinary (k, value, size);
free (value);
// the caller will see the changed key k
// with the name and metadata from copy (except
// metadata "binary", which stayed the same)
}

Restrain from coping everything yourself, because it will lead to wrong metadata and is not able to copy empty or cascading names:

void i (Key * k)
{
// k is not a copy of copy even if everything was successfully,
// because it still contains metadata from k
}
Parameters
destthe key which will be written to
sourcethe key which should be copied or NULL to clean the destination key
Return values
-1on failure when a NULL pointer was passed for dest or a dynamic property could not be written. The content will be unmodified then.
0when dest was cleaned
1when source was successfully copied
See also
keyDup() to get a duplication of a Key

◆ copyAllMeta()

void kdb::Key::copyAllMeta ( const Key other)
inline

Do a shallow copy of all metadata from source to dest.

The key dest will additionally have all metadata the source had. Meta data not present in source will not be changed. Meta data which was present in source and dest will be overwritten.

For example the metadata type is copied into the Key k:

void l (Key * k)
{
// receive copy
// the caller will see the changed key k
// with all the metadata from copy
}

The main purpose of this function is for plugins or applications which want to add the same metadata to n keys. When you do that with keySetMeta() it will take n times the memory for the key. This can be considerable amount of memory for many keys with some metadata for each.

To avoid that problem you can use keyCopyAllMeta() or keyCopyMeta():

void o (KeySet * ks)
{
Key * current;
Key * shared = keyNew (0);
keySetMeta (shared, "shared1", "this metadata should be shared among many keys");
keySetMeta (shared, "shared2", "this metadata should be shared among many keys also");
keySetMeta (shared, "shared3", "this metadata should be shared among many keys too");
ksRewind (ks);
while ((current = ksNext (ks)) != 0)
{
if (needsSharedData (current)) keyCopyAllMeta (current, shared);
}
keyDel (shared);
}
Postcondition
for every metaName present in source: keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Return values
1if was successfully copied
0if source did not have any metadata
-1on null pointer of dest or source
-1on memory problems
Parameters
destthe destination where the metadata should be copied too
sourcethe key where the metadata should be copied from
See also
getMeta(), setMeta(), copyMeta()

◆ copyMeta()

void kdb::Key::copyMeta ( const Key other,
const std::string &  metaName 
)
inline

Do a shallow copy of metadata from source to dest.

The key dest will have the same metadata referred with metaName afterwards then source.

For example the metadata type is copied into the Key k.

void l(Key *k)
{
// receive c
keyCopyMeta(k, c, "type");
// the caller will see the changed key k
// with the metadata "type" from c
}

The main purpose of this function is for plugins or applications which want to add the same metadata to n keys. When you do that with keySetMeta() it will take n times the memory for the key. This can be considerable amount of memory for many keys with some metadata for each.

To avoid that problem you can use keyCopyAllMeta() or keyCopyMeta().

void o(KeySet *ks)
{
Key *current;
Key *shared = keyNew (0);
keySetMeta(shared, "shared", "this metadata should be shared among many keys");
ksRewind(ks);
while ((current = ksNext(ks)) != 0)
{
if (needs_shared_data(current)) keyCopyMeta(current, shared, "shared");
}
}
Postcondition
keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Return values
1if was successfully copied
0if the metadata in dest was removed too
-1on null pointers (source or dest)
-1on memory problems
Parameters
destthe destination where the metadata should be copied too
sourcethe key where the metadata should be copied from
metaNamethe name of the metadata which should be copied
See also
getMeta(), setMeta(), copyAllMeta()

◆ currentMeta()

const Key kdb::Key::currentMeta ( ) const
inline

Returns the value of a meta-information which is current.

The pointer is NULL if you reached the end or after ksRewind().

Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
Returns
a buffer to the value pointed by key's cursor
Return values
0on NULL pointer
See also
keyNextMeta(), keyRewindMeta()
ksCurrent() for pedant in iterator interface of KeySet
Note
that the key will be null if last metadata is found.
k.rewindMeta();
while (meta = k.nextMeta())
{
cout << meta.getName() << " " << meta.getString() << endl;
}
See also
rewindMeta(), nextMeta()

◆ delBaseName()

void kdb::Key::delBaseName ( )
inline

Delete the baseName of a key.

Exceptions
KeyInvalidNameif the name is not valid

◆ delMeta()

void kdb::Key::delMeta ( const std::string &  metaName)
inline

Delete metadata for key.

See also
setMeta(), getMeta(), copyMeta(), copyAllMeta()

◆ dup()

ckdb::Key * kdb::Key::dup ( ) const
inline

Return a duplicate of a key.

Memory will be allocated as needed for dynamic properties.

The new key will not be member of any KeySet and will start with a new reference counter at 0. A subsequent keyDel() will delete the key.

int f (const Key * source)
{
Key * dup = keyDup (source);
// work with duplicate
keyDel (dup);
// everything related to dup is freed
// and source is unchanged
}

Like for a new key after keyNew() a subsequent ksAppend() makes a KeySet to take care of the lifecycle of the key.

int g (const Key * source, KeySet * ks)
{
Key * dup = keyDup (source);
// work with duplicate
ksAppendKey (ks, dup);
// ksDel(ks) will also free the duplicate
// source remains unchanged.
}

Duplication of keys should be preferred to keyNew(), because data like owner can be filled with a copy of the key instead of asking the environment. It can also be optimized in the checks, because the keyname is known to be valid.

Parameters
sourcehas to be an initialized source Key
Return values
0failure or on NULL pointer
Returns
a fully copy of source on success
See also
ksAppend(), keyDel(), keyNew()

◆ get()

template<class T >
T kdb::Key::get ( ) const
inline

Get a key value.

You can write your own template specialication, e.g.:

template <>
inline QColor Key::get() const
{
if (getStringSize() < 1)
{
throw KeyTypeConversion();
}
std::string str = getString();
QColor c(str.c_str());
return c;
}
Returns
the string directly from the key.

It should be the same as get().

Returns
empty string on null pointers
Exceptions
KeyExceptionon null key or not a valid size
KeyTypeMismatchif key holds binary data and not a string
Note
unlike in the C version, it is safe to change the returned string.
See also
isString(), getBinary()

This method tries to serialise the string to the given type.

◆ getBaseName()

std::string kdb::Key::getBaseName ( ) const
inline

Returns a pointer to the internal unescaped key name where the basename starts.

This is a much more efficient version of keyGetBaseName() and you should use it if you are responsible enough to not mess up things. The name might change or even point to a wrong place after a keySetName(). So make sure to copy the memory before the name changes.

keyBaseName() returns "" when there is no keyBaseName. The reason is

keySetName (k, "");
succeed_if_same_string (keyBaseName (k), "");
keySetName (k, "user");
succeed_if_same_string (keyBaseName (k), "");

And there is also support for really empty basenames:

keySetName (k, "system/valid");
succeed_if (keyAddBaseName (k, "") >= 0, "could not add a base name");
succeed_if_same_string (keyName (k), "system/valid/%");
succeed_if_same_string (keyBaseName (k), "");
Note
You must never use the pointer returned by keyBaseName() method to change the name, but you should use keySetBaseName() instead.
Do not assume that keyBaseName() points to the same region as keyName() does.
Parameters
keythe object to obtain the basename from
Returns
a pointer to the basename
Return values
""when the key has no (base)name
0on NULL pointer
See also
keyGetBaseName(), keyGetBaseNameSize()
keyName() to get a pointer to the name
keyOwner() to get a pointer to the owner

◆ getBaseNameSize()

ssize_t kdb::Key::getBaseNameSize ( ) const
inline

Calculates number of bytes needed to store basename of key.

Key names that have only root names (e.g. "system" or "user" or "user:domain" ) does not have basenames, thus the function will return 1 bytes to store "".

Basenames are denoted as:

  • system/some/thing/basename -> basename
  • user:domain/some/thing/base\/name > base\/name
Parameters
keythe key object to work with
Returns
size in bytes of key's basename including ending NULL
See also
keyBaseName(), keyGetBaseName()
keyName(), keyGetName(), keySetName()

◆ getBinary()

std::string kdb::Key::getBinary ( ) const
inline

Get the value of a key as a binary.

Returns
the binary Value of the key.
Return values
""on null pointers (size == 0) and on data only containing \0
Note
if you need to distinguish between null pointers and data containing \0 you can use getValue().
Exceptions
KeyExceptionon invalid binary size
KeyTypeMismatchif key is string and not a binary

If the type is not binary -1 will be returned.

When the binary data is empty (this is not the same as ""!) 0 will be returned and the returnedBinary will not be changed.

For string values see keyGetString() and keyIsString().

When the returnedBinary is to small to hold the data (its maximum size is given by maxSize), the returnedBinary will not be changed and -1 is returned.

Example:
Key *key = keyNew ("user/keyname", KEY_TYPE, KEY_TYPE_BINARY, KEY_END);
char buffer[300];
if (keyGetBinary(key,buffer,sizeof(buffer)) == -1)
{
// handle error
}
Parameters
keythe object to gather the value from
returnedBinarypre-allocated memory to store a copy of the key value
maxSizenumber of bytes of pre-allocated memory in returnedBinary
Returns
the number of bytes actually copied to returnedBinary
Return values
0if the binary is empty
-1on NULL pointers
-1if maxSize is 0
-1if maxSize is too small for string
-1if maxSize is larger than SSIZE_MAX
-1on type mismatch: binary expected, but found string
See also
keyValue(), keyGetValueSize(), keySetBinary()
keyGetString() and keySetString() as preferred alternative to binary
keyIsBinary() to see how to check for binary type
isBinary(), getString(), getValue()

◆ getBinarySize()

ssize_t kdb::Key::getBinarySize ( ) const
inline

Returns the number of bytes needed to store the key value, including the NULL terminator.

It returns the correct size, independent of the Key Type. If it is a binary there might be '\0' values in it.

For an empty string you need one byte to store the ending NULL. For that reason 1 is returned. This is not true for binary data, so there might be returned 0 too.

A binary key has no '\0' termination. String types have it, so to there length will be added 1 to have enough space to store it.

This method can be used with elektraMalloc() before keyGetString() or keyGetBinary() is called.

char *buffer;
buffer = elektraMalloc (keyGetValueSize (key));
// use this buffer to store the value (binary or string)
// pass keyGetValueSize (key) for maxSize
Parameters
keythe key object to work with
Returns
the number of bytes needed to store the key value
Return values
1when there is no data and type is not binary
0when there is no data and type is binary
-1on null pointer
See also
keyGetString(), keyGetBinary(), keyValue()

◆ getFullName()

std::string kdb::Key::getFullName ( ) const
inline

Get key full name, including the user domain name.

Returns
number of bytes written
Return values
1on empty name
-1on NULL pointers
-1if maxSize is 0 or larger than SSIZE_MAX
Parameters
keythe key object
returnedNamepre-allocated memory to write the key name
maxSizemaximum number of bytes that will fit in returnedName, including the final NULL
Exceptions
KeyExceptionif key is null

◆ getFullNameSize()

ssize_t kdb::Key::getFullNameSize ( ) const
inline

Bytes needed to store the key name including user domain and ending NULL.

Parameters
keythe key object to work with
Returns
number of bytes needed to store key name including user domain
Return values
1on empty name
-1on NULL pointer
See also
keyGetFullName(), keyGetNameSize()

◆ getFunc()

Key::func_t kdb::Key::getFunc ( ) const
inline

Elektra can store function pointers as binary.

This function returns such a function pointer.

Exceptions
KeyTypeMismatchif no binary data found, or binary data has not correct length
Returns
a function pointer stored with setBinary()

◆ getKey()

ckdb::Key * kdb::Key::getKey ( ) const
inline

Passes out the raw key pointer.

This pointer can be used to directly change the underlying key object.

Note
that the ownership remains in the object

◆ getMeta()

template<class T >
T kdb::Key::getMeta ( const std::string &  metaName) const
inline

Returns the value of a meta-information given by name.

You are not allowed to modify the resulting key.

int f(Key *k)
{
if (!strcmp(keyValue(keyGetMeta(k, "type")), "boolean"))
{
// the type of the key is boolean
}
}
Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
metaNamethe name of the meta information you want the value from
Return values
0if the key or metaName is 0
0if no such metaName is found
Returns
value of meta-information if meta-information is found
See also
keySetMeta()

You can specify your own template specialisation:

template<>
inline yourtype Key::getMeta(const std::string &name) const
{
yourtype x;
std::string str;
str = std::string(
static_cast<const char*>(
ckdb::keyGetMeta(key, name.c_str())
)
)
);
return yourconversion(str);
}
Exceptions
KeyTypeConversionif metadata could not be parsed
Note
No exception will be thrown if a const Key or char* is requested, but don't forget the const: getMeta<const Key>, otherwise you will get an compiler error.

If no meta is available:

◆ getName()

std::string kdb::Key::getName ( ) const
inline

Returns a pointer to the abbreviated real internal key name.

This is a much more efficient version of keyGetName() and can use it if you are responsible enough to not mess up things. You are not allowed to change anything in the returned array. The content of that string may change after keySetName() and similar functions. If you need a copy of the name, consider using keyGetName().

The name will be without owner, see keyGetFullName() if you need the name with its owner.

Return values
""when there is no keyName. The reason is
key=keyNew(0);
keySetName(key,"");
keyName(key); // you would expect "" here
keyDel(key);

Valid key names are:

  • spec/something for specification of other keys.
  • proc/something for in-memory keys, e.g. commandline.
  • dir/something for dir keys in current working directory
  • system/something for system keys in /etc or /
  • user/something for user keys in home directory
  • user:username/something for other users (deprecated: kdbGet() + kdbSet() currently unsupported)
  • /something for cascading keys (actually refers to one of the above, see also ksLookup())

    Note
    Note that the Key structure keeps its own size field that is calculated by library internal calls, so to avoid inconsistencies, you must never use the pointer returned by keyName() method to set a new value. Use keySetName() instead.
    Parameters
    keythe key object to work with
    Returns
    a pointer to the keyname which must not be changed.
    Return values
    ""when there is no (an empty) keyname
    0on NULL pointer
    See also
    keyGetNameSize() for the string length
    keyGetFullName(), keyGetFullNameSize() to get the full name
    keyGetName() as alternative to get a copy
    keyOwner() to get a pointer to owner
    keyUnescapedName to get an unescaped Key name
    Exceptions
    KeyExceptionif key is null
    Note
    unlike in the C version, it is safe to change the returned string.

◆ getNameSize()

ssize_t kdb::Key::getNameSize ( ) const
inline

Bytes needed to store the key name without owner.

For an empty key name you need one byte to store the ending NULL. For that reason 1 is returned.

Parameters
keythe key object to work with
Returns
number of bytes needed, including ending NULL, to store key name without owner
Return values
1if there is is no key Name
-1on NULL pointer
See also
keyGetName(), keyGetFullNameSize()
keyGetUnescapedNameSize to get size of unescaped name

◆ getNamespace()

std::string kdb::Key::getNamespace ( ) const
inline
Returns
namespace as string

Will return slash for cascading names.

See also
getName(), isUser(), isSystem()

◆ getReferenceCounter()

ssize_t kdb::Key::getReferenceCounter ( ) const
inline

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:

Key *k = keyNew("user/proper_name", KEY_END); // ref counter = 0
KeySet *ks = ksNew (1, k, KS_END);
keyDel(k); // key will not be deleted, because its in the keyset
ksDel(ks); // now the key will be deleted

You can even add the key to more KeySets:

Key *k = keyNew("user/proper_name", KEY_END); // ref counter 0
KeySet *ks1 = ksNew(1, k, KS_END); // ref counter of k 1
KeySet *ks2 = ksNew(1, k, KS_END); // ref counter of k 2
ksDel(ks1); // ref counter of k 1
ksDel(ks2); // k is now deleted

If you increment only by one with keyIncRef() the same as said above is valid:

Key *k = keyNew(0); // ref counter = 0
keyIncRef(k); // ref counter = 1
keyDel(k); // key will not be deleted
keyDel(k);

or use keyIncRef() more than once:

Key *k = keyNew(0); // ref counter 0
keyIncRef(k); // ref counter of key 1
keyDel (k); // has no effect
keyIncRef(k); // ref counter of key 2
keyDel (k); // has no effect
keyDecRef(k); // ref counter of key 1
keyDel (k); // has no effect
keyDecRef(k); // ref counter is now 0
keyDel (k); // k is now deleted

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

Note
keyDup() will reset the references for dupped key.

For your own applications you can use keyIncRef() and keyDecRef() for reference counting, too.

Parameters
keythe key object to work with
Returns
the number of references
Return values
-1on null pointer
See also
keyIncRef() and keyDecRef()

◆ getString()

std::string kdb::Key::getString ( ) const
inline
Returns
the string directly from the key.

It should be the same as get().

Returns
empty string on null pointers
Exceptions
KeyExceptionon null key or not a valid size
KeyTypeMismatchif key holds binary data and not a string
Note
unlike in the C version, it is safe to change the returned string.
See also
isString(), getBinary()

◆ getStringSize()

ssize_t kdb::Key::getStringSize ( ) const
inline

Returns the number of bytes needed to store the key value, including the NULL terminator.

It returns the correct size, independent of the Key Type. If it is a binary there might be '\0' values in it.

For an empty string you need one byte to store the ending NULL. For that reason 1 is returned. This is not true for binary data, so there might be returned 0 too.

A binary key has no '\0' termination. String types have it, so to there length will be added 1 to have enough space to store it.

This method can be used with elektraMalloc() before keyGetString() or keyGetBinary() is called.

char *buffer;
buffer = elektraMalloc (keyGetValueSize (key));
// use this buffer to store the value (binary or string)
// pass keyGetValueSize (key) for maxSize
Parameters
keythe key object to work with
Returns
the number of bytes needed to store the key value
Return values
1when there is no data and type is not binary
0when there is no data and type is binary
-1on null pointer
See also
keyGetString(), keyGetBinary(), keyValue()

◆ getValue()

const void * kdb::Key::getValue ( ) const
inline

Return a pointer to the real internal key value.

This is a much more efficient version of keyGetString() keyGetBinary(), and you should use it if you are responsible enough to not mess up things. You are not allowed to modify anything in the returned string. If you need a copy of the Value, consider to use keyGetString() or keyGetBinary() instead.

String Handling

If key is string (keyIsString()), you may cast the returned as a "char *" because you'll get a NULL terminated regular string.

keyValue() returns "" in string mode when there is no value. The reason is

key=keyNew(0);
keySetString(key,"");
keyValue(key); // you would expect "" here
keyDel(key);

Binary Data Handling

If the data is binary, the size of the value must be determined by keyGetValueSize(), any strlen() operations are not suitable to determine the size.

keyValue() returns 0 in binary mode when there is no value. The reason is

key=keyNew(0);
keySetBinary(key, 0, 0);
keyValue(key); // you would expect 0 here
keySetBinary(key,"", 1);
keyValue(key); // you would expect "" (a pointer to '\0') here
int i=23;
keySetBinary(key, (void*)&i, 4);
(int*)keyValue(key); // you would expect a pointer to (int)23 here
keyDel(key);
Note
Note that the Key structure keeps its own size field that is calculated by library internal calls, so to avoid inconsistencies, you must never use the pointer returned by keyValue() method to set a new value. Use keySetString() or keySetBinary() instead.
Warning
Binary keys will return a NULL pointer when there is no data in contrast to keyName(), keyBaseName(), keyOwner() and keyComment(). For string value the behaviour is the same.
Example:
KDB *handle = kdbOpen();
KeySet *ks=ksNew(0, KS_END);
Key *current=0;
kdbGetByName(handle,ks,"system/sw/my",KDB_O_SORT|KDB_O_RECURSIVE);
while (current=ksNext(ks)) {
size_t size=0;
if (keyIsBin(current)) {
size=keyGetValueSize(current);
printf("Key %s has a value of size %d bytes. Value: <BINARY>\nComment: %s",
keyName(current),
size,
keyComment(current));
} else {
size=elektraStrLen((char *)keyValue(current));
printf("Key %s has a value of size %d bytes. Value: %s\nComment: %s",
keyName(current),
size,
(char *)keyValue(current),
keyComment(current));
}
}
ksDel (ks);
kdbClose (handle);
Parameters
keythe key object to work with
Returns
a pointer to internal value
Return values
""when there is no data and key is not binary
0where there is no data and key is binary
0on NULL pointer
See also
keyGetValueSize(), keyGetString(), keyGetBinary()
Returns
the value of the key
See also
getBinary()

◆ hasMeta()

bool kdb::Key::hasMeta ( const std::string &  metaName) const
inline
Return values
trueif there is a metadata with given name
falseif no such metadata exists
See also
getMeta()

◆ isBelow()

bool kdb::Key::isBelow ( const Key k) const
inline

Check if the key check is below the key key or not.

Parameters
kthe other key
Returns
true if our key is below k

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(), keyIsDirectBelow()

◆ isBelowOrSame()

bool kdb::Key::isBelowOrSame ( const Key k) const
inline

Check if a key is below or same or not.

Parameters
kthe other key
Returns
true if our key is below k or the same as k
Parameters
keythe key object to work with
See also
keyIsBelow()

◆ isBinary()

bool kdb::Key::isBinary ( ) const
inline

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

◆ isCascading()

bool kdb::Key::isCascading ( ) const
inline

Determines if the key is in cascading namespace.

Return values
trueif it is a cascading key
falseotherwise

◆ isDir()

bool kdb::Key::isDir ( ) const
inline

Determines if the key is in dir namespace.

Return values
trueif it is a dir key
falseotherwise

◆ isDirectBelow()

bool kdb::Key::isDirectBelow ( const Key k) const
inline

Check if the key check is direct below the key key or not.

Parameters
kthe other key
Returns
true if our key is direct below k
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()

◆ isInactive()

bool kdb::Key::isInactive ( ) const
inline

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

◆ isNull()

bool kdb::Key::isNull ( ) const
inline

Checks if C++ wrapper has an underlying key.

See also
operator bool(), isValid()
Returns
true if no underlying key exists

◆ isProc()

bool kdb::Key::isProc ( ) const
inline

Determines if the key is in proc namespace.

Return values
trueif it is a proc key
falseotherwise

◆ isSpec()

bool kdb::Key::isSpec ( ) const
inline

Determines if the key is in spec namespace.

Return values
trueif it is a spec key
falseotherwise

◆ isString()

bool kdb::Key::isString ( ) const
inline

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

◆ isSystem()

bool kdb::Key::isSystem ( ) const
inline

Determines if the key is in system namespace.

Return values
trueif it is a system key
falseotherwise

◆ isUser()

bool kdb::Key::isUser ( ) const
inline

Determines if the key is in user namespace.

Return values
trueif it is a user key
falseotherwise

◆ isValid()

bool kdb::Key::isValid ( ) const
inline
Returns
if the key is valid

An invalid key has no name. The name of valid keys either start with user or system.

Return values
trueif the key has a valid name
falseif the key has an invalid name
See also
getName(), isUser(), isSystem(), getNamespace()

◆ needSync()

bool kdb::Key::needSync ( ) const
inline

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

◆ nextMeta()

const Key kdb::Key::nextMeta ( )
inline

Iterate to the next meta information.

Keys have an internal cursor that can be reset with keyRewindMeta(). Every time keyNextMeta() is called the cursor is incremented and the new current Name of Meta Information is returned.

You'll get a NULL pointer if the meta information after the end of the Key was reached. On subsequent calls of keyNextMeta() it will still return the NULL pointer.

The key internal cursor will be changed, so it is not const.

Note
That the resulting key is guaranteed to have a value, because meta information has no binary or null pointer semantics.
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
Returns
a key representing meta information
Return values
0when the end is reached
0on NULL pointer
See also
ksNext() for pedant in iterator interface of KeySet
rewindMeta(), currentMeta()

◆ operator bool()

kdb::Key::operator bool ( ) const
inline

This is for loops and lookups only.

Opposite of isNull()

For loops it checks if there are still more keys. For lookups it checks if a key could be found.

Warning
you should not construct or use null keys
See also
isNull(), isValid()
Returns
false on null keys
true otherwise

◆ operator!=()

bool kdb::Key::operator!= ( const Key k) const
inline

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.

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:

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
Return values
true!= 0

◆ operator*()

ckdb::Key * kdb::Key::operator* ( ) const
inline

Is an abbreviation for getKey.

Passes out the raw key pointer. This pointer can be used to directly change the underlying key object.

Note
that the ownership remains in the object
See also
getKey()

◆ operator++() [1/2]

void kdb::Key::operator++ ( int  ) const
inline

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.

Note
keyDup() will reset the references for dupped key.
Returns
the value of the new reference counter
Return values
-1on null pointer
SSIZE_MAXwhen maximum exceeded
Parameters
keythe key object to work with
See also
keyGetRef() for longer explanation, keyDecRef(), keyDel()

◆ operator++() [2/2]

void kdb::Key::operator++ ( ) const
inline

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.

Note
keyDup() will reset the references for dupped key.
Returns
the value of the new reference counter
Return values
-1on null pointer
SSIZE_MAXwhen maximum exceeded
Parameters
keythe key object to work with
See also
keyGetRef() for longer explanation, keyDecRef(), keyDel()

◆ operator--() [1/2]

void kdb::Key::operator-- ( int  ) const
inline

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.

Note
keyDup() will reset the references for dupped key.
Returns
the value of the new reference counter
Return values
-1on null pointer
0when the key is ready to be freed
Parameters
keythe key object to work with
See also
keyGetRef() for longer explanation, keyDel(), keyIncRef()

◆ operator--() [2/2]

void kdb::Key::operator-- ( ) const
inline

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.

Note
keyDup() will reset the references for dupped key.
Returns
the value of the new reference counter
Return values
-1on null pointer
0when the key is ready to be freed
Parameters
keythe key object to work with
See also
keyGetRef() for longer explanation, keyDel(), keyIncRef()

◆ operator->()

Key * kdb::Key::operator-> ( )
inline
Returns
a pointer to this object

Needed for KeySet iterators.

See also
KeySetIterator

◆ operator<()

bool kdb::Key::operator< ( const Key other) const
inline

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.

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:

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
Return values
true< 0

◆ operator<=()

bool kdb::Key::operator<= ( const Key other) const
inline

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.

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:

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
Return values
true<= 0

◆ operator=() [1/2]

Key & kdb::Key::operator= ( ckdb::Key *  k)
inline

Assign a C key.

Will call del() on the old key.

◆ operator=() [2/2]

Key & kdb::Key::operator= ( const Key k)
inline

Assign a key.

Will call del() on the old key.

◆ operator==()

bool kdb::Key::operator== ( const Key k) const
inline

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.

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:

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
Return values
true== 0

◆ operator>()

bool kdb::Key::operator> ( const Key other) const
inline

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.

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:

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
Return values
true> 0

◆ operator>=()

bool kdb::Key::operator>= ( const Key other) const
inline

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.

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:

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
Return values
true>= 0

◆ release()

ckdb::Key * kdb::Key::release ( )
inline

Passes out the raw key pointer and resets internal key handle.

Note
that the ownership is moved outside.
Return values
0if no key is held (null pointer), no action is done then.

◆ rewindMeta()

void kdb::Key::rewindMeta ( )
inline

Rewind the internal iterator to first metadata.

Use it to set the cursor to the beginning of the Key Meta Infos. keyCurrentMeta() will then always return NULL afterwards. So you want to keyNextMeta() first.

Key *key;
const Key *meta;
while ((meta = keyNextMeta (key))!=0)
{
printf ("name: %s, value: %s", keyName(meta), keyString(meta));
}
Parameters
keythe key object to work with
Return values
0on success
0if there is no meta information for that key (keyNextMeta() will always return 0 in that case)
-1on NULL pointer
See also
keyNextMeta(), keyCurrentMeta()
ksRewind() for pedant in iterator interface of KeySet
nextMeta(), currentMeta()

◆ set()

template<class T >
void kdb::Key::set ( x)
inline

Set a key value.

Set the value for key as newStringValue. The function will allocate and save a private copy of newStringValue, so the parameter can be freed after the call.

String values will be saved in backend storage, when kdbSetKey() will be called, in UTF-8 universal encoding, regardless of the program's current encoding, when iconv plugin is present.

Note
The type will be set to KEY_TYPE_STRING. When the type of the key is already a string type it won't be changed.
Parameters
keythe key to set the string value
newStringValueNULL-terminated text string to be set as key's value
Returns
the number of bytes actually saved in private struct including final NULL
Return values
1if newStringValue is a NULL pointer, this will make the string empty (string only containing null termination)
-1if key is a NULL pointer
See also
keyGetString(), keyValue(), keyString()

This method tries to deserialise the string to the given type.

◆ setBaseName()

void kdb::Key::setBaseName ( const std::string &  baseName)
inline

Sets a base name for a key.

Sets baseName as the new basename for key. Only the baseName will be affected and no other part of the key.

A simple example is:

Key * k = keyNew ("user/my/long/name", KEY_END);
keySetBaseName (k, "myname");
printf ("%s\n", keyName (k)); // will print user/my/long/myname
keyDel (k);

All text after the last '/' in the key keyname is erased and baseName is appended. If baseName is 0 (NULL), then the last part of the keyname is removed without replacement.

Let us suppose key has name "system/dir1/dir2/key1". If baseName is "key2", the resulting key name will be "system/dir1/dir2/key2". If baseName is 0 (NULL), the resulting key name will be "system/dir1/dir2". If baseName is empty, the resulting key name will be "system/dir1/dir2/%", where "%" denotes an empty base name, as also shown in the following code:

keySetName (k, "system/valid");
keySetBaseName (k, "");
succeed_if_same_string (keyName (k), "system/%");
succeed_if_same_string (keyBaseName (k), "");

keySetBaseName() does proper escaping on the supplied name argument.

You can use character sequences as baseName (e.g. "." (dot), ".." (dot-dot), "%" (empty basename)). They will be properly escaped and will not have their usual meaning.

See also
Name Manipulation Methods for more details on special names

If you want to add and not change the basename, use keyAddBaseName() instead. If you do not want escaping, use keyAddName() instead.

See also
keyAddBaseName() to add a basename instead of changing it
keyAddName() to add a name without escaping
keySetName() to set a completely new name

To add an inactive key name, use:

keySetName (k, "system/valid");
keySetBaseName (k, ".hiddenkey");
succeed_if_same_string (keyName (k), "system/.hiddenkey");
succeed_if_same_string (keyBaseName (k), ".hiddenkey");
Parameters
keythe key object to work with
baseNamethe string used to overwrite the basename of the key
Returns
the size in bytes of the new key name
Return values
-1on NULL pointers in key
-1if key was inserted to a keyset before
-1on allocation errors
Exceptions
KeyInvalidNameif the name is not valid

◆ setBinary()

ssize_t kdb::Key::setBinary ( const void *  newBinary,
size_t  dataSize 
)
inline

Set the value of a key as a binary.

A private copy of newBinary will allocated and saved inside key, so the parameter can be deallocated after the call.

Binary values might be encoded in another way then string values depending on the plugin. Typically character encodings should not take place on binary data. Consider using a string key instead.

When newBinary is a NULL pointer the binary will be freed and 0 will be returned.

Note
The metadata "binary" will be set to mark that the key is binary from now on. When the key is already binary the metadata won't be changed. This will only happen in the successful case, but not when -1 is returned.
Parameters
keythe object on which to set the value
newBinaryis a pointer to any binary data or NULL to free the previous set data
dataSizenumber of bytes to copy from newBinary
Returns
the number of bytes actually copied to internal struct storage
Return values
0when the internal binary was freed and is now a null pointer
-1if key is a NULL pointer
-1when dataSize is 0 (but newBinary not NULL) or larger than SSIZE_MAX
See also
keyGetBinary()
keyIsBinary() to check if the type is binary
keyGetString() and keySetString() as preferred alternative to binary

◆ setMeta()

template<class T >
void kdb::Key::setMeta ( const std::string &  metaName,
x 
)
inline

Set metadata for key.

Set a new meta-information. Will set a new meta-information pair consisting of metaName and newMetaString.

Will add a new Pair for meta-information if metaName was not added up to now.

It will modify an existing Pair of meta-information if the the metaName was inserted already.

It will remove a meta information if newMetaString is 0.

Parameters
keythe key object to work with
metaNamethe name of the meta information where you want to change the value
newMetaStringthe new value for the meta information
Return values
-1on error if key or metaName is 0, out of memory or names are not valid
0if the meta-information for metaName was removed
Returns
size (>0) of newMetaString if meta-information was successfully added
See also
keyGetMeta()
Warning
unlike the C Interface, it is not possible to remove metadata with this method. k.setMeta("something", NULL) will lead to set the number 0 or to something different (may depend on compiler definition of NULL). See discussion in Issue https://github.com/ElektraInitiative/libelektra/issues/8

Use delMeta() to avoid these issues.

See also
delMeta(), getMeta(), copyMeta(), copyAllMeta()

◆ setName()

void kdb::Key::setName ( const std::string &  newName)
inline

Set a new name to a key.

A valid name is one of the forms:

  • spec/something for specification of other keys.
  • proc/something for in-memory keys, e.g. commandline.
  • dir/something for dir keys in current working directory
  • system/something for system keys in /etc or /
  • user/something for user keys in home directory
  • user:username/something for other users (deprecated: kdbGet() + kdbSet() currently unsupported)
  • /something for cascading keys (actually refers to one of the above, see also ksLookup())

An invalid name either has an invalid namespace or a wrongly escaped \ at the end of the name.

See key names for the exact rules.

The last form has explicitly set the owner, to let the library know in which user folder to save the key. A owner is a user name. If it is not defined (the second form) current user is used.

You should always follow the guidelines for key tree structure creation.

A private copy of the key name will be stored, and the newName parameter can be freed after this call.

.., . and / will be handled as in filesystem paths. A valid name will be build out of the (valid) name what you pass, e.g. user///sw/../sw//././MyApp -> user/sw/MyApp

On invalid names, NULL or "" the name will be "" afterwards.

Return values
sizein bytes of this new key name including ending NULL
0if newName is an empty string or a NULL pointer (name will be empty afterwards)
-1if newName is invalid (name will be empty afterwards)
-1if key was inserted to a keyset before
Parameters
keythe key object to work with
newNamethe new key name
See also
keyNew(), keySetOwner()
keyGetName(), keyGetFullName(), keyName()
keySetBaseName(), keyAddBaseName() to manipulate a name
Exceptions
KeyInvalidNameif the name is not valid

◆ setString()

void kdb::Key::setString ( std::string  newString)
inline

Set the value for key as newStringValue.

The function will allocate and save a private copy of newStringValue, so the parameter can be freed after the call.

String values will be saved in backend storage, when kdbSetKey() will be called, in UTF-8 universal encoding, regardless of the program's current encoding, when iconv plugin is present.

Note
The type will be set to KEY_TYPE_STRING. When the type of the key is already a string type it won't be changed.
Parameters
keythe key to set the string value
newStringValueNULL-terminated text string to be set as key's value
Returns
the number of bytes actually saved in private struct including final NULL
Return values
1if newStringValue is a NULL pointer, this will make the string empty (string only containing null termination)
-1if key is a NULL pointer
See also
keyGetString(), keyValue(), keyString()

The documentation for this class was generated from the following files: