Elektra  0.9.0
Functions
Meta Info Manipulation Methods

Methods to do various operations on Key metadata. More...

Collaboration diagram for Meta Info Manipulation Methods:

Functions

int keyRewindMeta (Key *key)
 Rewind the internal iterator to first metadata. More...
 
const Key * keyNextMeta (Key *key)
 Iterate to the next meta information. More...
 
const Key * keyCurrentMeta (const Key *key)
 Returns the value of a meta-information which is current. More...
 
int keyCopyMeta (Key *dest, const Key *source, const char *metaName)
 Do a shallow copy of metadata from source to dest. More...
 
int keyCopyAllMeta (Key *dest, const Key *source)
 Do a shallow copy of all metadata from source to dest. More...
 
const Key * keyGetMeta (const Key *key, const char *metaName)
 Returns the value of a meta-information given by name. More...
 
ssize_t keySetMeta (Key *key, const char *metaName, const char *newMetaString)
 Set a new meta-information. More...
 

Detailed Description

Methods to do various operations on Key metadata.

To use them:

#include <kdb.h>

Next to Name (key and owner) and value (data and comment) there is the so called meta information inside every key.

Key meta information are an unlimited number of key/value pairs strongly related to a key. It main purpose is to give keys special semantics, so that plugins can treat them differently.

Metakey, as opposed to Key, deliberately has following limitations:

Examples for metadata

File system information (see stat(2) for more information):

The comment can contain userdata which directly belong to that key. The name of the meta information is "comment" for a general purpose comment about the key. Multi-Language comments are also supported by appending [LANG] to the name.

Validators are regular expressions which are tested against the key value. The metakey "validator" can hold a regular expression which will be matched against.

Types can be expressed with the meta information "type".

The relevance of the key can be tagged with a value from -20 to 20. Negative numbers are the more important and must be present in order to start the program.

A version of a key may be stored with "version". Its format is full.major.minor where all of these are integers.

The order inside a persistent storage can be described with the tag "order" which contains a positive number.

The metakey "app" describes to which application a key belongs. It can be used to remove keys from an application no longer installed.

The metakey "path" describes where the key is physically stored.

The "owner" is the user that owns the key. It only works for the user/ hierarchy. It rather says where the key is stored and says nothing about the filesystem properties.

Function Documentation

◆ keyCopyAllMeta()

int keyCopyAllMeta ( Key *  dest,
const Key *  source 
)

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

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

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

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

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

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

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

◆ keyCopyMeta()

int keyCopyMeta ( Key *  dest,
const Key *  source,
const char *  metaName 
)

Do a shallow copy of metadata from source to dest.

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

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

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

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

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

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

◆ keyCurrentMeta()

const Key* keyCurrentMeta ( const Key *  key)

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

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

Note
You must not delete or change the returned key, use keySetMeta() if you want to delete or change it.
Parameters
keythe key object to work with
Returns
a buffer to the value pointed by key's cursor
Return values
0on NULL pointer
See also
keyNextMeta(), keyRewindMeta()
ksCurrent() for pedant in iterator interface of KeySet

◆ keyGetMeta()

const Key* keyGetMeta ( const Key *  key,
const char *  metaName 
)

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

You are not allowed to modify the resulting key.

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

◆ keyNextMeta()

const Key* keyNextMeta ( Key *  key)

Iterate to the next meta information.

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

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

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

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

◆ keyRewindMeta()

int keyRewindMeta ( Key *  key)

Rewind the internal iterator to first metadata.

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

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

◆ keySetMeta()

ssize_t keySetMeta ( Key *  key,
const char *  metaName,
const char *  newMetaString 
)

Set a new meta-information.

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

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

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

It will remove a meta information if newMetaString is 0.

Parameters
keythe key object to work with
metaNamethe name of the meta information where you want to change the value
newMetaStringthe new value for the meta information
Return values
-1on error if key or metaName is 0, out of memory or names are not valid
0if the meta-information for metaName was removed
Returns
size (>0) of newMetaString if meta-information was successfully added
See also
keyGetMeta()