kdb::Key(3) value and metainfo .

Other Alias

A Key is the essential class that encapsulates key name

SYNOPSIS


#include <key.hpp>

Public Member Functions


Key ()

Key (ckdb::Key *k)

Key (Key &k)

Key (Key const &k)

Key (const char *keyName,...)

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

Key (const char *keyName, va_list ap)

void operator++ (int) const

void operator++ () const

void operator-- (int) const

void operator-- () const

ssize_t getReferenceCounter () const

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

Key & operator= (const Key &k)

void copy (const Key &other)

void clear ()

Key * operator-> ()

ckdb::Key * getKey () const

ckdb::Key * operator* () const

ckdb::Key * release ()

ckdb::Key * dup () const

~Key ()

std::string getName () const

ssize_t getNameSize () const

std::string getBaseName () const

ssize_t getBaseNameSize () const

std::string getDirName () const

void setName (const std::string &newName)

void setBaseName (const std::string &baseName)

void addBaseName (const std::string &baseName)

ssize_t getFullNameSize () const

std::string getFullName () const

Key & operator= (const std::string &newName)

Key & operator+= (const std::string &baseName)

Key & operator-= (const std::string &baseName)

Key & operator= (const char *newName)
(const std::string &newName)
Key & operator+= (const char *baseName)
(const std::string &)
Key & operator-= (const char *baseName)
(const std::string &)
bool operator== (const Key &k) const

bool operator!= (const Key &k) const

bool operator< (const Key &other) const

bool operator<= (const Key &other) const

bool operator> (const Key &other) const

bool operator>= (const Key &other) const

operator bool () const

template<class T > T get () const

template<class T > void set (T x)

std::string getString () const

void setString (std::string newString)

ssize_t getStringSize () const

func_t getFunc () const

const void * getValue () const

std::string getBinary () const

ssize_t getBinarySize () const

ssize_t setBinary (const void *newBinary, size_t dataSize)

bool hasMeta (const std::string &metaName) const

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

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

void delMeta (const std::string &metaName)

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

void copyAllMeta (const Key &other)

void rewindMeta () const

const Key nextMeta ()

const Key currentMeta () const

bool isValid () const

bool isSystem () const

bool isUser () const

bool isString () const

bool isBinary () const

bool isInactive () const

bool isBelow (const Key &k) const

bool isBelowOrSame (const Key &k) const

bool isDirectBelow (const Key &k) const

Detailed Description

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

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

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

Note:

that the reference counting in the keys is mutable, so that const keys can be passed around by value.

Constructor & Destructor Documentation

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(), operator bool()

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:

k the key to work with

See also:

isValid(), operator bool()

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:

k the key to work with

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:

k the key to work with

kdb::Key::Key (const char *keyName, ...) [inline], [explicit]

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

This function tries to mimic the C++ way for constructors.

To just get a key object, simple do:

Key *k = keyNew(0);
// 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 and cheaper to keyDup() the key.

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. See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135

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

The simplest and minimum way to use it is with no tags, only a key name:

Key *nullKey,*emptyNamedKey;
// Create a key that has no name, is completely empty, but is initialized
nullKey=keyNew(0);
keyDel (nullKey);
// Is the same as above
nullKey=keyNew("", KEY_END);
keyDel (nullKey);
// Create and initialize a key with a name and nothing else
emptyNamedKey=keyNew("user/some/example",KEY_END);
keyDel (emptyNamedKey);

keyNew() allocates memory for a key object and cleans everything up. After that, it processes the given argument list.

