bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
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 // From the STL:
51 #include <map>
52 #include <vector>
53 #include <iostream>
54 #include <ctime>
55 
56 namespace bpp
57 {
58 
91 {
92  public:
93 
106 
110  static time_t startTime;
111 
115  static size_t terminalWidth;
116 
120  static float terminalSplit;
121 
125  static bool interactive;
126 
127  public:
129  virtual ~ApplicationTools() {}
130 
131  public:
132 
140  static bool parameterExists(const std::string& parameterName, std::map<std::string, std::string>& params);
141 
153  static double getDoubleParameter(
154  const std::string& parameterName,
155  std::map<std::string, std::string>& params,
156  double defaultValue,
157  const std::string& suffix = "",
158  bool suffixIsOptional = true,
159  bool warn = true);
160 
172  static int getIntParameter(
173  const std::string& parameterName,
174  std::map<std::string, std::string>& params,
175  int defaultValue,
176  const std::string& suffix = "",
177  bool suffixIsOptional = true,
178  bool warn = true);
179 
191  static std::string getStringParameter(
192  const std::string& parameterName,
193  std::map<std::string, std::string>& params,
194  const std::string& defaultValue,
195  const std::string& suffix = "",
196  bool suffixIsOptional = true,
197  bool warn = true);
198 
210  static bool getBooleanParameter(
211  const std::string& parameterName,
212  std::map<std::string, std::string>& params,
213  bool defaultValue,
214  const std::string& suffix = "",
215  bool suffixIsOptional = true,
216  bool warn = true);
217 
229  template<class T> static T getParameter(
230  const std::string& parameterName,
231  std::map<std::string, std::string>& params,
232  T defaultValue,
233  const std::string& suffix = "",
234  bool suffixIsOptional = true,
235  bool warn = true)
236  {
237  T tParam = defaultValue;
238  if(parameterExists(parameterName + suffix, params))
239  {
240  tParam = TextTools::to<T>(params[parameterName + suffix]);
241  }
242  else if(suffixIsOptional && parameterExists(parameterName, params))
243  {
244  tParam = TextTools::to<T>(params[parameterName]);
245  } else if(warn)
246  {
247  displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
248  }
249  return tParam;
250  }
251 
252 
268  static std::string getAFilePath(
269  const std::string& parameter,
270  std::map<std::string, std::string>& params,
271  bool isRequired = true,
272  bool mustExist = true,
273  const std::string& suffix = "",
274  bool suffixIsOptional = false) throw (Exception);
275 
288  template<class T> static std::vector<T> getVectorParameter(
289  const std::string& parameterName,
290  std::map<std::string, std::string>& params,
291  char separator,
292  const std::string& defaultValue,
293  const std::string& suffix = "",
294  bool suffixIsOptional = true,
295  bool warn = true)
296  {
297  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
298  if (TextTools::isEmpty(s)) return std::vector<T>(0);
299  if (s[0] == '(' && s[s.size() - 1] == ')') {
300  //This is a delimited vector:
301  s = s.substr(1, s.size() - 2);
302  if (TextTools::isEmpty(s)) return std::vector<T>(0);
303  }
304  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
305  size_t n = st.numberOfRemainingTokens();
306  std::vector<T> v(n);
307  for (size_t i = 0; i < n; i++)
308  {
309  v[i] = TextTools::fromString<T>(st.nextToken());
310  }
311  return v;
312  }
313 
330  template<class T> static std::vector<T> getVectorParameter(
331  const std::string& parameterName,
332  std::map<std::string, std::string>& params,
333  char separator,
334  char rangeOperator,
335  const std::string& defaultValue,
336  const std::string& suffix = "",
337  bool suffixIsOptional = true,
338  bool warn = true)
339  {
340  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
341  StringTokenizer st(s, TextTools::toString(separator));
342  size_t n = st.numberOfRemainingTokens();
343  std::vector<T> v;
344  for (size_t i = 0; i < n; i++)
345  {
346  std::string token = st.nextToken();
347  std::string::size_type pos = token.find(rangeOperator);
348  if (pos == std::string::npos)
349  v.push_back(TextTools::fromString<T>(token));
350  else
351  {
352  T d1 = TextTools::fromString<T>(token.substr(0, pos));
353  T d2 = TextTools::fromString<T>(token.substr(pos + 1));
354  for (T j = d1; j < d2; j++)
355  {
356  v.push_back(j);
357  }
358  v.push_back(d2);
359  }
360  }
361  return v;
362  }
363 
375  static void displayMessage(const std::string& text);
376 
382  static void displayError(const std::string& text);
383 
389  static void displayWarning(const std::string& text);
390 
399  static void displayTask(const std::string& text, bool eof = false);
400 
406  static void displayTaskDone();
407 
418  template<class T>
419  static void displayResult(const std::string& text, const T& result)
420  {
421  displayMessage(TextTools::resizeRight(text, static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 1), '.') + ": " + TextTools::toString<T>(result));
422  }
423 
431  static void displayBooleanResult(const std::string& text, bool result)
432  {
433  displayResult(text, result ? std::string("yes") : std::string("no"));
434  }
435 
457  static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string& mes="");
458 
483  static void displayUnlimitedGauge(size_t iter, const std::string& mes="");
484 
485 
491  static void startTimer()
492  {
493  time(&startTime);
494  }
495 
501  static void displayTime(const std::string& msg);
502 
508  static double getTime();
509 };
510 
511 } //end of namespace bpp.
512 
513 #endif //_APPLICATIONTOOLS_H_
514