libassa 3.5.1
Loading...
Searching...
No Matches
Functions
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.
 
void sleep_for_seconds (long secs_to_sleep_)
 Portable sleep.
 

Function Documentation

◆ find_and_replace_char()

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

111{
112 string::iterator pos = text_.begin ();
113 while (pos != text_.end ()) {
114 if ((*pos) == src_) {
115 (*pos) = dest_;
116 }
117 pos++;
118 }
119}
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32

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

◆ get_cwd_name()

std::string ASSA::Utils::get_cwd_name ( void  )

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

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}

◆ ltrim()

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

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}

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

◆ rtrim()

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

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}

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

◆ sleep_for_seconds()

void ASSA::Utils::sleep_for_seconds ( long  secs_to_sleep_)
inline

Portable sleep.

Parameters
secs_to_sleep_Number of seconds to sleep

Definition at line 142 of file CommonUtils.h.

143 {
144#if defined (WIN32)
145 SleepEx (secs_to_sleep_ * 1000, FALSE);
146#else
147 ::sleep (secs_to_sleep_);
148#endif
149 }

◆ split()

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

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}

◆ split_pair()

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

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}

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

◆ strenv()

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

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}

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

◆ trim_sides()

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

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}

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