configurationhelper.cpp
1 /***************************************************************************
2  * Copyright (C) 2008-2011 by Tomassino Ferrauto, Gianluca Massera *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "configurationhelper.h"
22 #include "configurationexceptions.h"
23 
24 namespace farsa {
25 
26 int ConfigurationHelper::getInt( ConfigurationParameters& params, QString paramPath, int def ) {
27  QString str = params.getValue(paramPath);
28  if ( str.isEmpty() ) return def;
29  bool ok;
30  int ret = str.toInt(&ok);
31  if (!ok) {
32  return def;
33  }
34  return ret;
35 }
36 
37 unsigned int ConfigurationHelper::getUnsignedInt( ConfigurationParameters& params, QString paramPath, unsigned int def ) {
38  QString str = params.getValue(paramPath);
39  if ( str.isEmpty() ) return def;
40  bool ok;
41  unsigned int ret = str.toUInt(&ok);
42  if (!ok) {
43  return def;
44  }
45  return ret;
46 }
47 
48 double ConfigurationHelper::getDouble( ConfigurationParameters& params, QString paramPath, double def ) {
49  QString str = params.getValue(paramPath);
50  if ( str.isEmpty() ) return def;
51  bool ok;
52  double ret = str.toDouble(&ok);
53  if (!ok) {
54  return def;
55  }
56  return ret;
57 }
58 
59 bool ConfigurationHelper::getBool( ConfigurationParameters& params, QString paramPath, bool def ) {
60  QString str = params.getValue(paramPath).toLower();
61  if ((str == "t") || (str == "true") || (str == "1")) {
62  return true;
63  }
64  if ((str == "f") || (str == "false") || (str == "0")) {
65  return false;
66  }
67  // conversion failed
68  return def;
69 }
70 
71 QString ConfigurationHelper::getString( ConfigurationParameters& params, QString paramPath, QString def ) {
72  QString str = params.getValue(paramPath);
73  if ( str.isEmpty() ) {
74  return def;
75  }
76  return str;
77 }
78 
79 QStringList ConfigurationHelper::getStringList( ConfigurationParameters& params, QString paramPath, QString delimiter ) {
80  return params.getValue(paramPath).split( delimiter, QString::SkipEmptyParts );
81 }
82 
83 QVector<double> ConfigurationHelper::getVector( ConfigurationParameters& params, QString paramPath ) {
84  // param will be treated as a vector, that is a list of space-separated values
85  QString str = params.getValue(paramPath);
86  QVector<double> ret;
87  if ( str.isEmpty() ) return ret;
88  // split the string and convert the element into double
89  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
90  for( int i=0; i<list.size(); i++ ) {
91  // if toDouble will fail a 0.0 will be added
92  ret << list[i].toDouble();
93  }
94  return ret;
95 }
96 
98  //--- extract the group name
99  QString groupName = group.section( '/', -1 );
100  //--- extract the path
101  QString groupPath = group.section( '/', 0, -2 );
102  foreach( QString agroup, params.getGroupsList( groupPath ) ) {
103  if ( groupName.compare( agroup, (params.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive) ) == 0 ) {
104  return true;
105  }
106  }
107  return false;
108 }
109 
110 bool ConfigurationHelper::hasParameter( ConfigurationParameters& params, QString group, QString paramName ) {
111  return !( params.getValue( group+"/"+paramName ).isEmpty() );
112 }
113 
114 void ConfigurationHelper::throwUserConfigError(QString paramName, QString paramValue, QString description)
115 {
116  throw UserDefinedCheckFailureException(paramName.toAscii().data(), paramValue.toAscii().data(), description.toAscii().data());
117 }
118 
119 void ConfigurationHelper::throwUserMissingResourceError(QString resourceName, QString description)
120 {
121  throw UserRequiredResourceMissingException(resourceName.toAscii().data(), description.toAscii().data());
122 }
123 
124 QStringList ConfigurationHelper::getDescribedParameterNames( QString type, QString prefix ) {
126  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Parameters" );
127 }
128 
129 QStringList ConfigurationHelper::getDescribedSubgroupNames( QString type, QString prefix ) {
131  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Subgroups" );
132 }
133 
134 QString ConfigurationHelper::getParameterTypeName( QString type, QString paramName ) {
136  return ConfigurationHelper::getString( typeDescr,
137  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/type",
138  "missing" );
139 }
140 
141 bool ConfigurationHelper::isRuntimeParameter( QString type, QString paramName ) {
143  return ConfigurationHelper::getBool( typeDescr,
144  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/isRuntime",
145  false );
146 }
147 
148 bool ConfigurationHelper::parameterIsDouble( QString type, QString paramName ) {
150  QString precision = ConfigurationHelper::getString( typeDescr,
151  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/precision",
152  "float" );
153  return (precision == "double");
154 }
155 
156 QStringList ConfigurationHelper::getParameterEnumValues( QString type, QString paramName ) {
158  QStringList values = ConfigurationHelper::getStringList( typeDescr,
159  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/values",
160  QString(' ') );
161  return values;
162 }
163 
164 } // end namespace farsa
165