13 #include <boost/filesystem.hpp> 14 #include <boost/process.hpp> 58 auto_setenv(
const std::string& _VarName,
const std::string& _NewValue)
61 m_sVarName = _VarName;
62 m_sNewValue = _NewValue;
64 char* chTmp = getenv(m_sVarName.c_str());
70 setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
78 void set(
const std::string& _VarName,
const std::string& _NewValue)
82 m_sVarName = _VarName;
83 m_sNewValue = _NewValue;
85 char* chTmp = getenv(m_sVarName.c_str());
91 setenv(m_sVarName.c_str(), m_sNewValue.c_str(), 1);
98 unsetenv(m_sVarName.c_str());
102 if (!m_sVarName.empty())
105 setenv(m_sVarName.c_str(), m_sOldValue.c_str(), 1);
110 std::string m_sVarName;
111 std::string m_sNewValue;
112 std::string m_sOldValue;
124 template <
typename _T>
130 if (_pString->empty() || (*_pString)[_pString->size() - 1] != _ItemToAdd)
131 _pString->push_back(_ItemToAdd);
144 template <
typename _T>
147 return _pString->erase(_pString->find_last_not_of(_chWhat) + 1);
158 template <
typename _T>
161 return _pString->erase(0, _pString->find_first_not_of(_chWhat));
172 template <
typename _T>
173 _T&
trim(
_T* _pString,
const typename _T::value_type& _chWhat)
187 template <
typename _T>
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)
195 _pString->replace(pos, _what.length(), _with);
196 if (withLen > whatLen)
197 pos += withLen - whatLen + 1;
209 return std::tolower(c);
216 return std::toupper(c);
219 struct IsDigit : std::unary_function<int, int>
223 return std::isdigit(c);
234 template <
typename _T>
237 std::transform(_str.begin(), _str.end(), _str.begin(),
ToUpper());
248 template <
typename _T>
251 std::transform(_str.begin(), _str.end(), _str.begin(),
ToLower());
256 const std::string& _exePrefix,
257 std::string& _filePath,
258 std::string& _filename,
259 std::string& _cmdStr)
265 boost::process::posix::sighandler_t old_sig = signal(SIGCHLD, SIG_IGN);
266 signal(SIGCHLD, SIG_DFL);
270 int err = wordexp(_exeStr.c_str(), &result, 0);
273 signal(SIGCHLD, old_sig);
279 _filePath = result.we_wordv[0];
281 boost::filesystem::path exeFilePath(_filePath);
283 if (!exeFilePath.is_absolute() && exeFilePath.has_parent_path())
284 throw std::runtime_error(
285 "Relative paths are not supported: " + _filePath +
286 ". Use either absolute path or executable name which will be searched in PATH.");
288 _filename = exeFilePath.filename().generic_string();
291 if (!exeFilePath.is_absolute())
293 boost::filesystem::path exePath = boost::process::search_path(_filename);
294 _filePath = exePath.generic_string();
297 _cmdStr = (_exePrefix.empty()) ? _filePath : (_exePrefix + _filename);
298 for (
size_t i = 1; i < result.we_wordc; ++i)
301 _cmdStr += result.we_wordv[i];
310 throw std::runtime_error(
"memory error occurred while processing the user's executable path: " +
315 throw std::runtime_error(
"Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, } in " +
320 throw std::runtime_error(
321 "An undefined shell variable was referenced, and the WRDE_UNDEF flag told us to " 322 "consider this an error in " +
327 throw std::runtime_error(
328 "Command substitution occurred, and the WRDE_NOCMD flag told us to consider this an error in " +
332 throw std::runtime_error(
"Shell syntax error, such as unbalanced parentheses or unmatched quotes in " +
337 throw std::runtime_error(
"failed to process the user's executable path: " + _exeStr);
_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
_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 parseExe(const std::string &_exeStr, const std::string &_exePrefix, std::string &_filePath, std::string &_filename, std::string &_cmdStr)
Definition: MiscUtils.h:255
_T & trim_left(_T *_pString, const typename _T::value_type &_chWhat)
trims leading characters from the string.
Definition: MiscUtils.h:159
A helper class. Helps to automatically track environment variables.
Definition: MiscUtils.h:51
Definition: MiscUtils.h:212
_T & to_lower(_T &_str)
convert string to lower case.
Definition: MiscUtils.h:249
_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:54
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:28
_T & to_upper(_T &_str)
convert string to upper case.
Definition: MiscUtils.h:235
char operator()(char c) const
Definition: MiscUtils.h:214
~auto_setenv()
Definition: MiscUtils.h:72
~NONCopyable()
Definition: MiscUtils.h:34
auto_setenv(const std::string &_VarName, const std::string &_NewValue)
Definition: MiscUtils.h:58
_T & trim(_T *_pString, const typename _T::value_type &_chWhat)
trims trailing and leading characters from the string.
Definition: MiscUtils.h:173
Definition: MiscUtils.h:43
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
NONCopyable()
Definition: MiscUtils.h:31
int operator()(int c) const
Definition: MiscUtils.h:221
Definition: MiscUtils.h:219
void unset()
Definition: MiscUtils.h:93
void set(const std::string &_VarName, const std::string &_NewValue)
Definition: MiscUtils.h:78
Definition: MiscUtils.h:205
char operator()(char c) const
Definition: MiscUtils.h:207
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21