table of contents
keyvalue(3elektra) | Elektra | keyvalue(3elektra) |
NAME¶
keyvalue - Value Manipulation Methods
Methods to do various operations on Key values.
Functions¶
const void * keyValue (const Key *key)
Return a pointer to the real internal key value. const char *
keyString (const Key *key)
Get the c-string representing the value. ssize_t
keyGetValueSize (const Key *key)
Returns the number of bytes needed to store the key value, including the
NULL terminator. ssize_t keyGetString (const Key *key, char
*returnedString, size_t maxSize)
Get the value of a key as a string. ssize_t keySetString (Key
*key, const char *newStringValue)
Set the value for key as newStringValue. ssize_t
keyGetBinary (const Key *key, void *returnedBinary, size_t maxSize)
Get the value of a key as a binary. ssize_t keySetBinary (Key
*key, const void *newBinary, size_t dataSize)
Set the value of a key as a binary.
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¶
ssize_t keyGetBinary (const Key * key, void * returnedBinary, size_t maxSize)¶
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:
1 Key *key = keyNew ("user/keyname", KEY_TYPE, KEY_TYPE_BINARY, KEY_END); 2 char buffer[300]; 3 4 if (keyGetBinary(key,buffer,sizeof(buffer)) == -1) 5 { 6 // handle error 7 }
Parameters:
returnedBinary pre-allocated memory to store a copy of the key value
maxSize number of bytes of pre-allocated memory in returnedBinary
Returns:
Return values:
-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:
keyGetString() and keySetString() as preferred alternative to binary
keyIsBinary() to see how to check for binary type
ssize_t keyGetString (const Key * key, char * returnedString, size_t maxSize)¶
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:
1 Key *key = keyNew ("user/keyname", KEY_END); 2 char buffer[300]; 3 4 if (keyGetString(key,buffer,sizeof(buffer)) == -1) 5 { 6 // handle error 7 } else { 8 printf ("buffer: %s0, buffer); 9 }
Parameters:
returnedString pre-allocated memory to store a copy of the key value
maxSize number of bytes of allocated memory in returnedString
Returns:
Return values:
-1 on any NULL pointers
-1 on type mismatch: string expected, but found binary
-1 maxSize is 0
-1 if maxSize is too small for string
-1 if maxSize is larger than SSIZE_MAX
See also:
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 '\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.
1 char *buffer; 2 buffer = malloc (keyGetValueSize (key)); 3 // use this buffer to store the value (binary or string) 4 // pass keyGetValueSize (key) for maxSize
Parameters:
Returns:
Return values:
0 when there is no data and type is binary
-1 on null pointer
See also:
ssize_t keySetBinary (Key * key, const void * newBinary, size_t dataSize)¶
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:
Parameters:
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:
Return values:
-1 if key is a NULL pointer
-1 when dataSize is 0 (but newBinary not NULL) or larger than SSIZE_MAX
See also:
keyIsBinary() to check if the type is binary
keyGetString() and keySetString() as preferred alternative to binary
ssize_t keySetString (Key * key, const char * newStringValue)¶
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:
Parameters:
newStringValue NULL-terminated text string to be set as key's value
Returns:
Return values:
-1 if key is a NULL pointer
See also:
const char* keyString (const Key * key)¶
Get the c-string representing the value. Will return (null) on null pointers. Will return (binary) on binary data not ended with a null byte.
It is not checked if it is actually a string, only if it terminates for security reasons.
Returns:
Return values:
'' if no data found
(binary) on binary keys
Parameters:
const void* keyValue (const Key * key)¶
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
1 key=keyNew(0); 2 keySetString(key,""); 3 keyValue(key); // you would expect "" here 4 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
1 key=keyNew(0); 2 keySetBinary(key, 0, 0); 3 keyValue(key); // you would expect 0 here 4 5 keySetBinary(key,"", 1); 6 keyValue(key); // you would expect "" (a pointer to ' ') here 7 8 int i=23; 9 keySetBinary(key, (void*)&i, 4); 10 (int*)keyValue(key); // you would expect a pointer to (int)23 here 11 keyDel(key);
Note:
Warning:
Example:
1 KDB *handle = kdbOpen(); 2 KeySet *ks=ksNew(0, KS_END); 3 Key *current=0; 4 5 kdbGetByName(handle,ks,"system/sw/my",KDB_O_SORT|KDB_O_RECURSIVE); 6 7 ksRewind(ks); 8 while(current=ksNext(ks)) { 9 size_t size=0; 10 11 if (keyIsBin(current)) { 12 size=keyGetValueSize(current); 13 printf("Key %s has a value of size %d bytes. Value: <BINARY>0omment: %s", 14 keyName(current), 15 size, 16 keyComment(current)); 17 } else { 18 size=elektraStrLen((char *)keyValue(current)); 19 printf("Key %s has a value of size %d bytes. Value: %s0omment: %s", 20 keyName(current), 21 size, 22 (char *)keyValue(current), 23 keyComment(current)); 24 } 25 } 26 27 ksDel (ks); 28 kdbClose (handle);
Parameters:
Returns:
Return values:
0 where there is no data and key is binary
0 on NULL pointer
See also:
Author¶
Generated automatically by Doxygen for Elektra from the source code.
Sun May 29 2016 | Version 0.8.14 |