/home/vlg/develop/libASSA/libassa/assa/CommonUtils.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 // $Id: CommonUtils.cpp,v 1.10 2006/07/20 02:30:53 vlg Exp $
00004 //------------------------------------------------------------------------------
00005 //                          CommonUtils.cpp
00006 //------------------------------------------------------------------------------
00007 //  Copyright (C) 1997-2003  Vladislav Grinchenko 
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Library General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 
00015 #include <iomanip>
00016 #include <string.h>
00017 
00018 #include <stdlib.h>
00019 
00020 #include <ctype.h>
00021 #include <unistd.h>             // getcwd(3)
00022 
00023 #ifdef WIN32
00024 static char *home_dir = ".";    /* we feel (no|every)where at home */
00025 #else
00026 #    include <pwd.h>
00027 #endif
00028 
00029 #include "assa/Logger.h"
00030 #include "assa/CommonUtils.h"
00031 
00032 void
00033 ASSA::Utils::
00034 split (const char* src_, std::vector<std::string>& vec_)
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 }
00044 
00045 int
00046 ASSA::Utils::
00047 split_pair (const string& text_, char sep_, string& lhs_, string& rhs_)
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 }
00064 
00065 int
00066 ASSA::Utils::
00067 ltrim (std::string& text_, const std::string& delim_)
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 }
00077 
00078 int
00079 ASSA::Utils::
00080 rtrim (std::string& text_, const std::string& delim_)
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 }
00090 
00091 void
00092 ASSA::Utils::
00093 trim_sides (std::string& text_)
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 }
00107 
00108 void
00109 ASSA::Utils::
00110 find_and_replace_char (std::string& text_, char src_, char dest_)
00111 {
00112     string::iterator pos = text_.begin ();
00113     while (pos != text_.end ()) {
00114         if ((*pos) == src_) {
00115             (*pos) = dest_;
00116         }
00117         pos++;
00118     }
00119 }
00120 
00121 std::string
00122 ASSA::Utils::
00123 strenv (const char* in)
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 } 
00201 
00202 std::string
00203 ASSA::Utils::
00204 get_cwd_name (void)
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 }

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