Elektra
0.9.0
|
Key is an essential class that encapsulates key name , value and metainfo . More...
Modules | |
Meta Info Manipulation Methods | |
Methods to do various operations on Key metadata. | |
Methods for Making Tests | |
Methods to do various tests on Keys. | |
Name Manipulation Methods | |
Methods to do various operations on Key names. | |
Value Manipulation Methods | |
Methods to do various operations on Key values. | |
Enumerations | |
enum | keyswitch_t { KEY_NAME =1, KEY_VALUE =1<<1, KEY_FLAGS =3, KEY_OWNER =1<<2, KEY_COMMENT =1<<3, KEY_BINARY =1<<4, KEY_UID =1<<5, KEY_GID =1<<6, KEY_MODE =1<<7, KEY_ATIME =1<<8, KEY_MTIME =1<<9, KEY_CTIME =1<<10, KEY_SIZE =1<<11, KEY_DIR =1<<14, KEY_META =1<<15, KEY_NULL =1<<16, KEY_CASCADING_NAME =1<<20, KEY_META_NAME =1<<21, KEY_END =0 } |
Allows keyNew() to determine which information comes next. More... | |
enum | elektraNamespace { KEY_NS_NONE =0, KEY_NS_EMPTY =1, KEY_NS_META =2, KEY_NS_CASCADING =3, KEY_NS_FIRST =4, KEY_NS_SPEC =4, KEY_NS_PROC =5, KEY_NS_DIR =6, KEY_NS_USER =7, KEY_NS_SYSTEM =8, KEY_NS_LAST =8 } |
Elektra currently supported Key namespaces. More... | |
Functions | |
Key * | keyNew (const char *name,...) |
A practical way to fully create a Key object in one step. More... | |
Key * | keyDup (const Key *source) |
Return a duplicate of a key. More... | |
int | keyCopy (Key *dest, const Key *source) |
Copy or Clear a key. More... | |
int | keyDel (Key *key) |
A destructor for Key objects. More... | |
int | keyClear (Key *key) |
Key Object Cleaner. More... | |
ssize_t | keyIncRef (Key *key) |
Increment the viability of a key object. More... | |
ssize_t | keyDecRef (Key *key) |
Decrement the viability of a key object. More... | |
ssize_t | keyGetRef (const Key *key) |
Return how many references the key has. More... | |
Key is an essential class that encapsulates key name , value and metainfo .
To use it include:
Key properties are:
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.enum elektraNamespace |
Elektra currently supported Key namespaces.
Enumerator | |
---|---|
KEY_NS_NONE | no key given as parameter to keyGetNamespace() |
KEY_NS_EMPTY | key name was empty, e.g. invalid key name |
KEY_NS_META | metakey, i.e. any key name not under other categories |
KEY_NS_CASCADING | cascading key, starts with /, abstract name for any of the namespaces below |
KEY_NS_FIRST | For iteration over namespaces (first element, inclusive) |
KEY_NS_SPEC | spec contains the specification of the other namespaces |
KEY_NS_PROC | proc contains process-specific configuration |
KEY_NS_DIR | dir contains configuration from a specific directory |
KEY_NS_USER | user key in the home directory of the current user |
KEY_NS_SYSTEM | system key is shared for a computer system |
KEY_NS_LAST | For iteration over namespaces (last element, inclusive) |
enum keyswitch_t |
Allows keyNew() to determine which information comes next.
Enumerator | |
---|---|
KEY_NAME | Flag for the key name |
KEY_VALUE | Flag for the key data |
KEY_FLAGS | Allows to define multiple flags at once. |
KEY_OWNER | Flag for the key user domain |
KEY_COMMENT | Flag for the key comment |
KEY_BINARY | Flag if the key is binary |
KEY_UID | Flag for the key UID
|
KEY_GID | Flag for the key GID
|
KEY_MODE | Flag for the key permissions
|
KEY_ATIME | Flag for the key access time
|
KEY_MTIME | Flag for the key change time
|
KEY_CTIME | Flag for the key status change time
|
KEY_SIZE | Flag for maximum size to limit value |
KEY_DIR | Flag for the key directories
|
KEY_META | Flag for metadata |
KEY_NULL | Is not a flag, only as return value
|
KEY_CASCADING_NAME | Is default, no need to use it
|
KEY_META_NAME | Allow any key names (not only with known namespaces+cascading |
KEY_END | Used as a parameter terminator to keyNew() |
int keyClear | ( | Key * | key | ) |
Key Object Cleaner.
Will reset all internal data.
After this call you will receive a fresh key.
The reference counter will stay unmodified.
returns | 0 on success |
-1 | on null pointer |
key | the key object to work with |
int keyCopy | ( | Key * | dest, |
const Key * | source | ||
) |
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:
The reference counter will not be changed for both keys. Affiliation to keysets are also not affected.
The metadata will be duplicated for the destination key. So it will not take much additional space, even with lots of metadata.
When you pass a NULL-pointer as source the data of dest will be cleaned completely (except reference counter, see keyClear()) and you get a fresh dest key:
If you want to copy everything, except e.g. the value you can use keyCopy() too:
Restrain from coping everything yourself, because it will lead to wrong metadata and is not able to copy empty or cascading names:
dest | the key which will be written to |
source | the key which should be copied or NULL to clean the destination key |
-1 | on failure when a NULL pointer was passed for dest or a dynamic property could not be written. The content will be unmodified then. |
0 | when dest was cleaned |
1 | when source was successfully copied |
ssize_t keyDecRef | ( | Key * | key | ) |
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.
-1 | on null pointer |
0 | when the key is ready to be freed |
key | the key object to work with |
int keyDel | ( | Key * | key | ) |
A destructor for Key objects.
Every key created by keyNew() must be deleted with keyDel().
It is safe to delete keys which are in a keyset, the number of references will be returned then.
It is save to delete a nullpointer, -1 will be returned then.
It is also save to delete a multiple referenced key, nothing will happen then and the reference counter will be returned.
key | the key object to delete |
0 | when the key was freed |
-1 | on null pointers |
Key* keyDup | ( | const Key * | source | ) |
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.
Like for a new key after keyNew() a subsequent ksAppend() makes a KeySet to take care of the lifecycle of the key.
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.
source | has to be an initialized source Key |
0 | failure or on NULL pointer |
ssize_t keyGetRef | ( | const Key * | key | ) |
Return how many references the key has.
The reference counting is the essential property of keys to make sure that they can be put safely into data structures. E.g. if you put a Key into a KeySet:
You can even add the key to more KeySets:
If you increment only by one with keyIncRef() the same as said above is valid:
or use keyIncRef() more than once:
The key won't be deleted by a keyDel() as long refcounter is not 0.
The references will be incremented on successful calls to ksAppendKey() or ksAppend().
For your own applications you can use keyIncRef() and keyDecRef() for reference counting, too.
key | the key object to work with |
-1 | on null pointer |
ssize_t keyIncRef | ( | Key * | key | ) |
Increment the viability of a key object.
This function is intended for applications using their own reference counter for key objects. With it you can increment the reference and thus avoid destruction of the object in a subsequent keyDel().
The reference counter can't be incremented once it reached SSIZE_MAX. In that situation nothing will happen and SSIZE_MAX will be returned.
-1 | on null pointer |
SSIZE_MAX | when maximum exceeded |
key | the key object to work with |
Key* keyNew | ( | const char * | name, |
... | |||
) |
A practical way to fully create a Key object in one step.
To just get a key object, simple do:
keyNew() allocates memory for a key object and keyDel() cleans everything up.
We can also give an empty key name and a KEY_END tag with the same effect as before:
But we can also give the key a proper name right from the start:
If you want the key object to contain a name, value, comment and other meta info read on.
You can call keyNew() in many different ways depending on the attribute tags you pass as parameters. Tags are represented as keyswitch_t values, and tell keyNew() which Key attribute comes next. The Key attribute tags are the following:
keyName
is 0.name | a valid name to the key, or NULL to get a simple initialized, but really empty, object |
NULL | on allocation error or if an invalid name was passed (see keySetName()). |