blocxx
BLOCXX_NAMESPACE::EnvVars Class Reference

The EnvVars class is an abstraction for a set of environment variables. More...

#include <EnvVars.hpp>

Public Types

enum  EEnvVarFlag { E_CURRENT_ENVIRONMENT , E_EMPTY_ENVIRONMENT }
 
typedef Map< String, StringEnvMap
 
typedef EnvMap::const_iterator const_iterator
 

Public Member Functions

 EnvVars (EEnvVarFlag flag=E_EMPTY_ENVIRONMENT)
 Create an EnvVars object that is either empty or contains all of the environment variable from the current environment.
 
template<class InputIterator >
 EnvVars (EEnvVarFlag flag, InputIterator first, InputIterator last)
 Create an EnvVars object that is either empty or contains all of the environment variable from the current environment and override it with additional environment variables from a map range specified with InputIterators.
 
 EnvVars (const char *const envp[])
 Create an EnvVars object that contains the evironment variables specifed in a string array.
 
 EnvVars (const EnvVars &arg)
 Copy constructor.
 
 ~EnvVars ()
 Destructor.
 
EnvVarsoperator= (const EnvVars &arg)
 Assignment operator.
 
const char *const * getenvp () const
 
size_t size () const
 
String getValue (const String &key, const String &notFoundRetVal=String()) const
 Get the value associated with a given key.
 
bool removeVar (const String &key)
 Remove an environment variable from this EnvVars object.
 
bool setVar (const String &keyValue)
 Set the value for a given environemt variable.
 
bool setVar (const String &key, const String &value)
 Set the value for a given environemt variable.
 
bool addVar (const String &name, const String &value)
 Adds the variable name if its not already part of the set.
 
bool updateVar (const String &name, const String &value)
 Updates an existing variable.
 
const_iterator begin () const
 
const_iterator end () const
 

Private Member Functions

void deleteEnvp () const
 

Static Private Member Functions

static void fillEnvMap (EnvMap &envMap)
 
static void fillEnvMap (const char *const envp[], EnvMap &envMap)
 

Private Attributes

EnvMap m_envMap
 
char ** m_envp
 

Detailed Description

The EnvVars class is an abstraction for a set of environment variables.

Definition at line 49 of file EnvVars.hpp.

Member Typedef Documentation

◆ const_iterator

◆ EnvMap

Member Enumeration Documentation

◆ EEnvVarFlag

Enumerator
E_CURRENT_ENVIRONMENT 
E_EMPTY_ENVIRONMENT 

Definition at line 55 of file EnvVars.hpp.

Constructor & Destructor Documentation

◆ EnvVars() [1/4]

BLOCXX_NAMESPACE::EnvVars::EnvVars ( EEnvVarFlag flag = E_EMPTY_ENVIRONMENT)

Create an EnvVars object that is either empty or contains all of the environment variable from the current environment.

Definition at line 77 of file EnvVars.cpp.

References E_CURRENT_ENVIRONMENT, environ, fillEnvMap(), and m_envMap.

◆ EnvVars() [2/4]

template<class InputIterator >
BLOCXX_NAMESPACE::EnvVars::EnvVars ( EEnvVarFlag flag,
InputIterator first,
InputIterator last )
inline

Create an EnvVars object that is either empty or contains all of the environment variable from the current environment and override it with additional environment variables from a map range specified with InputIterators.

The map with the additional variables can contain either blocxx::String or std::string elements (has to provide the c_str() method returning pointer to the string). Variables with empty name or a name containing the '=' character (like for setenv in SUSv3) are discarded.

Parameters
flagWhether to construct using empty or current environment.
firstInputIterator pointing to the first variable.
lastInputIterator pointing to one past the last variable.

Definition at line 83 of file EnvVars.hpp.

◆ EnvVars() [3/4]

BLOCXX_NAMESPACE::EnvVars::EnvVars ( const char *const envp[])

Create an EnvVars object that contains the evironment variables specifed in a string array.

Parameters
envpA NULL terminated char* array that contains the environment variables for the EnvVars object. Each element of the array must be in the form of "ENV_VAR_NAME=VALUE".

Definition at line 88 of file EnvVars.cpp.

References fillEnvMap(), and m_envMap.

◆ EnvVars() [4/4]

BLOCXX_NAMESPACE::EnvVars::EnvVars ( const EnvVars & arg)

Copy constructor.

Parameters
argThe EnvVars object this object will be a copy of.

◆ ~EnvVars()

BLOCXX_NAMESPACE::EnvVars::~EnvVars ( )

Destructor.

Definition at line 96 of file EnvVars.cpp.

References deleteEnvp().

Member Function Documentation

◆ addVar()

bool BLOCXX_NAMESPACE::EnvVars::addVar ( const String & name,
const String & value )

Adds the variable name if its not already part of the set.

Returns
true if it was added, false if it already exists or the variable name is empty or contains a '=' character.

Definition at line 208 of file EnvVars.cpp.

