Fawkes API Fawkes Development Version
yaml.h
1
2/***************************************************************************
3 * yaml.h - Fawkes configuration stored in one or more YAML files
4 *
5 * Created: Wed Aug 01 15:44:33 2012
6 * Copyright 2006-2012 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#ifndef _CONFIG_YAML_H_
25#define _CONFIG_YAML_H_
26
27#include <config/config.h>
28#include <utils/system/fam.h>
29#include <yaml-cpp/yaml.h>
30
31#include <memory>
32#include <queue>
33#include <string>
34#include <vector>
35
36namespace fawkes {
37
38class Mutex;
39class FamThread;
40class YamlConfigurationNode;
41
43{
44public:
46 YamlConfiguration(const char *sysconfdir, const char *userconfdir = NULL);
47 virtual ~YamlConfiguration();
48
49 virtual void copy(Configuration *copyconf);
50
51 virtual void load(const char *file_path);
52
53 virtual bool exists(const char *path);
54 virtual bool is_float(const char *path);
55 virtual bool is_uint(const char *path);
56 virtual bool is_int(const char *path);
57 virtual bool is_bool(const char *path);
58 virtual bool is_string(const char *path);
59 virtual bool is_list(const char *path);
60
61 virtual bool is_default(const char *path);
62
63 virtual std::string get_type(const char *path);
64 virtual float get_float(const char *path);
65 virtual unsigned int get_uint(const char *path);
66 virtual int get_int(const char *path);
67 virtual bool get_bool(const char *path);
68 virtual std::string get_string(const char *path);
69 virtual std::vector<float> get_floats(const char *path);
70 virtual std::vector<unsigned int> get_uints(const char *path);
71 virtual std::vector<int> get_ints(const char *path);
72 virtual std::vector<bool> get_bools(const char *path);
73 virtual std::vector<std::string> get_strings(const char *path);
74 virtual ValueIterator * get_value(const char *path);
75 virtual std::string get_comment(const char *path);
76 virtual std::string get_default_comment(const char *path);
77
78 virtual void set_float(const char *path, float f);
79 virtual void set_uint(const char *path, unsigned int uint);
80 virtual void set_int(const char *path, int i);
81 virtual void set_bool(const char *path, bool b);
82 virtual void set_string(const char *path, std::string &s);
83 virtual void set_string(const char *path, const char *s);
84 virtual void set_floats(const char *path, std::vector<float> &f);
85 virtual void set_uints(const char *path, std::vector<unsigned int> &uint);
86 virtual void set_ints(const char *path, std::vector<int> &i);
87 virtual void set_bools(const char *path, std::vector<bool> &b);
88 virtual void set_strings(const char *path, std::vector<std::string> &s);
89 virtual void set_strings(const char *path, std::vector<const char *> &s);
90 virtual void set_comment(const char *path, std::string &comment);
91 virtual void set_comment(const char *path, const char *comment);
92
93 virtual void erase(const char *path);
94
95 virtual void set_default_float(const char *path, float f);
96 virtual void set_default_uint(const char *path, unsigned int uint);
97 virtual void set_default_int(const char *path, int i);
98 virtual void set_default_bool(const char *path, bool b);
99 virtual void set_default_string(const char *path, std::string &s);
100 virtual void set_default_string(const char *path, const char *s);
101 virtual void set_default_comment(const char *path, const char *comment);
102 virtual void set_default_comment(const char *path, std::string &comment);
103
104 virtual void erase_default(const char *path);
105
107 ValueIterator *search(const char *path);
108
109 void lock();
110 bool try_lock();
111 void unlock();
112
113 virtual void try_dump();
114
115 virtual void fam_event(const char *filename, unsigned int mask);
116
117public:
119 {
120 public:
122 YamlValueIterator(std::map<std::string, std::shared_ptr<YamlConfigurationNode>> &nodes);
123
124 virtual ~YamlValueIterator()
125 {
126 }
127 virtual bool next();
128 virtual bool valid() const;
129
130 virtual const char *path() const;
131 virtual const char *type() const;
132
133 virtual bool is_float() const;
134 virtual bool is_uint() const;
135 virtual bool is_int() const;
136 virtual bool is_bool() const;
137 virtual bool is_string() const;
138 virtual bool is_list() const;
139 virtual size_t get_list_size() const;
140
141 virtual float get_float() const;
142 virtual unsigned int get_uint() const;
143 virtual int get_int() const;
144 virtual bool get_bool() const;
145 virtual std::string get_string() const;
146 virtual std::vector<float> get_floats() const;
147 virtual std::vector<unsigned int> get_uints() const;
148 virtual std::vector<int> get_ints() const;
149 virtual std::vector<bool> get_bools() const;
150 virtual std::vector<std::string> get_strings() const;
151 virtual std::string get_as_string() const;
152
153 virtual std::string get_comment() const;
154
155 virtual bool is_default() const;
156
157 private:
158 bool first_;
159 std::map<std::string, std::shared_ptr<YamlConfigurationNode>> nodes_;
160 std::map<std::string, std::shared_ptr<YamlConfigurationNode>>::iterator current_;
161 };
162
163private:
164 /// @cond INTERNALS
165 class LoadQueueEntry
166 {
167 public:
168 LoadQueueEntry(std::string fn, bool im, bool id = false)
169 : filename(fn), ignore_missing(im), is_dir(id)
170 {
171 }
172
173 std::string filename;
174 bool ignore_missing;
175 bool is_dir;
176 };
177 /// @endcond
178
179 std::shared_ptr<YamlConfigurationNode> query(const char *path) const;
180 void
181 read_meta_doc(YAML::Node &doc, std::queue<LoadQueueEntry> &load_queue, std::string &host_file);
182 std::shared_ptr<YamlConfigurationNode> read_config_doc(const YAML::Node &doc);
183 std::shared_ptr<YamlConfigurationNode> read_yaml_file(std::string filename,
184 bool ignore_missing,
185 std::queue<LoadQueueEntry> &load_queue,
186 std::string & host_file);
187 void read_yaml_config(std::string filename,
188 std::string & host_file,
189 std::shared_ptr<YamlConfigurationNode> &root,
190 std::shared_ptr<YamlConfigurationNode> &host_root,
191 std::list<std::string> & files,
192 std::list<std::string> & dirs);
193 void write_host_file();
194
195 std::string config_file_;
196 std::string host_file_;
197
198 std::shared_ptr<YamlConfigurationNode> root_;
199 std::shared_ptr<YamlConfigurationNode> host_root_;
200
201 bool write_pending_;
202 Mutex *write_pending_mutex_;
203
204private:
205 Mutex *mutex;
206
207 typedef std::map<std::string, YAML::Node *> DocMap;
208 mutable DocMap documents_;
209
210 char *sysconfdir_;
211 char *userconfdir_;
212
213 FamThread *fam_thread_;
214};
215
216} // end namespace fawkes
217
218#endif
Iterator interface to iterate over config values.
Definition: config.h:75
Interface for configuration handling.
Definition: config.h:68
File Alteration Monitor Listener.
Definition: fam.h:36
Iterator for YAML config trees.
Definition: yaml.h:119
virtual int get_int() const
Get int value.
Definition: yaml.cpp:198
virtual std::string get_as_string() const
Get value as string.
Definition: yaml.cpp:225
virtual bool valid() const
Check if the current element is valid.
Definition: yaml.cpp:90
virtual std::vector< float > get_floats() const
Get list of values from configuration which is of type float.
Definition: yaml.cpp:238
virtual std::vector< int > get_ints() const
Get list of values from configuration which is of type int.
Definition: yaml.cpp:256
virtual bool is_uint() const
Check if current value is a unsigned int.
Definition: yaml.cpp:123
virtual std::vector< bool > get_bools() const
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:265
virtual std::vector< std::string > get_strings() const
Get list of values from configuration which is of type string.
Definition: yaml.cpp:274
virtual bool get_bool() const
Get bool value.
Definition: yaml.cpp:207
virtual bool is_int() const
Check if current value is a int.
Definition: yaml.cpp:132
virtual bool is_default() const
Check if current value was read from the default config.
Definition: yaml.cpp:289
virtual std::string get_comment() const
Get comment of value.
Definition: yaml.cpp:283
virtual const char * path() const
Path of value.
Definition: yaml.cpp:96
virtual bool is_bool() const
Check if current value is a bool.
Definition: yaml.cpp:141
virtual bool next()
Check if there is another element and advance to this if possible.
Definition: yaml.cpp:79
virtual bool is_string() const
Check if current value is a string.
Definition: yaml.cpp:150
virtual bool is_float() const
Check if current value is a float.
Definition: yaml.cpp:114
virtual std::vector< unsigned int > get_uints() const
Get list of values from configuration which is of type unsigned int.
Definition: yaml.cpp:247
virtual const char * type() const
Type of value.
Definition: yaml.cpp:105
virtual float get_float() const
Get float value.
Definition: yaml.cpp:180
virtual unsigned int get_uint() const
Get unsigned int value.
Definition: yaml.cpp:189
virtual bool is_list() const
Check if a value is a list.
Definition: yaml.cpp:159
virtual size_t get_list_size() const
Get number of elements in list value.
Definition: yaml.cpp:168
virtual std::string get_string() const
Get string value.
Definition: yaml.cpp:216
Configuration store using YAML documents.
Definition: yaml.h:43
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
Definition: yaml.cpp:986
virtual void erase(const char *path)
Erase the given value from the configuration.
Definition: yaml.cpp:1050
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
Definition: yaml.cpp:1022
virtual std::string get_type(const char *path)
Get type of value at given path.
Definition: yaml.cpp:729
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:830
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
Definition: yaml.cpp:980
virtual bool exists(const char *path)
Check if a given value exists.
Definition: yaml.cpp:718
virtual std::string get_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:740
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:944
virtual float get_float(const char *path)
Get value from configuration which is of type float.
Definition: yaml.cpp:782
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: yaml.cpp:788
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:995
YamlConfiguration()
Constructor.
Definition: yaml.cpp:303
ValueIterator * search(const char *path)
Iterator with search results.
Definition: yaml.cpp:1160
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
Definition: yaml.cpp:1004
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: yaml.cpp:529
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
Definition: yaml.cpp:1013
virtual bool is_int(const char *path)
Check if a value is of type int.
Definition: yaml.cpp:879
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: yaml.cpp:806
virtual bool is_default(const char *path)
Check if a value was read from the default config.
Definition: yaml.cpp:913
virtual void try_dump()
Try to dump configuration.
Definition: yaml.cpp:1147
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
Definition: yaml.cpp:935
bool try_lock()
Try to lock the config.
Definition: yaml.cpp:1126
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
Definition: yaml.cpp:864
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
Definition: yaml.cpp:1094
virtual bool is_bool(const char *path)
Check if a value is of type bool.
Definition: yaml.cpp:885
virtual bool is_list(const char *path)
Check if a value is a list.
Definition: yaml.cpp:897
void lock()
Lock the config.
Definition: yaml.cpp:1116
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
Definition: yaml.cpp:953
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
Definition: yaml.cpp:1070
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
Definition: yaml.cpp:1088
virtual ~YamlConfiguration()
Destructor.
Definition: yaml.cpp:347
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
Definition: yaml.cpp:818
virtual int get_int(const char *path)
Get value from configuration which is of type int.
Definition: yaml.cpp:794
ValueIterator * iterator()
Iterator for all values.
Definition: yaml.cpp:1152
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
Definition: yaml.cpp:800
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
Definition: yaml.cpp:836
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:907
virtual void load(const char *file_path)
Load configuration.
Definition: yaml.cpp:368
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
Definition: yaml.cpp:1064
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
Definition: yaml.cpp:919
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
Definition: yaml.cpp:1045
void unlock()
Unlock the config.
Definition: yaml.cpp:1135
virtual void copy(Configuration *copyconf)
Copies all values from the given configuration.
Definition: yaml.cpp:712
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
Definition: yaml.cpp:1076
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
Definition: yaml.cpp:812
virtual bool is_float(const char *path)
Check if a value is of type float.
Definition: yaml.cpp:858
virtual bool is_string(const char *path)
Check if a value is of type string.
Definition: yaml.cpp:891
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
Definition: yaml.cpp:962
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
Definition: yaml.cpp:1058
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
Definition: yaml.cpp:1106
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
Definition: yaml.cpp:824
Fawkes library namespace.