1 \section{Key :: Value Manipulation Methods}
2 \label{group__keyvalue}\index{Key :: Value Manipulation Methods@{Key :: Value Manipulation Methods}}
3 Methods to do various operations on Key values.
4 \subsection*{Functions}
7 const void $\ast$ {\bf keyValue} (const Key $\ast$key)
9 ssize\_\-t {\bf keyGetValueSize} (const Key $\ast$key)
11 ssize\_\-t {\bf keyGetString} (const Key $\ast$key, char $\ast$returnedString, size\_\-t maxSize)
13 ssize\_\-t {\bf keySetString} (Key $\ast$key, const char $\ast$newStringValue)
15 ssize\_\-t {\bf keyGetBinary} (const Key $\ast$key, void $\ast$returnedBinary, size\_\-t maxSize)
17 ssize\_\-t {\bf keySetBinary} (Key $\ast$key, const void $\ast$newBinary, size\_\-t dataSize)
19 const char $\ast$ {\bf keyComment} (const Key $\ast$key)
21 ssize\_\-t {\bf keyGetCommentSize} (const Key $\ast$key)
23 ssize\_\-t {\bf keyGetComment} (const Key $\ast$key, char $\ast$returnedComment, size\_\-t maxSize)
25 ssize\_\-t {\bf keySetComment} (Key $\ast$key, const char $\ast$newComment)
29 \subsection{Detailed Description}
30 Methods to do various operations on Key values.
32 A key can contain a value in different format. The most likely situation is, that the value is interpreted as text. Use \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} for that. You can save any Unicode Symbols and Elektra will take care that you get the same back, independent of your current environment.
34 In some situations this idea fails. When you need exactly the same value back without any interpretation of the characters, there is \doxyref{keySetBinary()}{p.}{group__keyvalue_ga50a5358fd328d373a45f395fa1b99e7}. 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 \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778}. To do so, you might use strtod() strtol() and then atol() or atof() to convert back.
38 \begin{Code}\begin{verbatim}#include <kdb.h>
44 \subsection{Function Documentation}
45 \index{keyvalue@{keyvalue}!keyComment@{keyComment}}
46 \index{keyComment@{keyComment}!keyvalue@{keyvalue}}
47 \subsubsection[keyComment]{\setlength{\rightskip}{0pt plus 5cm}const char$\ast$ keyComment (const Key $\ast$ {\em key})}\label{group__keyvalue_gc89fd319783b3457db45b4c09e55274a}
50 Return a pointer to the real internal {\tt key} comment.
52 This is a much more efficient version of \doxyref{keyGetComment()}{p.}{group__keyvalue_gfb89735689929ff717cc9f2d0d0b46a2} 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.
54 \doxyref{keyComment()}{p.}{group__keyvalue_gc89fd319783b3457db45b4c09e55274a} returns \char`\"{}\char`\"{} when there is no keyComment. The reason is
56 \begin{Code}\begin{verbatim}key=keyNew(0);
57 keySetComment(key,"");
58 keyComment(key); // you would expect "" here
65 See \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} for more information on comments.
68 \item[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 \doxyref{keyComment()}{p.}{group__keyvalue_gc89fd319783b3457db45b4c09e55274a} method to set a new value. Use \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} instead.\end{Desc}
72 \item[{\em key}]the key object to work with \end{description}
75 \item[Returns:]a pointer to the internal managed comment
77 \char`\"{}\char`\"{} when there is no comment
79 0 on NULL pointer \end{Desc}
81 \item[See also:]\doxyref{keyGetCommentSize()}{p.}{group__keyvalue_g0dd737fadc16d4cf16720d17f066a9d3} for size and \doxyref{keyGetComment()}{p.}{group__keyvalue_gfb89735689929ff717cc9f2d0d0b46a2} as alternative \end{Desc}
82 \index{keyvalue@{keyvalue}!keyGetBinary@{keyGetBinary}}
83 \index{keyGetBinary@{keyGetBinary}!keyvalue@{keyvalue}}
84 \subsubsection[keyGetBinary]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keyGetBinary (const Key $\ast$ {\em key}, \/ void $\ast$ {\em returnedBinary}, \/ size\_\-t {\em maxSize})}\label{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84}
87 Get the value of a key as a binary.
89 If the type is not binary -1 will be returned.
91 When the binary data is empty (this is not the same as \char`\"{}\char`\"{}!) 0 will be returned and the returnedBinary will not be changed.
93 For string values see \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} and \doxyref{keyIsString()}{p.}{group__keytest_gea7670778abd07fee0fe8ac12a149190}.
95 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.
100 \begin{Code}\begin{verbatim}Key *key = keyNew ("user/keyname", KEY_TYPE, KEY_TYPE_BINARY, KEY_END);
103 if (keyGetBinary(key,buffer,sizeof(buffer)) == -1)
114 \item[{\em key}]the object to gather the value from \item[{\em returnedBinary}]pre-allocated memory to store a copy of the key value \item[{\em maxSize}]number of bytes of pre-allocated memory in {\tt returnedBinary} \end{description}
117 \item[Returns:]the number of bytes actually copied to {\tt returnedBinary}
119 0 if the binary is empty
123 -1 when maxSize is 0, too small to hold the value or larger than SSIZE\_\-MAX
125 -1 on typing error when the key is not binary \end{Desc}
127 \item[See also:]\doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af}, \doxyref{keyGetValueSize()}{p.}{group__keyvalue_ge326672fffb7474abfe9baf53b73217e}, \doxyref{keySetBinary()}{p.}{group__keyvalue_ga50a5358fd328d373a45f395fa1b99e7}
129 \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} and \doxyref{keySetString()}{p.}{group__keyvalue_g622bde1eb0e0c4994728331326340ef2} as preferred alternative to binary
131 \doxyref{keyIsBinary()}{p.}{group__keytest_g9526b371087564e43e3dff8ad0dac949} to see how to check for binary type \end{Desc}
132 \index{keyvalue@{keyvalue}!keyGetComment@{keyGetComment}}
133 \index{keyGetComment@{keyGetComment}!keyvalue@{keyvalue}}
134 \subsubsection[keyGetComment]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keyGetComment (const Key $\ast$ {\em key}, \/ char $\ast$ {\em returnedComment}, \/ size\_\-t {\em maxSize})}\label{group__keyvalue_gfb89735689929ff717cc9f2d0d0b46a2}
137 Get the key comment.\subsection{Comments}\label{group__keyvalue_comment}
138 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.
140 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.
145 \item[{\em key}]the key object to work with \item[{\em returnedComment}]pre-allocated memory to copy the comments to \item[{\em maxSize}]number of bytes that will fit returnedComment \end{description}
148 \item[Returns:]the number of bytes actually copied to {\tt returnedString}, including final NULL
150 1 if the string is empty
154 -1 if maxSize is 0, not enough to store the comment or when larger then SSIZE\_\-MAX \end{Desc}
156 \item[See also:]\doxyref{keyGetCommentSize()}{p.}{group__keyvalue_g0dd737fadc16d4cf16720d17f066a9d3}, \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} \end{Desc}
157 \index{keyvalue@{keyvalue}!keyGetCommentSize@{keyGetCommentSize}}
158 \index{keyGetCommentSize@{keyGetCommentSize}!keyvalue@{keyvalue}}
159 \subsubsection[keyGetCommentSize]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keyGetCommentSize (const Key $\ast$ {\em key})}\label{group__keyvalue_g0dd737fadc16d4cf16720d17f066a9d3}
162 Calculates number of bytes needed to store a key comment, including final NULL.
164 Use this method to know to size for allocated memory to retrieve a key comment.
166 See \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} for more information on comments.
168 For an empty key name you need one byte to store the ending NULL. For that reason 1 is returned.
172 \begin{Code}\begin{verbatim}char *buffer;
173 buffer = malloc (keyGetCommentSize (key));
174 // use this buffer to store the comment
175 // pass keyGetCommentSize (key) for maxSize
184 \item[{\em key}]the key object to work with \end{description}
187 \item[Returns:]number of bytes needed
189 1 if there is no comment
191 -1 on NULL pointer \end{Desc}
193 \item[See also:]\doxyref{keyGetComment()}{p.}{group__keyvalue_gfb89735689929ff717cc9f2d0d0b46a2}, \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} \end{Desc}
194 \index{keyvalue@{keyvalue}!keyGetString@{keyGetString}}
195 \index{keyGetString@{keyGetString}!keyvalue@{keyvalue}}
196 \subsubsection[keyGetString]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keyGetString (const Key $\ast$ {\em key}, \/ char $\ast$ {\em returnedString}, \/ size\_\-t {\em maxSize})}\label{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778}
199 Get the value of a key as a string.
201 When there is no value inside the string, 1 will be returned and the returnedString will be empty \char`\"{}\char`\"{} to avoid programming errors that old strings are shown to the user.
203 For binary values see \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84} and \doxyref{keyIsBinary()}{p.}{group__keytest_g9526b371087564e43e3dff8ad0dac949}.
208 \begin{Code}\begin{verbatim}Key *key = keyNew ("user/keyname", KEY_END);
211 if (keyGetString(key,buffer,sizeof(buffer)) == -1)
215 printf ("buffer: %s\n", buffer);
224 \item[{\em key}]the object to gather the value from \item[{\em returnedString}]pre-allocated memory to store a copy of the key value \item[{\em maxSize}]number of bytes of allocated memory in {\tt returnedString} \end{description}
227 \item[Returns:]the number of bytes actually copied to {\tt returnedString}, including final NULL
229 1 if the string is empty
235 maxSize is 0, too small for string or is larger than SSIZE\_\-MAX \end{Desc}
237 \item[See also:]\doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af}, \doxyref{keyGetValueSize()}{p.}{group__keyvalue_ge326672fffb7474abfe9baf53b73217e}, \doxyref{keySetString()}{p.}{group__keyvalue_g622bde1eb0e0c4994728331326340ef2}
239 \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84} for working with binary data \end{Desc}
240 \index{keyvalue@{keyvalue}!keyGetValueSize@{keyGetValueSize}}
241 \index{keyGetValueSize@{keyGetValueSize}!keyvalue@{keyvalue}}
242 \subsubsection[keyGetValueSize]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keyGetValueSize (const Key $\ast$ {\em key})}\label{group__keyvalue_ge326672fffb7474abfe9baf53b73217e}
245 Returns the number of bytes needed to store the key value, including the NULL terminator.
247 It returns the correct size, independent of the Key Type. If it is a binary there might be '$\backslash$0' values in it.
249 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.
251 A binary key has no '$\backslash$0' termination. String types have it, so to there length will be added 1 to have enough space to store it.
253 This method can be used with malloc() before \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} or \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84} is called.
257 \begin{Code}\begin{verbatim}char *buffer;
258 buffer = malloc (keyGetValueSize (key));
259 // use this buffer to store the value (binary or string)
260 // pass keyGetValueSize (key) for maxSize
269 \item[{\em key}]the key object to work with \end{description}
272 \item[Returns:]the number of bytes needed to store the key value
274 1 when there is no data and type is not binary
276 0 when there is no data and type is binary
278 -1 on null pointer \end{Desc}
280 \item[See also:]\doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778}, \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84}, \doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af} \end{Desc}
281 \index{keyvalue@{keyvalue}!keySetBinary@{keySetBinary}}
282 \index{keySetBinary@{keySetBinary}!keyvalue@{keyvalue}}
283 \subsubsection[keySetBinary]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keySetBinary (Key $\ast$ {\em key}, \/ const void $\ast$ {\em newBinary}, \/ size\_\-t {\em dataSize})}\label{group__keyvalue_ga50a5358fd328d373a45f395fa1b99e7}
286 Set the value of a key as a binary.
288 A private copy of {\tt newBinary} will allocated and saved inside {\tt key}, so the parameter can be deallocated after the call.
290 The {\tt filesys} backend, when used through a \doxyref{kdbSetKey()}{p.}{group__kdbhighlevel_g23b2f5fead4cddeb5542051a197ddc20}, will make the value be kdbbEncoded into a human readable hex-digit text format.
292 Consider using a string key instead.
294 When newBinary is a NULL pointer the binary will be freed and 0 will be returned.
297 \item[Note:]When the type of the key is already a binary type it won't be changed.\end{Desc}
301 \item[{\em key}]the object on which to set the value \item[{\em newBinary}]is a pointer to any binary data or NULL to free the previous set data \item[{\em dataSize}]number of bytes to copy from {\tt newBinary} \end{description}
304 \item[Returns:]the number of bytes actually copied to internal struct storage
306 0 when the internal binary was freed
310 -1 when dataSize is 0 (but newBinary not NULL) or larger than SSIZE\_\-MAX \end{Desc}
312 \item[See also:]\doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84}
314 \doxyref{keyIsBinary()}{p.}{group__keytest_g9526b371087564e43e3dff8ad0dac949} to check if the type is binary
316 \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} and \doxyref{keySetString()}{p.}{group__keyvalue_g622bde1eb0e0c4994728331326340ef2} as preferred alternative to binary \end{Desc}
317 \index{keyvalue@{keyvalue}!keySetComment@{keySetComment}}
318 \index{keySetComment@{keySetComment}!keyvalue@{keyvalue}}
319 \subsubsection[keySetComment]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keySetComment (Key $\ast$ {\em key}, \/ const char $\ast$ {\em newComment})}\label{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1}
322 Set a comment for a key.
324 A key comment is like a configuration file comment. See \doxyref{keySetComment()}{p.}{group__keyvalue_g8863a877a84fa46e6017fe72e49b89c1} for more information.
329 \item[{\em key}]the key object to work with \item[{\em newComment}]the comment, that can be freed after this call. \end{description}
332 \item[Returns:]the number of bytes actually saved including final NULL
334 1 when the comment was freed
336 -1 on NULL pointer or memory problems \end{Desc}
338 \item[See also:]\doxyref{keyGetComment()}{p.}{group__keyvalue_gfb89735689929ff717cc9f2d0d0b46a2} \end{Desc}
339 \index{keyvalue@{keyvalue}!keySetString@{keySetString}}
340 \index{keySetString@{keySetString}!keyvalue@{keyvalue}}
341 \subsubsection[keySetString]{\setlength{\rightskip}{0pt plus 5cm}ssize\_\-t keySetString (Key $\ast$ {\em key}, \/ const char $\ast$ {\em newStringValue})}\label{group__keyvalue_g622bde1eb0e0c4994728331326340ef2}
344 Set the value for {\tt key} as {\tt newStringValue}.
346 The function will allocate and save a private copy of {\tt newStringValue}, so the parameter can be freed after the call.
348 String values will be saved in backend storage, when \doxyref{kdbSetKey()}{p.}{group__kdbhighlevel_g23b2f5fead4cddeb5542051a197ddc20} will be called, in UTF-8 universal encoding, regardless of the program's current encoding, when compiled with --enable-iconv.
350 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.
355 \item[{\em key}]the key to set the string value \item[{\em newStringValue}]NULL-terminated text string to be set as {\tt key's} value \end{description}
358 \item[Returns:]the number of bytes actually saved in private struct including final NULL
360 -1 on NULL pointer \end{Desc}
362 \item[See also:]\doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778}, \doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af} \end{Desc}
363 \index{keyvalue@{keyvalue}!keyValue@{keyValue}}
364 \index{keyValue@{keyValue}!keyvalue@{keyvalue}}
365 \subsubsection[keyValue]{\setlength{\rightskip}{0pt plus 5cm}const void$\ast$ keyValue (const Key $\ast$ {\em key})}\label{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af}
368 Return a pointer to the real internal {\tt key} value.
370 This is a much more efficient version of \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84}, 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 \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778} or \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84} instead.\subsection{String Handling}\label{group__keyvalue_string}
371 If {\tt key} is string (\doxyref{keyIsString()}{p.}{group__keytest_gea7670778abd07fee0fe8ac12a149190}), you may cast the returned as a {\tt \char`\"{}char $\ast$\char`\"{}} because you'll get a NULL terminated regular string.
373 \doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af} returns \char`\"{}\char`\"{} in string mode when there is no value. The reason is
375 \begin{Code}\begin{verbatim}key=keyNew(0);
376 keySetString(key,"");
377 keyValue(key); // you would expect "" here
382 \subsection{Binary Data Handling}\label{group__keyvalue_binary}
383 If the data is binary, the size of the value must be determined by \doxyref{keyGetValueSize()}{p.}{group__keyvalue_ge326672fffb7474abfe9baf53b73217e}, any strlen() operations are not suitable to determine the size.
385 \doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af} returns 0 in binary mode when there is no value. The reason is
387 \begin{Code}\begin{verbatim}int i=23;
389 keySetBinary(key, 0, 0);
390 keyValue(key); // you would expect 0 here
392 keySetBinary(key,"", 1);
393 keyValue(key); // you would expect "" (a pointer to '\0') here
395 keySetBinary(key, (void*)&i, 4);
396 (int*)keyValue(key); // you would expect a pointer to (int)23 here
404 \item[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 \doxyref{keyValue()}{p.}{group__keyvalue_g6f29609c5da53c6dc26a98678d5752af} method to set a new value. Use \doxyref{keySetString()}{p.}{group__keyvalue_g622bde1eb0e0c4994728331326340ef2} or \doxyref{keySetBinary()}{p.}{group__keyvalue_ga50a5358fd328d373a45f395fa1b99e7} instead.\end{Desc}
406 \item[Warning:]Binary keys will return a NULL pointer when there is no data in contrast to \doxyref{keyName()}{p.}{group__keyname_g8e805c726a60da921d3736cda7813513}, \doxyref{keyBaseName()}{p.}{group__keyname_gaff35e7ca8af5560c47e662ceb9465f5}, \doxyref{keyOwner()}{p.}{group__keyname_gf6485fb8599714b6bbd830cf915ffea5} and \doxyref{keyComment()}{p.}{group__keyvalue_gc89fd319783b3457db45b4c09e55274a}. For string value the behaviour is the same.\end{Desc}
410 \begin{Code}\begin{verbatim}KDB *handle = kdbOpen();
414 kdbGetByName(handle,ks,"system/sw/my",KDB_O_SORT|KDB_O_RECURSIVE);
417 while(current=ksNext(ks)) {
420 if (keyIsBin(current)) {
421 size=keyGetValueSize(current);
422 printf("Key %s has a value of size %d bytes. Value: <BINARY>\nComment: %s",
425 keyComment(current));
427 size=kdbiStrLen((char *)keyValue(current));
428 printf("Key %s has a value of size %d bytes. Value: %s\nComment: %s",
431 (char *)keyValue(current),
432 keyComment(current));
445 \item[{\em key}]the key object to work with \end{description}
448 \item[Returns:]a pointer to internal value
450 \char`\"{}\char`\"{} when there is no data and key is not binary
452 0 where there is no data and key is binary
454 0 on NULL pointer \end{Desc}
456 \item[See also:]\doxyref{keyGetValueSize()}{p.}{group__keyvalue_ge326672fffb7474abfe9baf53b73217e}, \doxyref{keyGetString()}{p.}{group__keyvalue_g41b9fac5ccddafe407fc0ae1e2eb8778}, \doxyref{keyGetBinary()}{p.}{group__keyvalue_g4c0d8a4a11174197699c231e0b5c3c84} \end{Desc}