Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <kdb.h>
10 : #include <stdlib.h>
11 : #include <string.h>
12 :
13 : Key * copy;
14 :
15 : //! [Basic Usage]
16 0 : void h (Key * k)
17 : {
18 : // receive key c
19 0 : keyCopy (k, copy);
20 : // the caller will see the changed key k
21 0 : }
22 : //! [Basic Usage]
23 :
24 : //! [Clear]
25 0 : void g (Key * k)
26 : {
27 0 : keyCopy (k, 0);
28 : // k is now an empty and fresh key
29 0 : }
30 : //! [Clear]
31 :
32 : //! [Copy Without Value]
33 0 : void j (Key * k)
34 : {
35 0 : size_t size = keyGetValueSize (k);
36 0 : char * value = malloc (size);
37 0 : int bstring = keyIsString (k);
38 :
39 : // receive key c
40 0 : memcpy (value, keyValue (k), size);
41 0 : keyCopy (k, copy);
42 0 : if (bstring)
43 0 : keySetString (k, value);
44 : else
45 0 : keySetBinary (k, value, size);
46 0 : free (value);
47 : // the caller will see the changed key k
48 : // with the name and metadata from copy (except
49 : // metadata "binary", which stayed the same)
50 0 : }
51 : //! [Copy Without Value]
52 :
53 : //! [Individual Copy]
54 0 : void i (Key * k)
55 : {
56 0 : keySetName (k, keyName (copy));
57 0 : keySetString (k, keyString (copy));
58 0 : keyCopyAllMeta (k, copy);
59 : // k is not a copy of copy even if everything was successfully,
60 : // because it still contains metadata from k
61 0 : }
62 : //! [Individual Copy]
63 :
64 0 : int main (void)
65 : {
66 0 : Key * k = keyNew ("user/hello", KEY_VALUE, "my content", KEY_END);
67 :
68 0 : copy = keyNew ("user/copy", KEY_VALUE, "copies content", KEY_END);
69 :
70 0 : h (k);
71 0 : g (k);
72 0 : j (k);
73 0 : i (k);
74 :
75 0 : keyDel (k);
76 0 : keyDel (copy);
77 : }
|