$darkmode
Elektra 0.11.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 a key with the name /. 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 reference counter of a Key object. More...
 
void operator++ () const
 Increment the reference counter of a Key object. More...
 
void operator-- (int) const
 Decrement the reference counter of a Key object. More...
 
void operator-- () const
 Decrement the reference counter of a Key object. More...
 
uint16_t getReferenceCounter () const
 Return the current reference counter value of a Key object. More...
 
Keyoperator= (ckdb::Key *k)
 Assign a C key. More...
 
Keyoperator= (const Key &k)
 Assign a key. More...
 
void copy (const Key &other, elektraCopyFlags flags=KEY_CP_ALL)
 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...
 
Key dup (elektraCopyFlags flags=KEY_CP_ALL) const
 
 ~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's name (excluding owner). More...
 
std::string getBaseName () const
 Returns a pointer to the unescaped Key's name where the basename starts. More...
 
ssize_t getBaseNameSize () const
 Calculates number of bytes needed to store basename of key (including NULL terminator). 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...
 
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...
 
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 (const char *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
 Copy the binary value of a Key into returnedBinary. 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 to the binary value newBinary. More...
 
bool hasMeta (const std::string &metaName) const
 
template<class T >
getMeta (const std::string &metaName) const
 Returns the Key for a metadata entry with name metaName. 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 with name metaName from source to dest. More...
 
void copyAllMeta (const Key &other)
 Do a shallow copy of all metadata from source to dest. More...
 
bool isValid () const
 
ElektraNamespace getNamespace () const
 
ssize_t setNamespace (ElektraNamespace ns) const
 Set the namespace of the key. More...
 
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 the value of key is of string type. More...
 
bool isBinary () const
 Check if the value of a key is of binary type. More...
 
bool isBelow (std::string const &name) const
 Check if the Key check is below the Key key or not. More...
 
bool isBelow (const Key &k) const
 Check if the Key check is below the Key key or not. More...
 
bool isBelowOrSame (std::string const &name) const
 Check if a key is below or same. More...
 
bool isBelowOrSame (const Key &k) const
 Check if a key is below or same. More...
 
bool isDirectBelow (std::string const &name) const
 Check whether the Key check is directly below the Key key. More...
 
bool isDirectBelow (const Key &k) const
 Check whether the Key check is directly below the Key key. More...
 
bool isNameLocked () const
 
bool isValueLocked () const
 
bool isMetaLocked () const
 

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.
Copy-On-Write
Keys employ copy-on-write techniques to minimize memory footprint. If keys are copied or duplicated, they will point at the same name and value as the source key. Only if this data is changed, additional memory is allocated.

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 a key with the name /.

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:

// Create and initialize a key with a name and nothing else
Key *k=keyNew("user:/some/example", KEY_END);
// work with it
keyDel (k);
Key()
Constructs a key with the name /.
Definition: key.hpp:560
int keyDel(Key *key)
A destructor for Key objects.
Definition: key.c:459
Key * keyNew(const char *name,...)
A practical way to fully create a Key object in one step.
Definition: key.c:144
@ KEY_END
Definition: kdbenum.c:95

keyNew() allocates memory for a key object and keyDel() cleans everything up.

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

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 elektraKeyFlags 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_VALUE
    Definition: kdbenum.c:88
  • 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_SIZE
    Definition: kdbenum.c:91
  • KEY_META
    Next two parameter is a metaname and a metavalue. See keySetMeta().
    Key *k=keyNew("user:/tmp/ex3",
    KEY_META, "comment/#0", "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_META
    Definition: kdbenum.c:92
  • 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_BINARY, // binary key
    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
    Definition: kdbenum.c:90
  • 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 *k=keyNew("user:/tmp/ex4",
    KEY_BINARY, // key type
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_META, "comment/#0", "value is truncated",
    KEY_END); // end of args
    Precondition
    name is a valid Key name
    Variable arguments are a valid combination
    Postcondition
    returns a new, fully initialized Key object with the valid Key name and all data given by variable arguments
    Parameters
    namea valid name to the key (see keySetName())
    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()).
    Since
    1.0.0
    See also
    keyDel() for deallocating a created Key object
    keySetName() for rules about which names are considered valid
    Exceptions
    KeyInvalidNameif key could not be constructed
    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:

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

keyNew() allocates memory for a key object and keyDel() cleans everything up.

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

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 elektraKeyFlags 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/#0", "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_BINARY, // binary key
    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 *k=keyNew("user:/tmp/ex4",
    KEY_BINARY, // key type
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_META, "comment/#0", "value is truncated",
    KEY_END); // end of args
    Precondition
    name is a valid Key name
    Variable arguments are a valid combination
    Postcondition
    returns a new, fully initialized Key object with the valid Key name and all data given by variable arguments
    Parameters
    namea valid name to the key (see keySetName())
    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()).
    Since
    1.0.0
    See also
    keyDel() for deallocating a created Key object
    keySetName() for rules about which names are considered valid
    Exceptions
    KeyInvalidNameif key could not be constructed
    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:

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

