libassa  3.5.1
CommonUtils.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // $Id: CommonUtils.cpp,v 1.10 2006/07/20 02:30:53 vlg Exp $
4 //------------------------------------------------------------------------------
5 // CommonUtils.cpp
6 //------------------------------------------------------------------------------
7 // Copyright (C) 1997-2003 Vladislav Grinchenko
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 
15 #include <iomanip>
16 #include <string.h>
17 
18 #include <stdlib.h>
19 
20 #include <ctype.h>
21 #include <unistd.h> // getcwd(3)
22 
23 #ifdef WIN32
24 static char *home_dir = "."; /* we feel (no|every)where at home */
25 #else
26 # include <pwd.h>
27 #endif
28 
29 #include "assa/Logger.h"
30 #include "assa/CommonUtils.h"
31 
32 void
34 split (const char* src_, std::vector<std::string>& vec_)
35 {
36  std::istringstream input (src_);
37  vec_.erase (vec_.begin (), vec_.end ());
38 
39  std::string token;
40  while (input >> token) {
41  vec_.push_back (token);
42  }
43 }
44 
45 int
47 split_pair (const string& text_, char sep_, string& lhs_, string& rhs_)
48 {
49  int pos = 0;
50  if ((pos = text_.find (sep_)) == string::npos) {
51  return -1;
52  }
53  lhs_ = text_.substr (0, pos);
54  rhs_ = text_.substr (pos+1, text_.size ());
55  pos = rhs_.size () -1;
56  if (rhs_[0] == '"' || rhs_[0] == '\'') {
57  rhs_[0] = ' ';
58  }
59  if (rhs_[pos] == '"' || rhs_[pos] == '\'') {
60  rhs_[pos] = ' ';
61  }
62  return 0;
63 }
64 
65 int
67 ltrim (std::string& text_, const std::string& delim_)
68 {
69  std::string::size_type idx;
70  idx = text_.find_first_of (delim_);
71  if (idx != std::string::npos) {
72  text_.replace (0, idx+1, "");
73  return 0;
74  }
75  return -1;
76 }
77 
78 int
80 rtrim (std::string& text_, const std::string& delim_)
81 {
82  std::string::size_type idx;
83  idx = text_.find_last_of (delim_);
84  if (idx != std::string::npos) {
85  text_.replace (idx, text_.size (), "");
86  return 0;
87  }
88  return -1;
89 }
90 
91 void
93 trim_sides (std::string& text_)
94 {
95  std::string::size_type idx;
96 
97  idx = text_.find_first_not_of (" \t");
98  if (idx != std::string::npos) {
99  text_.replace (0, idx, "");
100  }
101 
102  idx = text_.find_last_not_of (" \t");
103  if (idx != std::string::npos) {
104  text_.replace (idx + 1, text_.size (), "");
105  }
106 }
107 
108 void
110 find_and_replace_char (std::string& text_, char src_, char dest_)
111 {
112  string::iterator pos = text_.begin ();
113  while (pos != text_.end ()) {
114  if ((*pos) == src_) {
115  (*pos) = dest_;
116  }
117  pos++;
118  }
119 }
120 
121 std::string
123 strenv (const char* in)
124 {
125  char b [1024];
126  char* ret = b;
127  char* r = ret;
128 
129  if (*in == '~') { // '~' OR '~/'
130  if ( *(in+1) == 0 || *(in+1) == '/' ) {
131  in++;
132  strcpy (ret, getenv ("HOME") ? getenv ("HOME") : "");
133  r += strlen (ret);
134  }
135  else {
136  in++;
137  char lname [256];
138  char* lp = lname;
139  const char* sp = strchr (in, '/'); // find first '/' in string
140  if ( sp ) {
141  while (in != sp) *lp++ = *in++;
142  *lp = 0;
143  }
144  else {
145  while (*in) *lp++ = *in++;
146  *lp = 0;
147  }
148 #ifdef WIN32
149  strcpy (ret, home_dir);
150  r += strlen (ret);
151 #else
152  // lookup user's home directory in /etc/passwd file
153  struct passwd* p = getpwnam (lname);
154  if ( p ) {
155  strcpy (ret, p->pw_dir ? p->pw_dir : "");
156  r += strlen (ret);
157  }
158 #endif
159  }
160  }
161 
162  while (*in) {
163  if (*in == '$') {
164  char varname [80];
165  if (*++in == '(') {
166  ++in;
167  const char *end = strchr (in,')');
168  if (!end)
169  break;
170  strncpy (varname, in, end-in);
171  varname [end-in] = '\0';
172  in = end+1;
173  }
174  else if (*in == '{') {
175  const char *end = strchr (in,'}');
176  if (!end)
177  break;
178  strncpy (varname, in, end-in);
179  varname [end-in] = '\0';
180  in = end+1;
181  }
182  else {
183  char* vp = varname;
184  while (isalnum (*in) || *in == '_' ) { // letter OR digit
185  *vp++ = *in++;
186  }
187  *vp = '\0';
188  }
189  char* ep = ::getenv (varname);
190  while (ep && *ep) *r++ = *ep++;
191  continue;
192  }
193  else if (*in == '\\' && *(in+1)) {
194  in++; // allow escaped dollar signs
195  }
196  *r++ = *in++;
197  }
198  *r = '\0';
199  return ret;
200 }
201 
202 std::string
204 get_cwd_name (void)
205 {
206  std::string ret;
207  int size = 256;
208  char* chr_ptr = 0;
209 
210  while (true) {
211  chr_ptr = new char [size];
212  if (::getcwd (chr_ptr, size-1) != NULL) {
213  ret = chr_ptr;
214  delete [] chr_ptr;
215  return ret;
216  }
217  if (errno != ERANGE) {
218  return ret; // Any error other then a path name too long
219  // for the buffer is bad news.
220  }
221  delete [] chr_ptr;
222  size += 256;
223  }
224 }
An abstraction to message logging facility.
int split_pair(const string &text_, char sep_, string &lhs_, string &rhs_)
Split input string into two parts separated by the separator character.
Definition: CommonUtils.cpp:47
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_.
void trim_sides(std::string &text_)
Trim white spaces and tabs from the beginning and the end of the text string.
Definition: CommonUtils.cpp:93
void split(const char *text_, std::vector< std::string > &vec_)
Split character string into tokens separated by the whitespace character (blank, tab,...
Definition: CommonUtils.cpp:34
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.
int ltrim(std::string &text_, const std::string &delim_)
Trim string from the beginning to the left of the delimiter.
Definition: CommonUtils.cpp:67
int rtrim(std::string &text_, const std::string &delim_)
Trim string from the delimiter to the end of the string.
Definition: CommonUtils.cpp:80