Fawkes API  Fawkes Development Version
qa_config_change_handler.cpp
1 
2 /***************************************************************************
3  * qa_config_change_handler.cpp - QA for configuration change handlers
4  *
5  * Created: Mon Nov 12 19:11:06 2007
6  * Copyright 2006-2007 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 /// @cond QA
25 
26 #include <config/sqlite.h>
27 
28 #include <cstdio>
29 #include <iostream>
30 
31 using namespace std;
32 using namespace fawkes;
33 
34 class QAConfigChangeHandler : public ConfigurationChangeHandler
35 {
36 public:
37  QAConfigChangeHandler() : ConfigurationChangeHandler("/testing")
38  {
39  }
40 
41  virtual void
42  config_tag_changed(const char *new_tag)
43  {
44  printf("CCH: New tag '%s'\n", new_tag);
45  }
46 
47  virtual void
48  config_value_changed(const Configuration::ValueIterator *v)
49  {
50  if (v->is_string()) {
51  printf("CCH: String '%s' changed to %s\n", v->path(), v->get_string().c_str());
52  } else if (v->is_bool()) {
53  printf("CCH: Bool '%s' changed to %i\n", v->path(), v->get_bool());
54  } else if (v->is_int()) {
55  printf("CCH: Integer '%s' changed to %i\n", v->path(), v->get_int());
56  } else if (v->is_uint()) {
57  printf("CCH: Unsigned Integer '%s' changed to %u\n", v->path(), v->get_uint());
58  } else if (v->is_float()) {
59  printf("CCH: Float '%s' changed to %f\n", v->path(), v->get_float());
60  }
61  }
62 
63  virtual void
64  config_comment_changed(const Configuration::ValueIterator *v)
65  {
66  printf("CCH: Comment of '%s' changed to %s\n", v->path(), v->get_comment().c_str());
67  }
68 
69  virtual void
70  config_value_erased(const char *path)
71  {
72  printf("CCH: Value '%s' erased\n", path);
73  }
74 };
75 
76 int
77 main(int argc, char **argv)
78 {
79  SQLiteConfiguration *config = new SQLiteConfiguration(CONFDIR);
80 
81  QAConfigChangeHandler qach;
82  config->add_change_handler(&qach);
83 
84  try {
85  cout << "Loading configuration..." << flush;
86  config->load("qa.db", "qa_defaults.db");
87  cout << "done" << endl;
88  } catch (CouldNotOpenConfigException &e) {
89  cout << "failed" << endl;
90  e.print_trace();
91  }
92 
93  try {
94  float of = 5.234;
95  cout << "[FLOAT] set f=" << of << "..." << endl;
96  config->set_float("/testing/float", of);
97  cout << "[FLOAT] get..." << endl;
98  float f = config->get_float("/testing/float");
99  printf("done, f=%f\n", f);
100  } catch (ConfigurationException &e) {
101  cout << "failed" << endl;
102  e.print_trace();
103  }
104 
105  try {
106  unsigned int ou = 6;
107  cout << "[UINT] set u=" << ou << "..." << endl;
108  config->set_uint("/testing/uint", ou);
109  cout << "[UINT] get..." << endl;
110  unsigned int u = config->get_uint("/testing/uint");
111  printf("done, u=%u\n", u);
112  } catch (ConfigurationException &e) {
113  cout << "failed" << endl;
114  e.print_trace();
115  }
116 
117  try {
118  int oi = -7;
119  cout << "[INT] set i=" << oi << "..." << endl;
120  config->set_int("/testing/int", oi);
121  cout << "[INT] get..." << endl;
122  int i = config->get_int("/testing/int");
123  printf("done, i=%i\n", i);
124  } catch (ConfigurationException &e) {
125  cout << "failed" << endl;
126  e.print_trace();
127  }
128 
129  try {
130  bool ob = true;
131  cout << "[BOOL] set b=" << ob << "..." << endl;
132  config->set_bool("/testing/bool", ob);
133  cout << "[BOOL] get..." << endl;
134  bool b = config->get_bool("/testing/bool");
135  printf("done, b=%s\n", (b ? "true" : "false"));
136  } catch (ConfigurationException &e) {
137  cout << "failed" << endl;
138  e.print_trace();
139  }
140 
141  try {
142  string os = "This ain't no paradoxon";
143  cout << "[STRING] set s='" << os << "'..." << endl;
144  config->set_string("/testing/string", os);
145  cout << "[STRING] get..." << endl;
146  string s = config->get_string("/testing/string");
147  printf("done, s='%s'\n", s.c_str());
148  } catch (ConfigurationException &e) {
149  cout << "failed" << endl;
150  e.print_trace();
151  }
152 
153  try {
154  cout << "[EXIST] Checking if test string exists..." << endl;
155  if (config->exists("/testing/string")) {
156  cout << "success";
157  } else {
158  cout << "failed";
159  }
160  cout << endl;
161  } catch (ConfigurationException &e) {
162  cout << "failed" << endl;
163  e.print_trace();
164  }
165 
166  try {
167  string os = "This ain't no paradoxon";
168  cout << "[LONGSTRING] set s='" << os << "'..." << endl;
169  config->set_string("/testing/veryveryveryverylongstring", os);
170  cout << "[LONGSTRING] get..." << endl;
171  string s = config->get_string("/testing/veryveryveryverylongstring");
172  printf("done, s='%s'\n", s.c_str());
173  } catch (ConfigurationException &e) {
174  cout << "failed" << endl;
175  e.print_trace();
176  }
177 
178  cout << "[ERASE] erasing all values" << endl;
179  config->erase("/testing/float");
180  config->erase("/testing/uint");
181  config->erase("/testing/int");
182  config->erase("/testing/bool");
183  config->erase("/testing/string");
184  config->erase("/testing/veryveryveryverylongstring");
185 
186  config->rem_change_handler(&qach);
187 
188  delete config;
189 
190  return 0;
191 }
192 
193 /// @endcond
Interface for configuration change handling.
Generic configuration exception.
Definition: config.h:40
Iterator interface to iterate over config values.
Definition: config.h:75
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual const char * path() const =0
Path of value.
virtual bool get_bool() const =0
Get bool value.
virtual unsigned int get_uint() const =0
Get unsigned int value.
virtual float get_float() const =0
Get float value.
virtual bool is_float() const =0
Check if current value is a float.
virtual bool is_int() const =0
Check if current value is a int.
virtual bool is_string() const =0
Check if current value is a string.
virtual bool is_bool() const =0
Check if current value is a bool.
virtual int get_int() const =0
Get int value.
virtual std::string get_comment() const =0
Get comment of value.
virtual std::string get_string() const =0
Get string value.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual void set_uint(const char *path, unsigned int uint)=0
Set new value in configuration of type unsigned int.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual void set_bool(const char *path, bool b)=0
Set new value in configuration of type bool.
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
Definition: config.cpp:619
virtual void set_float(const char *path, float f)=0
Set new value in configuration of type float.
virtual void set_string(const char *path, std::string &s)=0
Set new value in configuration of type string.
virtual void load(const char *file_path)=0
Load configuration.
virtual bool exists(const char *path)=0
Check if a given value exists.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual void set_int(const char *path, int i)=0
Set new value in configuration of type int.
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
Definition: config.cpp:603
virtual void erase(const char *path)=0
Erase the given value from the configuration.
Thrown if config could not be opened.
Definition: config.h:59
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
Configuration storage using SQLite.
Definition: sqlite.h:41
Fawkes library namespace.