keyNew() allocates memory for a key object and keyDel() cleans everything up.

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

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 elektraKeyFlags 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/#0", "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_BINARY, // binary key
    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 *k=keyNew("user:/tmp/ex4",
    KEY_BINARY, // key type
    KEY_SIZE, 7, // assume binary length 7
    KEY_VALUE, "some data", // value that will be truncated in 7 bytes
    KEY_META, "comment/#0", "value is truncated",
    KEY_END); // end of args
    Precondition
    name is a valid Key name
    Variable arguments are a valid combination
    Postcondition
    returns a new, fully initialized Key object with the valid Key name and all data given by variable arguments
    Parameters
    namea valid name to the key (see keySetName())
    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()).
    Since
    1.0.0
    See also
    keyDel() for deallocating a created Key object
    keySetName() for rules about which names are considered valid
    Exceptions
    KeyInvalidNameif key could not be constructed
    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 to the name of key. baseName will be escaped before adding it to the name of key. No other part of the Key's name will be affected.

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

If key has the 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);
const char * keyName(const Key *key)
Returns a pointer to the abbreviated real internal key name.
Definition: elektra/keyname.c:429
ssize_t keyAddBaseName(Key *key, const char *baseName)
Adds baseName to the name of key.
Definition: elektra/keyname.c:1724

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), ".");
ssize_t keySetName(Key *key, const char *newName)
Set a new name to a Key.
Definition: elektra/keyname.c:678
const char * keyBaseName(const Key *key)
Returns a pointer to the unescaped Key's name where the basename starts.
Definition: elektra/keyname.c:1394
Parameters
keythe Key to add the basename to
baseNamethe string to append to the Key's name
Returns
the size in bytes of the Key's new name including the NULL terminator
Return values
-1if the Key has no name
-1on NULL pointers
-1if Key was inserted into KeySet before
-1if the Key was read-only
-1on memory allocation errors
Since
1.0.0
See also
keySetBaseName() for setting the basename of a Key
keySetName() for setting the name of a Key
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()

Will clear all internal data of a Key. After this call you will receive a fresh Key - with no value, metadata or name.

The reference counter will stay unmodified.

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
}
int keyClear(Key *key)
Will clear all internal data of a Key.
Definition: key.c:518
ssize_t keySetString(Key *key, const char *newStringValue)
Set the value for key as newStringValue.
Definition: keyvalue.c:381
Postcondition
key's name is "/"
key's metadata is empty
Parameters
keythe Key that should be cleared
Return values
0on success
-1on NULL pointer
Since
1.0.0
See also
keyDel() for completely deleting a Key

◆ copy()

void kdb::Key::copy ( const Key other,
elektraCopyFlags  flags = KEY_CP_ALL 
)
inline

Copy or clear a key.

Depending on the chosen flags keyCopy() only copies certain parts of source into dest.

  • If KEY_CP_NAME is set, the key name will be copied from source to dest.
  • If KEY_CP_META is set, the meta keys will be copied from source to dest.
  • If KEY_CP_VALUE is set, the key value will be copied from source to dest. Additionally, if source is a binary key (keyIsBinary()), dest will also be marked as binary. This means that even if KEY_CP_META is not set, the binary meta key will be copied with KEY_CP_VALUE.
  • If KEY_CP_STRING is set, the key value will be copied from source to dest, but only, if source is not a binary key (keyIsBinary()). If source is binary, keyCopy() fails. If dest is binary, it will still be marked as binary after the copy. This cannot be used together with KEY_CP_VALUE. The main purpose of KEY_CP_STRING is for copying into known string keys. It ensure that you don't accidentally convert string keys into binary keys.

