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 : #ifndef ELEKTRA_KEYSET_IO_HPP
10 : #define ELEKTRA_KEYSET_IO_HPP
11 :
12 : #include <iostream>
13 :
14 : #include <keyio.hpp>
15 :
16 : #include <keyset.hpp>
17 :
18 : namespace kdb
19 : {
20 :
21 : /**
22 : * @brief Outputs line per line the keynames
23 : *
24 : * To output values you should use the plugin framework.
25 : *
26 : * @param os the stream to write to
27 : * @param cks the keyset which should be streamed
28 : *
29 : * Use unsetf(std::ios_base::skipws) or use noskipws iomanip on the stream
30 : * if you want a null terminated sequence of key names.
31 : *
32 : * Use setf(std::ios_base::unitbuf) on the stream if you want to
33 : * flush the buffer after each key.
34 : *
35 : * @return the stream
36 : */
37 10 : inline std::ostream & operator<< (std::ostream & os, kdb::KeySet const & cks)
38 : {
39 10 : kdb::KeySet & ks = const_cast<kdb::KeySet &> (cks);
40 10 : cursor_t c = ks.getCursor ();
41 10 : ks.rewind ();
42 10 : kdb::Key k;
43 192 : while ((k = ks.next ()))
44 : {
45 38 : os << k;
46 76 : if (os.flags () & std::ios_base::skipws)
47 : {
48 : os << '\n';
49 : }
50 : else
51 : {
52 : os << '\0';
53 : }
54 :
55 76 : if (os.flags () & std::ios_base::unitbuf)
56 : {
57 : os << std::flush;
58 : }
59 : }
60 10 : ks.setCursor (c);
61 :
62 20 : return os;
63 : }
64 :
65 : /**
66 : * @brief Reads line per line key names and appends those keys to ks.
67 : *
68 : * To input values you need to use the plugin framework.
69 : *
70 : * @param is the stream to read from
71 : * @param ks the keyset to append to
72 : *
73 : * @return the stream
74 : */
75 : inline std::istream & operator>> (std::istream & is, kdb::KeySet & ks)
76 : {
77 : cursor_t c = ks.getCursor ();
78 : while (!is.eof ())
79 : {
80 : kdb::Key k;
81 : is >> k;
82 : ks.append (k);
83 : }
84 : ks.setCursor (c); // jump back to previous cursor
85 :
86 : return is;
87 : }
88 : } // namespace kdb
89 :
90 : #endif
|