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