There is also the shorthand KEY_CP_ALL. It is equivalent to KEY_CP_NAME | KEY_CP_VALUE | KEY_CP_META, i.e. all key data supported by keyCopy() will be copied from source to dest.

Use this function when you need to copy into an existing key, e.g. because it was passed by a pointer in a function you can do so:

void copy(const Key &other, elektraCopyFlags flags=KEY_CP_ALL)
Copy or clear a key.
Definition: key.hpp:758
Key * keyCopy(Key *dest, const Key *source, elektraCopyFlags flags)
Copy or clear a key.
Definition: key.c:319
@ KEY_CP_ALL
Definition: kdbenum.c:110

Most often you will want to duplicate an existing key. For this purpose the alias keyDup() exists. Calling

copy = keyDup (orig, KEY_CP_ALL);

is equivalent to

copy = keyCopy (keyNew ("/", KEY_END), orig, KEY_CP_ALL);

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

Since metadata uses copy-on-write semantics there is only a constant memory cost to copying metadata.

When you pass a NULL-pointer as source the pieces of dest specified by flags will be cleared.

Calling keyCopy (dest, NULL, KEY_CP_ALL) is different from calling keyClear(). The key will not be fully reset, the reference counter and internal flags will remain unchanged. Additionally, keyCopy() respects keyLock() state, while keyClear() always works.

keyCopy (k, NULL, KEY_CP_ALL);
// name, value and metadata of k have now been clear
// lock flags, reference count, etc. remain unchanged
Precondition
dest must be a valid Key (created with keyNew)
dest must not have read-only flags set
source must be a valid Key or NULL
Invariant
Key name stays valid until delete
Postcondition
Value from Key source is written to Key dest
Parameters
destthe key which will be written to
sourcethe key which should be copied or NULL to clear the data of dest
flagsspecifies which parts of the key should be copied
Returns
dest
Return values
NULLon memory allocation problems
NULLwhen a part of dest that should be modified (e.g. name, value) was marked read-only, e.g. the name of dest will be read-only if dest is part of a KeySet
NULLwhen dest is NULL
NULLwhen both KEY_CP_VALUE and KEY_CP_STRING are set in flags
NULLwhen both KEY_CP_STRING is set in flags and source is a binary key (keyIsBinary())
Since
0.9.5
See also
keyDup() for duplicating an existing 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. Metadata not present in source will not be changed. Metadata which was present in source and dest will be overwritten. If the dest Key is read-only it will not be changed.

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
}
int keyCopyAllMeta(Key *dest, const Key *source)
Do a shallow copy of all metadata from source to dest.
Definition: keymeta.c:324

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 ("/", KEY_END);
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");
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
current = ksAtCursor (ks, it);
if (needsSharedData (current)) keyCopyAllMeta (current, shared);
}
keyDel (shared);
}
ssize_t keySetMeta(Key *key, const char *metaName, const char *newMetaString)
Set a new metadata Key.
Definition: keymeta.c:442
ssize_t ksGetSize(const KeySet *ks)
Return the number of Keys that ks contains.
Definition: keyset.c:791
Key * ksAtCursor(const KeySet *ks, elektraCursor pos)
Return Key at given position pos.
Definition: keyset.c:1978
Precondition
dest's metadata is not read-only
Postcondition
for every metaName present in source: keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Parameters
destthe destination where the metadata should be copied too
sourcethe key where the metadata should be copied from
Return values
1if metadata was successfully copied
0if source did not have any metadata
-1on null pointer of dest or source
-1on memory problems
Since
1.0.0
See also
keyCopyMeta() for copying one metadata Key from dest to source
getMeta(), setMeta(), copyMeta()

◆ copyMeta()

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

Do a shallow copy of metadata with name metaName from source to dest.

Afterwards source and dest will have the same metadata referred with metaName. If the Key with name metaName doesn't exist in source - it gets deleted in dest.

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
}
int keyCopyMeta(Key *dest, const Key *source, const char *metaName)
Do a shallow copy of metadata with name metaName from source to dest.
Definition: keymeta.c:228

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 keySetMeta() will take n times the memory for the key. This can be a 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 *shared = keyNew ("/", KEY_END);
keySetMeta(shared, "shared", "this metadata should be shared among many keys");
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
Key * current = ksAtCursor (ks, it);
if (needs_shared_data(current)) keyCopyMeta(current, shared, "shared");
}
}
Precondition
dest's metadata is not read-only
Postcondition
keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)
Parameters
destthe destination where the metadata should be copied to
sourcethe key where the metadata should be copied from
metaNamethe name of the metadata Key which should be copied
Return values
1if was successfully copied
0if the metadata in dest was removed too
-1on null pointers (source or dest)
-1on memory problems
-1if metadata is read-only
Since
1.0.0
See also
keyCopyAllMeta() copies all metadata from dest to src
getMeta(), setMeta(), copyAllMeta()

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

