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_KEY_IO_HPP
10 : #define ELEKTRA_KEY_IO_HPP
11 :
12 : #include <key.hpp>
13 :
14 : #include <iostream>
15 :
16 : namespace kdb
17 : {
18 :
19 : /**
20 : * @brief Stream the name of a key
21 : *
22 : * Use setf(std::ios_base::showbase) on the stream if you want to
23 : * also output all metakeys (warning, cannot be parsed back!)
24 : *
25 : * If you also want to stream the value, use the plugin framework.
26 : *
27 : * @param os the stream to write to
28 : * @param k the key which name should be streamed
29 : *
30 : * @return the stream
31 : */
32 382 : inline std::ostream & operator<< (std::ostream & os, kdb::Key const & k)
33 : {
34 1146 : os << k.getName ();
35 764 : if (os.flags () & std::ios_base::showbase)
36 : {
37 6 : kdb::Key d = k.dup ();
38 2 : d.rewindMeta ();
39 2 : kdb::Key meta;
40 28 : while ((meta = d.nextMeta ()))
41 : {
42 16 : os << " " << meta.getName ();
43 : }
44 : }
45 :
46 382 : return os;
47 : }
48 :
49 : /**
50 : * @brief Reads a line with a keys name
51 : *
52 : * @param is the stream to read from
53 : * @param k the key whose name will be set
54 : *
55 : * Use unsetf(std::ios_base::skipws) on the stream if the keyname is
56 : * terminated with an null character and not a newline.
57 : *
58 : * @return the stream
59 : */
60 4 : inline std::istream & operator>> (std::istream & is, kdb::Key & k)
61 : {
62 8 : std::string name;
63 4 : char delim = '\0';
64 8 : if (is.flags () & std::ios_base::skipws)
65 : {
66 4 : delim = '\n';
67 : }
68 4 : getline (is, name, delim);
69 8 : if (is.flags () & std::ios_base::showbase)
70 : {
71 4 : std::stringstream iis (name);
72 2 : iis >> name;
73 2 : std::string n;
74 12 : while ((iis >> n))
75 : {
76 4 : k.setMeta (n, "");
77 : }
78 : }
79 4 : k.setName (name);
80 :
81 8 : return is;
82 : }
83 : } // namespace kdb
84 :
85 : #endif
|