Git init
[pkgs/e/elektra.git] / src / bindings / cpp / kdb.cpp
1 #include <kdb>
2 #include <iostream>
3
4 namespace kdb
5 {
6
7 /**
8  * Constructs a class KDB.
9  */
10 KDB::KDB ()
11 {
12         handle = ckdb::kdbOpen();
13 }
14
15 /**
16  * The destructor closes the database.
17  */
18 KDB::~KDB ()
19 {
20         ckdb::kdbClose(handle);
21 }
22
23 /**
24  * Get all keys below parentKey inside returned.
25  *
26  * @param returned the keyset where the keys will be in
27  * @param parentKey the parentKey of returned
28  * @param options to change the behaviour which keys to fetch
29  */
30 size_t KDB::get (KeySet & returned, const Key & parentKey, option_t options)
31 {
32         ssize_t ret = ckdb::kdbGet (handle, returned.getKeySet(), parentKey.getKey(), options);
33         if (ret == -1) throw KDBException();
34         return ret;
35 }
36
37
38 size_t KDB::set (KeySet & returned, const Key & parentKey, option_t options)
39 {
40         ssize_t ret = ckdb::kdbSet(handle, returned.getKeySet(), parentKey.getKey(), options);
41         if (ret == -1) throw KDBException();
42         return ret;
43 }
44
45
46 size_t KDB::get (KeySet & returned, const std::string &parentName, option_t options)
47 {
48         ssize_t ret = ckdb::kdbGetByName (handle, returned.getKeySet(), parentName.c_str(), options);
49         if (ret == -1) throw KDBException();
50         return ret;
51 }
52
53 size_t KDB::get (KeySet & returned, const char * parentName, option_t options)
54 {
55         ssize_t ret = ckdb::kdbGetByName (handle, returned.getKeySet(), parentName, options);
56         if (ret == -1) throw KDBException();
57         return ret;
58 }
59
60
61 void KDB::getString (const std::string &keyname, std::string value, size_t maxSize)
62 {
63         char *c = new char[maxSize];
64         ckdb::kdbGetString(handle, keyname.c_str(), c, maxSize);
65         value = c;
66         delete (c);
67 }
68
69 void KDB::setString (const std::string &keyname, const std::string &value)
70 {
71         ckdb::kdbSetString(handle, keyname.c_str(), value.c_str());
72 }
73
74 void KDB::remove (const std::string &keyname)
75 {
76         ckdb::kdbRemove(handle, keyname.c_str());
77 }
78
79 /**
80  * Get a single key.
81  *
82  * @param toGet the key to get
83  */
84 void KDB::get (Key & toGet)
85 {
86         int ret = ckdb::kdbGetKey(handle, toGet.getKey());
87         if (ret == -1) throw KDBException();
88 }
89
90 void KDB::set (const Key & toSet)
91 {
92         int ret = ckdb::kdbSetKey(handle, toSet.getKey());
93         if (ret == -1) throw KDBException();
94 }
95
96 } // end of namespace kdb
97