solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/configuration/CConfigHelper.cpp
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (CConfigHelper.cpp), the license terms are:
00011 
00012  You can redistribute it and/or modify it under the terms of the GNU
00013  General Public License as published by the Free Software Foundation; either
00014  version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00033 #include <iostream>
00034 
00035 #include "configuration/CConfigHelper.h"
00036 #include "configuration/Registry.h"
00037 
00038 using namespace std;
00039 using namespace libconfig;
00040 
00041 CConfigHelper::CConfigHelper( const string& configurationpath )
00042 {
00043      cfgpath = configurationpath;
00044 }
00045 
00046 CConfigHelper::~CConfigHelper()
00047 {
00048      // TODO Auto-generated destructor stub
00049 }
00050 
00051 bool CConfigHelper::CheckConfig( const string & setting,
00052      libconfig::Setting::Type type, bool optional, bool printerr )
00053 {
00054      libconfig::Config* cfg = Registry::Instance().Configuration();
00055      string reason = "";
00056 
00057      string tmp = cfgpath + "." + setting;
00058 
00059      if (!cfg->exists(tmp)) {
00060           if (optional) {
00061                return true;
00062           }
00063           if (printerr)
00064                reason = "was not found";
00065      } else {
00066           libconfig::Setting & set = cfg->lookup(tmp);
00067           if (printerr)
00068                reason = "is of wrong type.";
00069 
00070           switch (type) {
00071           case Setting::TypeInt:
00072           case Setting::TypeFloat:
00073           case Setting::TypeInt64:
00074           {
00075                if (set.isNumber())
00076                     return true;
00077                break;
00078           }
00079           default:
00080                if (set.getType() == type) {
00081                     return true;
00082                }
00083           }
00084      }
00085 
00086      if (printerr) {
00087           string name = cfgpath;
00088           cfg->lookupValue(cfgpath + ".name", name);
00089           LOGERROR(Registry::GetMainLogger(), "Setting " << setting <<
00090                " of " << name << " " << reason);
00091      }
00092      return false;
00093 }