Key :: Value Manipulation Methods(3) Library Functions Manual Key :: Value Manipulation Methods(3)
NAME
Key :: Value Manipulation Methods -
Methods to do various operations on Key values.
Functions
const void * keyValue (const Key *key)
ssize_t keyGetValueSize (const Key *key)
ssize_t keyGetString (const Key *key, char *returnedString, size_t maxSize)
ssize_t keySetString (Key *key, const char *newStringValue)
ssize_t keyGetBinary (const Key *key, void *returnedBinary, size_t maxSize)
ssize_t keySetBinary (Key *key, const void *newBinary, size_t dataSize)
const char * keyComment (const Key *key)
ssize_t keyGetCommentSize (const Key *key)
ssize_t keyGetComment (const Key *key, char *returnedComment, size_t maxSize)
ssize_t keySetComment (Key *key, const char *newComment)
Detailed Description
Methods to do various operations on Key values.
A key can contain a value in different format. The most likely situation is, that the value is interpreted as text. Use keyGetString() for
that. You can save any Unicode Symbols and Elektra will take care that you get the same back, independent of your current environment.
In some situations this idea fails. When you need exactly the same value back without any interpretation of the characters, there is
keySetBinary(). If you use that, its very likely that your Configuration is not according to the standard. Also for Numbers, Booleans and
Date you should use keyGetString(). To do so, you might use strtod() strtol() and then atol() or atof() to convert back.
To use them:
#include <kdb.h>
Function Documentation
const char* keyComment (const Key *key) Return a pointer to the real internal key comment.
This is a much more efficient version of keyGetComment() and you should use it if you are responsible enough to not mess up things. You are
not allowed to change anything in the memory region the returned pointer points to.
keyComment() returns '' when there is no keyComment. The reason is
key=keyNew(0);
keySetComment(key,'');
keyComment(key); // you would expect '' here
keyDel(key);
See keySetComment() for more information on comments.
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 keyComment() method to set a new value. Use keySetComment() instead.
Parameters:
key the key object to work with
Returns:
a pointer to the internal managed comment
0 on NULL pointer
See also:
keyGetCommentSize() for size and keyGetComment() as alternative
ssize_t keyGetBinary (const Key *key, void *returnedBinary, size_tmaxSize) 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
0 if the binary is empty
-1 on NULL pointers
-1 when maxSize is 0, too small to hold the value or larger than SSIZE_MAX
-1 on typing error when the key is not binary
See also:
keyValue(), keyGetValueSize(), keySetBinary()
keyGetString() and keySetString() as preferred alternative to binary
keyIsBinary() to see how to check for binary type
ssize_t keyGetComment (const Key *key, char *returnedComment, size_tmaxSize) Get the key comment.
Comments
A Key comment is description for humans what this key is for. It may be a textual explanation of valid values, when and why a user or
administrator changed the key or any other text that helps the user or administrator related to that key.
Don't depend on a comment in your program. A user is always allowed to remove or change it in any way he wants to. But you are allowed or
even encouraged to always show the content of the comment to the user and allow him to change it.
Parameters:
key the key object to work with
returnedComment pre-allocated memory to copy the comments to
maxSize number of bytes that will fit returnedComment
Returns:
the number of bytes actually copied to returnedString, including final NULL
1 if the string is empty
-1 on NULL pointer
-1 if maxSize is 0, not enough to store the comment or when larger then SSIZE_MAX
See also:
keyGetCommentSize(), keySetComment()
ssize_t keyGetCommentSize (const Key *key) Calculates number of bytes needed to store a key comment, including final NULL.
Use this method to know to size for allocated memory to retrieve a key comment.
See keySetComment() for more information on comments.
For an empty key name you need one byte to store the ending NULL. For that reason 1 is returned.
char *buffer;
buffer = malloc (keyGetCommentSize (key));
// use this buffer to store the comment
// pass keyGetCommentSize (key) for maxSize
Parameters:
key the key object to work with
Returns:
number of bytes needed
1 if there is no comment
-1 on NULL pointer
See also:
keyGetComment(), keySetComment()
ssize_t keyGetString (const Key *key, char *returnedString, size_tmaxSize) Get the value of a key as a string.
When there is no value inside the string, 1 will be returned and the returnedString will be empty '' to avoid programming errors that old
strings are shown to the user.
For binary values see keyGetBinary() and keyIsBinary().
Example:
Key *key = keyNew ('user/keyname', KEY_END);
char buffer[300];
if (keyGetString(key,buffer,sizeof(buffer)) == -1)
{
// handle error
} else {
printf ('buffer: %s0, buffer);
}
Parameters:
key the object to gather the value from
returnedString pre-allocated memory to store a copy of the key value
maxSize number of bytes of allocated memory in returnedString
Returns:
the number of bytes actually copied to returnedString, including final NULL
1 if the string is empty
-1 on NULL pointer
-1 on type mismatch
maxSize is 0, too small for string or is larger than SSIZE_MAX
See also:
keyValue(), keyGetValueSize(), keySetString()
keyGetBinary() for working with binary data
ssize_t keyGetValueSize (const Key *key) 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 '