41 #ifndef BPP_APP_APPLICATIONTOOLS_H
42 #define BPP_APP_APPLICATIONTOOLS_H
45 #include "../Io/FileTools.h"
46 #include "../Io/OutputStream.h"
47 #include "../Numeric/Matrix/Matrix.h"
48 #include "../Text/NestedStringTokenizer.h"
49 #include "../Text/StringTokenizer.h"
50 #include "../Text/TextTools.h"
97 static std::shared_ptr<OutputStream>
error;
144 static bool parameterExists(
const std::string& parameterName,
const std::map<std::string, std::string>& params)
146 std::map<std::string, std::string>::const_iterator it = params.find(parameterName);
152 static bool parameterExists(
const std::string& parameterName, std::vector<std::string>& params)
154 for (
size_t i = 0; i < params.size(); ++i)
156 if (params[i] == parameterName)
172 static std::vector<std::string>
matchingParameters(
const std::string& pattern,
const std::map<std::string, std::string>& params);
174 static std::vector<std::string>
matchingParameters(
const std::string& pattern, std::vector<std::string>& params);
188 const std::string& parameterName,
189 const std::map<std::string, std::string>& params,
191 const std::string& suffix =
"",
192 bool suffixIsOptional =
true,
207 const std::string& parameterName,
208 const std::map<std::string, std::string>& params,
210 const std::string& suffix =
"",
211 bool suffixIsOptional =
true,
226 const std::string& parameterName,
227 const std::map<std::string, std::string>& params,
228 const std::string& defaultValue,
229 const std::string& suffix =
"",
230 bool suffixIsOptional =
true,
233 std::string sParam = defaultValue;
234 std::map<std::string, std::string>::const_iterator it1 = params.find(parameterName + suffix);
236 sParam = it1->second;
239 std::map<std::string, std::string>::const_iterator it2 = params.find(parameterName);
241 sParam = it2->second;
244 displayWarning(
"Parameter " + parameterName +
" not specified. Default used instead: " + defaultValue);
264 const std::string& parameterName,
265 const std::map<std::string, std::string>& params,
267 const std::string& suffix =
"",
268 bool suffixIsOptional =
true,
283 const std::string& parameterName,
284 const std::map<std::string, std::string>& params,
286 const std::string& suffix =
"",
287 bool suffixIsOptional =
true,
290 T tParam = defaultValue;
293 tParam = TextTools::to<T>(params.at(parameterName + suffix));
297 tParam = TextTools::to<T>(params.at(parameterName));
325 const std::string& parameter,
326 const std::map<std::string, std::string>& params,
327 bool isRequired =
true,
328 bool mustExist =
true,
329 const std::string& suffix =
"",
330 bool suffixIsOptional =
false,
331 const std::string& defaultPath =
"none",
348 const std::string& parameterName,
349 const std::map<std::string, std::string>& params,
351 const std::string& defaultValue,
352 const std::string& suffix =
"",
353 bool suffixIsOptional =
true,
356 if (separator ==
' ')
throw Exception(
"ApplicationTools::getVectorParameter(). Separator cannot be a space character.");
357 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
359 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
362 s = s.substr(1, s.size() - 2);
368 for (
size_t i = 0; i < n; ++i)
370 v[i] = TextTools::fromString<T>(st.
nextToken());
389 const std::string& parameterName,
390 const std::map<std::string, std::string>& params,
392 const std::string& defaultValue,
393 const std::string& suffix =
"",
394 bool suffixIsOptional =
true,
397 if (separator ==
' ')
throw Exception(
"ApplicationTools::getVectorOfVectorsParameter(). Separator cannot be a space character.");
398 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
400 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
403 s = s.substr(1, s.size() - 2);
409 std::vector< std::vector<T> > v(n);
410 for (
size_t i = 0; i < n; ++i)
413 if (s2[0] ==
'(' && s2[s2.size() - 1] ==
')')
416 s2 = s2.substr(1, s2.size() - 2);
423 for (
size_t j = 0; j < n2; ++j)
425 v[i][j] = TextTools::fromString<T>(st2.
nextToken());
449 const std::string& parameterName,
450 const std::map<std::string, std::string>& params,
453 const std::string& defaultValue,
454 const std::string& suffix =
"",
455 bool suffixIsOptional =
true,
458 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
459 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
462 s = s.substr(1, s.size() - 2);
468 for (
size_t i = 0; i < n; ++i)
471 std::string::size_type pos = token.find(rangeOperator);
472 if (pos == std::string::npos)
473 v.push_back(TextTools::fromString<T>(token));
476 T d1 = TextTools::fromString<T>(token.substr(0, pos));
477 T d2 = TextTools::fromString<T>(token.substr(pos + 1));
478 for (T j = d1; j < d2; j++)
510 const std::string& parameterName,
511 const std::map<std::string, std::string>& params,
513 const std::string& defaultValue,
514 const std::string& suffix =
"",
515 bool suffixIsOptional =
true,
520 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
522 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
525 s = s.substr(1, s.size() - 2);
538 for (
size_t i = 0; i < n; i++)
540 v[i] = TextTools::fromString<T>(st2.
nextToken());
585 static void displayTask(
const std::string& text,
bool eof =
false);
619 displayResult(text, result ? std::string(
"yes") : std::string(
"no"));
643 static void displayGauge(
size_t iter,
size_t total,
char symbol =
'>',
const std::string& mes =
"");
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
An improved tokenizer for strings.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
void addRow(const std::vector< Scalar > &newRow)
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
bool hasMoreToken() const
Tell if some tokens are still available.
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces.
std::string toString(T t)
General template method to convert to a string.
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)