Fawkes API  Fawkes Development Version
qa_config_change_handler.cpp
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 #include <cstdio>
00030 
00031 
00032 using namespace std;
00033 using namespace fawkes;
00034 
00035 class QAConfigChangeHandler : public ConfigurationChangeHandler
00036 {
00037 public:
00038   QAConfigChangeHandler() : ConfigurationChangeHandler("/testing") {}
00039 
00040   virtual void
00041   config_tag_changed(const char *new_tag)
00042   {
00043     printf("CCH: New tag '%s'\n", new_tag);
00044   }
00045 
00046   virtual void
00047   config_value_changed(const Configuration::ValueIterator *v)
00048   {
00049     if (v->is_string()) {
00050       printf("CCH: String '%s' changed to %s\n",
00051              v->path(), v->get_string().c_str());
00052     } else if (v->is_bool()) {
00053       printf("CCH: Bool '%s' changed to %i\n", v->path(), v->get_bool());
00054     } else if (v->is_int()) {
00055       printf("CCH: Integer '%s' changed to %i\n", v->path(), v->get_int());
00056     } else if (v->is_uint()) {
00057       printf("CCH: Unsigned Integer '%s' changed to %u\n",
00058              v->path(), v->get_uint());
00059     } else if (v->is_float()) {
00060       printf("CCH: Float '%s' changed to %f\n", v->path(), v->get_float());
00061     }
00062   }
00063 
00064   virtual void
00065   config_comment_changed(const Configuration::ValueIterator *v)
00066   {
00067     printf("CCH: Comment of '%s' changed to %s\n",
00068            v->path(), v->get_comment().c_str());
00069   }
00070 
00071 
00072   virtual void
00073   config_value_erased(const char *path)
00074   {
00075     printf("CCH: Value '%s' erased\n", path);
00076   }
00077 
00078 };
00079 
00080 int
00081 main(int argc, char **argv)
00082 {
00083   SQLiteConfiguration *config = new SQLiteConfiguration(CONFDIR);
00084 
00085   QAConfigChangeHandler qach;
00086   config->add_change_handler(&qach);
00087 
00088   try {
00089     cout << "Loading configuration..." << flush;
00090     config->load("qa.db", "qa_defaults.db");
00091     cout << "done" << endl;
00092   } catch (CouldNotOpenConfigException &e) {
00093     cout << "failed" << endl;
00094     e.print_trace();
00095   }
00096 
00097   try {
00098     float of = 5.234;
00099     cout << "[FLOAT] set f=" << of << "..." << endl;
00100     config->set_float("/testing/float", of);
00101     cout << "[FLOAT] get..." << endl;
00102     float f = config->get_float("/testing/float");
00103     printf("done, f=%f\n", f);
00104   } catch (ConfigurationException &e) {
00105     cout << "failed" << endl;
00106     e.print_trace();
00107   }
00108 
00109   try {
00110     unsigned int ou = 6;
00111     cout << "[UINT] set u=" << ou << "..." << endl;
00112     config->set_uint("/testing/uint", ou);
00113     cout << "[UINT] get..." << endl;
00114     unsigned int u = config->get_uint("/testing/uint");
00115     printf("done, u=%u\n", u);
00116   } catch (ConfigurationException &e) {
00117     cout << "failed" << endl;
00118     e.print_trace();
00119   }
00120 
00121   try {
00122     int oi = -7;
00123     cout << "[INT] set i=" << oi << "..." << endl;
00124     config->set_int("/testing/int", oi);
00125     cout << "[INT] get..." << endl;
00126     int i = config->get_int("/testing/int");
00127     printf("done, i=%i\n", i);
00128   } catch (ConfigurationException &e) {
00129     cout << "failed" << endl;
00130     e.print_trace();
00131   }
00132 
00133   try {
00134     bool ob = true;
00135     cout << "[BOOL] set b=" << ob << "..." << endl;
00136     config->set_bool("/testing/bool", ob);
00137     cout << "[BOOL] get..." << endl;
00138     bool b = config->get_bool("/testing/bool");
00139     printf("done, b=%s\n", (b ? "true" : "false"));
00140   } catch (ConfigurationException &e) {
00141     cout << "failed" << endl;
00142     e.print_trace();
00143   }
00144 
00145   try {
00146     string os = "This ain't no paradoxon";
00147     cout << "[STRING] set s='" << os << "'..." << endl;
00148     config->set_string("/testing/string", os);
00149     cout << "[STRING] get..." << endl;
00150     string s = config->get_string("/testing/string");
00151     printf("done, s='%s'\n", s.c_str());
00152   } catch (ConfigurationException &e) {
00153     cout << "failed" << endl;
00154     e.print_trace();
00155   }
00156 
00157   try {
00158     cout << "[EXIST] Checking if test string exists..." << endl;
00159     if ( config->exists("/testing/string") ) {
00160       cout << "success";
00161     } else {
00162       cout << "failed";
00163     }
00164     cout << endl;
00165   } catch (ConfigurationException &e) {
00166     cout << "failed" << endl;
00167     e.print_trace();
00168   }
00169 
00170   try {
00171     string os = "This ain't no paradoxon";
00172     cout << "[LONGSTRING] set s='" << os << "'..." << endl;
00173     config->set_string("/testing/veryveryveryverylongstring", os);
00174     cout << "[LONGSTRING] get..." << endl;
00175     string s = config->get_string("/testing/veryveryveryverylongstring");
00176     printf("done, s='%s'\n", s.c_str());
00177   } catch (ConfigurationException &e) {
00178     cout << "failed" << endl;
00179     e.print_trace();
00180   }
00181 
00182   cout << "[ERASE] erasing all values" << endl;
00183   config->erase("/testing/float");
00184   config->erase("/testing/uint");
00185   config->erase("/testing/int");
00186   config->erase("/testing/bool");
00187   config->erase("/testing/string");
00188   config->erase("/testing/veryveryveryverylongstring");
00189 
00190   config->rem_change_handler(&qach);
00191 
00192   delete config;
00193 
00194   return 0;
00195 }
00196 
00197 
00198 
00199 /// @endcond