00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iomanip>
00016 #include <string.h>
00017
00018 #include <stdlib.h>
00019 #include <pwd.h>
00020 #include <ctype.h>
00021 #include <unistd.h>
00022
00023 #include "assa/Logger.h"
00024 #include "assa/CommonUtils.h"
00025
00026 void
00027 ASSA::Utils::
00028 split (const char* src_, std::vector<std::string>& vec_)
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 }
00038
00039 int
00040 ASSA::Utils::
00041 split_pair (const string& text_, char sep_, string& lhs_, string& rhs_)
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 }
00058
00059 int
00060 ASSA::Utils::
00061 ltrim (std::string& text_, const std::string& delim_)
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 }
00071
00072 int
00073 ASSA::Utils::
00074 rtrim (std::string& text_, const std::string& delim_)
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 }
00084
00085 void
00086 ASSA::Utils::
00087 trim_sides (std::string& text_)
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 }
00101
00102 void
00103 ASSA::Utils::
00104 find_and_replace_char (std::string& text_, char src_, char dest_)
00105 {
00106 string::iterator pos = text_.begin ();
00107 while (pos != text_.end ()) {
00108 if ((*pos) == src_) {
00109 (*pos) = dest_;
00110 }
00111 pos++;
00112 }
00113 }
00114
00115 std::string
00116 ASSA::Utils::
00117 strenv (const char* in)
00118 {
00119 char b [1024];
00120 char* ret = b;
00121 char* r = ret;
00122
00123 if (*in == '~') {
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, '/');
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
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 == '_' ) {
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++;
00184 }
00185 *r++ = *in++;
00186 }
00187 *r = '\0';
00188 return ret;
00189 }
00190
00191 std::string
00192 ASSA::Utils::
00193 get_cwd_name (void)
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;
00208
00209 }
00210 delete [] chr_ptr;
00211 size += 256;
00212 }
00213 }