5 #ifndef _DDS_SYSHELPER_H_ 6 #define _DDS_SYSHELPER_H_ 14 #include <sys/syscall.h> 15 #include <sys/types.h> 18 #include <sys/sysctl.h> 24 #include <boost/asio.hpp> 28 #define HOST_NAME_MAX 64 47 passwd* pwd(getpwuid(geteuid()));
48 *_RetVal = pwd ? std::string(pwd->pw_name) :
"";
61 passwd* pwd = getpwuid(_uid);
62 *_RetVal = pwd ? std::string(pwd->pw_dir) :
"";
70 inline void get_homedir(
const char* _UName, std::string* _RetVal)
75 passwd* pwd = getpwnam(_UName);
76 *_RetVal = pwd ? std::string(pwd->pw_dir) :
"";
97 if (
nullptr == _Path || _Path->empty())
101 std::string path(*_Path);
105 std::string path(*_Path);
113 path.erase(path.begin(), path.begin() + 2);
120 typename _T::size_type p = path.find(
_T(
"/" ));
123 const std::string uname = path.substr(1, p - 1);
124 std::string home_dir;
126 path.erase(path.begin(), path.begin() + p);
127 path = home_dir + path;
133 typename _T::size_type p_begin = _Path->find(
_T(
"$" ));
134 if (_T::npos == p_begin)
137 char resolved_path[PATH_MAX];
138 char* res = realpath(_Path->c_str(), resolved_path);
142 std::string::iterator it = _Path->end() - 1;
143 bool trailing_slash = (*it ==
'/');
144 *_Path = resolved_path;
154 typename _T::size_type p_end = _Path->find(
_T(
"/" ), p_begin);
155 if (_T::npos == p_end)
156 p_end = _Path->size();
158 const _T env_var(_Path->substr(p_begin, p_end - p_begin));
160 LPCTSTR szvar(getenv(env_var.c_str()));
163 const _T var_val(szvar);
167 replace(_Path,
_T(
"$" ) + env_var, var_val);
190 *_RetVal = boost::asio::ip::host_name();
205 v.th = pthread_self();
208 return syscall(__NR_gettid);
255 pthread_mutex_init(&m, 0);
260 pthread_mutex_lock(&m);
265 pthread_mutex_unlock(&m);
300 extern "C" char*
__cxa_demangle(
const char* mangled,
char* buf,
size_t* len,
int* status);
301 inline std::string
demangle(
const std::type_info& ti)
309 inline void get_env(
const std::string& _EnvVarName, std::string* _RetVal)
314 char* szBuf(getenv(_EnvVarName.c_str()));
316 _RetVal->assign(szBuf);
327 const int fd(::open(_FileName.c_str(), O_RDONLY));
329 throw system_error(
"Can't get file size of \"" + _FileName +
"\"");
332 const int ret(::fstat(fd, &fs));
336 throw system_error(
"Can't get file size of \"" + _FileName +
"\"");
365 #ifdef __APPLE__ // FreeBSD, MacOS X, NetBSD, OpenBSD, etc. 367 size_t len =
sizeof(numCPU);
371 mib[1] = HW_AVAILCPU;
374 sysctl(mib, 2, &numCPU, &len, NULL, 0);
379 sysctl(mib, 2, &numCPU, &len, NULL, 0);
386 #elif __linux // Linux, Solaris, & AIX (per comments) 387 numCPU = sysconf(_SC_NPROCESSORS_ONLN);
_T & trim_left(_T *_pString, const typename _T::value_type &_chWhat)
trims leading characters from the string.
Definition: MiscUtils.h:159
void get_cuser_name(std::string *_RetVal)
The function returns current user name.
Definition: SysHelper.h:42
void get_homedir(uid_t _uid, std::string *_RetVal)
The function returns home directory path of the given user.
Definition: SysHelper.h:56
bool file_exists(const std::string &_FileName)
Definition: SysHelper.h:345
~smart_mutex()
Definition: SysHelper.h:283
const char * LPCTSTR
Long Pointer to a Constant null-Terminated String.
Definition: def.h:66
CMutex()
Definition: SysHelper.h:253
void Unlock()
Definition: SysHelper.h:263
off_t file_size(const std::string &_FileName)
The function file_size() retrieves file size of a given file.
Definition: SysHelper.h:325
std::string demangle(const std::type_info &ti)
Definition: SysHelper.h:301
char * __cxa_demangle(const char *mangled, char *buf, size_t *len, int *status)
demangling C++ symbols.
size_t getNCores()
the function returns a number of available CPU cores
Definition: SysHelper.h:362
smart_mutex(CMutex &_mutex)
Definition: SysHelper.h:278
void Lock()
Definition: SysHelper.h:258
unsigned long gettid()
A system helper, which helps to get a Thread ID of the current thread.
Definition: SysHelper.h:197
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:82
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:76
_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 get_env(const std::string &_EnvVarName, std::string *_RetVal)
Definition: SysHelper.h:309
void smart_path(_T *_Path)
The function extends any environment variable found in the give path to its value.
Definition: SysHelper.h:95
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:28
void get_cuser_homedir(std::string *_RetVal)
The function returns home directory path of the current user.
Definition: SysHelper.h:83
void get_hostname(std::string *_RetVal)
The function is used to access the host name (with FCDN) of the current processor.
Definition: SysHelper.h:185
A smart CMutex helper.
Definition: SysHelper.h:275
_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
A Mutex wrapper. Based on pthread calls.
Definition: SysHelper.h:250
Definition: BoostHelper.h:14