Loading...
Searching...
No Matches
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

## find_and_replace_char()

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 109 of file CommonUtils.cpp.

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

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

## get_cwd_name()

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 203 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;
}
}

## ltrim()

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 66 of file CommonUtils.cpp.

{
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;
}

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

## rtrim()

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 79 of file CommonUtils.cpp.

{
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;
}

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

## sleep_for_seconds()

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
}

## split()

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 33 of file CommonUtils.cpp.

{
std::istringstream input (src_);
vec_.erase (vec_.begin (), vec_.end ());
std::string token;
while (input >> token) {
vec_.push_back (token);
}
}

## split_pair()

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 46 of file CommonUtils.cpp.

{
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;
}

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

## strenv()

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 122 of file CommonUtils.cpp.

{
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;
}

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

## trim_sides()

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 92 of file CommonUtils.cpp.

{
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 (), "");
}
}

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