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 
30 #include <yaml-cpp/yaml.h>
31 #ifdef USE_REGEX_CPP
32 // we do not use it atm because it does not work as epxect atm,
33 // cf. https://bugzilla.redhat.com/show_bug.cgi?id=718711
34 # include <regex>
35 #else
36 # include <regex.h>
37 #endif
38 #include <memory>
39 #include <string>
40 #include <vector>
41 #include <queue>
42 
43 namespace fawkes {
44 
45 class Mutex;
46 class FamThread;
47 class YamlConfigurationNode;
48 
50 : public Configuration,
51  public FamListener
52 {
53  public:
55  YamlConfiguration(const char *sysconfdir, const char *userconfdir = NULL);
56  virtual ~YamlConfiguration();
57 
58  virtual void copy(Configuration *copyconf);
59 
60  virtual void load(const char *file_path);
61 
62  virtual bool exists(const char *path);
63  virtual bool is_float(const char *path);
64  virtual bool is_uint(const char *path);
65  virtual bool is_int(const char *path);
66  virtual bool is_bool(const char *path);
67  virtual bool is_string(const char *path);
68  virtual bool is_list(const char *path);
69 
70  virtual bool is_default(const char *path);
71 
72  virtual std::string get_type(const char *path);
73  virtual float get_float(const char *path);
74  virtual unsigned int get_uint(const char *path);
75  virtual int get_int(const char *path);
76  virtual bool get_bool(const char *path);
77  virtual std::string get_string(const char *path);
78  virtual std::vector<float> get_floats(const char *path);
79  virtual std::vector<unsigned int> get_uints(const char *path);
80  virtual std::vector<int> get_ints(const char *path);
81  virtual std::vector<bool> get_bools(const char *path);
82  virtual std::vector<std::string> get_strings(const char *path);
83  virtual ValueIterator * get_value(const char *path);
84  virtual std::string get_comment(const char *path);
85  virtual std::string get_default_comment(const char *path);
86 
87  virtual void set_float(const char *path, float f);
88  virtual void set_uint(const char *path, unsigned int uint);
89  virtual void set_int(const char *path, int i);
90  virtual void set_bool(const char *path, bool b);
91  virtual void set_string(const char *path, std::string &s);
92  virtual void set_string(const char *path, const char *s);
93  virtual void set_floats(const char *path, std::vector<float> &f);
94  virtual void set_uints(const char *path, std::vector<unsigned int> &uint);
95  virtual void set_ints(const char *path, std::vector<int> &i);
96  virtual void set_bools(const char *path, std::vector<bool> &b);
97  virtual void set_strings(const char *path, std::vector<std::string> &s);
98  virtual void set_strings(const char *path, std::vector<const char *> &s);
99  virtual void set_comment(const char *path, std::string &comment);
100  virtual void set_comment(const char *path, const char *comment);
101 
102  virtual void erase(const char *path);
103 
104  virtual void set_default_float(const char *path, float f);
105  virtual void set_default_uint(const char *path, unsigned int uint);
106  virtual void set_default_int(const char *path, int i);
107  virtual void set_default_bool(const char *path, bool b);
108  virtual void set_default_string(const char *path, std::string &s);
109  virtual void set_default_string(const char *path, const char *s);
110  virtual void set_default_comment(const char *path, const char *comment);
111  virtual void set_default_comment(const char *path, std::string &comment);
112 
113  virtual void erase_default(const char *path);
114 
116  ValueIterator * search(const char *path);
117 
118  void lock();
119  bool try_lock();
120  void unlock();
121 
122  virtual void try_dump();
123 
124  virtual void fam_event(const char *filename, unsigned int mask);
125 
126  public:
128  {
129  public:
131  YamlValueIterator(std::map<std::string, YamlConfigurationNode *> &nodes);
132 
133  virtual ~YamlValueIterator() {}
134  virtual bool next();
135  virtual bool valid() const;
136 
137  virtual const char * path() const;
138  virtual const char * type() const;
139 
140  virtual bool is_float() const;
141  virtual bool is_uint() const;
142  virtual bool is_int() const;
143  virtual bool is_bool() const;
144  virtual bool is_string() const;
145  virtual bool is_list() const;
146  virtual size_t get_list_size() const;
147 
148  virtual float get_float() const;
149  virtual unsigned int get_uint() const;
150  virtual int get_int() const;
151  virtual bool get_bool() const;
152  virtual std::string get_string() const;
153  virtual std::vector<float> get_floats() const;
154  virtual std::vector<unsigned int> get_uints() const;
155  virtual std::vector<int> get_ints() const;
156  virtual std::vector<bool> get_bools() const;
157  virtual std::vector<std::string> get_strings() const;
158  virtual std::string get_as_string() const;
159 
160  virtual std::string get_comment() const;
161 
162  virtual bool is_default() const;
163 
164  private:
165  bool first_;
166  std::map<std::string, YamlConfigurationNode *> nodes_;
167  std::map<std::string, YamlConfigurationNode *>::iterator current_;
168  };
169 
170  private:
171  /// @cond INTERNALS
172  class LoadQueueEntry {
173  public:
174  LoadQueueEntry(std::string fn, bool im, bool id = false)
175  : filename(fn), ignore_missing(im), is_dir(id) {}
176 
177  std::string filename;
178  bool ignore_missing;
179  bool is_dir;
180  };
181  /// @endcond
182 
183  YamlConfigurationNode * query(const char *path) const;
184  void read_meta_doc(YAML::Node &doc, std::queue<LoadQueueEntry> &load_queue,
185  std::string &host_file);
186  void read_config_doc(const YAML::Node &doc, YamlConfigurationNode *&node, std::string path = "");
187  YamlConfigurationNode * read_yaml_file(std::string filename, bool ignore_missing,
188  std::queue<LoadQueueEntry> &load_queue, std::string &host_file);
189  void read_yaml_config(std::string filename, std::string &host_file,
190  YamlConfigurationNode *& root, YamlConfigurationNode *& host_root,
191  std::list<std::string> &files, std::list<std::string> &dirs);
192  void write_host_file();
193 
194  std::string config_file_;
195  std::string host_file_;
196 
197  YamlConfigurationNode *root_;
198  YamlConfigurationNode *host_root_;
199 
200  bool write_pending_;
201  Mutex *write_pending_mutex_;
202 
203  private:
204  Mutex *mutex;
205 
206 #ifdef USE_REGEX_CPP
207  std::regex __yaml_regex;
208  std::regex __url_regex;
209  std::regex __frame_regex;
210 #else
211  regex_t __yaml_regex;
212  regex_t __url_regex;
213  regex_t __frame_regex;
214 #endif
215 
216  typedef std::map<std::string, YAML::Node *> DocMap;
217  mutable DocMap __documents;
218 
219  char *__sysconfdir;
220  char *__userconfdir;
221 
222  FamThread *fam_thread_;
223 };
224 
225 
226 } // end namespace fawkes
227 
228 #endif
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
Definition: yaml.cpp:1180
virtual std::vector< int > get_ints() const
Get list of values from configuration which is of type int.
Definition: yaml.cpp:268
virtual bool is_float() const
Check if current value is a float.
Definition: yaml.cpp:125
virtual bool is_list() const
Check if a value is a list.
Definition: yaml.cpp:170
virtual std::string get_string() const
Get string value.
Definition: yaml.cpp:228
ValueIterator * search(const char *path)
Iterator with search results.
Definition: yaml.cpp:1384
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
Definition: yaml.cpp:1090
virtual int get_int() const
Get int value.
Definition: yaml.cpp:210
virtual float get_float() const
Get float value.
Definition: yaml.cpp:192
ValueIterator * iterator()
Iterator for all values.
Definition: yaml.cpp:1376
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
Definition: yaml.cpp:1264
virtual std::vector< float > get_floats() const
Get list of values from configuration which is of type float.
Definition: yaml.cpp:250
YamlConfiguration()
Constructor.
Definition: yaml.cpp:319
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
Definition: yaml.cpp:1327
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:1172
virtual bool is_list(const char *path)
Check if a value is a list.
Definition: yaml.cpp:1123
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:1041
Fawkes library namespace.
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
Definition: yaml.cpp:1243
virtual bool valid() const
Check if the current element is valid.
Definition: yaml.cpp:101
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
Definition: yaml.cpp:1205
void lock()
Lock the config.
Definition: yaml.cpp:1337
virtual bool is_bool(const char *path)
Check if a value is of type bool.
Definition: yaml.cpp:1110
virtual bool is_uint() const
Check if current value is a unsigned int.
Definition: yaml.cpp:134
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
Definition: yaml.cpp:1314
virtual bool is_int(const char *path)
Check if a value is of type int.
Definition: yaml.cpp:1104
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
Definition: yaml.cpp:1060
virtual bool is_default(const char *path)
Check if a value was read from the default config.
Definition: yaml.cpp:1140
virtual bool is_float(const char *path)
Check if a value is of type float.
Definition: yaml.cpp:1084
virtual bool is_bool() const
Check if current value is a bool.
Definition: yaml.cpp:152
virtual std::vector< unsigned int > get_uints() const
Get list of values from configuration which is of type unsigned int.
Definition: yaml.cpp:259
virtual std::string get_comment() const
Get comment of value.
Definition: yaml.cpp:296
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
Definition: yaml.cpp:1147
Iterator for YAML config trees.
Definition: yaml.h:127
virtual int get_int(const char *path)
Get value from configuration which is of type int.
Definition: yaml.cpp:1015
virtual void load(const char *file_path)
Load configuration.
Definition: yaml.cpp:427
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
Definition: yaml.cpp:1048
virtual std::vector< bool > get_bools() const
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:277
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: yaml.cpp:1027
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: yaml.cpp:1009
virtual const char * path() const
Path of value.
Definition: yaml.cpp:107
virtual std::string get_type(const char *path)
Get type of value at given path.
Definition: yaml.cpp:948
Configuration store using YAML documents.
Definition: yaml.h:49
virtual std::string get_as_string() const
Get value as string.
Definition: yaml.cpp:237
virtual size_t get_list_size() const
Get number of elements in list value.
Definition: yaml.cpp:180
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
Definition: yaml.cpp:1188
File Alteration Monitor Listener.
Definition: fam.h:35
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
Definition: yaml.cpp:1283
void unlock()
Unlock the config.
Definition: yaml.cpp:1357
virtual bool get_bool() const
Get bool value.
Definition: yaml.cpp:219
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
Definition: yaml.cpp:1235
virtual ~YamlConfiguration()
Destructor.
Definition: yaml.cpp:398
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
Definition: yaml.cpp:1164
virtual unsigned int get_uint() const
Get unsigned int value.
Definition: yaml.cpp:201
virtual void try_dump()
Try to dump configuration.
Definition: yaml.cpp:1370
bool try_lock()
Try to lock the config.
Definition: yaml.cpp:1348
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
Definition: yaml.cpp:1211
virtual bool next()
Check if there is another element and advance to this if possible.
Definition: yaml.cpp:89
virtual std::string get_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:959
virtual bool is_string(const char *path)
Check if a value is of type string.
Definition: yaml.cpp:1116
virtual bool is_default() const
Check if current value was read from the default config.
Definition: yaml.cpp:302
virtual bool exists(const char *path)
Check if a given value exists.
Definition: yaml.cpp:936
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
Definition: yaml.cpp:1308
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
Definition: yaml.cpp:1227
Iterator interface to iterate over config values.
Definition: config.h:72
virtual std::vector< std::string > get_strings() const
Get list of values from configuration which is of type string.
Definition: yaml.cpp:286
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
Definition: yaml.cpp:1021
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
Definition: yaml.cpp:1219
virtual void erase(const char *path)
Erase the given value from the configuration.
Definition: yaml.cpp:1269
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
Definition: yaml.cpp:1295
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
Definition: yaml.cpp:1277
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.
Definition: yaml.cpp:1289
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
Definition: yaml.cpp:1134
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
Definition: yaml.cpp:1034
FileAlterationMonitor thread wrapper.
Definition: fam_thread.h:35
virtual bool is_string() const
Check if current value is a string.
Definition: yaml.cpp:161
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: yaml.cpp:603
virtual float get_float(const char *path)
Get value from configuration which is of type float.
Definition: yaml.cpp:1003
virtual bool is_int() const
Check if current value is a int.
Definition: yaml.cpp:143
Mutex mutual exclusion lock.
Definition: mutex.h:32
virtual void copy(Configuration *copyconf)
Copies all values from the given configuration.
Definition: yaml.cpp:930
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
Definition: yaml.cpp:1054
Interface for configuration handling.
Definition: config.h:67
virtual const char * type() const
Type of value.
Definition: yaml.cpp:116