The Key attribute tags are the following:

  • keyswitch_t::KEY_TYPE

     Next parameter is a type of the value. Default assumed is KEY_TYPE_UNDEFINED. Set this attribute so that a subsequent KEY_VALUE can toggle to keySetString() or keySetBinary() regarding to keyIsString() or keyIsBinary(). If you don't use KEY_TYPE but a KEY_VALUE follows afterwards, KEY_TYPE_STRING will be used.
  • keyswitch_t::KEY_SIZE

     Define a maximum length of the value. This is especially useful for setting a binary key. So make sure you use that before you KEY_VALUE for binary keys.
  • keyswitch_t::KEY_VALUE

     Next parameter is a pointer to the value that will be set to the key If no keyswitch_t::KEY_TYPE was used before, keyswitch_t::KEY_TYPE_STRING is assumed. If KEY_TYPE was previously passed with a KEY_TYPE_BINARY, you should have passed KEY_SIZE before! Otherwise it will be cut of with first \0 in string!
  • keyswitch_t::KEY_UID, keyswitch_t::KEY_GID

     Next parameter is taken as the UID (uid_t) or GID (gid_t) that will be defined on the key. See keySetUID() and keySetGID().
  • keyswitch_t::KEY_MODE

     Next parameter is taken as mode permissions (int) to the key. See keySetMode().
  • keyswitch_t::KEY_DIR

     Define that the key is a directory rather than a ordinary key. This means its executable bits in its mode are set. This option allows the key to have subkeys. See keySetDir().
  • keyswitch_t::KEY_OWNER

     Next parameter is the owner. See keySetOwner().
  • keyswitch_t::KEY_COMMENT

     Next parameter is a comment. See keySetComment().
  • keyswitch_t::KEY_END

     Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.

Example:

KeySet *ks=ksNew(0);
ksAppendKey(ks,keyNew(0));       // an empty key
ksAppendKey(ks,keyNew("user/sw",              // the name of the key
        KEY_END));                      // no more args
ksAppendKey(ks,keyNew("user/tmp/ex1",
        KEY_VALUE,"some data",          // set a string value
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex2",
        KEY_VALUE,"some data",          // with a simple value
        KEY_MODE,0777,                  // permissions
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex4",
        KEY_TYPE,KEY_TYPE_BINARY,       // key type
        KEY_SIZE,7,                     // assume binary length 7
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex5",
        KEY_TYPE,
                KEY_TYPE_DIR | KEY_TYPE_BINARY,// dir key with a binary value
        KEY_SIZE,7,
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksDel(ks);

The reference counter (see keyGetRef()) 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 keyInc()) and the key can't be be deleted by a keyDel().

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks, k); // ref counter of key 1
ksDel(ks); // key will be deleted with keyset

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

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

If you add the key to more keySets:

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks1, k); // ref counter of key 1
ksAppendKey(ks2, k); // ref counter of key 2
ksDel(ks1); // ref counter of key 1
ksDel(ks2); // k is now deleted

or use keyInc() 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

they key won't be deleted by a keyDel() as long refcounter is not 0.

The key's sync bit will always be set for any call, except:

Key *k = keyNew(0);
// keyNeedSync() will be false

Parameters:

name a 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:

NULL on malloc error or if an invalid name was passed (see keySetName()).

Exceptions:

KeyInvalidName if key could not be constructed (typically name wrong or at runtime on allocation problems)

Parameters:

keyName the name of the new key

kdb::Key::Key (const std::stringkeyName, ...) [inline], [explicit]

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

This function tries to mimic the C++ way for constructors.

To just get a key object, simple do:

Key *k = keyNew(0);
// 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 and cheaper to keyDup() the key.

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. See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135

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

The simplest and minimum way to use it is with no tags, only a key name:

Key *nullKey,*emptyNamedKey;
// Create a key that has no name, is completely empty, but is initialized
nullKey=keyNew(0);
keyDel (nullKey);
// Is the same as above
nullKey=keyNew("", KEY_END);
keyDel (nullKey);
// Create and initialize a key with a name and nothing else
emptyNamedKey=keyNew("user/some/example",KEY_END);
keyDel (emptyNamedKey);

keyNew() allocates memory for a key object and cleans everything up. After that, it processes the given argument list.

The Key attribute tags are the following:

  • keyswitch_t::KEY_TYPE

     Next parameter is a type of the value. Default assumed is KEY_TYPE_UNDEFINED. Set this attribute so that a subsequent KEY_VALUE can toggle to keySetString() or keySetBinary() regarding to keyIsString() or keyIsBinary(). If you don't use KEY_TYPE but a KEY_VALUE follows afterwards, KEY_TYPE_STRING will be used.
  • keyswitch_t::KEY_SIZE

     Define a maximum length of the value. This is especially useful for setting a binary key. So make sure you use that before you KEY_VALUE for binary keys.
  • keyswitch_t::KEY_VALUE

     Next parameter is a pointer to the value that will be set to the key If no keyswitch_t::KEY_TYPE was used before, keyswitch_t::KEY_TYPE_STRING is assumed. If KEY_TYPE was previously passed with a KEY_TYPE_BINARY, you should have passed KEY_SIZE before! Otherwise it will be cut of with first \0 in string!
  • keyswitch_t::KEY_UID, keyswitch_t::KEY_GID

     Next parameter is taken as the UID (uid_t) or GID (gid_t) that will be defined on the key. See keySetUID() and keySetGID().
  • keyswitch_t::KEY_MODE

     Next parameter is taken as mode permissions (int) to the key. See keySetMode().
  • keyswitch_t::KEY_DIR

     Define that the key is a directory rather than a ordinary key. This means its executable bits in its mode are set. This option allows the key to have subkeys. See keySetDir().
  • keyswitch_t::KEY_OWNER

     Next parameter is the owner. See keySetOwner().
  • keyswitch_t::KEY_COMMENT

     Next parameter is a comment. See keySetComment().
  • keyswitch_t::KEY_END

     Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.

