Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CommonUtils.cpp File Reference

#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <pwd.h>
#include "assa/Logger.h"
#include "assa/CommonUtils.h"

Go to the source code of this file.

Functions

void ASSA::Utils::split (const char *src_, std::vector< std::string > &vec_)
int ASSA::Utils::split_pair (const string &text_, char sep_, string &lhs_, string &rhs_)
int ASSA::Utils::ltrim (std::string &text_, const std::string &delim_)
int ASSA::Utils::rtrim (std::string &text_, const std::string &delim_)
void ASSA::Utils::trim_sides (std::string &text_)
void ASSA::Utils::find_and_replace_char (std::string &text_, char src_, char dest_)
std::string ASSA::Utils::strenv (const char *in)
std::string ASSA::Utils::get_cwd_name (void)


Function Documentation

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

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 void   ) 
 

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_
 

Definition at line 67 of file CommonUtils.cpp.

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_
 

Definition at line 80 of file CommonUtils.cpp.

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::split const char *  src_,
std::vector< std::string > &  vec_
 

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_
 

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  ) 
 

Definition at line 123 of file CommonUtils.cpp.

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_  ) 
 

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 Mon Mar 26 22:47:35 2007 for libassa by  doxygen 1.4.2