Key kdb::Key::dup ( elektraCopyFlags  flags = KEY_CP_ALL) const
inline

◆ get()

template<class T >
T kdb::Key::get
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;
}
ssize_t getStringSize() const
Returns the number of bytes needed to store the key value, including the NULL terminator.
Definition: key.hpp:1163
T get() const
Get a key value.
Definition: key.hpp:1083
std::string getString() const
Definition: key.hpp:1139
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 unescaped Key's 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 the Key has no basename. The reason is

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

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. You should use keySetBaseName() instead.
Do not assume that keyBaseName() points to the same region as keyName() does.
Parameters
keythe Key to obtain the basename from
Returns
a pointer to the Key's basename
Return values
""when the Key has no (base)name
0on NULL pointer
Since
1.0.0
See also
keyGetBaseName() for getting a copy of the Key's basename
keyGetBaseNameSize() for getting the size of the Key's basename
keyName() for getting a pointer to the Key's name

◆ getBaseNameSize()

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

Calculates number of bytes needed to store basename of key (including NULL terminator).

Key names consisting of only root names (e.g. "system:/" or "user:/" or "user:domain" ) do not have basenames. In this case the function will return 1, because only a NULL terminator is needed for storage.

Basenames are denoted as:

  • system:/some/thing/basename -> basename
  • user:domain/some/thing/base\/name > base\/name
Parameters
keythe Key to get the size of the basename from
Returns
size in bytes of the Key's basename including NULL terminator
Return values
-1if the Key or the Key's basename is NULL
Since
1.0.0
See also
keyBaseName() for getting a pointer to a Key's basename
keyGetBaseName() for getting a copy of a Key's basename
keyName(), keyGetName() for getting a pointer / copy of the whole name
keySetName() for setting a Key's name

◆ getBinary()

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

Copy the binary value of a Key into returnedBinary.

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 returnedBinary will not be changed.

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

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

Example:
Key *key = keyNew ("user:/keyname", KEY_BINARY, KEY_END);
char buffer[300];
if (keyGetBinary(key,buffer,sizeof(buffer)) == -1)
{
// handle error
}
ssize_t keyGetBinary(const Key *key, void *returnedBinary, size_t maxSize)
Copy the binary value of a Key into returnedBinary.
Definition: keyvalue.c:443
Parameters
keythe Key object to get the binary value from
returnedBinarypre-allocated memory to store a copy of the Key's value
maxSizenumber of bytes of pre-allocated memory in returnedBinary
Returns
the number of bytes copied to returnedBinary
Return values
0if the binary is empty
-1on NULL pointers
-1if maxSize is 0, too small for the value or larger than SSIZE_MAX
-1if the Key's value is a string
Since
1.0.0
See also
keyValue() for getting a raw pointer to the Key's value
keyGetValueSize() for getting the size of the Key's value
keySetBinary() for setting the binary value of a Key
keyIsBinary() for checking whether a Key's value is binary
keyGetString(), keySetString() for working with string values
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 the value is 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 0 will be returned.

A binary key has no '\0' termination. String types are null-terminated, and the terminator will be considered for the length.

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
ssize_t keyGetValueSize(const Key *key)
Returns the number of bytes needed to store the key value, including the NULL terminator.
Definition: keyvalue.c:266
void * elektraMalloc(size_t size)
Allocate memory for Elektra.
Definition: internal.c:274
Postcondition
returns the exact amount of bytes needed to store key's value (including NULL terminators)
Parameters
keythe Key object to get the size of the value from
Returns
the number of bytes needed to store the Key's value
Return values
1when there is no data and type is a string
0when there is no data and type is binary
-1on null pointer
Since
1.0.0
See also
keyGetString() for getting the Key's value as a string
keyGetBinary() for getting the Key's value as a binary
keyValue() for getting a pointer to the Key's value

◆ 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 Key for a metadata entry with name metaName.

You are not allowed to modify the resulting key.

If metaName does not start with 'meta:/', it will be prefixed with 'meta:/'.

Key metaData = keyGetMeta(k, "type")
// keyType == "boolean"
char keyType[] = keyValue(metaData)
const Key * keyGetMeta(const Key *key, const char *metaName)
Returns the Key for a metadata entry with name metaName.
Definition: keymeta.c:379
const void * keyValue(const Key *key)
Return a pointer to the real internal key value.
Definition: keyvalue.c:163
Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Precondition
key contains metadata
metaName is prefixed with "meta:/"
Parameters
keythe Key from which to get metadata
metaNamethe name of the meta information you want the Key from.
Returns
value of meta-information if meta-information is found
Return values
0if key or metaName is NULL
0if no such metaName is found
Since
1.0.0
See also
keySetMeta() for setting metadata
keyMeta() for getting the KeySet containing metadata

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);
}
T getMeta(const std::string &metaName) const
Returns the Key for a metadata entry with name metaName.
Definition: key.hpp:1328
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().

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 you want to get the name from
Returns
a pointer to the Key's name which must not be changed.
Return values
""when Key's name is empty
0on NULL pointer
Since
1.0.0
See also
keyGetNameSize() for the string length
keyGetName() as alternative to get a copy
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's name (excluding owner).

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

Parameters
keythe Key to get the name size from
Returns
number of bytes needed, including NULL terminator, to store Key's name (excluding owner)
Return values
1if Key has no name
-1on NULL pointer
Since
1.0.0
See also
keyGetName() for getting the Key's name
keyGetUnescapedNameSize() for getting the size of the unescaped name

◆ getNamespace()

ElektraNamespace kdb::Key::getNamespace ( ) const
inline
Returns
namespace of the key
See also
ElektraNamespace, keyGetNamespace

◆ getReferenceCounter()

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

Return the current reference counter value of a Key object.

Parameters
keythe Key whose reference counter to retrieve
Returns
the value of the key's reference counter
Return values
-1on NULL pointer
Since
1.0.0
See also
keyIncRef() for increasing the reference counter and for a more complete explanation of the reference counting system
keyDecRef() for decreasing the reference counter

◆ 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 the value is 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 0 will be returned.

A binary key has no '\0' termination. String types are null-terminated, and the terminator will be considered for the length.

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
Postcondition
returns the exact amount of bytes needed to store key's value (including NULL terminators)
Parameters
keythe Key object to get the size of the value from
Returns
the number of bytes needed to store the Key's value
Return values
1when there is no data and type is a string
0when there is no data and type is binary
-1on null pointer
Since
1.0.0
See also
keyGetString() for getting the Key's value as a string
keyGetBinary() for getting the Key's value as a binary
keyValue() for getting a pointer to the Key's value