Example:

KeySet *ks=ksNew(0);
ksAppendKey(ks,keyNew(0));       // an empty key
ksAppendKey(ks,keyNew("user/sw",              // the name of the key
        KEY_END));                      // no more args
ksAppendKey(ks,keyNew("user/tmp/ex1",
        KEY_VALUE,"some data",          // set a string value
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex2",
        KEY_VALUE,"some data",          // with a simple value
        KEY_MODE,0777,                  // permissions
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex4",
        KEY_TYPE,KEY_TYPE_BINARY,       // key type
        KEY_SIZE,7,                     // assume binary length 7
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex5",
        KEY_TYPE,
                KEY_TYPE_DIR | KEY_TYPE_BINARY,// dir key with a binary value
        KEY_SIZE,7,
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksDel(ks);

The reference counter (see keyGetRef()) 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 keyInc()) and the key can't be be deleted by a keyDel().

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks, k); // ref counter of key 1
ksDel(ks); // key will be deleted with keyset

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

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

If you add the key to more keySets:

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks1, k); // ref counter of key 1
ksAppendKey(ks2, k); // ref counter of key 2
ksDel(ks1); // ref counter of key 1
ksDel(ks2); // k is now deleted

or use keyInc() 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

they key won't be deleted by a keyDel() as long refcounter is not 0.

The key's sync bit will always be set for any call, except:

Key *k = keyNew(0);
// keyNeedSync() will be false

Parameters:

name a 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:

NULL on malloc error or if an invalid name was passed (see keySetName()).

Exceptions:

KeyInvalidName if key could not be constructed (typically name wrong or at runtime on allocation problems)

Warning:

Not supported on some compilers, e.g. clang which require you to only pass non-POD in varg lists.

Parameters:

keyName the name of the new key

kdb::Key::Key (const char *keyName, va_listap) [inline], [explicit]

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

This function tries to mimic the C++ way for constructors.

To just get a key object, simple do:

Key *k = keyNew(0);
// 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 and cheaper to keyDup() the key.

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. See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135

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

The simplest and minimum way to use it is with no tags, only a key name:

Key *nullKey,*emptyNamedKey;
// Create a key that has no name, is completely empty, but is initialized
nullKey=keyNew(0);
keyDel (nullKey);
// Is the same as above
nullKey=keyNew("", KEY_END);
keyDel (nullKey);
// Create and initialize a key with a name and nothing else
emptyNamedKey=keyNew("user/some/example",KEY_END);
keyDel (emptyNamedKey);

keyNew() allocates memory for a key object and cleans everything up. After that, it processes the given argument list.

The Key attribute tags are the following:

  • keyswitch_t::KEY_TYPE

     Next parameter is a type of the value. Default assumed is KEY_TYPE_UNDEFINED. Set this attribute so that a subsequent KEY_VALUE can toggle to keySetString() or keySetBinary() regarding to keyIsString() or keyIsBinary(). If you don't use KEY_TYPE but a KEY_VALUE follows afterwards, KEY_TYPE_STRING will be used.
  • keyswitch_t::KEY_SIZE

     Define a maximum length of the value. This is especially useful for setting a binary key. So make sure you use that before you KEY_VALUE for binary keys.
  • keyswitch_t::KEY_VALUE

     Next parameter is a pointer to the value that will be set to the key If no keyswitch_t::KEY_TYPE was used before, keyswitch_t::KEY_TYPE_STRING is assumed. If KEY_TYPE was previously passed with a KEY_TYPE_BINARY, you should have passed KEY_SIZE before! Otherwise it will be cut of with first \0 in string!
  • keyswitch_t::KEY_UID, keyswitch_t::KEY_GID

     Next parameter is taken as the UID (uid_t) or GID (gid_t) that will be defined on the key. See keySetUID() and keySetGID().
  • keyswitch_t::KEY_MODE

     Next parameter is taken as mode permissions (int) to the key. See keySetMode().
  • keyswitch_t::KEY_DIR

     Define that the key is a directory rather than a ordinary key. This means its executable bits in its mode are set. This option allows the key to have subkeys. See keySetDir().
  • keyswitch_t::KEY_OWNER

     Next parameter is the owner. See keySetOwner().
  • keyswitch_t::KEY_COMMENT

     Next parameter is a comment. See keySetComment().
  • keyswitch_t::KEY_END

     Must be the last parameter passed to keyNew(). It is always required, unless the keyName is 0.

