libcsdbg  1.28
C++ exception (and generic) stack trace debug library
chain< string > * csdbg::util::getenv ( const i8 var)
static

Parse a shell (environment) variable to its components.

Parameters
[in]varthe variable name
Returns
the variable components, tokenized with ':' (heap allocated)
Exceptions
std::bad_alloc
csdbg::exception

Definition at line 128 of file util.cpp.

References __D_ASSERT, csdbg::chain< T >::add(), csdbg::string::append(), csdbg::string::length(), likely, and unlikely.

Referenced by csdbg::tracer::on_lib_load().

129 {
130  __D_ASSERT(var != NULL);
131  if ( unlikely(var == NULL) )
132  return NULL;
133 
134  const i8 *val = ::getenv(var);
135  if ( unlikely(val == NULL) )
136  return NULL;
137 
138  chain<string> *retval = new chain<string>;
139  string *token = NULL;
140 
141  try {
142  token = new string;
143 
144  /* Token parsing */
145  for (u32 i = 0, sz = strlen(val); likely(i < sz); i++) {
146  i8 ch = val[i];
147 
148  if ( likely(ch != ':') )
149  token->append(ch);
150 
151  /* New token */
152  else if ( likely(token->length() > 0) ) {
153  retval->add(token);
154  token = NULL;
155  token = new string;
156  }
157  }
158 
159  /* Final token */
160  if ( likely(token->length() > 0) )
161  retval->add(token);
162  else
163  delete token;
164 
165  return retval;
166  }
167 
168  catch (...) {
169  delete retval;
170  delete token;
171  throw;
172  }
173 }
char i8
8-bit signed integer
Definition: config.hpp:72
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
static chain< string > * getenv(const i8 *)
Parse a shell (environment) variable to its components.
Definition: util.cpp:128
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268

+ Here is the call graph for this function:

+ Here is the caller graph for this function: