Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief example for set
5 : *
6 : * gcc -Wall -g elektra_set.c -o elektra-set `pkg-config --cflags --libs elektra`
7 : * Thanks to Kai-Uwe Behrmann <ku.b@gmx.de> for that example
8 : *
9 : * to clean up after executing this example you have to use:
10 : *
11 : * kdb rm user/sw/MyApp/Tests/TestKey1
12 : *
13 : *
14 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
15 : */
16 :
17 : #include <kdb.h>
18 : #include <stdio.h>
19 :
20 0 : void print_warnings (Key * err)
21 : {
22 0 : const Key * meta = 0;
23 0 : keyRewindMeta (err);
24 0 : while ((meta = keyNextMeta (err)) != 0)
25 : {
26 0 : printf ("%s:\t%s\n", keyName (meta), keyString (meta));
27 : }
28 0 : }
29 :
30 : /** After writing the key this function rereads the key and print it*/
31 0 : void check_key (void)
32 : {
33 0 : Key * error_key = keyNew (0);
34 0 : KDB * kdb_handle = kdbOpen (error_key);
35 0 : Key * top = keyNew (0);
36 0 : keySetName (top, "user/sw/MyApp"); // == 14
37 0 : KeySet * ks = ksNew (0, KS_END);
38 0 : kdbGet (kdb_handle, ks, top);
39 0 : Key * key = keyNew (0);
40 0 : keySetName (key, "user/sw/MyApp/Tests/TestKey1"); // == 14
41 0 : Key * result = ksLookup (ks, key, KDB_O_NONE);
42 0 : const char * key_name = keyName (result);
43 0 : const char * key_value = keyString (result);
44 0 : const char * key_comment = keyString (keyGetMeta (result, "comment"));
45 0 : printf ("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);
46 0 : ksDel (ks);
47 0 : keyDel (key);
48 0 : keyDel (top);
49 0 : kdbClose (kdb_handle, error_key);
50 0 : keyDel (error_key);
51 0 : }
52 :
53 : // typical usage of Elektra
54 0 : int main (void)
55 : {
56 0 : Key * error_key = keyNew (0);
57 0 : KDB * kdb_handle = kdbOpen (error_key);
58 0 : Key * top = keyNew (0);
59 0 : keySetName (top, "user/sw/MyApp");
60 :
61 0 : KeySet * ks = ksNew (0, KS_END);
62 0 : kdbGet (kdb_handle, ks, top);
63 :
64 0 : Key * key = keyNew (0);
65 0 : keySetName (key, "user/sw/MyApp/Tests/TestKey1"); // == 31
66 0 : keySetString (key, "NULLTestValue"); // == 14
67 0 : keySetMeta (key, "comment", "NULLTestComment"); // == 16
68 0 : ksAppendKey (ks, key); // == 1
69 0 : keyNeedSync (key);
70 0 : kdbSet (kdb_handle, ks, top); // == -1
71 0 : print_warnings (top);
72 0 : keyDel (top);
73 0 : ksDel (ks);
74 0 : kdbClose (kdb_handle, error_key);
75 0 : keyDel (error_key);
76 :
77 0 : check_key ();
78 :
79 : return 0;
80 : }
|