Example:

KeySet *ks=ksNew(0);
ksAppendKey(ks,keyNew(0));       // an empty key
ksAppendKey(ks,keyNew("user/sw",              // the name of the key
        KEY_END));                      // no more args
ksAppendKey(ks,keyNew("user/tmp/ex1",
        KEY_VALUE,"some data",          // set a string value
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex2",
        KEY_VALUE,"some data",          // with a simple value
        KEY_MODE,0777,                  // permissions
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex4",
        KEY_TYPE,KEY_TYPE_BINARY,       // key type
        KEY_SIZE,7,                     // assume binary length 7
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksAppendKey(ks,keyNew("user/tmp/ex5",
        KEY_TYPE,
                KEY_TYPE_DIR | KEY_TYPE_BINARY,// dir key with a binary value
        KEY_SIZE,7,
        KEY_VALUE,"some data",          // value that will be truncated in 7 bytes
        KEY_COMMENT,"value is truncated",
        KEY_OWNER,"root",               // owner (not uid) is root
        KEY_UID,0,                      // root uid
        KEY_END));                      // end of args
ksDel(ks);

The reference counter (see keyGetRef()) 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 keyInc()) and the key can't be be deleted by a keyDel().

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks, k); // ref counter of key 1
ksDel(ks); // key will be deleted with keyset

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

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

If you add the key to more keySets:

Key *k = keyNew(0); // ref counter 0
ksAppendKey(ks1, k); // ref counter of key 1
ksAppendKey(ks2, k); // ref counter of key 2
ksDel(ks1); // ref counter of key 1
ksDel(ks2); // k is now deleted

or use keyInc() 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

they key won't be deleted by a keyDel() as long refcounter is not 0.

The key's sync bit will always be set for any call, except:

Key *k = keyNew(0);
// keyNeedSync() will be false

Parameters:

name a 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:

NULL on malloc error or if an invalid name was passed (see keySetName()).

Exceptions:

KeyInvalidName if key could not be constructed (typically name wrong or at runtime on allocation problems)

Parameters:

keyName the name of the new key
ap the variable argument list pointer

kdb::Key::~Key () [inline]

Destructs the key.

See also:

del()

Member Function Documentation

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

Adds a base name for a key

Adds baseName to the current key name.

Assumes that key is a directory. baseName is appended to it. The function adds '/' if needed while concatenating.

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

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

Warning:

You should not change a keys name once it belongs to a keyset because it would destroy the order.

TODO: does not recognice .. and . in the string!

Parameters:

key the key object to work with
baseName the string to append to the name

Returns:

the size in bytes of the new key name including the ending NULL

-1 if the key had no name

-1 on NULL pointers

See also:

keySetBaseName()

keySetName() to set a new name.

Exceptions:

KeyInvalidName if the name is not valid

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(), operator bool()

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
}

Returns:

returns 0 on success

-1 on null pointer

Parameters:

key the key object to work with

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, c);
        // 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.

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
}

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

If you want to copy all metadata, but keep the old 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, c);
        if (bstring) keySetString (k, value);
        else keySetBinary (k, value, size);
        free (value);
        // the caller will see the changed key k
        // with the metadata from c
}

Note:

Next to the value itself we also need to remember if the value was string or binary. So in fact the meta data of the resulting key k in that example is not a complete duplicate, because the meta data 'binary' may differ. Similar considerations might be necessary for the type of the key and so on, depending on the concrete situation.

Parameters:

