14 #include <sys/syscall.h> 15 #include <sys/types.h> 18 #include <sys/sysctl.h> 26 #define HOST_NAME_MAX 64 45 passwd* pwd(getpwuid(geteuid()));
46 *_RetVal = pwd ? std::string(pwd->pw_name) :
"";
59 passwd* pwd = getpwuid(_uid);
60 *_RetVal = pwd ? std::string(pwd->pw_dir) :
"";
68 inline void get_homedir(
const char* _UName, std::string* _RetVal)
73 passwd* pwd = getpwnam(_UName);
74 *_RetVal = pwd ? std::string(pwd->pw_dir) :
"";
95 if (
nullptr == _Path || _Path->empty())
99 std::string path(*_Path);
103 std::string path(*_Path);
111 path.erase(path.begin(), path.begin() + 2);
118 typename _T::size_type p = path.find(
_T(
"/" ));
121 const std::string uname = path.substr(1, p - 1);
122 std::string home_dir;
124 path.erase(path.begin(), path.begin() + p);
125 path = home_dir + path;
131 typename _T::size_type p_begin = _Path->find(
_T(
"$" ));
132 if (_T::npos == p_begin)
135 char resolved_path[PATH_MAX];
136 char* res = realpath(_Path->c_str(), resolved_path);
140 std::string::iterator it = _Path->end() - 1;
141 bool trailing_slash = (*it ==
'/');
142 *_Path = resolved_path;
152 typename _T::size_type p_end = _Path->find(
_T(
"/" ), p_begin);
153 if (_T::npos == p_end)
154 p_end = _Path->size();
156 const _T env_var(_Path->substr(p_begin, p_end - p_begin));
158 LPCTSTR szvar(getenv(env_var.c_str()));
161 const _T var_val(szvar);
165 replace(_Path,
_T(
"$" ) + env_var, var_val);
190 gethostname(&Buf[0], Buf.capacity());
193 hostent* h = gethostbyname(std::string(&Buf[0]).c_str());
197 *_RetVal = h->h_name;
211 v.th = pthread_self();
214 return syscall(__NR_gettid);
261 pthread_mutex_init(&m, 0);
266 pthread_mutex_lock(&m);
271 pthread_mutex_unlock(&m);
306 extern "C" char*
__cxa_demangle(
const char* mangled,
char* buf,
size_t* len,
int* status);
307 inline std::string
demangle(
const std::type_info& ti)
315 inline void get_env(
const std::string& _EnvVarName, std::string* _RetVal)
320 char* szBuf(getenv(_EnvVarName.c_str()));
322 _RetVal->assign(szBuf);
333 const int fd(::open(_FileName.c_str(), O_RDONLY));
335 throw system_error(
"Can't get file size of \"" + _FileName +
"\"");
338 const int ret(::fstat(fd, &fs));
342 throw system_error(
"Can't get file size of \"" + _FileName +
"\"");
371 #ifdef __APPLE__ // FreeBSD, MacOS X, NetBSD, OpenBSD, etc. 373 size_t len =
sizeof(numCPU);
377 mib[1] = HW_AVAILCPU;
380 sysctl(mib, 2, &numCPU, &len, NULL, 0);
385 sysctl(mib, 2, &numCPU, &len, NULL, 0);
392 #elif __linux // Linux, Solaris, & AIX (per comments) 393 numCPU = sysconf(_SC_NPROCESSORS_ONLN);
_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
void get_cuser_name(std::string *_RetVal)
The function returns current user name.
Definition: SysHelper.h:40
CMutex()
Definition: SysHelper.h:259
~smart_mutex()
Definition: SysHelper.h:289
std::string demangle(const std::type_info &ti)
Definition: SysHelper.h:307
char * __cxa_demangle(const char *mangled, char *buf, size_t *len, int *status)
demangling C++ symbols.
smart_mutex(CMutex &_mutex)
Definition: SysHelper.h:284
The system_error exception class retrieves a string, which represent the last error.
Definition: ErrorCode.h:77
void Unlock()
Definition: SysHelper.h:269
A smart CMutex helper.
Definition: SysHelper.h:281
off_t file_size(const std::string &_FileName)
The function file_size() retrieves file size of a given file.
Definition: SysHelper.h:331
Class which makes child to be non-copyable object.
Definition: MiscUtils.h:24
bool file_exists(const std::string &_FileName)
Definition: SysHelper.h:351
void get_homedir(uid_t _uid, std::string *_RetVal)
The function returns home directory path of the given user.
Definition: SysHelper.h:54
void get_env(const std::string &_EnvVarName, std::string *_RetVal)
Definition: SysHelper.h:315
unsigned long gettid()
A system helper, which helps to get a Thread ID of the current thread.
Definition: SysHelper.h:204
std::vector< char > CHARVector_t
An STL vector of char(s).
Definition: def.h:121
#define HOST_NAME_MAX
Definition: SysHelper.h:26
void get_hostname(std::string *_RetVal)
The function is used to access the host name (with FCDN) of the current processor.
Definition: SysHelper.h:183
void get_cuser_homedir(std::string *_RetVal)
The function returns home directory path of the current user.
Definition: SysHelper.h:81
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
size_t getNCores()
the function returns a number of available CPU cores
Definition: SysHelper.h:368
void smart_path(_T *_Path)
The function extends any environment variable found in the give path to its value.
Definition: SysHelper.h:93
const char * LPCTSTR
Long Pointer to a Constant null-Terminated String.
Definition: def.h:69
A Mutex wrapper. Based on pthread calls.
Definition: SysHelper.h:256
void Lock()
Definition: SysHelper.h:264
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21