Fawkes API  Fawkes Development Version
qa_config.cpp
00001 
00002 /***************************************************************************
00003  *  qa_config.h - QA for configuration storage
00004  *
00005  *  Generated: Mon Dec 18 19:09:18 2006
00006  *  Copyright  2005-2006  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 using namespace std;
00032 using namespace fawkes;
00033 
00034 int
00035 main(int argc, char **argv)
00036 {
00037   SQLiteConfiguration *config = new SQLiteConfiguration(CONFDIR);
00038 
00039   try {
00040     cout << "Loading configuration..." << flush;
00041     config->load("qa.db", "qa_defaults.db");
00042     cout << "done" << endl;
00043   } catch (CouldNotOpenConfigException &e) {
00044     cout << "failed" << endl;
00045     e.print_trace();
00046   }
00047 
00048   try {
00049     float of = 5.234;
00050     cout << "[FLOAT] set f=" << of << "..." << flush;
00051     config->set_float("/testing/float", of);
00052     cout << "done" << endl;
00053     cout << "[FLOAT] get..." << flush;
00054     float f = config->get_float("/testing/float");
00055     printf("done, f=%f\n", f);
00056   } catch (ConfigurationException &e) {
00057     cout << "failed" << endl;
00058     e.print_trace();
00059   }
00060 
00061   try {
00062     float of = 5.234;
00063     cout << "[DEFAULT FLOAT] set f=" << of << "..." << flush;
00064     config->set_default_float("/testing/default_float", of);
00065     cout << "done" << endl;
00066     cout << "[DEFAULT_FLOAT] get..." << flush;
00067     float f = config->get_float("/testing/default_float");
00068     if ( ! config->is_default("/testing/default_float") ) {
00069       throw ConfigurationException("/testing/default_float is not in default config");
00070     }
00071     printf("done, f=%f\n", f);
00072   } catch (ConfigurationException &e) {
00073     cout << "failed" << endl;
00074     e.print_trace();
00075   }
00076 
00077   try {
00078     unsigned int ou = 6;
00079     cout << "[UINT] set u=" << ou << "..." << flush;
00080     config->set_uint("/testing/uint", ou);
00081     cout << "done" << endl;
00082     cout << "[UINT] get..." << flush;
00083     unsigned int u = config->get_uint("/testing/uint");
00084     printf("done, u=%u\n", u);
00085   } catch (ConfigurationException &e) {
00086     cout << "failed" << endl;
00087     e.print_trace();
00088   }
00089 
00090   try {
00091     int oi = -7;
00092     cout << "[INT] set i=" << oi << "..." << flush;
00093     config->set_int("/testing/int", oi);
00094     cout << "done" << endl;
00095     cout << "[INT] get..." << flush;
00096     int i = config->get_int("/testing/int");
00097     printf("done, i=%i\n", i);
00098   } catch (ConfigurationException &e) {
00099     cout << "failed" << endl;
00100     e.print_trace();
00101   }
00102 
00103   try {
00104     bool ob = true;
00105     cout << "[BOOL] set b=" << ob << "..." << flush;
00106     config->set_bool("/testing/bool", ob);
00107     cout << "done" << endl;
00108     cout << "[BOOL] get..." << flush;
00109     bool b = config->get_bool("/testing/bool");
00110     printf("done, b=%s\n", (b ? "true" : "false"));
00111   } catch (ConfigurationException &e) {
00112     cout << "failed" << endl;
00113     e.print_trace();
00114   }
00115 
00116   try {
00117     string os = "This ain't no paradoxon";
00118     cout << "[STRING] set s='" << os << "'..." << flush;
00119     config->set_string("/testing/string", os);
00120     cout << "done" << endl;
00121     cout << "[STRING] get..." << flush;
00122     string s = config->get_string("/testing/string");
00123     printf("done, s='%s'\n", s.c_str());
00124   } catch (ConfigurationException &e) {
00125     cout << "failed" << endl;
00126     e.print_trace();
00127   }
00128 
00129   try {
00130     cout << "[EXIST] Checking if test string exists..." << flush;
00131     if ( config->exists("/testing/string") ) {
00132       cout << "success";
00133     } else {
00134       cout << "failed";
00135     }
00136     cout << endl;
00137   } catch (ConfigurationException &e) {
00138     cout << "failed" << endl;
00139     e.print_trace();
00140   }
00141 
00142   try {
00143     string os = "This ain't no paradoxon";
00144     cout << "[LONGSTRING] set s='" << os << "'..." << flush;
00145     config->set_string("/testing/veryveryveryverylongstring", os);
00146     cout << "done" << endl;
00147     cout << "[LONGSTRING] get..." << flush;
00148     string s = config->get_string("/testing/veryveryveryverylongstring");
00149     printf("done, s='%s'\n", s.c_str());
00150   } catch (ConfigurationException &e) {
00151     cout << "failed" << endl;
00152     e.print_trace();
00153   }
00154 
00155   Configuration::ValueIterator *i = config->iterator();
00156   while (i->next()) {
00157     if ( i->is_float() ) {
00158       printf("FLOAT: %s = %f (default: %i)\n", i->path(), i->get_float(), i->is_default());
00159     }
00160   }
00161 
00162   SQLiteConfiguration *config2 = new SQLiteConfiguration(CONFDIR);
00163 
00164   try {
00165     cout << "Loading configuration for 2nd db..." << flush;
00166     config2->load("qa2.db", "qa2_defaults.db");
00167     cout << "done" << endl;
00168   } catch (CouldNotOpenConfigException &e) {
00169     cout << "failed" << endl;
00170     e.print_trace();
00171   }
00172 
00173   try {
00174     cout << "Copying configuration..." << flush;
00175     config2->copy(config);
00176     cout << "done" << endl;
00177   } catch (ConfigurationException &e) {
00178     cout << "failed" << endl;
00179     e.print_trace();
00180   }
00181 
00182   delete config2;
00183   delete config;
00184 
00185   return 0;
00186 }
00187 
00188 
00189 
00190 /// @endcond