1 /*
2 * This file is part of GtkEveMon.
3 *
4 * GtkEveMon is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * You should have received a copy of the GNU General Public License
10 * along with GtkEveMon. If not, see <http://www.gnu.org/licenses/>.
11 */
12
13 #ifndef CONF_HEADER
14 #define CONF_HEADER
15
16 #include <ostream>
17 #include <string>
18 #include <map>
19
20 #include "ref_ptr.h"
21
22 class ConfSection;
23 class ConfValue;
24 typedef ref_ptr<ConfSection> ConfSectionPtr;
25 typedef ref_ptr<ConfValue> ConfValuePtr;
26 typedef std::map<std::string, ConfSectionPtr> conf_sections_t;
27 typedef std::map<std::string, ConfValuePtr> conf_values_t;
28
29 /* ---------------------------------------------------------------- */
30
31 class ConfValue
32 {
33 private:
34 std::string value;
35
36 protected:
ConfValue(void)37 ConfValue (void) {}
38
39 public:
40 static ConfValuePtr create (void);
41 static ConfValuePtr create (std::string const& _value);
42 static ConfValuePtr create (int value);
43
44 std::string& get_string (void);
45 double get_double (void);
46 int get_int (void);
47 bool get_bool (void);
48
49 std::string& operator* (void);
50
51 void set (std::string const& value);
52 void set (int value);
53 void set (double value);
54 void set (bool value);
55 };
56
57 /* ---------------------------------------------------------------- */
58
59 class ConfSection
60 {
61 private:
62 conf_sections_t sections;
63 conf_values_t values;
64 bool do_stream;
65
66 protected:
ConfSection(void)67 ConfSection (void) { this->do_stream = true; }
68
69 public:
70 static ConfSectionPtr create (void);
71
72 /* Adding and getting. */
73 void add (std::string const& key, ConfSectionPtr section);
74 void add (std::string const& key, ConfValuePtr value);
75 ConfSectionPtr get_section (std::string const& key);
76 ConfValuePtr get_value (std::string const& key);
77 void remove_section (std::string const& key);
78 void remove_value (std::string const& key);
79
80 /* Iteration. */
81 conf_values_t::iterator values_begin (void);
82 conf_values_t::iterator values_end (void);
83 conf_sections_t::iterator sections_begin (void);
84 conf_sections_t::iterator sections_end (void);
85
86 conf_values_t::iterator find_value (std::string const& key);
87 conf_sections_t::iterator find_section (std::string const& key);
88
89 /* Clearing. */
90 void clear_values (void);
91 void clear_sections (void);
92
93 /* Output to stream (file, stdout, etc). */
94 void set_do_stream (bool value = true);
95 void to_stream (std::ostream& outstr, std::string prefix = "");
96 };
97
98 /* ---------------------------------------------------------------- */
99
100 class Conf
101 {
102 private:
103 ConfSectionPtr root;
104
105 void clip_string (std::string& str);
106
107 public:
108 Conf (void);
109
110 ConfValuePtr get_value (std::string const& key);
111 ConfSectionPtr get_section (std::string const& key);
112 ConfSectionPtr get_or_create_section (std::string const& key);
113
114 void clear (void);
115 void add_from_file (std::istream& instr);
116 void add_from_file (std::string const& filename);
117 void add_from_string (std::string const& conf_string);
118 void to_file (std::string const& filename);
119 void to_stream (std::ostream& outstr);
120 };
121
122 /* ---------------------------------------------------------------- */
123
124 class ConfHelpers
125 {
126 public:
127 static double get_double_from_string (std::string const& str);
128 static int get_int_from_string (std::string const& str);
129 static std::string get_string_from_int (int val);
130 static std::string get_string_from_double (double val);
131 };
132
133 /* ---------------------------------------------------------------- */
134
135 inline std::string&
get_string(void)136 ConfValue::get_string (void)
137 {
138 return this->value;
139 }
140
141 inline std::string&
142 ConfValue::operator* (void)
143 {
144 return this->value;
145 }
146
147 inline void
set(std::string const & _value)148 ConfValue::set (std::string const& _value)
149 {
150 this->value = _value;
151 }
152
153 inline conf_values_t::iterator
values_begin(void)154 ConfSection::values_begin (void)
155 {
156 return this->values.begin();
157 }
158
159 inline conf_values_t::iterator
values_end(void)160 ConfSection::values_end (void)
161 {
162 return this->values.end();
163 }
164
165 inline conf_sections_t::iterator
sections_begin(void)166 ConfSection::sections_begin (void)
167 {
168 return this->sections.begin();
169 }
170
171 inline conf_sections_t::iterator
sections_end(void)172 ConfSection::sections_end (void)
173 {
174 return this->sections.end();
175 }
176
177 inline conf_values_t::iterator
find_value(std::string const & key)178 ConfSection::find_value (std::string const& key)
179 {
180 return this->values.find(key);
181 }
182
183 inline conf_sections_t::iterator
find_section(std::string const & key)184 ConfSection::find_section (std::string const& key)
185 {
186 return this->sections.find(key);
187 }
188
189 inline void
clear_values(void)190 ConfSection::clear_values (void)
191 {
192 this->values.clear();
193 }
194
195 inline void
clear_sections(void)196 ConfSection::clear_sections (void)
197 {
198 this->sections.clear();
199 }
200
201 #endif /* CONF_HEADER */
202