dest the key which will be written to
source the key which should be copied or NULL to clean the destination key

Returns:

-1 on failure when a NULL pointer was passed for dest or a dynamic property could not be written. Both name and value are empty then.

0 when dest was cleaned

1 when source was successfully copied

See also:

keyDup() to get a duplication of a Key

void kdb::Key::copyAllMeta (const Key &other) [inline]

Do a shallow copy of all meta data from source to dest.

The key dest will additionally have all meta data 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 meta data type is copied into the Key k.

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

The main purpose of this function is for plugins or applications which want to add the same meta data 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 meta data 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 meta data should be shared among many keys");
        keySetMeta(shared, "shared2", "this meta data should be shared among many keys also");
        keySetMeta(shared, "shared3", "this meta data should be shared among many keys too");
        ksRewind(ks);
        while ((current = ksNext(ks)) != 0)
        {
                if (needs_shared_data(current)) keyCopyAllMeta(current, shared);
        }
}

Postcondition:

for every metaName present in source: keyGetMeta(source, metaName) == keyGetMeta(dest, metaName)

Returns:

1 if was successfully copied

0 if source did not have any meta data

-1 on null pointers (source or dest)

-1 on memory problems

Parameters:

dest the destination where the meta data should be copied too
source the key where the meta data should be copied from

See also:

getMeta(), setMeta(), copyMeta()

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

Do a shallow copy of meta data from source to dest.

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

For example the meta data 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 meta data 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 meta data 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 meta data 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)

Returns:

1 if was successfully copied

0 if the meta data in dest was removed too

-1 on null pointers (source or dest)

-1 on memory problems

Parameters:

dest the destination where the meta data should be copied too
source the key where the meta data should be copied from
metaName the name of the meta data which should be copied

See also:

getMeta(), setMeta(), copyAllMeta()

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:

key the key object to work with

Returns:

a buffer to the value pointed by key's cursor

0 on NULL pointer

See also:

keyNextMeta(), keyRewindMeta()

ksCurrent() for pedant in iterator interface of KeySet

Note:

that the key will be null if last meta data is found.

k.rewindMeta();
while (meta = k.nextMeta())
{
        cout << meta.getName() << " " << meta.getString() << endl;
}

See also:

rewindMeta(), nextMeta()

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

Delete metadata for key.

See also:

setMeta(), getMeta(), copyMeta(), copyAllMeta()

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:

source has to be an initializised source Key

Returns:

0 failure or on NULL pointer

a fully copy of source on success

See also:

ksAppend(), keyDel(), keyNew()

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

Get a key value.

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


Returns:

the string directly from the key.

It should be the same as get().

Returns:

empty string on null pointers

Exceptions:

KeyException on null key or not a valid size
KeyTypeMismatch if 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.

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

Returns a pointer to the real internal 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(). If you need a copy of the basename consider to use keyGetBaseName().

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

key=keyNew(0);
keySetName(key,"");
keyBaseName(key); // you would expect "" here
keySetName(key,"user");
keyBaseName(key); // you would expect "" 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 keyBaseName() method to set a new value. Use keySetBaseName() instead.

Parameters:

key the object to obtain the basename from

Returns:

a pointer to the basename

0 on NULL pointer

See also:

keyGetBaseName(), keyGetBaseNameSize()

keyName() to get a pointer to the name

keyOwner() to get a pointer to the owner

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:

key the key object to work with

Returns:

size in bytes of key's basename including ending NULL

See also:

keyBaseName(), keyGetBaseName()

keyName(), keyGetName(), keySetName()

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

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:

KeyException on invalid binary size
KeyTypeMismatch if key is string and not a binary

Get the value of a key as 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:

key the object to gather the value from
returnedBinary pre-allocated memory to store a copy of the key value
maxSize number of bytes of pre-allocated memory in returnedBinary

Returns:

the number of bytes actually copied to returnedBinary

Return values:

0 if the binary is empty
-1 on NULL pointers
-1 if maxSize is 0
-1 if maxSize is too small for string
-1 if maxSize is larger than SSIZE_MAX
-1 on 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()

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 malloc() before keyGetString() or keyGetBinary() is called.

char *buffer;
buffer = malloc (keyGetValueSize (key));
// use this buffer to store the value (binary or string)
// pass keyGetValueSize (key) for maxSize

Parameters:

key the key object to work with

Returns:

