Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * qa_config_change_handler.cpp - QA for configuration change handlers 00004 * 00005 * Created: Mon Nov 12 19:11:06 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 /// @cond QA 00025 00026 #include <config/sqlite.h> 00027 00028 #include <iostream> 00029 00030 using namespace std; 00031 using namespace fawkes; 00032 00033 class QAConfigChangeHandler : public ConfigurationChangeHandler 00034 { 00035 public: 00036 QAConfigChangeHandler() : ConfigurationChangeHandler("/testing") {} 00037 00038 virtual void 00039 config_tag_changed(const char *new_tag) 00040 { 00041 printf("CCH: New tag '%s'\n", new_tag); 00042 } 00043 00044 virtual void 00045 config_value_changed(const char *path, int value) 00046 { 00047 printf("CCH: Integer '%s' changed to %i\n", path, value); 00048 } 00049 00050 virtual void 00051 config_value_changed(const char *path, unsigned int value) 00052 { 00053 printf("CCH: Unsigned Integer '%s' changed to %u\n", path, value); 00054 } 00055 00056 virtual void 00057 config_value_changed(const char *path, float value) 00058 { 00059 printf("CCH: Float '%s' changed to %f\n", path, value); 00060 } 00061 00062 virtual void 00063 config_value_changed(const char *path, bool value) 00064 { 00065 printf("CCH: Bool '%s' changed to %i\n", path, value); 00066 } 00067 00068 virtual void 00069 config_value_changed(const char *path, const char *value) 00070 { 00071 printf("CCH: String '%s' changed to %s\n", path, value); 00072 } 00073 00074 virtual void 00075 config_value_erased(const char *path) 00076 { 00077 printf("CCH: Value '%s' erased\n", path); 00078 } 00079 00080 }; 00081 00082 int 00083 main(int argc, char **argv) 00084 { 00085 SQLiteConfiguration *config = new SQLiteConfiguration(CONFDIR); 00086 00087 QAConfigChangeHandler qach; 00088 config->add_change_handler(&qach); 00089 00090 try { 00091 cout << "Loading configuration..." << flush; 00092 config->load("qa.db", "qa_defaults.db"); 00093 cout << "done" << endl; 00094 } catch (CouldNotOpenConfigException &e) { 00095 cout << "failed" << endl; 00096 e.print_trace(); 00097 } 00098 00099 try { 00100 float of = 5.234; 00101 cout << "[FLOAT] set f=" << of << "..." << endl; 00102 config->set_float("/testing/float", of); 00103 cout << "[FLOAT] get..." << endl; 00104 float f = config->get_float("/testing/float"); 00105 printf("done, f=%f\n", f); 00106 } catch (ConfigurationException &e) { 00107 cout << "failed" << endl; 00108 e.print_trace(); 00109 } 00110 00111 try { 00112 unsigned int ou = 6; 00113 cout << "[UINT] set u=" << ou << "..." << endl; 00114 config->set_uint("/testing/uint", ou); 00115 cout << "[UINT] get..." << endl; 00116 unsigned int u = config->get_uint("/testing/uint"); 00117 printf("done, u=%u\n", u); 00118 } catch (ConfigurationException &e) { 00119 cout << "failed" << endl; 00120 e.print_trace(); 00121 } 00122 00123 try { 00124 int oi = -7; 00125 cout << "[INT] set i=" << oi << "..." << endl; 00126 config->set_int("/testing/int", oi); 00127 cout << "[INT] get..." << endl; 00128 int i = config->get_int("/testing/int"); 00129 printf("done, i=%i\n", i); 00130 } catch (ConfigurationException &e) { 00131 cout << "failed" << endl; 00132 e.print_trace(); 00133 } 00134 00135 try { 00136 bool ob = true; 00137 cout << "[BOOL] set b=" << ob << "..." << endl; 00138 config->set_bool("/testing/bool", ob); 00139 cout << "[BOOL] get..." << endl; 00140 bool b = config->get_bool("/testing/bool"); 00141 printf("done, b=%s\n", (b ? "true" : "false")); 00142 } catch (ConfigurationException &e) { 00143 cout << "failed" << endl; 00144 e.print_trace(); 00145 } 00146 00147 try { 00148 string os = "This ain't no paradoxon"; 00149 cout << "[STRING] set s='" << os << "'..." << endl; 00150 config->set_string("/testing/string", os); 00151 cout << "[STRING] get..." << endl; 00152 string s = config->get_string("/testing/string"); 00153 printf("done, s='%s'\n", s.c_str()); 00154 } catch (ConfigurationException &e) { 00155 cout << "failed" << endl; 00156 e.print_trace(); 00157 } 00158 00159 try { 00160 cout << "[EXIST] Checking if test string exists..." << endl; 00161 if ( config->exists("/testing/string") ) { 00162 cout << "success"; 00163 } else { 00164 cout << "failed"; 00165 } 00166 cout << endl; 00167 } catch (ConfigurationException &e) { 00168 cout << "failed" << endl; 00169 e.print_trace(); 00170 } 00171 00172 try { 00173 string os = "This ain't no paradoxon"; 00174 cout << "[LONGSTRING] set s='" << os << "'..." << endl; 00175 config->set_string("/testing/veryveryveryverylongstring", os); 00176 cout << "[LONGSTRING] get..." << endl; 00177 string s = config->get_string("/testing/veryveryveryverylongstring"); 00178 printf("done, s='%s'\n", s.c_str()); 00179 } catch (ConfigurationException &e) { 00180 cout << "failed" << endl; 00181 e.print_trace(); 00182 } 00183 00184 cout << "[ERASE] erasing all values" << endl; 00185 config->erase("/testing/float"); 00186 config->erase("/testing/uint"); 00187 config->erase("/testing/int"); 00188 config->erase("/testing/bool"); 00189 config->erase("/testing/string"); 00190 config->erase("/testing/veryveryveryverylongstring"); 00191 00192 config->rem_change_handler(&qach); 00193 00194 delete config; 00195 00196 return 0; 00197 } 00198 00199 00200 00201 /// @endcond