bpp-core3  3.0.0
ApplicationTools.h
Go to the documentation of this file.
1 //
2 // File: ApplicationTools.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2005-10-21 16:19:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide basal and
12  utilitary classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_APP_APPLICATIONTOOLS_H
42 #define BPP_APP_APPLICATIONTOOLS_H
43 
44 
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"
51 
52 // From the STL:
53 #include <map>
54 #include <vector>
55 #include <iostream>
56 #include <ctime>
57 
58 namespace bpp
59 {
92 {
93 public:
97  static std::shared_ptr<OutputStream> error;
101  static std::shared_ptr<OutputStream> message;
105  static std::shared_ptr<OutputStream> warning;
106 
110  static time_t startTime;
111 
115  static size_t terminalWidth;
116 
120  static float terminalSplit;
121 
125  static bool interactive;
126 
130  static int warningLevel;
131 
132 public:
134  virtual ~ApplicationTools() {}
135 
136 public:
144  static bool parameterExists(const std::string& parameterName, const std::map<std::string, std::string>& params)
145  {
146  std::map<std::string, std::string>::const_iterator it = params.find(parameterName);
147 
148  return it != params.end() && !TextTools::isEmpty(it->second);
149  }
150 
151 
152  static bool parameterExists(const std::string& parameterName, std::vector<std::string>& params)
153  {
154  for (size_t i = 0; i < params.size(); ++i)
155  {
156  if (params[i] == parameterName)
157  return true;
158  }
159 
160  return false;
161  }
162 
172  static std::vector<std::string> matchingParameters(const std::string& pattern, const std::map<std::string, std::string>& params);
173 
174  static std::vector<std::string> matchingParameters(const std::string& pattern, std::vector<std::string>& params);
175 
187  static double getDoubleParameter(
188  const std::string& parameterName,
189  const std::map<std::string, std::string>& params,
190  double defaultValue,
191  const std::string& suffix = "",
192  bool suffixIsOptional = true,
193  int warn = 0);
194 
206  static int getIntParameter(
207  const std::string& parameterName,
208  const std::map<std::string, std::string>& params,
209  int defaultValue,
210  const std::string& suffix = "",
211  bool suffixIsOptional = true,
212  int warn = 0);
213 
225  static std::string getStringParameter(
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,
231  int warn = 0)
232  {
233  std::string sParam = defaultValue;
234  std::map<std::string, std::string>::const_iterator it1 = params.find(parameterName + suffix);
235  if (it1 != params.end() && !TextTools::isEmpty(it1->second))
236  sParam = it1->second;
237  else
238  {
239  std::map<std::string, std::string>::const_iterator it2 = params.find(parameterName);
240  if (suffixIsOptional && it2 != params.end() && !TextTools::isEmpty(it2->second))
241  sParam = it2->second;
242  else if (warn <= warningLevel)
243  {
244  displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + defaultValue);
245  }
246  }
247 
248  return sParam;
249  }
250 
251 
263  static bool getBooleanParameter(
264  const std::string& parameterName,
265  const std::map<std::string, std::string>& params,
266  bool defaultValue,
267  const std::string& suffix = "",
268  bool suffixIsOptional = true,
269  int warn = 0);
270 
282  template<class T> static T getParameter(
283  const std::string& parameterName,
284  const std::map<std::string, std::string>& params,
285  T defaultValue,
286  const std::string& suffix = "",
287  bool suffixIsOptional = true,
288  int warn = 0)
289  {
290  T tParam = defaultValue;
291  if (parameterExists(parameterName + suffix, params))
292  {
293  tParam = TextTools::to<T>(params.at(parameterName + suffix));
294  }
295  else if (suffixIsOptional && parameterExists(parameterName, params))
296  {
297  tParam = TextTools::to<T>(params.at(parameterName));
298  }
299  else if (warn <= warningLevel)
300  {
301  displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
302  }
303  return tParam;
304  }
305 
306 
324  static std::string getAFilePath(
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",
332  int warn = 0);
333 
347  template<class T> static std::vector<T> getVectorParameter(
348  const std::string& parameterName,
349  const std::map<std::string, std::string>& params,
350  char separator,
351  const std::string& defaultValue,
352  const std::string& suffix = "",
353  bool suffixIsOptional = true,
354  int warn = 0)
355  {
356  if (separator == ' ') throw Exception("ApplicationTools::getVectorParameter(). Separator cannot be a space character.");
357  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
358  if (TextTools::isEmpty(s)) return std::vector<T>(0);
359  if (s[0] == '(' && s[s.size() - 1] == ')')
360  {
361  // This is a delimited vector:
362  s = s.substr(1, s.size() - 2);
363  if (TextTools::isEmpty(s)) return std::vector<T>(0);
364  }
365  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
366  size_t n = st.numberOfRemainingTokens();
367  std::vector<T> v(n);
368  for (size_t i = 0; i < n; ++i)
369  {
370  v[i] = TextTools::fromString<T>(st.nextToken());
371  }
372  return v;
373  }
374 
388  template<class T> static std::vector< std::vector<T> > getVectorOfVectorsParameter(
389  const std::string& parameterName,
390  const std::map<std::string, std::string>& params,
391  char separator,
392  const std::string& defaultValue,
393  const std::string& suffix = "",
394  bool suffixIsOptional = true,
395  int warn = 0)
396  {
397  if (separator == ' ') throw Exception("ApplicationTools::getVectorOfVectorsParameter(). Separator cannot be a space character.");
398  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
399  if (TextTools::isEmpty(s)) return std::vector< std::vector<T> >(0);
400  if (s[0] == '(' && s[s.size() - 1] == ')')
401  {
402  // This is a delimited vector:
403  s = s.substr(1, s.size() - 2);
404  if (TextTools::isEmpty(s)) return std::vector< std::vector<T> >(0);
405  }
406  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
407  size_t n = st.numberOfRemainingTokens();
408  std::string s2;
409  std::vector< std::vector<T> > v(n);
410  for (size_t i = 0; i < n; ++i)
411  {
412  s2 = st.nextToken();
413  if (s2[0] == '(' && s2[s2.size() - 1] == ')')
414  {
415  // This is a delimited vector:
416  s2 = s2.substr(1, s2.size() - 2);
417  }
418  if (!TextTools::isEmpty(s2))
419  {
420  NestedStringTokenizer st2(s2, "(", ")", TextTools::toString(separator));
421  size_t n2 = st2.numberOfRemainingTokens();
422  v[i].resize(n2);
423  for (size_t j = 0; j < n2; ++j)
424  {
425  v[i][j] = TextTools::fromString<T>(st2.nextToken());
426  }
427  }
428  }
429  return v;
430  }
431 
448  template<class T> static std::vector<T> getVectorParameter(
449  const std::string& parameterName,
450  const std::map<std::string, std::string>& params,
451  char separator,
452  char rangeOperator,
453  const std::string& defaultValue,
454  const std::string& suffix = "",
455  bool suffixIsOptional = true,
456  bool warn = true)
457  {
458  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
459  if (s[0] == '(' && s[s.size() - 1] == ')')
460  {
461  // This is a delimited vector:
462  s = s.substr(1, s.size() - 2);
463  if (TextTools::isEmpty(s)) return std::vector<T>(0);
464  }
465  StringTokenizer st(s, TextTools::toString(separator));
466  size_t n = st.numberOfRemainingTokens();
467  std::vector<T> v;
468  for (size_t i = 0; i < n; ++i)
469  {
470  std::string token = st.nextToken();
471  std::string::size_type pos = token.find(rangeOperator);
472  if (pos == std::string::npos)
473  v.push_back(TextTools::fromString<T>(token));
474  else
475  {
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++)
479  {
480  v.push_back(j);
481  }
482  v.push_back(d2);
483  }
484  }
485  return v;
486  }
487 
509  template<class T> static RowMatrix<T> getMatrixParameter(
510  const std::string& parameterName,
511  const std::map<std::string, std::string>& params,
512  char separator,
513  const std::string& defaultValue,
514  const std::string& suffix = "",
515  bool suffixIsOptional = true,
516  bool warn = true)
517  {
518  RowMatrix<T> mat;
519 
520  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
521  if (TextTools::isEmpty(s)) return RowMatrix<T>(0, 0);
522  if (s[0] == '(' && s[s.size() - 1] == ')')
523  {
524  // This is a delimited vector:
525  s = s.substr(1, s.size() - 2);
526  if (TextTools::isEmpty(s)) return RowMatrix<T>(0, 0);
527  }
528 
529  StringTokenizer st1(s, "()");
530 
531  while (st1.hasMoreToken())
532  {
533  std::string si = st1.nextToken();
534  StringTokenizer st2(si, TextTools::toString(separator));
535  size_t n = st2.numberOfRemainingTokens();
536 
537  std::vector<T> v(n);
538  for (size_t i = 0; i < n; i++)
539  {
540  v[i] = TextTools::fromString<T>(st2.nextToken());
541  }
542 
543  if (v.size() != 0)
544  mat.addRow(v);
545  }
546  return mat;
547  }
548 
549 
561  static void displayMessage(const std::string& text);
562 
568  static void displayError(const std::string& text);
569 
575  static void displayWarning(const std::string& text);
576 
585  static void displayTask(const std::string& text, bool eof = false);
586 
592  static void displayTaskDone();
593 
604  template<class T>
605  static void displayResult(const std::string& text, const T& result)
606  {
607  displayMessage(TextTools::resizeRight(text, static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 1), '.') + ": " + TextTools::toString<T>(result));
608  }
609 
617  static void displayBooleanResult(const std::string& text, bool result)
618  {
619  displayResult(text, result ? std::string("yes") : std::string("no"));
620  }
621 
643  static void displayGauge(size_t iter, size_t total, char symbol = '>', const std::string& mes = "");
644 
669  static void displayUnlimitedGauge(size_t iter, const std::string& mes = "");
670 
671 
677  static void startTimer()
678  {
679  time(&startTime);
680  }
681 
687  static void displayTime(const std::string& msg);
688 
694  static double getTime();
695 };
696 } // end of namespace bpp.
697 #endif // BPP_APP_APPLICATIONTOOLS_H
This class provides some common tools for developping applications.
static int warningLevel
Specify the amount of warning to display.
static void displayMessage(const std::string &text)
Print a message.
static std::vector< std::vector< T > > getVectorOfVectorsParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a vector of vectors.
static std::vector< std::string > matchingParameters(const std::string &pattern, const std::map< std::string, std::string > &params)
Returns a vector of parameter names that match a given pattern.
static T getParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, T defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a parameter.
static std::shared_ptr< OutputStream > warning
The output stream where warnings have to be displayed.
static float terminalSplit
The fraction of terminal width dedicated to messages.
static void displayTask(const std::string &text, bool eof=false)
Print a task message.
static std::shared_ptr< OutputStream > error
The output stream where errors have to be displayed.
static std::shared_ptr< OutputStream > message
The output stream where messages have to be displayed.
static bool parameterExists(const std::string &parameterName, const std::map< std::string, std::string > &params)
Tells if a parameter have been specified.
static void displayUnlimitedGauge(size_t iter, const std::string &mes="")
Display a gauge for unefined amount of iterations.
static void displayError(const std::string &text)
Print an error message.
static size_t terminalWidth
The width of the output terminal (in character).
static void startTimer()
Starts the timer.
static void displayTaskDone()
Print a task ended message.
static RowMatrix< T > getMatrixParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
Get a RowMatrix. The input is made of embedded parenthesis, such as ((1,2),(3,4)),...
static std::vector< T > getVectorParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, char rangeOperator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
Get a vector.
static bool getBooleanParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a boolean parameter.
static std::string getStringParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a string parameter.
static double getTime()
Get the current timer value.
static void displayWarning(const std::string &text)
Print a warning message.
static bool interactive
Tell if the program is interactive (typically run in foreground). Default to yes.
static void displayBooleanResult(const std::string &text, bool result)
Print a boolean result message ("yes" or "no").
static double getDoubleParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, double defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a double parameter.
static int getIntParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, int defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get an integer parameter.
static void displayResult(const std::string &text, const T &result)
Print a result message.
static std::vector< T > getVectorParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a vector.
static std::string getAFilePath(const std::string &parameter, const std::map< std::string, std::string > &params, bool isRequired=true, bool mustExist=true, const std::string &suffix="", bool suffixIsOptional=false, const std::string &defaultPath="none", int warn=0)
Get a file path.
static void displayTime(const std::string &msg)
Display the current timer value to the 'message' stream.
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
Display a gauge.
static bool parameterExists(const std::string &parameterName, std::vector< std::string > &params)
static time_t startTime
Timer variable.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
An improved tokenizer for strings.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
Matrix storage by row.
Definition: Matrix.h:131
void addRow(const std::vector< Scalar > &newRow)
Definition: Matrix.h:222
A tokenizer for strings.
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.
Definition: TextTools.cpp:58
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:264