the number of bytes needed to store the key value

1 when there is no data and type is not binary

0 when there is no data and type is binary

-1 on null pointer

See also:

keyGetString(), keyGetBinary(), keyValue()

std::string kdb::Key::getDirName () const [inline]

Returns:

the dir name of the key

e.g. system/sw/dir/key will return system/sw/dir

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

Get key full name, including the user domain name.

Returns:

number of bytes written

1 on empty name

-1 on NULL pointers

-1 if maxSize is 0 or larger than SSIZE_MAX

Parameters:

key the key object
returnedName pre-allocated memory to write the key name
maxSize maximum number of bytes that will fit in returnedName, including the final NULL

Exceptions:

KeyException if key is null

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

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

Parameters:

key the key object to work with

Returns:

number of bytes needed to store key name including user domain

1 on empty name

-1 on NULL pointer

See also:

keyGetFullName(), keyGetNameSize()

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

Elektra can store function pointers as binary. This function returns such a function pointer.

Exceptions:

KeyTypeMismatch if no binary data found, or binary data has not correct length

Returns:

a function pointer stored with setBinary()

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

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

Returns the Value of a Meta-Information given by name.

This is a much more efficient version of keyGetMeta(). But unlike with keyGetMeta you are not allowed to modify the resulting string.

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:

key the key object to work with
metaName the name of the meta information you want the value from

Returns:

0 if the key or metaName is 0

0 if no such metaName is found

value of Meta-Information if Meta-Information is found

See also:

keyGetMeta(), 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::keyValue(
                                ckdb::keyGetMeta(key, name.c_str())
                                )
                        )
                );
        return yourconversion(str);
}

Exceptions:

KeyTypeConversion if meta data 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:

  • char* is null (evaluates to 0)
  • const Key is null (evaluate to false)
  • otherwise the default constructed type will be returned

See also:

hasMeta

delMeta(), setMeta(), copyMeta(), copyAllMeta()

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.

keyName() returns '' when there is no keyName. The reason is

key=keyNew(0);
keySetName(key,"");
keyName(key); // you would expect "" 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 keyName() method to set a new value. Use keySetName() instead.

Parameters:

key the key object to work with

Returns:

a pointer to the keyname which must not be changed.

0 on 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

Note:

unlike in the C version, it is safe to change the returned string.

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:

key the key object to work with

Returns:

number of bytes needed, including ending NULL, to store key name without owner

1 if there is is no key Name

-1 on NULL pointer

See also:

keyGetName(), keyGetFullNameSize()

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

Return how many references the key has.

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. Keys with zero references will be deleted when using keyDel().

Parameters:

key the key object to work with

Returns:

the number of references

-1 on null pointer

See also:

keyIncRef() and keyDecRef()

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:

KeyException on null key or not a valid size
KeyTypeMismatch if 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()

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 malloc() before keyGetString() or keyGetBinary() is called.

char *buffer;
buffer = malloc (keyGetValueSize (key));
// use this buffer to store the value (binary or string)
// pass keyGetValueSize (key) for maxSize

Parameters:

key the key object to work with

Returns:

the number of bytes needed to store the key value

1 when there is no data and type is not binary

0 when there is no data and type is binary

-1 on null pointer

See also:

keyGetString(), keyGetBinary(), keyValue()

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 ' ') 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);
Key *current=0;
kdbGetByName(handle,ks,"system/sw/my",KDB_O_SORT|KDB_O_RECURSIVE);
ksRewind(ks);
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>omment: %s",
                        keyName(current),
                        size,
                        keyComment(current));
        } else {
                size=elektraStrLen((char *)keyValue(current));
                printf("Key %s has a value of size %d bytes. Value: %somment: %s",
                        keyName(current),
                        size,
                        (char *)keyValue(current),
                        keyComment(current));
        }
}
ksDel (ks);
kdbClose (handle);

Parameters:

key the key object to work with

Returns:

a pointer to internal value

0 where there is no data and key is binary

0 on NULL pointer

See also:

keyGetValueSize(), keyGetString(), keyGetBinary()

Returns:

the value of the key

See also:

getBinary()

bool kdb::Key::hasMeta (const std::string &metaName) const [inline]

Return values:

true if there is a metadata with given name
false if no such metadata exists

See also:

getMeta()

bool kdb::Key::isBelow (const Key &k) const [inline]

Parameters:

