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.

00111 {
00112     string::iterator pos = text_.begin ();
00113     while (pos != text_.end ()) {
00114         if ((*pos) == src_) {
00115             (*pos) = dest_;
00116         }
00117         pos++;
00118     }
00119 }

std::string ASSA::Utils::get_cwd_name  ) 
 

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.

00205 {
00206     std::string ret;
00207     int size = 256;
00208     char* chr_ptr = 0;
00209 
00210     while (true) {
00211     chr_ptr = new char [size];
00212     if (::getcwd (chr_ptr, size-1) != NULL) {
00213         ret = chr_ptr;
00214         delete [] chr_ptr;
00215         return ret;
00216     }
00217     if (errno != ERANGE) {
00218         return ret;     // Any error other then a path name too long
00219         // for the buffer is bad news.
00220     }
00221     delete [] chr_ptr;
00222     size += 256;
00223     }
00224 }

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().

00068 {
00069     std::string::size_type idx;
00070     idx = text_.find_first_of (delim_);
00071     if (idx != std::string::npos) {
00072         text_.replace (0, idx+1, "");
00073         return 0;
00074     }
00075     return -1;
00076 }

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().

00081 {
00082     std::string::size_type idx;
00083     idx = text_.find_last_of (delim_);
00084     if (idx != std::string::npos) {
00085         text_.replace (idx, text_.size (), "");
00086         return 0;
00087     }
00088     return -1;
00089 }

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 141 of file CommonUtils.h.

00142     {
00143 #if defined (WIN32)
00144         SleepEx (secs_to_sleep_ * 1000, FALSE);
00145 #else
00146         ::sleep (secs_to_sleep_);
00147 #endif
00148     }

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.

00035 {
00036     std::istringstream input (src_);
00037     vec_.erase (vec_.begin (), vec_.end ());
00038 
00039     std::string token;
00040     while (input >> token) {
00041         vec_.push_back (token);
00042     }
00043 }

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.

00048 {
00049     int pos = 0;
00050     if ((pos = text_.find (sep_)) == string::npos) {
00051         return -1;
00052     }
00053     lhs_ = text_.substr (0, pos);
00054     rhs_ = text_.substr (pos+1, text_.size ());
00055     pos = rhs_.size () -1;
00056     if (rhs_[0] == '"' || rhs_[0] == '\'') {
00057         rhs_[0] = ' ';
00058     }
00059     if (rhs_[pos] == '"' || rhs_[pos] == '\'') {
00060         rhs_[pos] = ' ';
00061     }
00062     return 0;
00063 }

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_internals(), and ASSA::PidFileLock::lock().

00124 {
00125     char b [1024];
00126     char* ret = b;
00127     char* r = ret;
00128 
00129     if (*in == '~') {           //  '~' OR '~/'
00130         if ( *(in+1) == 0 || *(in+1) == '/' ) {
00131             in++;
00132             strcpy (ret, getenv ("HOME") ? getenv ("HOME") : "");
00133             r += strlen (ret);
00134         }
00135         else {
00136             in++;
00137             char lname [256];
00138             char* lp = lname;
00139             const char* sp = strchr (in, '/'); // find first '/' in string
00140             if ( sp ) {
00141                 while (in != sp) *lp++ = *in++;
00142                 *lp = 0;
00143             }
00144             else {
00145                 while (*in) *lp++ = *in++;
00146                 *lp = 0;
00147             }
00148 #ifdef WIN32
00149             strcpy (ret, home_dir);
00150             r += strlen (ret);
00151 #else
00152             // lookup user's home directory in /etc/passwd file
00153             struct passwd* p = getpwnam (lname); 
00154             if ( p ) {
00155                 strcpy (ret, p->pw_dir ? p->pw_dir : "");
00156                 r += strlen (ret);
00157             }
00158 #endif
00159         }
00160     }
00161 
00162     while (*in) {
00163         if (*in == '$') {
00164             char varname [80];
00165             if (*++in == '(') {       
00166                 ++in;
00167                 const char *end = strchr (in,')');
00168                 if (!end)
00169                     break;
00170                 strncpy (varname, in, end-in);
00171                 varname [end-in] = '\0';
00172                 in = end+1;
00173             }
00174             else if (*in == '{') {
00175                 const char *end = strchr (in,'}');
00176                 if (!end)
00177                     break;
00178                 strncpy (varname, in, end-in);
00179                 varname [end-in] = '\0';
00180                 in = end+1;
00181             }
00182             else {       
00183                 char* vp = varname;
00184                 while (isalnum (*in) || *in == '_' ) { // letter OR digit
00185                     *vp++ = *in++;
00186                 }
00187                 *vp = '\0';
00188             }
00189             char* ep = ::getenv (varname);
00190             while (ep && *ep) *r++ = *ep++;
00191             continue;
00192         }
00193         else if (*in == '\\' && *(in+1)) {
00194             in++;  // allow escaped dollar signs
00195         }
00196         *r++ = *in++;
00197     }
00198     *r = '\0';
00199     return ret;
00200 } 

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.

00094 {
00095     std::string::size_type idx;
00096 
00097     idx = text_.find_first_not_of (" \t");
00098     if (idx != std::string::npos) {
00099         text_.replace (0, idx, "");
00100     }
00101 
00102     idx = text_.find_last_not_of (" \t");
00103     if (idx != std::string::npos) {
00104         text_.replace (idx + 1, text_.size (), "");
00105     }
00106 }


Generated on Sun Aug 13 15:08:22 2006 for libassa by  doxygen 1.4.6