bpp-core  2.2.0
ApplicationTools.h
Go to the documentation of this file.
1 //
2 // File: ApplicationTools.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Oct 21 16:19 2005
5 // From old file created on: Sun Dec 14 09:36:26 2003
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 _APPLICATIONTOOLS_H_
42 #define _APPLICATIONTOOLS_H_
43 
44 #include "../Io/FileTools.h"
45 #include "../Io/OutputStream.h"
46 #include "../Text/TextTools.h"
47 #include "../Text/StringTokenizer.h"
48 #include "../Text/NestedStringTokenizer.h"
49 
50 #include "../Numeric/Matrix/Matrix.h"
51 
52 // From the STL:
53 #include <map>
54 #include <vector>
55 #include <iostream>
56 #include <ctime>
57 
58 namespace bpp
59 {
60 
93  {
94  public:
95 
108 
112  static time_t startTime;
113 
117  static size_t terminalWidth;
118 
122  static float terminalSplit;
123 
127  static bool interactive;
128 
132  static int warningLevel;
133 
134  public:
136  virtual ~ApplicationTools() {}
137 
138  public:
139 
147  static bool parameterExists(const std::string& parameterName, std::map<std::string, std::string>& params);
148 
149  static bool parameterExists(const std::string& parameterName, std::vector<std::string>& params);
150 
160  static std::vector<std::string> matchingParameters(const std::string& pattern, std::map<std::string, std::string>& params);
161 
162  static std::vector<std::string> matchingParameters(const std::string& pattern, std::vector<std::string>& params);
163 
175  static double getDoubleParameter(
176  const std::string& parameterName,
177  std::map<std::string, std::string>& params,
178  double defaultValue,
179  const std::string& suffix = "",
180  bool suffixIsOptional = true,
181  int warn = 0);
182 
194  static int getIntParameter(
195  const std::string& parameterName,
196  std::map<std::string, std::string>& params,
197  int defaultValue,
198  const std::string& suffix = "",
199  bool suffixIsOptional = true,
200  int warn = 0);
201 
213  static std::string getStringParameter(
214  const std::string& parameterName,
215  std::map<std::string, std::string>& params,
216  const std::string& defaultValue,
217  const std::string& suffix = "",
218  bool suffixIsOptional = true,
219  int warn = 0);
220 
232  static bool getBooleanParameter(
233  const std::string& parameterName,
234  std::map<std::string, std::string>& params,
235  bool defaultValue,
236  const std::string& suffix = "",
237  bool suffixIsOptional = true,
238  int warn = 0);
239 
251  template<class T> static T getParameter(
252  const std::string& parameterName,
253  std::map<std::string, std::string>& params,
254  T defaultValue,
255  const std::string& suffix = "",
256  bool suffixIsOptional = true,
257  int warn = 0)
258  {
259  T tParam = defaultValue;
260  if (parameterExists(parameterName + suffix, params))
261  {
262  tParam = TextTools::to<T>(params[parameterName + suffix]);
263  }
264  else if (suffixIsOptional && parameterExists(parameterName, params))
265  {
266  tParam = TextTools::to<T>(params[parameterName]);
267  }
268  else if (warn <= warningLevel)
269  {
270  displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
271  }
272  return tParam;
273  }
274 
275 
293  static std::string getAFilePath(
294  const std::string& parameter,
295  std::map<std::string, std::string>& params,
296  bool isRequired = true,
297  bool mustExist = true,
298  const std::string& suffix = "",
299  bool suffixIsOptional = false,
300  const std::string& defaultPath = "none",
301  int warn = 0) throw (Exception);
302 
315  template<class T> static std::vector<T> getVectorParameter(
316  const std::string& parameterName,
317  std::map<std::string, std::string>& params,
318  char separator,
319  const std::string& defaultValue,
320  const std::string& suffix = "",
321  bool suffixIsOptional = true,
322  int warn = 0)
323  {
324  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
325  if (TextTools::isEmpty(s)) return std::vector<T>(0);
326  if (s[0] == '(' && s[s.size() - 1] == ')') {
327  //This is a delimited vector:
328  s = s.substr(1, s.size() - 2);
329  if (TextTools::isEmpty(s)) return std::vector<T>(0);
330  }
331  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
332  size_t n = st.numberOfRemainingTokens();
333  std::vector<T> v(n);
334  for (size_t i = 0; i < n; i++)
335  {
336  v[i] = TextTools::fromString<T>(st.nextToken());
337  }
338  return v;
339  }
340 
357  template<class T> static std::vector<T> getVectorParameter(
358  const std::string& parameterName,
359  std::map<std::string, std::string>& params,
360  char separator,
361  char rangeOperator,
362  const std::string& defaultValue,
363  const std::string& suffix = "",
364  bool suffixIsOptional = true,
365  bool warn = true)
366  {
367  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
368  if (s[0] == '(' && s[s.size() - 1] == ')') {
369  //This is a delimited vector:
370  s = s.substr(1, s.size() - 2);
371  if (TextTools::isEmpty(s)) return std::vector<T>(0);
372  }
373  StringTokenizer st(s, TextTools::toString(separator));
374  size_t n = st.numberOfRemainingTokens();
375  std::vector<T> v;
376  for (size_t i = 0; i < n; i++)
377  {
378  std::string token = st.nextToken();
379  std::string::size_type pos = token.find(rangeOperator);
380  if (pos == std::string::npos)
381  v.push_back(TextTools::fromString<T>(token));
382  else
383  {
384  T d1 = TextTools::fromString<T>(token.substr(0, pos));
385  T d2 = TextTools::fromString<T>(token.substr(pos + 1));
386  for (T j = d1; j < d2; j++)
387  {
388  v.push_back(j);
389  }
390  v.push_back(d2);
391  }
392  }
393  return v;
394  }
395 
417  template<class T> static RowMatrix<T> getMatrixParameter(
418  const std::string& parameterName,
419  std::map<std::string, std::string>& params,
420  char separator,
421  const std::string& defaultValue,
422  const std::string& suffix = "",
423  bool suffixIsOptional = true,
424  bool warn = true)
425  {
426  RowMatrix<T> mat;
427 
428  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
429  if (TextTools::isEmpty(s)) return RowMatrix<T>(0,0);
430  if (s[0] == '(' && s[s.size() - 1] == ')') {
431  //This is a delimited vector:
432  s = s.substr(1, s.size() - 2);
433  if (TextTools::isEmpty(s)) return RowMatrix<T>(0,0);
434  }
435 
436  StringTokenizer st1(s, "()");
437 
438  while (st1.hasMoreToken())
439  {
440  std::string si=st1.nextToken();
441  StringTokenizer st2(si, TextTools::toString(separator));
442  size_t n = st2.numberOfRemainingTokens();
443 
444  std::vector<T> v(n);
445  for (size_t i = 0; i < n; i++)
446  {
447  v[i] = TextTools::fromString<T>(st2.nextToken());
448  }
449 
450  if (v.size()!=0)
451  mat.addRow(v);
452  }
453  return mat;
454  }
455 
456 
468  static void displayMessage(const std::string& text);
469 
475  static void displayError(const std::string& text);
476 
482  static void displayWarning(const std::string& text);
483 
492  static void displayTask(const std::string& text, bool eof = false);
493 
499  static void displayTaskDone();
500 
511  template<class T>
512  static void displayResult(const std::string& text, const T& result)
513  {
514  displayMessage(TextTools::resizeRight(text, static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 1), '.') + ": " + TextTools::toString<T>(result));
515  }
516 
524  static void displayBooleanResult(const std::string& text, bool result)
525  {
526  displayResult(text, result ? std::string("yes") : std::string("no"));
527  }
528 
550  static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string& mes="");
551 
576  static void displayUnlimitedGauge(size_t iter, const std::string& mes="");
577 
578 
584  static void startTimer()
585  {
586  time(&startTime);
587  }
588 
594  static void displayTime(const std::string& msg);
595 
601  static double getTime();
602  };
603 
604 } //end of namespace bpp.
605 
606 #endif //_APPLICATIONTOOLS_H_
607 
static void displayTaskDone()
Print a task ended message.
static void displayWarning(const std::string &text)
Print a warning message.
static int getIntParameter(const std::string &parameterName, std::map< std::string, std::string > &params, int defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get an integer parameter.
A tokenizer for strings.
static std::vector< T > getVectorParameter(const std::string &parameterName, 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::vector< T > getVectorParameter(const std::string &parameterName, 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.
This class allows to perform a correspondence analysis.
static int warningLevel
Specify the amount of warning to display.
static void displayTime(const std::string &msg)
Display the current timer value to the &#39;message&#39; stream.
bool hasMoreToken() const
Tell if some tokens are still available.
static std::string resizeRight(const std::string &s, size_t newSize, char fill=' ')
Send a string of size &#39;newSize&#39;, which is a copy of &#39;s&#39; truncated or filled with character &#39;fill&#39; at ...
Definition: TextTools.cpp:324
static RowMatrix< T > getMatrixParameter(const std::string &parameterName, 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)), where the matrix is filled by lines. Here, the matrix would be: .
static void displayResult(const std::string &text, const T &result)
Print a result message.
static void displayTask(const std::string &text, bool eof=false)
Print a task message.
STL namespace.
static bool getBooleanParameter(const std::string &parameterName, std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a boolean parameter.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
static size_t terminalWidth
The width of the output terminal (in character).
static double getDoubleParameter(const std::string &parameterName, std::map< std::string, std::string > &params, double defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a double parameter.
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
This class provides some common tools for developping applications.
Matrix storage by row.
Definition: Matrix.h:129
static std::string getAFilePath(const std::string &parameter, 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 startTimer()
Starts the timer.
static bool isEmpty(const std::string &s)
Tell if a string is empty.
Definition: TextTools.cpp:52
static time_t startTime
Timer variable.
static OutputStream * message
The output stream where messages have to be displayed.
static bool parameterExists(const std::string &parameterName, std::map< std::string, std::string > &params)
Tells if a parameter have been specified.
static bool interactive
Tell if the program is interactive (typically run in foreground). Default to yes. ...
static void displayMessage(const std::string &text)
Print a message.
OutputStream interface.
Definition: OutputStream.h:64
static float terminalSplit
The fraction of terminal width dedicated to messages.
static T getParameter(const std::string &parameterName, std::map< std::string, std::string > &params, T defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a parameter.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
static void displayBooleanResult(const std::string &text, bool result)
Print a boolean result message ("yes" or "no").
Exception base class.
Definition: Exceptions.h:57
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
Display a gauge.
static std::string getStringParameter(const std::string &parameterName, 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 OutputStream * error
The output stream where errors have to be displayed.
static void displayUnlimitedGauge(size_t iter, const std::string &mes="")
Display a gauge for unefined amount of iterations.
void addRow(const std::vector< Scalar > &newRow)
Definition: Matrix.h:217
An improved tokenizer for strings.
static std::vector< std::string > matchingParameters(const std::string &pattern, std::map< std::string, std::string > &params)
Returns a vector of parameter names that match a given pattern.
static OutputStream * warning
The output stream where warnings have to be displayed.
static void displayError(const std::string &text)
Print an error message.
static double getTime()
Get the current timer value.