5 #ifndef BPP_APP_APPLICATIONTOOLS_H 6 #define BPP_APP_APPLICATIONTOOLS_H 9 #include "../Io/FileTools.h" 10 #include "../Io/OutputStream.h" 11 #include "../Numeric/Matrix/Matrix.h" 12 #include "../Text/NestedStringTokenizer.h" 13 #include "../Text/StringTokenizer.h" 14 #include "../Text/TextTools.h" 61 static std::shared_ptr<OutputStream>
error;
65 static std::shared_ptr<OutputStream>
message;
69 static std::shared_ptr<OutputStream>
warning;
108 static bool parameterExists(
const std::string& parameterName,
const std::map<std::string, std::string>& params)
110 std::map<std::string, std::string>::const_iterator it = params.find(parameterName);
116 static bool parameterExists(
const std::string& parameterName, std::vector<std::string>& params)
118 for (
size_t i = 0; i < params.size(); ++i)
120 if (params[i] == parameterName)
136 static std::vector<std::string>
matchingParameters(
const std::string& pattern,
const std::map<std::string, std::string>& params);
138 static std::vector<std::string>
matchingParameters(
const std::string& pattern, std::vector<std::string>& params);
152 const std::string& parameterName,
153 const std::map<std::string, std::string>& params,
155 const std::string& suffix =
"",
156 bool suffixIsOptional =
true,
171 const std::string& parameterName,
172 const std::map<std::string, std::string>& params,
174 const std::string& suffix =
"",
175 bool suffixIsOptional =
true,
190 const std::string& parameterName,
191 const std::map<std::string, std::string>& params,
192 const std::string& defaultValue,
193 const std::string& suffix =
"",
194 bool suffixIsOptional =
true,
197 std::string sParam = defaultValue;
198 std::map<std::string, std::string>::const_iterator it1 = params.find(parameterName + suffix);
200 sParam = it1->second;
203 std::map<std::string, std::string>::const_iterator it2 = params.find(parameterName);
205 sParam = it2->second;
206 else if (warn <= warningLevel)
208 displayWarning(
"Parameter " + parameterName +
" not specified. Default used instead: " + defaultValue);
228 const std::string& parameterName,
229 const std::map<std::string, std::string>& params,
231 const std::string& suffix =
"",
232 bool suffixIsOptional =
true,
247 const std::string& parameterName,
248 const std::map<std::string, std::string>& params,
250 const std::string& suffix =
"",
251 bool suffixIsOptional =
true,
254 T tParam = defaultValue;
257 tParam = TextTools::to<T>(params.at(parameterName + suffix));
261 tParam = TextTools::to<T>(params.at(parameterName));
289 const std::string& parameter,
290 const std::map<std::string, std::string>& params,
291 bool isRequired =
true,
292 bool mustExist =
true,
293 const std::string& suffix =
"",
294 bool suffixIsOptional =
false,
295 const std::string& defaultPath =
"none",
312 const std::string& parameterName,
313 const std::map<std::string, std::string>& params,
315 const std::string& defaultValue,
316 const std::string& suffix =
"",
317 bool suffixIsOptional =
true,
320 if (separator ==
' ')
throw Exception(
"ApplicationTools::getVectorParameter(). Separator cannot be a space character.");
321 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
323 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
326 s = s.substr(1, s.size() - 2);
332 for (
size_t i = 0; i < n; ++i)
334 v[i] = TextTools::fromString<T>(st.
nextToken());
353 const std::string& parameterName,
354 const std::map<std::string, std::string>& params,
356 const std::string& defaultValue,
357 const std::string& suffix =
"",
358 bool suffixIsOptional =
true,
361 if (separator ==
' ')
throw Exception(
"ApplicationTools::getVectorOfVectorsParameter(). Separator cannot be a space character.");
362 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
364 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
367 s = s.substr(1, s.size() - 2);
373 std::vector< std::vector<T>> v(n);
374 for (
size_t i = 0; i < n; ++i)
377 if (s2[0] ==
'(' && s2[s2.size() - 1] ==
')')
380 s2 = s2.substr(1, s2.size() - 2);
387 for (
size_t j = 0; j < n2; ++j)
389 v[i][j] = TextTools::fromString<T>(st2.
nextToken());
413 const std::string& parameterName,
414 const std::map<std::string, std::string>& params,
417 const std::string& defaultValue,
418 const std::string& suffix =
"",
419 bool suffixIsOptional =
true,
422 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
423 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
426 s = s.substr(1, s.size() - 2);
432 for (
size_t i = 0; i < n; ++i)
435 std::string::size_type pos = token.find(rangeOperator);
436 if (pos == std::string::npos)
437 v.push_back(TextTools::fromString<T>(token));
440 T d1 = TextTools::fromString<T>(token.substr(0, pos));
441 T d2 = TextTools::fromString<T>(token.substr(pos + 1));
442 for (T j = d1; j < d2; j++)
473 const std::string& parameterName,
474 const std::map<std::string, std::string>& params,
476 const std::string& defaultValue,
477 const std::string& suffix =
"",
478 bool suffixIsOptional =
true,
483 std::string s =
getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
485 if (s[0] ==
'(' && s[s.size() - 1] ==
')')
488 s = s.substr(1, s.size() - 2);
501 for (
size_t i = 0; i < n; i++)
503 v[i] = TextTools::fromString<T>(st2.
nextToken());
548 static void displayTask(
const std::string& text,
bool eof =
false);
582 displayResult(text, result ? std::string(
"yes") : std::string(
"no"));
606 static void displayGauge(
size_t iter,
size_t total,
char symbol =
'>',
const std::string& mes =
"");
660 #endif // BPP_APP_APPLICATIONTOOLS_H
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
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.
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.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
void addRow(const std::vector< Scalar > &newRow)
An improved tokenizer for strings.
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.