49 setenv(
const char* var_name,
const char* new_value,
int change_flag)
52 std::string
value{new_value};
63 if (
variable.find(
'=') != std::string::npos)
72 char* old_value = std::getenv(var_name);
73 if (old_value !=
nullptr)
80 return _putenv_s(var_name, new_value);
89 unsetenv(
const char* var_name)
91 return _putenv_s(var_name,
"");
116 #define NS_LOCAL_LOG(msg) \
117 std::cerr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): " << msg << std::endl
119 #define NS_LOCAL_ASSERT(cond, msg) \
124 NS_LOCAL_LOG("assert failed. cond=\"" << #cond << "\", " << msg); \
129 #define NS_LOCAL_LOG(msg)
130 #define NS_LOCAL_ASSERT(cond, msg)
149 std::shared_ptr<EnvironmentVariable::Dictionary>
153 std::shared_ptr<Dictionary> dict;
162 NS_LOCAL_LOG(
"envvar not in cache, checking environment");
163 dict = std::make_shared<Dictionary>(envvar, delim);
173 const std::string& key ,
174 const std::string& delim )
177 return dict->Get(key);
184 int fail = setenv(
variable.c_str(), value.c_str(), 1);
192 int fail = unsetenv(
variable.c_str());
212 auto loc =
m_dict.find(key);
216 NS_LOCAL_LOG(
"found: key '" << key <<
"', value: '" << loc->second <<
"'");
217 return {
true, loc->second};
225 const std::string& delim )
229 const char* envCstr = std::getenv(envvar.c_str());
239 m_variable = envCstr;
240 NS_LOCAL_LOG(
"found envvar in environment with value '" << m_variable <<
"'");
243 if (m_variable.empty())
249 NS_LOCAL_ASSERT(keyvals.empty(),
"Unexpected empty keyvals from non-empty m_variable");
250 for (
const auto& keyval : keyvals)
257 std::size_t equals = keyval.find_first_of(
'=');
258 std::string key{keyval, 0, equals};
260 if (equals < keyval.size() - 1)
262 value = keyval.substr(equals + 1, keyval.size());
264 NS_LOCAL_LOG(
"found key '" << key <<
"' with value '" << value <<
"'");
265 m_dict.insert({key, value});
Dictionary(const std::string &envvar, const std::string &delim=";")
Constructor.
KeyFoundType Get(const std::string &key="") const
Get the value corresponding to a key from this dictionary.
std::unordered_map< std::string, std::string > KeyValueStore
Key, value store type.
std::string m_variable
The raw environment variable.
bool m_exists
Whether the environment variable exists in the environment.
KeyValueStore m_dict
The key, value store.
KeyValueStore GetStore() const
Get the underlying store, for iterating.
static DictionaryList & Instance()
Access the DictionaryStore instance.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
static bool Unset(const std::string &variable)
Unset an environment variable.
std::unordered_map< std::string, std::shared_ptr< Dictionary > > DictionaryList
How Dictionaries are stored.
std::pair< bool, std::string > KeyFoundType
Result of a key lookup.
static void Clear()
Clear the instance, forcing all new lookups.
static bool Set(const std::string &variable, const std::string &value)
Set an environment variable.
static std::shared_ptr< Dictionary > GetDictionary(const std::string &envvar, const std::string &delim=";")
Get the dictionary for a particular environment variable.
#define NS_LOCAL_ASSERT(cond, msg)
File-local assert macro for environment-variable.cc Our usual assert doesn't work here because these ...
Class Environment declaration.
#define NS_LOCAL_LOG(msg)
File-local logging macro for environment-variable.cc Our usual Logging doesn't work here because thes...
std::string variable
variable or class member
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::vector< std::string > StringVector
Return type of SplitString.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
ns3::StringValue attribute value declarations.