◆ 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(). 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);
ssize_t keySetBinary(Key *key, const void *newBinary, size_t dataSize)
Set the value of a Key to the binary value newBinary.
Definition: keyvalue.c:514
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() and keyComment(). For string value the behaviour is the same.
Example:
KDB *handle = kdbOpen();
KeySet *ks=ksNew(0, KS_END);
kdbGetByName(handle,ks,"system:/sw/my",KDB_O_SORT|KDB_O_RECURSIVE);
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
Key * current = ksAtCursor (ks, it);
size_t size=0;
if (keyIsBinary(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);
KDB * kdbOpen(const KeySet *contract, Key *errorKey)
Opens the session with the Key database.
Definition: kdb.c:967
int kdbClose(KDB *handle, Key *errorKey)
Closes the session with the Key database.
Definition: kdb.c:1105
int ksDel(KeySet *ks)
A destructor for KeySet objects.
Definition: keyset.c:521
KeySet * ksNew(size_t alloc,...)
Allocate, initialize and return a new KeySet object.
Definition: keyset.c:282
#define KS_END
End of a list of keys.
Definition: kdbenum.c:156
int keyIsBinary(const Key *key)
Check if the value of a key is of binary type.
Definition: keytest.c:309
const char * keyComment(const Key *key)
Return a pointer to the real internal key comment.
Definition: meta.c:91
size_t elektraStrLen(const char *s)
Calculates the length in bytes of a string.
Definition: internal.c:385
Precondition
key is not NULL and has stored data
Postcondition
returned pointer points to the stored internal value
if the value is a string, the value is NULL terminated
Parameters
keythe Key from which to get the value
Returns
a pointer to the Key's internal value
Return values
""when there is no value and Key is not binary
0where there is no value and Key is binary
0on NULL pointer
Since
1.0.0
See also
keyGetValueSize() to get the size of the Key's value
keyGetString() for getting the Key's value as string
keyGetBinary() for getting the Key's value as binary
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() [1/2]

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 indirectly below key

Obviously, there is no Key above a namespace (e.g. user, system, /):

key *
check user
Parameters
keythe Key object to check against
checkthe Key object for which it should be checked whether it is below key
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
Since
1.0.0
See also
keyIsDirectlyBelow() for checking whether a Key is directly below another
keyGetName(), keySetName() for getting / setting the Key's name

◆ isBelow() [2/2]

bool kdb::Key::isBelow ( std::string const &  name) const
inline

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

Parameters
namethe name of the 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 indirectly below key

Obviously, there is no Key above a namespace (e.g. user, system, /):

key *
check user
Parameters
keythe Key object to check against
checkthe Key object for which it should be checked whether it is below key
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
Since
1.0.0
See also
keyIsDirectlyBelow() for checking whether a Key is directly below another
keyGetName(), keySetName() for getting / setting the Key's name

◆ isBelowOrSame() [1/2]

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

Check if a key is below or same.

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

◆ isBelowOrSame() [2/2]

bool kdb::Key::isBelowOrSame ( std::string const &  name) const
inline

Check if a key is below or same.

Parameters
namethe name of the 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 the value of a key is of binary type.

The function checks if the value of key is binary. Contrary 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.

Parameters
keythe Key to check
Return values
1if the value of key is binary
0if the value of key is not binary
-1on NULL pointer
See also
keyGetBinary(), keySetBinary() for getting / setting a Key's value as binary

◆ 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() [1/2]

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

Check whether the Key check is directly below the Key key.

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 directly below key

Example:
key user:/sw/app
check user:/sw/app/folder/key

does not return true, because it is only indirectly below

Parameters
keythe Key object to check against
checkthe Key object for which it should be checked whether it is directly below key
Return values
1if check is directly below key
0if check is not directly below key or if it is the same
-1on null pointer
Since
1.0.0
See also
keyIsBelow() for checking whether a Key is below another
keyGetName(), keySetName() for getting / setting the Key's name

◆ isDirectBelow() [2/2]

bool kdb::Key::isDirectBelow ( std::string const &  name) const
inline

Check whether the Key check is directly below the Key key.

Parameters
namethe name of the 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 directly below key

Example:
key user:/sw/app
check user:/sw/app/folder/key

does not return true, because it is only indirectly below

Parameters
keythe Key object to check against
checkthe Key object for which it should be checked whether it is directly below key
Return values
1if check is directly below key
0if check is not directly below key or if it is the same
-1on null pointer
Since
1.0.0
See also
keyIsBelow() for checking whether a Key is below another
keyGetName(), keySetName() for getting / setting the Key's name

◆ isMetaLocked()

bool kdb::Key::isMetaLocked ( ) const
inline
Returns
true if the metadata of our key has been locked

◆ isNameLocked()

bool kdb::Key::isNameLocked ( ) const
inline
Returns
true if the name of our key has been locked

◆ 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 the value of key is of 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.

Parameters
keythe Key to check
Return values
1if the value of key is string
0if the value of key is not string
-1on NULL pointer
See also
keyGetString(), keySetString() for getting / setting a Key's value as string

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

◆ isValueLocked()

bool kdb::Key::isValueLocked ( ) const
inline
Returns
true if the value of our key has been locked

◆ 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.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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");
int keyCmp(const Key *k1, const Key *k2)
Compare the name of two Keys.
Definition: keyset.c:768

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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() 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++ ( ) const
inline

Increment the reference counter of a Key object.

As long as the reference counter is non-zero, keyDel() operations on key will be a no-op and return an error code.

Elektra's system for reference counting is not based on a concept of shared ownership. It is more similar to a shared lock, where the counter is used to keep track of how many clients hold the lock.

Initially, the reference counter will be 0. This is can be interpreted as the lock being unlocked. When you increment the reference counter, the lock becomes locked and keyDel() is blocked and fails. Only when the reference counter is fully decremented back down to 0 again, will keyDel() work again.

Note
The reference counter can never exceed UINT16_MAX - 1. UINT16_MAX is reserved as an error code.
Postcondition
key's reference counter is > 0
key's reference counter is <= UINT16_MAX - 1
Parameters
keythe Key object whose reference counter should be increased
Returns
the updated value of the reference counter
Return values
UINT16_MAXon NULL pointer
UINT16_MAXwhen the reference counter already was the maximum value UINT16_MAX - 1, the reference counter will not be modified in this case
Since
1.0.0
See also
keyGetRef() to retrieve the current reference count
keyDecRef() for decreasing the reference counter
keyDel() for deleting a Key

◆ operator++() [2/2]

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

Increment the reference counter of a Key object.

As long as the reference counter is non-zero, keyDel() operations on key will be a no-op and return an error code.

Elektra's system for reference counting is not based on a concept of shared ownership. It is more similar to a shared lock, where the counter is used to keep track of how many clients hold the lock.

Initially, the reference counter will be 0. This is can be interpreted as the lock being unlocked. When you increment the reference counter, the lock becomes locked and keyDel() is blocked and fails. Only when the reference counter is fully decremented back down to 0 again, will keyDel() work again.

Note
The reference counter can never exceed UINT16_MAX - 1. UINT16_MAX is reserved as an error code.
Postcondition
key's reference counter is > 0
key's reference counter is <= UINT16_MAX - 1
Parameters
keythe Key object whose reference counter should be increased
Returns
the updated value of the reference counter
Return values
UINT16_MAXon NULL pointer
UINT16_MAXwhen the reference counter already was the maximum value UINT16_MAX - 1, the reference counter will not be modified in this case
Since
1.0.0
See also
keyGetRef() to retrieve the current reference count
keyDecRef() for decreasing the reference counter
keyDel() for deleting a Key

◆ operator--() [1/2]

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

Decrement the reference counter of a Key object.

As long as the reference counter is non-zero, keyDel() operations on key will be a no-op and return an error code.

Postcondition
key's reference counter is >= 0
key's reference counter is < SSIZE_MAX
Parameters
keythe Key object whose reference counter should get decreased
Returns
the updated value of the reference counter
Return values
UINT16_MAXon NULL pointer
0when the reference counter already was the minimum value 0, the reference counter will not be modified in this case
Since
1.0.0
See also
keyGetRef() to retrieve the current reference count
keyIncRef() for increasing the reference counter and for a more complete explanation of the reference counting system
keyDel() for deleting a Key

◆ operator--() [2/2]

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

Decrement the reference counter of a Key object.

As long as the reference counter is non-zero, keyDel() operations on key will be a no-op and return an error code.

Postcondition
key's reference counter is >= 0
key's reference counter is < SSIZE_MAX
Parameters
keythe Key object whose reference counter should get decreased
Returns
the updated value of the reference counter
Return values
UINT16_MAXon NULL pointer
0when the reference counter already was the minimum value 0, the reference counter will not be modified in this case
Since
1.0.0
See also
keyGetRef() to retrieve the current reference count
keyIncRef() for increasing the reference counter and for a more complete explanation of the reference counting system
keyDel() for deleting a Key

◆ 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.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() during searching
Return values
true< 0

◆ operator<=()

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

Compare the name of two Keys.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() 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.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() during searching
Return values
true== 0

◆ operator>()

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

Compare the name of two Keys.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() during searching
Return values
true> 0

◆ operator>=()

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

Compare the name of two Keys.

The comparison is based on a memcmp of the Key's names. If the names match, 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 than 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

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

Precondition
The Keys k1 and k2 have been properly initialized via keyNew() or are NULL
Invariant
All parts of the Keys remain unchanged
Postcondition
If the result is 0, k1 and k2 cannot be used in the same KeySet
Parameters
k1the first Key to be compared
k2the second Key to be compared
Return values
<0if k1 < k2
0if k1 == k2
>0if k1 > k2
Since
1.0.0
See also
ksAppendKey(), ksAppend() will compare Keys via keyCmp() when appending
ksLookup() will compare Keys via keyCmp() 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.

◆ 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 in UTF-8 universal encoding, regardless of the program's current encoding (if the iconv plugin is available).

Precondition
newStringValue is a NULL terminated string
Postcondition
Value of the Key is set to the UTF-8 encoded value of newStringValue
Metakey meta:/binary is cleared
Parameters
keythe Key for which to set the string value
newStringValueNULL-terminated 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
Since
1.0.0
See also
keyString() for getting a pointer to the Key's value
keyGetString() for getting a copy of the Key's value
keySetBinary() for setting binary data

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 of the Key will be affected.

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);
ssize_t keySetBaseName(Key *key, const char *baseName)
Sets baseName as the new basename for key.
Definition: elektra/keyname.c:1782