k the other key

Returns:

true if our key is below k

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

Example:

key user/sw/app
check user/sw/app/key

returns true because check is below key

Example:

key user/sw/app
check user/sw/app/folder/key

returns also true because check is indirect below key

Parameters:

key the key object to work with
check the key to find the relative position of

Returns:

1 if check is below key

0 if it is not below or if it is the same key

See also:

keySetName(), keyGetName(), keyIsDirectBelow()

bool kdb::Key::isBelowOrSame (const Key &k) const [inline]

Parameters:

k the other key

Returns:

true if our key is below k or the same as k

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.

Returns:

1 if it is binary

0 if it is not

-1 on NULL pointer

See also:

keyGetBinary(), keySetBinary()

Parameters:

key the key to check

bool kdb::Key::isDirectBelow (const Key &k) const [inline]

Parameters:

k the other key

Returns:

true if our key is direct below k

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

Example:
key user/sw/app
check user/sw/app/key
returns true because check is below key
Example:
key user/sw/app
check user/sw/app/folder/key
does not return true, because there is only a indirect relation

Parameters:

key the key object to work with
check the key to find the relative position of

Returns:

1 if check is below key

0 if it is not below or if it is the same key

-1 on null pointer

See also:

keyIsBelow(), keySetName(), keyGetName()

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

Check whether a key is inactive or not.

In elektra terminology any key is inactive if the it's basename starts with '.'. Inactive keys must not have any meaning to applications, they are reserved for users and administrators.

To remove a whole hierarchy in elektra, don't forget to pass option_t::KDB_O_INACTIVE to kdbGet() to receive the inactive keys in order to remove them.

Otherwise you should not fetch these keys.

Parameters:

key the key object to work with

Returns:

1 if the key is inactive, 0 otherwise

-1 on NULL pointer or when key has no name

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.

Returns:

1 if it is string

0 if it is not

-1 on NULL pointer

See also:

keyGetString(), keySetString()

Parameters:

key the key to check

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

Name starts with 'system'.

Return values:

true if it is a system key
false otherwise

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

Name starts with 'user'.

Return values:

true if it is a user key
false otherwise

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:

true if the key has a valid name
false if the key has an invalid name

See also:

getName(), isUser(), isSystem()

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:

key the key object to work with

Returns:

a key representing meta information

0 when the end is reached

0 on NULL pointer

See also:

ksNext() for pedant in iterator interface of KeySet

rewindMeta(), currentMeta()

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

This is for loops and lookups only.

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

Returns:

false on null keys

true otherwise

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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

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

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

Key *k;
keyInc (k);
function_that_keyDec(k);
// work with k
keyDel (k); // now really free it

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

-1 on null pointer

SSIZE_MAX when maximum exceeded

Parameters:

key the key object to work with

See also:

keyGetRef(), keyDecRef(), keyDel()

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

Key *k;
keyInc (k);
function_that_keyDec(k);
// work with k
keyDel (k); // now really free it

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

-1 on null pointer

SSIZE_MAX when maximum exceeded

Parameters:

key the key object to work with

See also:

keyGetRef(), keyDecRef(), keyDel()

Key & kdb::Key::operator+= (const std::string &newAddBaseName) [inline]

Add a new basename.

See also:

keyAddBaseName()

Key & kdb::Key::operator+= (const char *newAddBaseName) [inline]

(const std::string &) Add a new basename.

See also:

keyAddBaseName() (const std::string &)

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

-1 on null pointer

0 when the key is ready to be freed

Parameters:

key the key object to work with

See also:

keyGetRef(), keyDel(), keyIncRef()

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

-1 on null pointer

0 when the key is ready to be freed

Parameters:

key the key object to work with

See also:

keyGetRef(), keyDel(), keyIncRef()

Key & kdb::Key::operator-= (const std::string &newSetBaseName) [inline]

Set a new basename.

See also:

keySetBaseName()

Key & kdb::Key::operator-= (const char *newSetBaseName) [inline]

(const std::string &) Set a new basename.

See also:

keySetBaseName() (const std::string &)

Key * kdb::Key::operator-> () [inline]

Returns:

a pointer to this object

Needed for KeySet iterators.

See also:

KeySetIterator

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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

Assign a C key.

Will call del() on the old key.

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

Assign a key.

Will call del() on the old key.

Key & kdb::Key::operator= (const std::string &newName) [inline]

