Fawkes API Fawkes Development Version
qa_config.cpp
1
2/***************************************************************************
3 * qa_config.h - QA for configuration storage
4 *
5 * Generated: Mon Dec 18 19:09:18 2006
6 * Copyright 2005-2006 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
31using namespace std;
32using namespace fawkes;
33
34int
35main(int argc, char **argv)
36{
37 SQLiteConfiguration *config = new SQLiteConfiguration(CONFDIR);
38
39 try {
40 cout << "Loading configuration..." << flush;
41 config->load("qa.db", "qa_defaults.db");
42 cout << "done" << endl;
43 } catch (CouldNotOpenConfigException &e) {
44 cout << "failed" << endl;
45 e.print_trace();
46 }
47
48 try {
49 float of = 5.234;
50 cout << "[FLOAT] set f=" << of << "..." << flush;
51 config->set_float("/testing/float", of);
52 cout << "done" << endl;
53 cout << "[FLOAT] get..." << flush;
54 float f = config->get_float("/testing/float");
55 printf("done, f=%f\n", f);
56 } catch (ConfigurationException &e) {
57 cout << "failed" << endl;
58 e.print_trace();
59 }
60
61 try {
62 float of = 5.234;
63 cout << "[DEFAULT FLOAT] set f=" << of << "..." << flush;
64 config->set_default_float("/testing/default_float", of);
65 cout << "done" << endl;
66 cout << "[DEFAULT_FLOAT] get..." << flush;
67 float f = config->get_float("/testing/default_float");
68 if (!config->is_default("/testing/default_float")) {
69 throw ConfigurationException("/testing/default_float is not in default config");
70 }
71 printf("done, f=%f\n", f);
72 } catch (ConfigurationException &e) {
73 cout << "failed" << endl;
74 e.print_trace();
75 }
76
77 try {
78 unsigned int ou = 6;
79 cout << "[UINT] set u=" << ou << "..." << flush;
80 config->set_uint("/testing/uint", ou);
81 cout << "done" << endl;
82 cout << "[UINT] get..." << flush;
83 unsigned int u = config->get_uint("/testing/uint");
84 printf("done, u=%u\n", u);
85 } catch (ConfigurationException &e) {
86 cout << "failed" << endl;
87 e.print_trace();
88 }
89
90 try {
91 int oi = -7;
92 cout << "[INT] set i=" << oi << "..." << flush;
93 config->set_int("/testing/int", oi);
94 cout << "done" << endl;
95 cout << "[INT] get..." << flush;
96 int i = config->get_int("/testing/int");
97 printf("done, i=%i\n", i);
98 } catch (ConfigurationException &e) {
99 cout << "failed" << endl;
100 e.print_trace();
101 }
102
103 try {
104 bool ob = true;
105 cout << "[BOOL] set b=" << ob << "..." << flush;
106 config->set_bool("/testing/bool", ob);
107 cout << "done" << endl;
108 cout << "[BOOL] get..." << flush;
109 bool b = config->get_bool("/testing/bool");
110 printf("done, b=%s\n", (b ? "true" : "false"));
111 } catch (ConfigurationException &e) {
112 cout << "failed" << endl;
113 e.print_trace();
114 }
115
116 try {
117 string os = "This ain't no paradoxon";
118 cout << "[STRING] set s='" << os << "'..." << flush;
119 config->set_string("/testing/string", os);
120 cout << "done" << endl;
121 cout << "[STRING] get..." << flush;
122 string s = config->get_string("/testing/string");
123 printf("done, s='%s'\n", s.c_str());
124 } catch (ConfigurationException &e) {
125 cout << "failed" << endl;
126 e.print_trace();
127 }
128
129 try {
130 cout << "[EXIST] Checking if test string exists..." << flush;
131 if (config->exists("/testing/string")) {
132 cout << "success";
133 } else {
134 cout << "failed";
135 }
136 cout << endl;
137 } catch (ConfigurationException &e) {
138 cout << "failed" << endl;
139 e.print_trace();
140 }
141
142 try {
143 string os = "This ain't no paradoxon";
144 cout << "[LONGSTRING] set s='" << os << "'..." << flush;
145 config->set_string("/testing/veryveryveryverylongstring", os);
146 cout << "done" << endl;
147 cout << "[LONGSTRING] get..." << flush;
148 string s = config->get_string("/testing/veryveryveryverylongstring");
149 printf("done, s='%s'\n", s.c_str());
150 } catch (ConfigurationException &e) {
151 cout << "failed" << endl;
152 e.print_trace();
153 }
154
156 while (i->next()) {
157 if (i->is_float()) {
158 printf("FLOAT: %s = %f (default: %i)\n", i->path(), i->get_float(), i->is_default());
159 }
160 }
161
162 SQLiteConfiguration *config2 = new SQLiteConfiguration(CONFDIR);
163
164 try {
165 cout << "Loading configuration for 2nd db..." << flush;
166 config2->load("qa2.db", "qa2_defaults.db");
167 cout << "done" << endl;
168 } catch (CouldNotOpenConfigException &e) {
169 cout << "failed" << endl;
170 e.print_trace();
171 }
172
173 try {
174 cout << "Copying configuration..." << flush;
175 config2->copy(config);
176 cout << "done" << endl;
177 } catch (ConfigurationException &e) {
178 cout << "failed" << endl;
179 e.print_trace();
180 }
181
182 delete config2;
183 delete config;
184
185 return 0;
186}
187
188/// @endcond
Generic configuration exception.
Definition: config.h:40
Iterator interface to iterate over config values.
Definition: config.h:75
virtual const char * path() const =0
Path of value.
virtual bool next()=0
Check if there is another element and advance to this if possible.
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_default() const =0
Check if current value was read from the default config.
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 ValueIterator * iterator()=0
Iterator for all values.
virtual void set_default_float(const char *path, float f)=0
Set new default value in configuration of type float.
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 bool is_default(const char *path)=0
Check if a value was read from the default config.
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
virtual void copy(Configuration *copyconf)
Copy all values from the given configuration.
Definition: sqlite.cpp:719
virtual void load(const char *filename)
Load configuration.
Definition: sqlite.cpp:564
Fawkes library namespace.