References deleteEnvp(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::end(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::find(), m_envMap, and name.

◆ begin()

const_iterator BLOCXX_NAMESPACE::EnvVars::begin ( ) const
inline
Returns
A read only iterator that points to the first environemt variable in the EnvVars object.

Definition at line 190 of file EnvVars.hpp.

◆ deleteEnvp()

void BLOCXX_NAMESPACE::EnvVars::deleteEnvp ( ) const
private

Definition at line 128 of file EnvVars.cpp.

References i, and m_envp.

Referenced by addVar(), getenvp(), removeVar(), setVar(), updateVar(), and ~EnvVars().

◆ end()

const_iterator BLOCXX_NAMESPACE::EnvVars::end ( ) const
inline
Returns
A read only iterator that points one past the last environemt variable in the EnvVars object.

Definition at line 199 of file EnvVars.hpp.

◆ fillEnvMap() [1/2]

void BLOCXX_NAMESPACE::EnvVars::fillEnvMap ( const char *const envp[],
EnvMap & envMap )
staticprivate

Definition at line 112 of file EnvVars.cpp.

References BLOCXX_NAMESPACE::Map< Key, T, Compare >::clear(), and i.

◆ fillEnvMap() [2/2]

void BLOCXX_NAMESPACE::EnvVars::fillEnvMap ( EnvMap & envMap)
staticprivate

Definition at line 104 of file EnvVars.cpp.

References environ, and fillEnvMap().

Referenced by EnvVars(), EnvVars(), and fillEnvMap().

◆ getenvp()

const char *const * BLOCXX_NAMESPACE::EnvVars::getenvp ( ) const
Returns
A null terminated char* array that contains all the environment variables contained by this EnvVars object. Each element of the returned array will be of the form "ENV_VAR_NAME=VALUE". Note, that in case the EnvVars object doesn't contain any variables, the function returns a envp pointer without any content (the terminating element only).

Definition at line 159 of file EnvVars.cpp.

References BLOCXX_NAMESPACE::Map< Key, T, Compare >::begin(), deleteEnvp(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::end(), i, m_envMap, m_envp, and BLOCXX_NAMESPACE::Map< Key, T, Compare >::size().

Referenced by BLOCXX_NAMESPACE::Cstr::CstrArr< EnvVars >::CstrArr().

◆ getValue()

String BLOCXX_NAMESPACE::EnvVars::getValue ( const String & key,
const String & notFoundRetVal = String() ) const

Get the value associated with a given key.

Parameters
keyThe name for the value to retrieve.
notFoundRetValIf the environment variable by the name of 'key' is not found, then return the value specified by this parameter.
Returns
The value associated with a given key. If the key does not exist in this EnvVars object, the return value will be an empty string.

Definition at line 150 of file EnvVars.cpp.

References BLOCXX_NAMESPACE::Map< Key, T, Compare >::end(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::find(), and m_envMap.

◆ operator=()

EnvVars & BLOCXX_NAMESPACE::EnvVars::operator= ( const EnvVars & arg)

Assignment operator.

Parameters
argThe EnvVars object to assign to this one.
Returns
A reference to this EnvVars object after the assignment is done.

◆ removeVar()

bool BLOCXX_NAMESPACE::EnvVars::removeVar ( const String & key)

Remove an environment variable from this EnvVars object.

Parameters
keyThe name of the environment variable to be removed.
Returns
true if key was found and removed. Otherwise return false.

Definition at line 192 of file EnvVars.cpp.

References deleteEnvp(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::end(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::erase(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::find(), and m_envMap.

◆ setVar() [1/2]

bool BLOCXX_NAMESPACE::EnvVars::setVar ( const String & key,
const String & value )

Set the value for a given environemt variable.

If the environment variable does not exist in the EnvVars object, it will be added.

Parameters
keyThe name of the environment variable
valueThe value to set the environment variable to.
Returns
false, if the key is empty or contains a '=' character.

Definition at line 222 of file EnvVars.cpp.

References deleteEnvp(), and m_envMap.

◆ setVar() [2/2]

bool BLOCXX_NAMESPACE::EnvVars::setVar ( const String & keyValue)

Set the value for a given environemt variable.

If the environment variable does not exist in the EnvVars object, it will be added.

Parameters
keyValueA key=value string that represent the key/value of the environemt variable to be set/added.
Returns
false, if the key is empty or contains a '=' character.

Definition at line 236 of file EnvVars.cpp.

References BLOCXX_NAMESPACE::String::c_str(), and setVar().

Referenced by setVar().

◆ size()

size_t BLOCXX_NAMESPACE::EnvVars::size ( ) const
inline
Returns
The number of environment variables contained by this object as a size_t.

Definition at line 132 of file EnvVars.hpp.

◆ updateVar()

bool BLOCXX_NAMESPACE::EnvVars::updateVar ( const String & name,
const String & value )

Updates an existing variable.

Returns
true if variable name was updated, false it it doesn't exist.

Definition at line 245 of file EnvVars.cpp.

References deleteEnvp(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::end(), BLOCXX_NAMESPACE::Map< Key, T, Compare >::find(), m_envMap, and name.

Member Data Documentation

◆ m_envMap

EnvMap BLOCXX_NAMESPACE::EnvVars::m_envMap
private

Definition at line 209 of file EnvVars.hpp.

Referenced by addVar(), EnvVars(), EnvVars(), getenvp(), getValue(), removeVar(), setVar(), and updateVar().

◆ m_envp

char** BLOCXX_NAMESPACE::EnvVars::m_envp
mutableprivate

Definition at line 210 of file EnvVars.hpp.

Referenced by deleteEnvp(), and getenvp().


The documentation for this class was generated from the following files: