1 .TH "Key :: Value Manipulation Methods" 3 "30 Jun 2009" "Elektra Projekt" \" -*- nroff -*-
5 Key :: Value Manipulation Methods \- Methods to do various operations on Key values.
12 .RI "const void * \fBkeyValue\fP (const Key *key)"
15 .RI "ssize_t \fBkeyGetValueSize\fP (const Key *key)"
18 .RI "ssize_t \fBkeyGetString\fP (const Key *key, char *returnedString, size_t maxSize)"
21 .RI "ssize_t \fBkeySetString\fP (Key *key, const char *newStringValue)"
24 .RI "ssize_t \fBkeyGetBinary\fP (const Key *key, void *returnedBinary, size_t maxSize)"
27 .RI "ssize_t \fBkeySetBinary\fP (Key *key, const void *newBinary, size_t dataSize)"
30 .RI "const char * \fBkeyComment\fP (const Key *key)"
33 .RI "ssize_t \fBkeyGetCommentSize\fP (const Key *key)"
36 .RI "ssize_t \fBkeyGetComment\fP (const Key *key, char *returnedComment, size_t maxSize)"
39 .RI "ssize_t \fBkeySetComment\fP (Key *key, const char *newComment)"
42 .SH "Detailed Description"
44 Methods to do various operations on Key values.
46 A key can contain a value in different format. The most likely situation is, that the value is interpreted as text. Use \fBkeyGetString()\fP for that. You can save any Unicode Symbols and Elektra will take care that you get the same back, independent of your current environment.
48 In some situations this idea fails. When you need exactly the same value back without any interpretation of the characters, there is \fBkeySetBinary()\fP. 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 \fBkeyGetString()\fP. To do so, you might use strtod() strtol() and then atol() or atof() to convert back.
58 .SH "Function Documentation"
60 .SS "const char* keyComment (const Key * key)"
62 Return a pointer to the real internal \fCkey\fP comment.
64 This is a much more efficient version of \fBkeyGetComment()\fP 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.
66 \fBkeyComment()\fP returns '' when there is no keyComment. The reason is
70 keySetComment(key,'');
71 keyComment(key); // you would expect '' here
77 See \fBkeySetComment()\fP for more information on comments.
81 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 \fBkeyComment()\fP method to set a new value. Use \fBkeySetComment()\fP instead.
86 \fIkey\fP the key object to work with
91 a pointer to the internal managed comment
93 '' when there is no comment
100 \fBkeyGetCommentSize()\fP for size and \fBkeyGetComment()\fP as alternative
104 .SS "ssize_t keyGetBinary (const Key * key, void * returnedBinary, size_t maxSize)"
106 Get the value of a key as a binary.
108 If the type is not binary -1 will be returned.
110 When the binary data is empty (this is not the same as ''!) 0 will be returned and the returnedBinary will not be changed.
112 For string values see \fBkeyGetString()\fP and \fBkeyIsString()\fP.
114 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.
120 Key *key = keyNew ('user/keyname', KEY_TYPE, KEY_TYPE_BINARY, KEY_END);
123 if (keyGetBinary(key,buffer,sizeof(buffer)) == -1)
134 \fIkey\fP the object to gather the value from
136 \fIreturnedBinary\fP pre-allocated memory to store a copy of the key value
138 \fImaxSize\fP number of bytes of pre-allocated memory in \fCreturnedBinary\fP
143 the number of bytes actually copied to \fCreturnedBinary\fP
145 0 if the binary is empty
149 -1 when maxSize is 0, too small to hold the value or larger than SSIZE_MAX
151 -1 on typing error when the key is not binary
156 \fBkeyValue()\fP, \fBkeyGetValueSize()\fP, \fBkeySetBinary()\fP
158 \fBkeyGetString()\fP and \fBkeySetString()\fP as preferred alternative to binary
160 \fBkeyIsBinary()\fP to see how to check for binary type
164 .SS "ssize_t keyGetComment (const Key * key, char * returnedComment, size_t maxSize)"
169 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.
171 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.
175 \fIkey\fP the key object to work with
177 \fIreturnedComment\fP pre-allocated memory to copy the comments to
179 \fImaxSize\fP number of bytes that will fit returnedComment
184 the number of bytes actually copied to \fCreturnedString\fP, including final NULL
186 1 if the string is empty
190 -1 if maxSize is 0, not enough to store the comment or when larger then SSIZE_MAX
195 \fBkeyGetCommentSize()\fP, \fBkeySetComment()\fP
199 .SS "ssize_t keyGetCommentSize (const Key * key)"
201 Calculates number of bytes needed to store a key comment, including final NULL.
203 Use this method to know to size for allocated memory to retrieve a key comment.
205 See \fBkeySetComment()\fP for more information on comments.
207 For an empty key name you need one byte to store the ending NULL. For that reason 1 is returned.
212 buffer = malloc (keyGetCommentSize (key));
213 // use this buffer to store the comment
214 // pass keyGetCommentSize (key) for maxSize
220 \fIkey\fP the key object to work with
225 number of bytes needed
227 1 if there is no comment
234 \fBkeyGetComment()\fP, \fBkeySetComment()\fP
238 .SS "ssize_t keyGetString (const Key * key, char * returnedString, size_t maxSize)"
240 Get the value of a key as a string.
242 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.
244 For binary values see \fBkeyGetBinary()\fP and \fBkeyIsBinary()\fP.
250 Key *key = keyNew ('user/keyname', KEY_END);
253 if (keyGetString(key,buffer,sizeof(buffer)) == -1)
257 printf ('buffer: %s\n', buffer);
266 \fIkey\fP the object to gather the value from
268 \fIreturnedString\fP pre-allocated memory to store a copy of the key value
270 \fImaxSize\fP number of bytes of allocated memory in \fCreturnedString\fP
275 the number of bytes actually copied to \fCreturnedString\fP, including final NULL
277 1 if the string is empty
283 maxSize is 0, too small for string or is larger than SSIZE_MAX
288 \fBkeyValue()\fP, \fBkeyGetValueSize()\fP, \fBkeySetString()\fP
290 \fBkeyGetBinary()\fP for working with binary data
294 .SS "ssize_t keyGetValueSize (const Key * key)"
296 Returns the number of bytes needed to store the key value, including the NULL terminator.
298 It returns the correct size, independent of the Key Type. If it is a binary there might be '\\0' values in it.
300 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.
302 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.
304 This method can be used with malloc() before \fBkeyGetString()\fP or \fBkeyGetBinary()\fP is called.
309 buffer = malloc (keyGetValueSize (key));
310 // use this buffer to store the value (binary or string)
311 // pass keyGetValueSize (key) for maxSize
317 \fIkey\fP the key object to work with
322 the number of bytes needed to store the key value
324 1 when there is no data and type is not binary
326 0 when there is no data and type is binary
333 \fBkeyGetString()\fP, \fBkeyGetBinary()\fP, \fBkeyValue()\fP
337 .SS "ssize_t keySetBinary (Key * key, const void * newBinary, size_t dataSize)"
339 Set the value of a key as a binary.
341 A private copy of \fCnewBinary\fP will allocated and saved inside \fCkey\fP, so the parameter can be deallocated after the call.
343 The \fCfilesys\fP backend, when used through a \fBkdbSetKey()\fP, will make the value be kdbbEncoded into a human readable hex-digit text format.
345 Consider using a string key instead.
347 When newBinary is a NULL pointer the binary will be freed and 0 will be returned.
351 When the type of the key is already a binary type it won't be changed.
356 \fIkey\fP the object on which to set the value
358 \fInewBinary\fP is a pointer to any binary data or NULL to free the previous set data
360 \fIdataSize\fP number of bytes to copy from \fCnewBinary\fP
365 the number of bytes actually copied to internal struct storage
367 0 when the internal binary was freed
371 -1 when dataSize is 0 (but newBinary not NULL) or larger than SSIZE_MAX
378 \fBkeyIsBinary()\fP to check if the type is binary
380 \fBkeyGetString()\fP and \fBkeySetString()\fP as preferred alternative to binary
384 .SS "ssize_t keySetComment (Key * key, const char * newComment)"
386 Set a comment for a key.
388 A key comment is like a configuration file comment. See \fBkeySetComment()\fP for more information.
392 \fIkey\fP the key object to work with
394 \fInewComment\fP the comment, that can be freed after this call.
399 the number of bytes actually saved including final NULL
401 1 when the comment was freed
403 -1 on NULL pointer or memory problems
408 \fBkeyGetComment()\fP
412 .SS "ssize_t keySetString (Key * key, const char * newStringValue)"
414 Set the value for \fCkey\fP as \fCnewStringValue\fP.
416 The function will allocate and save a private copy of \fCnewStringValue\fP, so the parameter can be freed after the call.
418 String values will be saved in backend storage, when \fBkdbSetKey()\fP will be called, in UTF-8 universal encoding, regardless of the program's current encoding, when compiled with --enable-iconv.
420 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.
424 \fIkey\fP the key to set the string value
426 \fInewStringValue\fP NULL-terminated text string to be set as \fCkey's\fP value
431 the number of bytes actually saved in private struct including final NULL
438 \fBkeyGetString()\fP, \fBkeyValue()\fP
442 .SS "const void* keyValue (const Key * key)"
444 Return a pointer to the real internal \fCkey\fP value.
446 This is a much more efficient version of \fBkeyGetString()\fP \fBkeyGetBinary()\fP, 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 \fBkeyGetString()\fP or \fBkeyGetBinary()\fP instead.
447 .SH "String Handling"
449 If \fCkey\fP is string (\fBkeyIsString()\fP), you may cast the returned as a \fC'char *'\fP because you'll get a NULL terminated regular string.
451 \fBkeyValue()\fP returns '' in string mode when there is no value. The reason is
455 keySetString(key,'');
456 keyValue(key); // you would expect '' here
461 .SH "Binary Data Handling"
463 If the data is binary, the size of the value must be determined by \fBkeyGetValueSize()\fP, any strlen() operations are not suitable to determine the size.
465 \fBkeyValue()\fP returns 0 in binary mode when there is no value. The reason is
470 keySetBinary(key, 0, 0);
471 keyValue(key); // you would expect 0 here
473 keySetBinary(key,'', 1);
474 keyValue(key); // you would expect '' (a pointer to '\0') here
476 keySetBinary(key, (void*)&i, 4);
477 (int*)keyValue(key); // you would expect a pointer to (int)23 here
485 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 \fBkeyValue()\fP method to set a new value. Use \fBkeySetString()\fP or \fBkeySetBinary()\fP instead.
490 Binary keys will return a NULL pointer when there is no data in contrast to \fBkeyName()\fP, \fBkeyBaseName()\fP, \fBkeyOwner()\fP and \fBkeyComment()\fP. For string value the behaviour is the same.
497 KDB *handle = kdbOpen();
501 kdbGetByName(handle,ks,'system/sw/my',KDB_O_SORT|KDB_O_RECURSIVE);
504 while(current=ksNext(ks)) {
507 if (keyIsBin(current)) {
508 size=keyGetValueSize(current);
509 printf('Key %s has a value of size %d bytes. Value: <BINARY>\nComment: %s',
512 keyComment(current));
514 size=kdbiStrLen((char *)keyValue(current));
515 printf('Key %s has a value of size %d bytes. Value: %s\nComment: %s',
518 (char *)keyValue(current),
519 keyComment(current));
532 \fIkey\fP the key object to work with
537 a pointer to internal value
539 '' when there is no data and key is not binary
541 0 where there is no data and key is binary
548 \fBkeyGetValueSize()\fP, \fBkeyGetString()\fP, \fBkeyGetBinary()\fP