Assign the name of a key.

See also:

keySetName

Key & kdb::Key::operator= (const char *newName) [inline]

(const std::string &newName) Assign a C key.

Will call del() on the old key. (const std::string &newName)

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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. They only take account when none of the preceding rules matched.

  • 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.
  • No owner will be found to be smaller then every other owner. If both don't have a 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 then 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:

// keyCmp(0,0) == 0
// keyCmp(k1,0) ==  1
// keyCmp(0,k2) == -1

You can write similar equation for the other rules.

Here are some more examples with equation:

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

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

Warning:

Do not try to strcmp the keyName() yourself because the used strcmp implementation is allowed to differ from simple ascii comparison.

Parameters:

k1 the first key object to compare with
k2 the 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

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

Passes out the raw key pointer.

Note:

that the ownership is moved outside.

The key will stay empty.

void kdb::Key::rewindMeta () const [inline]

Rewind the internal iterator to first meta data.

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;
keyRewindMeta (key);
while ((meta = keyNextMeta (key))!=0)
{
        printf ("name: %s, value: %s", keyName(meta), (const char*)keyValue(meta));
}

Parameters:

key the key object to work with

Returns:

0 on success

0 if there is no meta information for that key (keyNextMeta() will always return 0 in that case)

-1 on NULL pointer

See also:

keyNextMeta(), keyCurrentMeta()

ksRewind() for pedant in iterator interface of KeySet

nextMeta(), currentMeta()

template<class T > void kdb::Key::set (Tx) [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:

key the key to set the string value
newStringValue NULL-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:

1 if newStringValue is a NULL pointer, this will make the string empty (string only containing null termination)
-1 if key is a NULL pointer

See also:

keyGetString(), keyValue(), keyString()

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

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

Sets a base name for a key.

Sets baseName as the new basename for key.

All text after the last '/' in the key keyname is erased and baseName is appended.

So lets 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 empty or NULL, the resulting key name will be 'system/dir1/dir2'.

Warning:

You should not change a keys name once it belongs to a keyset because it would destroy the order.

TODO: does not work with .. and .

Parameters:

key the key object to work with
baseName the string used to overwrite the basename of the key

Returns:

the size in bytes of the new key name

-1 on NULL pointers

See also:

keyAddBaseName()

keySetName() to set a new name

Exceptions:

KeyInvalidName if the name is not valid

ssize_t kdb::Key::setBinary (const void *newBinary, size_tdataSize) [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 meta data 'binary' will be set to mark that the key is binary from now on. When the key is already binary the meta data won't be changed. This will only happen in the successful case, but not when -1 is returned.

Parameters:

key the object on which to set the value
newBinary is a pointer to any binary data or NULL to free the previous set data
dataSize number of bytes to copy from newBinary

Returns:

the number of bytes actually copied to internal struct storage

0 when the internal binary was freed and is now a null pointer

-1 if key is a NULL pointer

-1 when 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

template<class T > void kdb::Key::setMeta (const std::string &metaName, Tx) [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 a existing Pair of Meta-Information if the the metaName was inserted already.

It will remove a meta information if newMetaString is 0.

Parameters:

key the key object to work with
metaName the name of the meta information where you want to change the value
newMetaString the new value for the meta information

Returns:

-1 on error if key or metaName is 0, out of memory or names are not valid

0 if the Meta-Information for metaName was removed

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

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

Set a new name to a key.

A valid name is of the forms:

  • system/something
  • user/something
  • user:username/something

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

Warning:

You shall not change a key name once it belongs to a keyset.

Return values:

size in bytes of this new key name including ending NULL
0 if newName is an empty string or a NULL pointer (name will be empty afterwards)
-1 if newName is invalid (name will be empty afterwards)

Parameters:

key the key object to work with
newName the new key name

See also:

keyNew(), keySetOwner()

keyGetName(), keyGetFullName(), keyName()

keySetBaseName(), keyAddBaseName() to manipulate a name

Exceptions:

KeyInvalidName if the name is not valid

void kdb::Key::setString (std::stringnewString) [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:

key the key to set the string value
newStringValue NULL-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:

1 if newStringValue is a NULL pointer, this will make the string empty (string only containing null termination)
-1 if key is a NULL pointer

See also:

keyGetString(), keyValue(), keyString()

Author

Generated automatically by Doxygen for Elektra from the source code.