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 <assert.h>
10 : #include <kdb.h>
11 : #include <stdio.h>
12 :
13 0 : void f (const Key * source)
14 : {
15 0 : Key * dup = keyDup (source);
16 0 : printf ("\tin f\n");
17 :
18 0 : keyDel (dup);
19 0 : }
20 :
21 0 : void g (const Key * source, KeySet * ks)
22 : {
23 0 : Key * dup = keyDup (source);
24 0 : printf ("\tin g\n");
25 :
26 0 : ksAppendKey (ks, dup);
27 0 : }
28 :
29 0 : void h (Key * k)
30 : {
31 0 : Key * c = keyNew ("user/from/h", KEY_END);
32 0 : printf ("\tin h\n");
33 :
34 0 : keyCopy (k, c);
35 0 : keyDel (c);
36 : /* the caller will see the changed key k */
37 0 : }
38 :
39 : // clang-format off
40 :
41 0 : void simpleAppend (void)
42 : {
43 : //! [simple append]
44 0 : KeySet * ks = ksNew (1, KS_END);
45 0 : ksAppendKey (ks, keyNew ("user/my/new/key", KEY_END));
46 0 : ksDel (ks);
47 : // key deleted, too!
48 : //! [simple append]
49 0 : }
50 :
51 :
52 0 : void refAppend (void)
53 : {
54 : //! [ref append]
55 0 : KeySet * ks = ksNew (1, KS_END);
56 0 : Key * k = keyNew ("user/ref/key", KEY_END);
57 0 : keyIncRef (k);
58 0 : ksAppendKey (ks, k);
59 0 : ksDel (ks);
60 : // now we still can work with the key k!
61 0 : keyDecRef (k);
62 0 : keyDel (k);
63 : //! [ref append]
64 0 : }
65 :
66 0 : void dupAppend (void)
67 : {
68 : //! [dup append]
69 0 : KeySet * ks = ksNew (1, KS_END);
70 0 : Key * k = keyNew ("user/ref/key", KEY_END);
71 0 : ksAppendKey (ks, keyDup (k));
72 0 : ksDel (ks);
73 : // now we still can work with the key k!
74 0 : keyDel (k);
75 : //! [dup append]
76 0 : }
77 :
78 0 : int main (void)
79 : {
80 : Key * origKey;
81 0 : KeySet * ks = ksNew (0, KS_END);
82 :
83 0 : Key * key = keyNew ("user/test/name", KEY_VALUE, "myvalue", KEY_END);
84 0 : printf ("Created key %s with value %s\n", keyName (key), keyString (key));
85 :
86 0 : f (key);
87 0 : printf ("Key is unchanged with value %s\n", keyString (key));
88 :
89 0 : g (key, ks);
90 0 : printf ("A duplication was appended in keyset with name %s\n", keyName (ksHead (ks)));
91 :
92 0 : h (key);
93 0 : printf ("Key has changed to name %s with value %s\n", keyName (key), keyString (key));
94 :
95 0 : simpleAppend ();
96 0 : refAppend ();
97 0 : dupAppend ();
98 :
99 : /* key is yet independent */
100 0 : keyDel (key);
101 :
102 0 : ksRewind (ks);
103 0 : origKey = ksNext (ks);
104 0 : key = keyDup (origKey);
105 0 : printf ("A duplication of the key %s with value %s\n", keyName (key), keyString (key));
106 :
107 0 : keyDel (key);
108 0 : ksDel (ks);
109 : return 0;
110 : }
|