00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00085 QString str = params.getValue(paramPath);
00086 QVector<double> ret;
00087 if ( str.isEmpty() ) return ret;
00088
00089 QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
00090 for( int i=0; i<list.size(); i++ ) {
00091
00092 ret << list[i].toDouble();
00093 }
00094 return ret;
00095 }
00096
00097 bool ConfigurationHelper::hasGroup( ConfigurationParameters& params, QString group ) {
00098
00099 QString groupName = group.section( '/', -1 );
00100
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 }
00125