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 : // clang-format off
10 :
11 : #include <kdb.h>
12 : #include <stdio.h>
13 :
14 0 : static void shortExamples(void)
15 : {
16 : {
17 : //! [Simple]
18 0 : Key *k = keyNew(0);
19 : // work with it
20 0 : keyDel (k);
21 : //! [Simple]
22 :
23 : }{
24 :
25 : //! [Alternative]
26 0 : Key *k =keyNew("", KEY_END); // Has the same effect as above
27 : // work with it
28 0 : keyDel (k);
29 : //! [Alternative]
30 :
31 : }{
32 :
33 : //! [With Name]
34 : // Create and initialize a key with a name and nothing else
35 0 : Key *k=keyNew("user/some/example", KEY_END);
36 : // work with it
37 0 : keyDel (k);
38 : //! [With Name]
39 :
40 : }{
41 :
42 : //! [With Value]
43 : // Create and initialize a key with a name and nothing else
44 0 : Key *k=keyNew("user/tmp/ex0",
45 : KEY_VALUE, "some data", // set a string value
46 : KEY_END); // end of args
47 : //! [With Value]
48 0 : keyDel(k);
49 :
50 : }{
51 :
52 : //! [With Size]
53 : // Create and initialize a key with a name and nothing else
54 0 : Key *k=keyNew("user/tmp/ex1",
55 : KEY_SIZE, 4, // has no effect on strings
56 : KEY_VALUE, "some data", // set a string value
57 : KEY_END); // end of args
58 : //! [With Size]
59 0 : printf ("%s\n", keyString(k));
60 0 : keyDel(k);
61 :
62 : }{
63 :
64 : //! [With Binary]
65 : // Create and initialize a key with a name and nothing else
66 0 : Key *k=keyNew("user/tmp/ex2",
67 : KEY_BINARY,
68 : KEY_SIZE, 4, // now the size is important
69 : KEY_VALUE, "some data", // sets the binary value ("some")
70 : KEY_END); // end of args
71 : //! [With Binary]
72 0 : printf ("%.4s\n", (char*)keyValue(k));
73 0 : keyDel(k);
74 :
75 :
76 : }{
77 :
78 : //! [With Mode]
79 0 : Key *k=keyNew("user/tmp/ex3",
80 : KEY_VALUE, "some data", // with a simple value
81 : KEY_MODE, 0777, // permissions
82 : KEY_END); // end of args
83 : //! [With Mode]
84 0 : keyDel(k);
85 :
86 : }{
87 :
88 : //! [With Meta]
89 0 : Key *k=keyNew("user/tmp/ex3",
90 : KEY_META, "comment", "a comment", // with a comment
91 : KEY_META, "owner", "root", // and an owner
92 : KEY_META, "special", "yes", // and any other metadata
93 : KEY_END); // end of args
94 : //! [With Meta]
95 0 : keyDel(k);
96 :
97 : }{
98 :
99 : //! [With Flags]
100 0 : Key *k=keyNew("user/tmp/ex3",
101 : KEY_FLAGS, KEY_BINARY | KEY_CASCADING_NAME, // flags
102 : KEY_SIZE, 7, // assume binary length 7
103 : KEY_VALUE, "some data", // value that will be truncated in 7 bytes
104 : KEY_END); // end of args
105 : //! [With Flags]
106 0 : printf ("%.7s\n", (char*)keyValue(k));
107 0 : keyDel(k);
108 :
109 : }{
110 :
111 : //! [With Everything]
112 0 : Key *k=keyNew("user/tmp/ex4",
113 : KEY_BINARY, // key type
114 : KEY_SIZE, 7, // assume binary length 7
115 : KEY_VALUE, "some data", // value that will be truncated in 7 bytes
116 : KEY_COMMENT, "value is truncated",
117 : KEY_OWNER, "root", // owner (not uid) is root
118 : KEY_UID, 0, // root uid
119 : KEY_END); // end of args
120 : //! [With Everything]
121 0 : printf ("%.7s\n", (char*)keyValue(k));
122 0 : keyDel(k);
123 :
124 : }{
125 :
126 : //! [Ref in KeySet]
127 0 : Key *k = keyNew("user/proper_name", KEY_END); // ref counter = 0
128 0 : KeySet *ks = ksNew (1, k, KS_END);
129 0 : keyDel(k); // key will not be deleted, because its in the keyset
130 0 : ksDel(ks); // now the key will be deleted
131 : //! [Ref in KeySet]
132 :
133 : }{
134 :
135 : //! [Ref in multiple KeySets]
136 0 : Key *k = keyNew("user/proper_name", KEY_END); // ref counter 0
137 0 : KeySet *ks1 = ksNew(1, k, KS_END); // ref counter of k 1
138 0 : KeySet *ks2 = ksNew(1, k, KS_END); // ref counter of k 2
139 0 : ksDel(ks1); // ref counter of k 1
140 0 : ksDel(ks2); // k is now deleted
141 : //! [Ref in multiple KeySets]
142 :
143 : }{
144 :
145 : //! [Ref]
146 0 : Key *k = keyNew(0); // ref counter = 0
147 0 : keyIncRef(k); // ref counter = 1
148 0 : keyDel(k); // key will not be deleted
149 0 : keyDecRef(k);
150 0 : keyDel(k);
151 : //! [Ref]
152 :
153 : }{
154 :
155 : //! [Multi Ref]
156 0 : Key *k = keyNew(0); // ref counter 0
157 0 : keyIncRef(k); // ref counter of key 1
158 0 : keyDel (k); // has no effect
159 0 : keyIncRef(k); // ref counter of key 2
160 0 : keyDel (k); // has no effect
161 0 : keyDecRef(k); // ref counter of key 1
162 0 : keyDel (k); // has no effect
163 0 : keyDecRef(k); // ref counter is now 0
164 0 : keyDel (k); // k is now deleted
165 : //! [Multi Ref]
166 :
167 : }
168 :
169 0 : }
170 :
171 0 : int main(void)
172 : {
173 0 : shortExamples();
174 : }
|