All text after the last '/' in the Key's name is erased and baseName is appended. If baseName is 0 (NULL), then the last part of the Key's name is removed without replacement. The root name of the Key will not be removed though.

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.

If you want to add to the basename instead of changing it, use keyAddBaseName(). If you do not want any escaping, use keyAddName().

Parameters
keythe Key whose basename to set
baseNamethe new basename for the Key
Returns
the size in bytes of the new key name
Return values
-1if Key is NULL
-1if Key was inserted into KeySet before
-1if Key is read-only
-1on allocation errors
Since
1.0.0
See also
keyAddBaseName() for adding a basename instead of changing it
keyAddName() for adding a name without escaping
keySetName() for setting a completely new name
Name Manipulation Methods for more details on special names
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 to the binary value newBinary.

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

Binary values might be encoded in another way than string values depending on the plugin. Typically character encodings should not take place on binary data. Consider using a string Key instead, if encoding should occur.

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

Read-only keys will stay unchanged after calling this function.

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.
Precondition
dataSize matches the length of newBinary
newBinary is not NULL and dataSize > 0
key is not read-only
Postcondition
key's value set exactly to the data in newBinary
"binary" key set in key's metadata
Parameters
keythe Key object where the value should be set
newBinarya pointer to any binary data or NULL (to clear the stored value)
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 NULL
-1when dataSize is 0 (and newBinary not NULL) or larger than SSIZE_MAX
-1if key is read-only
Since
1.0.0
See also
keyGetBinary() for getting a Key's value as binary
keyIsBinary() to check if the Key's value is binary
keyGetString() and keySetString() for working with string values

◆ setMeta()

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

Set metadata for key.

Set a new metadata Key. Will set a new metadata pair with name metaName and value newMetaString.

Will add a new metadata Key, if metaName was unused until now.

It will modify an existing Pair of metadata if metaName was already present.

It will remove a metadata Key if newMetaString is 0.

If metaName does not start with 'meta:/', it will be prefixed with 'meta:/'.

Precondition
metaName is prefixed with "meta:/"
key's metadata is not read-only
Postcondition
The value in key's metadata Keyset for metaName is newMetaString
Parameters
keyKey whose metadata should be set
metaNamename of the metadata Key that should be set
newMetaStringnew value for the metadata Key
Returns
size (>0) of newMetaString if metadata has been successfully added
Return values
0if the meta-information for metaName was removed
-1if key or metaName is 0
-1if system is out of memory
-1if metaName is not a valid metadata name
Since
1.0.0
See also
keyGetMeta() for getting the value of a metadata Key
keyMeta() for getting the KeySet containing metadata
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

Trailing slashes will be stripped.

On invalid names, the name stays unchanged.

Returns
size of the new Key name in bytes, including NULL terminator
Return values
-1if key or keyName is NULL or keyName is empty or invalid
-1if Key was inserted to a KeySet before
-1if Key name is read-only
Parameters
keythe Key whose name to set
newNamethe new name for the Key
Since
1.0.0
See also
keyGetName() for getting a copy of the Key's name
keyName() for getting a pointer to the Key's name
keySetBaseName(), keyAddBaseName() for manipulating the base name
Exceptions
KeyInvalidNameif the name is not valid

◆ setNamespace()

ssize_t kdb::Key::setNamespace ( ElektraNamespace  ns) const
inline

Set the namespace of the key.

See also
ElektraNamespace, keySetNamespace

◆ setString()

void kdb::Key::setString ( const char *  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 in UTF-8 universal encoding, regardless of the program's current encoding (if the iconv plugin is available).

Precondition
newStringValue is a NULL terminated string
Postcondition
Value of the Key is set to the UTF-8 encoded value of newStringValue
Metakey meta:/binary is cleared
Parameters
keythe Key for which to set the string value
newStringValueNULL-terminated 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
Since
1.0.0
See also
keyString() for getting a pointer to the Key's value
keyGetString() for getting a copy of the Key's value
keySetBinary() for setting binary data

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