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