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, QString def ) {
84  // param will be treated as a vector, that is a list of space-separated values
85  const QString paramValue = params.getValue(paramPath);
86  QString str = paramValue.isEmpty() ? def : paramValue;
87  QVector<double> ret;
88  if ( str.isEmpty() ) return ret;
89  // split the string and convert the element into double
90  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
91  for( int i=0; i<list.size(); i++ ) {
92  // if toDouble will fail a 0.0 will be added
93  ret << list[i].toDouble();
94  }
95  return ret;
96 }
97 
98 QVector<int> ConfigurationHelper::getIntegerVector(ConfigurationParameters& params, QString paramPath, QString def) {
99  QVector<int> vector;
100  const QString paramValue = params.getValue(paramPath);
101  const QString stringVector = paramValue.isEmpty() ? def : paramValue;
102 
103  if (stringVector.isEmpty()) {
104  return vector;
105  }
106 
107  // Split the string and convert the element into integers
108  QStringList list = stringVector.split(QRegExp("\\s+"), QString::SkipEmptyParts);
109  for (int i = 0; i < list.size(); i++) {
110  // If toInt fails, a 0 will be added
111  vector << list[i].toInt();
112  }
113 
114  return vector;
115 }
116 
117 QVector<unsigned int> ConfigurationHelper::getUnsignedIntegerVector(ConfigurationParameters& params, QString paramPath, QString def) {
118  QVector<unsigned int> vector;
119  const QString paramValue = params.getValue(paramPath);
120  const QString stringVector = paramValue.isEmpty() ? def : paramValue;
121 
122  if (stringVector.isEmpty()) {
123  return vector;
124  }
125 
126  // Split the string and convert the element into integers
127  QStringList list = stringVector.split(QRegExp("\\s+"), QString::SkipEmptyParts);
128  for (int i = 0; i < list.size(); i++) {
129  // If toUInt fails, a 0 will be added
130  vector << list[i].toUInt();
131  }
132 
133  return vector;
134 }
135 
136 QVector<bool> ConfigurationHelper::getBoolVector(ConfigurationParameters& params, QString paramPath, QString def) {
137  QVector<bool> vector;
138  const QString paramValue = params.getValue(paramPath);
139  const QString stringVector = paramValue.isEmpty() ? def : paramValue;
140 
141  for (int i = 0; i < stringVector.size(); i++) {
142  if (stringVector[i] == '0') {
143  vector.append(false);
144  } else if (stringVector[i] == '1') {
145  vector.append(true);
146  } else {
147  return QVector<bool>();
148  }
149  }
150 
151  return vector;
152 }
153 
155  //--- extract the group name
156  QString groupName = group.section( '/', -1 );
157  //--- extract the path
158  QString groupPath = group.section( '/', 0, -2 );
159  foreach( QString agroup, params.getGroupsList( groupPath ) ) {
160  if ( groupName.compare( agroup, (params.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive) ) == 0 ) {
161  return true;
162  }
163  }
164  return false;
165 }
166 
167 bool ConfigurationHelper::hasParameter( ConfigurationParameters& params, QString group, QString paramName ) {
168  return !( params.getValue( group+"/"+paramName ).isEmpty() );
169 }
170 
171 void ConfigurationHelper::throwUserConfigError(QString paramName, QString paramValue, QString description)
172 {
173  throw UserDefinedCheckFailureException(paramName.toAscii().data(), paramValue.toAscii().data(), description.toAscii().data());
174 }
175 
176 void ConfigurationHelper::throwUserMissingResourceError(QString resourceName, QString description)
177 {
178  throw UserRequiredResourceMissingException(resourceName.toAscii().data(), description.toAscii().data());
179 }
180 
181 QStringList ConfigurationHelper::getDescribedParameterNames( QString type, QString prefix ) {
183  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Parameters" );
184 }
185 
186 QStringList ConfigurationHelper::getDescribedSubgroupNames( QString type, QString prefix ) {
188  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Subgroups" );
189 }
190 
191 QString ConfigurationHelper::getParameterTypeName( QString type, QString paramName ) {
193  return ConfigurationHelper::getString( typeDescr,
194  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/type",
195  "missing" );
196 }
197 
198 bool ConfigurationHelper::isRuntimeParameter( QString type, QString paramName ) {
200  return ConfigurationHelper::getBool( typeDescr,
201  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/isRuntime",
202  false );
203 }
204 
205 bool ConfigurationHelper::parameterIsDouble( QString type, QString paramName ) {
207  QString precision = ConfigurationHelper::getString( typeDescr,
208  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/precision",
209  "float" );
210  return (precision == "double");
211 }
212 
213 QStringList ConfigurationHelper::getParameterEnumValues( QString type, QString paramName ) {
215  QStringList values = ConfigurationHelper::getStringList( typeDescr,
216  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/values",
217  QString(' ') );
218  return values;
219 }
220 
221 } // end namespace farsa
222