configuration/src/configurationhelper.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008-2011 by Tomassino Ferrauto, Gianluca Massera       *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "configurationhelper.h"
00022 #include "configurationexceptions.h"
00023 
00024 namespace farsa {
00025 
00026 int ConfigurationHelper::getInt( ConfigurationParameters& params, QString paramPath, int def ) {
00027     QString str = params.getValue(paramPath);
00028     if ( str.isEmpty() ) return def;
00029     bool ok;
00030     int ret = str.toInt(&ok);
00031     if (!ok) {
00032         return def;
00033     }
00034     return ret;
00035 }
00036 
00037 unsigned int ConfigurationHelper::getUnsignedInt( ConfigurationParameters& params, QString paramPath, unsigned int def ) {
00038     QString str = params.getValue(paramPath);
00039     if ( str.isEmpty() ) return def;
00040     bool ok;
00041     unsigned int ret = str.toUInt(&ok);
00042     if (!ok) {
00043         return def;
00044     }
00045     return ret;
00046 }
00047 
00048 double ConfigurationHelper::getDouble( ConfigurationParameters& params, QString paramPath, double def ) {
00049     QString str = params.getValue(paramPath);
00050     if ( str.isEmpty() ) return def;
00051     bool ok;
00052     double ret = str.toDouble(&ok);
00053     if (!ok) {
00054         return def;
00055     }
00056     return ret;
00057 }
00058 
00059 bool ConfigurationHelper::getBool( ConfigurationParameters& params, QString paramPath, bool def ) {
00060     QString str = params.getValue(paramPath).toLower();
00061     if ((str == "t") || (str == "true") || (str == "1")) {
00062         return true;
00063     }
00064     if ((str == "f") || (str == "false") || (str == "0")) {
00065         return false;
00066     }
00067     // conversion failed
00068     return def;
00069 }
00070 
00071 QString ConfigurationHelper::getString( ConfigurationParameters& params, QString paramPath, QString def ) {
00072     QString str = params.getValue(paramPath);
00073     if ( str.isEmpty() ) {
00074         return def;
00075     }
00076     return str;
00077 }
00078 
00079 QStringList ConfigurationHelper::getStringList( ConfigurationParameters& params, QString paramPath, QString delimiter ) {
00080     return params.getValue(paramPath).split( delimiter, QString::SkipEmptyParts );
00081 }
00082 
00083 QVector<double> ConfigurationHelper::getVector( ConfigurationParameters& params, QString paramPath ) {
00084     // param will be treated as a vector, that is a list of space-separated values
00085     QString str = params.getValue(paramPath);
00086     QVector<double> ret;
00087     if ( str.isEmpty() ) return ret;
00088     // split the string and convert the element into double
00089     QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
00090     for( int i=0; i<list.size(); i++ ) {
00091         // if toDouble will fail a 0.0 will be added
00092         ret << list[i].toDouble();
00093     }
00094     return ret;
00095 }
00096 
00097 bool ConfigurationHelper::hasGroup( ConfigurationParameters& params, QString group ) {
00098     //--- extract the group name
00099     QString groupName = group.section( '/', -1 );
00100     //--- extract the path 
00101     QString groupPath = group.section( '/', 0, -2 );
00102     foreach( QString agroup, params.getGroupsList( groupPath ) ) {
00103         if ( groupName.compare( agroup, (params.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive) ) == 0 ) {
00104             return true;
00105         }
00106     }
00107     return false;
00108 }
00109 
00110 bool ConfigurationHelper::hasParameter( ConfigurationParameters& params, QString group, QString paramName ) {
00111     return !( params.getValue( group+"/"+paramName ).isEmpty() );
00112 }
00113 
00114 void ConfigurationHelper::throwUserConfigError(QString paramName, QString paramValue, QString description)
00115 {
00116     throw UserDefinedCheckFailureException(paramName.toAscii().data(), paramValue.toAscii().data(), description.toAscii().data());
00117 }
00118 
00119 void ConfigurationHelper::throwUserMissingResourceError(QString resourceName, QString description)
00120 {
00121     throw UserRequiredResourceMissingException(resourceName.toAscii().data(), description.toAscii().data());
00122 }
00123 
00124 } // end namespace farsa
00125