DDS  ver. 3.6
MiscUtils.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // a set of general helpers
4 //
5 #ifndef _DDS_MISCUTILS_H_
6 #define _DDS_MISCUTILS_H_
7 
8 // STD
9 #include <algorithm>
10 #include <iostream>
11 // BOOST
12 #include <boost/filesystem.hpp>
13 #include <boost/process.hpp>
14 #include <boost/program_options/parsers.hpp>
15 
21 namespace dds::misc
22 {
29  {
30  protected:
32  {
33  }
35  {
36  }
37 
38  private:
39  NONCopyable(const NONCopyable&);
40  const NONCopyable& operator=(const NONCopyable&);
41  };
42 
43  class NullType
44  {
45  };
52  {
53  public:
55  : m_unset(false)
56  {
57  }
58  auto_setenv(const std::string& _VarName, const std::string& _NewValue)
59  : m_unset(false)
60  {
61  m_sVarName = _VarName;
62  m_sNewValue = _NewValue;
63 
64  char* chTmp = getenv(m_sVarName.c_str());
65  if (chTmp)
66  m_sOldValue = chTmp;
67  else
68  m_unset = true;
69  // TODO: check error code
70  setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
71  }
73  {
74  unset();
75  }
76 
77  public:
78  void set(const std::string& _VarName, const std::string& _NewValue)
79  {
80  unset();
81 
82  m_sVarName = _VarName;
83  m_sNewValue = _NewValue;
84 
85  char* chTmp = getenv(m_sVarName.c_str());
86  if (chTmp)
87  m_sOldValue = chTmp;
88  else
89  m_unset = true;
90  // TODO: check error code
91  setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
92  }
93  void unset()
94  {
95  if (m_unset)
96  {
97  m_unset = false;
98  unsetenv(m_sVarName.c_str());
99  return;
100  }
101 
102  if (!m_sVarName.empty())
103  {
104  m_sVarName.clear();
105  setenv(m_sVarName.c_str(), m_sOldValue.c_str(), 1);
106  }
107  }
108 
109  private:
110  std::string m_sVarName;
111  std::string m_sNewValue;
112  std::string m_sOldValue;
113  bool m_unset;
114  };
115 
124  template <typename _T>
125  _T* smart_append(_T* _pString, const typename _T::value_type _ItemToAdd)
126  {
127  if (!_pString)
128  return _pString;
129 
130  if (_pString->empty() || (*_pString)[_pString->size() - 1] != _ItemToAdd)
131  _pString->push_back(_ItemToAdd);
132 
133  return _pString;
134  }
135 
144  template <typename _T>
145  _T& trim_right(_T* _pString, const typename _T::value_type& _chWhat)
146  {
147  return _pString->erase(_pString->find_last_not_of(_chWhat) + 1);
148  }
149 
158  template <typename _T>
159  _T& trim_left(_T* _pString, const typename _T::value_type& _chWhat)
160  {
161  return _pString->erase(0, _pString->find_first_not_of(_chWhat));
162  }
163 
172  template <typename _T>
173  _T& trim(_T* _pString, const typename _T::value_type& _chWhat)
174  {
175  return trim_right(&trim_left(_pString, _chWhat), _chWhat);
176  }
177 
187  template <typename _T>
188  _T& replace(_T* _pString, const _T& _what, const _T& _with)
189  {
190  typename _T::size_type pos = 0;
191  typename _T::size_type withLen = _with.length();
192  typename _T::size_type whatLen = _what.length();
193  while ((pos = _pString->find(_what, pos)) != _T::npos)
194  {
195  _pString->replace(pos, _what.length(), _with);
196  if (withLen > whatLen)
197  pos += withLen - whatLen + 1;
198  }
199  return (*_pString);
200  }
201 
202  // HACK: because of the bug in gcc 3.3 we need to use this nasty ToLower and ToUpper instead of direct calls of
203  // tolower (tolower.. is inline in this version
204  // of std lib)...
205  struct ToLower
206  {
207  char operator()(char c) const
208  {
209  return std::tolower(c);
210  }
211  };
212  struct ToUpper
213  {
214  char operator()(char c) const
215  {
216  return std::toupper(c);
217  }
218  };
219  struct IsDigit : std::unary_function<int, int>
220  {
221  int operator()(int c) const
222  {
223  return std::isdigit(c);
224  }
225  };
226 
234  template <typename _T>
235  _T& to_upper(_T& _str)
236  {
237  std::transform(_str.begin(), _str.end(), _str.begin(), ToUpper());
238  return _str;
239  }
240 
248  template <typename _T>
249  _T& to_lower(_T& _str)
250  {
251  std::transform(_str.begin(), _str.end(), _str.begin(), ToLower());
252  return _str;
253  }
254 
256  {
257  std::uintmax_t size{};
258 
259  private:
260  friend std::ostream& operator<<(std::ostream& os, HumanReadable hr)
261  {
262  int i{};
263  double mantissa = hr.size;
264  for (; mantissa >= 1024.; mantissa /= 1024., ++i)
265  {
266  }
267  mantissa = std::ceil(mantissa * 10.) / 10.;
268  os << mantissa << "BKMGTPE"[i];
269  return i == 0 ? os : os << "B (" << hr.size << ')';
270  }
271  };
272 }; // namespace dds::misc
273 #endif // _DDS_MISCUTILS_H_
Definition: MiscUtils.h:219
std::uintmax_t size
Definition: MiscUtils.h:257
_T & trim_left(_T *_pString, const typename _T::value_type &_chWhat)
trims leading characters from the string.
Definition: MiscUtils.h:159
friend std::ostream & operator<<(std::ostream &os, HumanReadable hr)
Definition: MiscUtils.h:260
NONCopyable()
Definition: MiscUtils.h:31
void set(const std::string &_VarName, const std::string &_NewValue)
Definition: MiscUtils.h:78
Definition: MiscUtils.h:255
_T & to_upper(_T &_str)
convert string to upper case.
Definition: MiscUtils.h:235
Definition: MiscUtils.h:212
_T & trim(_T *_pString, const typename _T::value_type &_chWhat)
trims trailing and leading characters from the string.
Definition: MiscUtils.h:173
_T & trim_right(_T *_pString, const typename _T::value_type &_chWhat)
trims trailing characters from the string.
Definition: MiscUtils.h:145
~auto_setenv()
Definition: MiscUtils.h:72
int operator()(int c) const
Definition: MiscUtils.h:221
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:82
char operator()(char c) const
Definition: MiscUtils.h:207
auto_setenv(const std::string &_VarName, const std::string &_NewValue)
Definition: MiscUtils.h:58
auto_setenv()
Definition: MiscUtils.h:54
~NONCopyable()
Definition: MiscUtils.h:34
_T & replace(_T *_pString, const _T &_what, const _T &_with)
finds elements in a string match a specified string and replaces it.
Definition: MiscUtils.h:188
void unset()
Definition: MiscUtils.h:93
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:28
_T & to_lower(_T &_str)
convert string to lower case.
Definition: MiscUtils.h:249
A helper class. Helps to automatically track environment variables.
Definition: MiscUtils.h:51
Definition: MiscUtils.h:205
Definition: MiscUtils.h:43
_T * smart_append(_T *_pString, const typename _T::value_type _ItemToAdd)
appends character _ItemToAdd to the string _pString if there is no such suffix on the end of _pString...
Definition: MiscUtils.h:125
char operator()(char c) const
Definition: MiscUtils.h:214
Definition: BoostHelper.h:14