38setenv(
const char* var_name,
const char* new_value,
int change_flag)
41 std::string
value{new_value};
52 if (
variable.find(
'=') != std::string::npos)
61 char* old_value = std::getenv(var_name);
62 if (old_value !=
nullptr)
69 return _putenv_s(var_name, new_value);
78unsetenv(
const char* var_name)
80 return _putenv_s(var_name,
"");
105#define NS_LOCAL_LOG(msg) \
106 std::cerr << __FILE__ << ":" << __LINE__ << ":" << __FUNCTION__ << "(): " << msg << std::endl
108#define NS_LOCAL_ASSERT(cond, msg) \
113 NS_LOCAL_LOG("assert failed. cond=\"" << #cond << "\", " << msg); \
118#define NS_LOCAL_LOG(msg)
119#define NS_LOCAL_ASSERT(cond, msg)
138std::shared_ptr<EnvironmentVariable::Dictionary>
142 std::shared_ptr<Dictionary> dict;
151 NS_LOCAL_LOG(
"envvar not in cache, checking environment");
152 dict = std::make_shared<Dictionary>(envvar, delim);
162 const std::string& key ,
163 const std::string& delim )
166 return dict->Get(key);
173 int fail = setenv(variable.c_str(), value.c_str(), 1);
181 int fail = unsetenv(variable.c_str());
201 auto loc =
m_dict.find(key);
205 NS_LOCAL_LOG(
"found: key '" << key <<
"', value: '" << loc->second <<
"'");
206 return {
true, loc->second};
214 const std::string& delim )
218 const char* envCstr = std::getenv(envvar.c_str());
228 m_variable = envCstr;
229 NS_LOCAL_LOG(
"found envvar in environment with value '" << m_variable <<
"'");
232 if (m_variable.empty())
238 NS_LOCAL_ASSERT(keyvals.empty(),
"Unexpected empty keyvals from non-empty m_variable");
239 for (
const auto& keyval : keyvals)
246 std::size_t equals = keyval.find_first_of(
'=');
247 std::string key{keyval, 0, equals};
249 if (equals < keyval.size() - 1)
251 value = keyval.substr(equals + 1, keyval.size());
253 NS_LOCAL_LOG(
"found key '" << key <<
"' with value '" << value <<
"'");
254 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::pair< bool, std::string > KeyFoundType
Result of a key lookup.
static void Clear()
Clear the instance, forcing all new lookups.
std::unordered_map< std::string, std::shared_ptr< Dictionary > > DictionaryList
How Dictionaries are stored.
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.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
std::vector< std::string > StringVector
Return type of SplitString.
ns3::StringValue attribute value declarations.