DDS  ver. 1.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 MISCUTILS_H
6 #define MISCUTILS_H
7 
8 // STD
9 #include <algorithm>
10 #include <iostream>
11 
17 namespace MiscCommon
18 {
25  {
26  protected:
28  {
29  }
31  {
32  }
33 
34  private:
35  NONCopyable(const NONCopyable&);
36  const NONCopyable& operator=(const NONCopyable&);
37  };
38 
39  class NullType
40  {
41  };
48  {
49  public:
51  : m_unset(false)
52  {
53  }
54  auto_setenv(const std::string& _VarName, const std::string& _NewValue)
55  : m_unset(false)
56  {
57  m_sVarName = _VarName;
58  m_sNewValue = _NewValue;
59 
60  char* chTmp = getenv(m_sVarName.c_str());
61  if (chTmp)
62  m_sOldValue = chTmp;
63  else
64  m_unset = true;
65  // TODO: check error code
66  setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
67  }
69  {
70  unset();
71  }
72 
73  public:
74  void set(const std::string& _VarName, const std::string& _NewValue)
75  {
76  unset();
77 
78  m_sVarName = _VarName;
79  m_sNewValue = _NewValue;
80 
81  char* chTmp = getenv(m_sVarName.c_str());
82  if (chTmp)
83  m_sOldValue = chTmp;
84  else
85  m_unset = true;
86  // TODO: check error code
87  setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
88  }
89  void unset()
90  {
91  if (m_unset)
92  {
93  m_unset = false;
94  unsetenv(m_sVarName.c_str());
95  return;
96  }
97 
98  if (!m_sVarName.empty())
99  {
100  m_sVarName.clear();
101  setenv(m_sVarName.c_str(), m_sOldValue.c_str(), 1);
102  }
103  }
104 
105  private:
106  std::string m_sVarName;
107  std::string m_sNewValue;
108  std::string m_sOldValue;
109  bool m_unset;
110  };
111 
120  template <typename _T>
121  _T* smart_append(_T* _pString, const typename _T::value_type _ItemToAdd)
122  {
123  if (!_pString)
124  return _pString;
125 
126  if (_pString->empty() || (*_pString)[_pString->size() - 1] != _ItemToAdd)
127  _pString->push_back(_ItemToAdd);
128 
129  return _pString;
130  }
131 
140  template <typename _T>
141  _T& trim_right(_T* _pString, const typename _T::value_type& _chWhat)
142  {
143  return _pString->erase(_pString->find_last_not_of(_chWhat) + 1);
144  }
145 
154  template <typename _T>
155  _T& trim_left(_T* _pString, const typename _T::value_type& _chWhat)
156  {
157  return _pString->erase(0, _pString->find_first_not_of(_chWhat));
158  }
159 
168  template <typename _T>
169  _T& trim(_T* _pString, const typename _T::value_type& _chWhat)
170  {
171  return trim_right(&trim_left(_pString, _chWhat), _chWhat);
172  }
173 
183  template <typename _T>
184  _T& replace(_T* _pString, const _T& _what, const _T& _with)
185  {
186  typename _T::size_type pos = 0;
187  typename _T::size_type withLen = _with.length();
188  typename _T::size_type whatLen = _what.length();
189  while ((pos = _pString->find(_what, pos)) != _T::npos)
190  {
191  _pString->replace(pos, _what.length(), _with);
192  if (withLen > whatLen)
193  pos += withLen - whatLen + 1;
194  }
195  return (*_pString);
196  }
197 
198  // HACK: because of the bug in gcc 3.3 we need to use this nasty ToLower and ToUpper instead of direct calls of
199  // tolower (tolower.. is inline in this version
200  // of std lib)...
201  struct ToLower
202  {
203  char operator()(char c) const
204  {
205  return std::tolower(c);
206  }
207  };
208  struct ToUpper
209  {
210  char operator()(char c) const
211  {
212  return std::toupper(c);
213  }
214  };
215  struct IsDigit : std::unary_function<int, int>
216  {
217  int operator()(int c) const
218  {
219  return std::isdigit(c);
220  }
221  };
222 
230  template <typename _T>
231  _T& to_upper(_T& _str)
232  {
233  std::transform(_str.begin(), _str.end(), _str.begin(), ToUpper());
234  return _str;
235  }
236 
244  template <typename _T>
245  _T& to_lower(_T& _str)
246  {
247  std::transform(_str.begin(), _str.end(), _str.begin(), ToLower());
248  return _str;
249  }
250 };
251 #endif
_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:121
_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:184
_T & trim_left(_T *_pString, const typename _T::value_type &_chWhat)
trims leading characters from the string.
Definition: MiscUtils.h:155
A helper class. Helps to automatically track environment variables.
Definition: MiscUtils.h:47
Definition: MiscUtils.h:208
_T & to_lower(_T &_str)
convert string to lower case.
Definition: MiscUtils.h:245
_T & trim_right(_T *_pString, const typename _T::value_type &_chWhat)
trims trailing characters from the string.
Definition: MiscUtils.h:141
auto_setenv()
Definition: MiscUtils.h:50
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:24
_T & to_upper(_T &_str)
convert string to upper case.
Definition: MiscUtils.h:231
char operator()(char c) const
Definition: MiscUtils.h:210
~auto_setenv()
Definition: MiscUtils.h:68
~NONCopyable()
Definition: MiscUtils.h:30
auto_setenv(const std::string &_VarName, const std::string &_NewValue)
Definition: MiscUtils.h:54
_T & trim(_T *_pString, const typename _T::value_type &_chWhat)
trims trailing and leading characters from the string.
Definition: MiscUtils.h:169
Definition: MiscUtils.h:39
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
NONCopyable()
Definition: MiscUtils.h:27
int operator()(int c) const
Definition: MiscUtils.h:217
Definition: MiscUtils.h:215
void unset()
Definition: MiscUtils.h:89
Definition: MiscUtils.h:201
char operator()(char c) const
Definition: MiscUtils.h:203
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21