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 
154 bool ConfigurationHelper::orderByNumberAfterColon(const QString& s1, const QString& s2) {
155  // If a string doesn't contain any colon, it always follows the other string; this way strings without colons are always
156  // at the end when sorting
157  QStringList list = s1.split(':', QString::SkipEmptyParts);
158  if (list.size() < 2) {
159  return false;
160  }
161  const double ns1 = list[1].toDouble();
162  list = s2.split(':', QString::SkipEmptyParts);
163  if (list.size() < 2) {
164  return true;
165  }
166  const double ns2 = list[1].toDouble();
167 
168  return (ns1 < ns2);
169 }
170 
172  //--- extract the group name
173  QString groupName = group.section( '/', -1 );
174  //--- extract the path
175  QString groupPath = group.section( '/', 0, -2 );
176  foreach( QString agroup, params.getGroupsList( groupPath ) ) {
177  if ( groupName.compare( agroup, (params.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive) ) == 0 ) {
178  return true;
179  }
180  }
181  return false;
182 }
183 
184 bool ConfigurationHelper::hasParameter( ConfigurationParameters& params, QString group, QString paramName ) {
185  return !( params.getValue( group+"/"+paramName ).isEmpty() );
186 }
187 
188 void ConfigurationHelper::throwUserConfigError(QString paramName, QString paramValue, QString description)
189 {
190  throw UserDefinedCheckFailureException(paramName.toLatin1().data(), paramValue.toLatin1().data(), description.toLatin1().data());
191 }
192 
193 void ConfigurationHelper::throwUserMissingResourceError(QString resourceName, QString description)
194 {
195  throw UserRequiredResourceMissingException(resourceName.toLatin1().data(), description.toLatin1().data());
196 }
197 
198 QStringList ConfigurationHelper::getDescribedParameterNames( QString type, QString prefix ) {
200  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Parameters" );
201 }
202 
203 QStringList ConfigurationHelper::getDescribedSubgroupNames( QString type, QString prefix ) {
205  ParameterSettable::fullSubgroupDescriptionPath(type, prefix)+"/Subgroups" );
206 }
207 
208 QString ConfigurationHelper::getParameterTypeName( QString type, QString paramName ) {
210  return ConfigurationHelper::getString( typeDescr,
211  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/type",
212  "missing" );
213 }
214 
215 bool ConfigurationHelper::isRuntimeParameter( QString type, QString paramName ) {
217  return ConfigurationHelper::getBool( typeDescr,
218  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/isRuntime",
219  false );
220 }
221 
222 bool ConfigurationHelper::parameterIsDouble( QString type, QString paramName ) {
224  QString precision = ConfigurationHelper::getString( typeDescr,
225  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/precision",
226  "float" );
227  return (precision == "double");
228 }
229 
230 QStringList ConfigurationHelper::getParameterEnumValues( QString type, QString paramName ) {
232  QStringList values = ConfigurationHelper::getStringList( typeDescr,
233  ParameterSettable::fullParameterDescriptionPath( type, paramName )+"/values",
234  QString(' ') );
235  return values;
236 }
237 
238 } // end namespace farsa
239