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.


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

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

00105 {
00106     string::iterator pos = text_.begin ();
00107     while (pos != text_.end ()) {
00108         if ((*pos) == src_) {
00109             (*pos) = dest_;
00110         }
00111         pos++;
00112     }
00113 }

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

00194 {
00195     std::string ret;
00196     int size = 256;
00197     char* chr_ptr = 0;
00198 
00199     while (true) {
00200     chr_ptr = new char [size];
00201     if (::getcwd (chr_ptr, size-1) != NULL) {
00202         ret = chr_ptr;
00203         delete [] chr_ptr;
00204         return ret;
00205     }
00206     if (errno != ERANGE) {
00207         return ret;     // Any error other then a path name too long
00208         // for the buffer is bad news.
00209     }
00210     delete [] chr_ptr;
00211     size += 256;
00212     }
00213 }

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

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

00062 {
00063     std::string::size_type idx;
00064     idx = text_.find_first_of (delim_);
00065     if (idx != std::string::npos) {
00066         text_.replace (0, idx+1, "");
00067         return 0;
00068     }
00069     return -1;
00070 }

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

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

00075 {
00076     std::string::size_type idx;
00077     idx = text_.find_last_of (delim_);
00078     if (idx != std::string::npos) {
00079         text_.replace (idx, text_.size (), "");
00080         return 0;
00081     }
00082     return -1;
00083 }

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

00029 {
00030     std::istringstream input (src_);
00031     vec_.erase (vec_.begin (), vec_.end ());
00032 
00033     std::string token;
00034     while (input >> token) {
00035         vec_.push_back (token);
00036     }
00037 }

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

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

00042 {
00043     int pos = 0;
00044     if ((pos = text_.find (sep_)) == (size_t)(-1)) {
00045         return -1;
00046     }
00047     lhs_ = text_.substr (0, pos);
00048     rhs_ = text_.substr (pos+1, text_.size ());
00049     pos = rhs_.size () -1;
00050     if (rhs_[0] == '"' || rhs_[0] == '\'') {
00051         rhs_[0] = ' ';
00052     }
00053     if (rhs_[pos] == '"' || rhs_[pos] == '\'') {
00054         rhs_[pos] = ' ';
00055     }
00056     return 0;
00057 }

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

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

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

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

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

00088 {
00089     std::string::size_type idx;
00090 
00091     idx = text_.find_first_not_of (" \t");
00092     if (idx != std::string::npos) {
00093         text_.replace (0, idx, "");
00094     }
00095 
00096     idx = text_.find_last_not_of (" \t");
00097     if (idx != std::string::npos) {
00098         text_.replace (idx + 1, text_.size (), "");
00099     }
00100 }


Generated on Sat Dec 31 19:52:19 2005 for libassa by  doxygen 1.4.5