libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Functions
ASSA::Utils Namespace Reference

Functions

void split (const char *text_, std::vector< std::string > &vec_)
 Split character string into tokens separated by the whitespace character (blank, tab, newline, formfeed, and carriage return).
int split_pair (const string &text_, char sep_, string &lhs_, string &rhs_)
 Split input string into two parts separated by the separator character.
int ltrim (std::string &text_, const std::string &delim_)
 Trim string from the beginning to the left of the delimiter.
int rtrim (std::string &text_, const std::string &delim_)
 Trim string from the delimiter to the end of the string.
void trim_sides (std::string &text_)
 Trim white spaces and tabs from the beginning and the end of the text string.
void find_and_replace_char (std::string &text_, char src_, char dest_)
 Find and relpace all instances of src_ character with dest_ character in a string text_.
std::string strenv (const char *in_)
 Expand the passed string in_ by substituting environment variable names for their values.
std::string get_cwd_name ()
 Get current working directory.
void sleep_for_seconds (long secs_to_sleep_)
 Portable sleep.

Function Documentation

void ASSA::Utils::find_and_replace_char ( std::string &  text_,
char  src_,
char  dest_ 
)

Find and relpace all instances of src_ character with dest_ character in a string text_.

Parameters
text_String to modify
src_Find the character
dest_Character to replace with

Definition at line 110 of file CommonUtils.cpp.

Referenced by ASSA::CmdLineOpts::parse_config_file().

{
string::iterator pos = text_.begin ();
while (pos != text_.end ()) {
if ((*pos) == src_) {
(*pos) = dest_;
}
pos++;
}
}
std::string ASSA::Utils::get_cwd_name ( void  )

Get current working directory.

Returns
the current working directory on success, and an empty string on failure with errno set to indicate the error occured.

Definition at line 204 of file CommonUtils.cpp.

{
std::string ret;
int size = 256;
char* chr_ptr = 0;
while (true) {
chr_ptr = new char [size];
if (::getcwd (chr_ptr, size-1) != NULL) {
ret = chr_ptr;
delete [] chr_ptr;
return ret;
}
if (errno != ERANGE) {
return ret; // Any error other then a path name too long
// for the buffer is bad news.
}
delete [] chr_ptr;
size += 256;
}
}
int ASSA::Utils::ltrim ( std::string &  text_,
const std::string &  delim_ 
)

Trim string from the beginning to the left of the delimiter.

Delimiter is removed as well.

Parameters
text_String to modify
delim_Delimiter character
Returns
0 on success; -1 on error

Definition at line 67 of file CommonUtils.cpp.

Referenced by ASSA::IniFile::trim_section_name().

{
std::string::size_type idx;
idx = text_.find_first_of (delim_);
if (idx != std::string::npos) {
text_.replace (0, idx+1, "");
return 0;
}
return -1;
}
int ASSA::Utils::rtrim ( std::string &  text_,
const std::string &  delim_ 
)

Trim string from the delimiter to the end of the string.

Delimiter is removed as well.

Parameters
text_String to modify
delim_Delimiter character
Returns
0 on success; -1 on error

Definition at line 80 of file CommonUtils.cpp.

Referenced by ASSA::IniFile::trim_section_name().

{
std::string::size_type idx;
idx = text_.find_last_of (delim_);
if (idx != std::string::npos) {
text_.replace (idx, text_.size (), "");
return 0;
}
return -1;
}
void ASSA::Utils::sleep_for_seconds ( long  secs_to_sleep_)
inline

Portable sleep.

Parameters
secs_to_sleep_Number of seconds to sleep

Definition at line 142 of file CommonUtils.h.

{
#if defined (WIN32)
SleepEx (secs_to_sleep_ * 1000, FALSE);
#else
::sleep (secs_to_sleep_);
#endif
}
void ASSA::Utils::split ( const char *  text_,
std::vector< std::string > &  vec_ 
)

Split character string into tokens separated by the whitespace character (blank, tab, newline, formfeed, and carriage return).

The vec_ vector is emptied out prior parsing string text_.

Parameters
text_string of tokens to split
vec_vector with tokens extracted from the string str_

Definition at line 34 of file CommonUtils.cpp.

{
std::istringstream input (src_);
vec_.erase (vec_.begin (), vec_.end ());
std::string token;
while (input >> token) {
vec_.push_back (token);
}
}
int ASSA::Utils::split_pair ( const string &  text_,
char  sep_,
string &  lhs_,
string &  rhs_ 
)

Split input string into two parts separated by the separator character.

Parameters
text_Input string to split
sep_Separator character
lhs_Return left-hand side of the input string
rhs_Return right-hand side of the input string
Returns
0 on success; -1 if separator character was not found.

Definition at line 47 of file CommonUtils.cpp.

Referenced by ASSA::IniFile::load().

{
int pos = 0;
if ((pos = text_.find (sep_)) == string::npos) {
return -1;
}
lhs_ = text_.substr (0, pos);
rhs_ = text_.substr (pos+1, text_.size ());
pos = rhs_.size () -1;
if (rhs_[0] == '"' || rhs_[0] == '\'') {
rhs_[0] = ' ';
}
if (rhs_[pos] == '"' || rhs_[pos] == '\'') {
rhs_[pos] = ' ';
}
return 0;
}
std::string ASSA::Utils::strenv ( const char *  in_)

Expand the passed string in_ by substituting environment variable names for their values.

Environment variables must be preceeded by dollar sign and optionally enclosed in parentheses: $ENV_NAME, or , or ${ENV_NAME}. $HOME is equivalent to '~' or '~username'. If later is used, "username" is looked up in the password file.

Definition at line 123 of file CommonUtils.cpp.

Referenced by ASSA::GenServer::init(), ASSA::GenServer::init_internals(), and ASSA::PidFileLock::lock().

{
char b [1024];
char* ret = b;
char* r = ret;
if (*in == '~') { // '~' OR '~/'
if ( *(in+1) == 0 || *(in+1) == '/' ) {
in++;
strcpy (ret, getenv ("HOME") ? getenv ("HOME") : "");
r += strlen (ret);
}
else {
in++;
char lname [256];
char* lp = lname;
const char* sp = strchr (in, '/'); // find first '/' in string
if ( sp ) {
while (in != sp) *lp++ = *in++;
*lp = 0;
}
else {
while (*in) *lp++ = *in++;
*lp = 0;
}
#ifdef WIN32
strcpy (ret, home_dir);
r += strlen (ret);
#else
// lookup user's home directory in /etc/passwd file
struct passwd* p = getpwnam (lname);
if ( p ) {
strcpy (ret, p->pw_dir ? p->pw_dir : "");
r += strlen (ret);
}
#endif
}
}
while (*in) {
if (*in == '$') {
char varname [80];
if (*++in == '(') {
++in;
const char *end = strchr (in,')');
if (!end)
break;
strncpy (varname, in, end-in);
varname [end-in] = '\0';
in = end+1;
}
else if (*in == '{') {
const char *end = strchr (in,'}');
if (!end)
break;
strncpy (varname, in, end-in);
varname [end-in] = '\0';
in = end+1;
}
else {
char* vp = varname;
while (isalnum (*in) || *in == '_' ) { // letter OR digit
*vp++ = *in++;
}
*vp = '\0';
}
char* ep = ::getenv (varname);
while (ep && *ep) *r++ = *ep++;
continue;
}
else if (*in == '\\' && *(in+1)) {
in++; // allow escaped dollar signs
}
*r++ = *in++;
}
*r = '\0';
return ret;
}
void ASSA::Utils::trim_sides ( std::string &  text_)

Trim white spaces and tabs from the beginning and the end of the text string.

Parameters
text_String to trim

Definition at line 93 of file CommonUtils.cpp.

Referenced by ASSA::IniFile::load().

{
std::string::size_type idx;
idx = text_.find_first_not_of (" \t");
if (idx != std::string::npos) {
text_.replace (0, idx, "");
}
idx = text_.find_last_not_of (" \t");
if (idx != std::string::npos) {
text_.replace (idx + 1, text_.size (), "");
}
}