7#ifndef YARP_CONF_ENVIRONMENT_H
8#define YARP_CONF_ENVIRONMENT_H
38template <
typename ContainerT = std::vector<std::
string>>
39inline ContainerT
split_path(
const typename ContainerT::value_type& s)
51template <
typename ContainerT = std::vector<std::
string>>
52inline typename ContainerT::value_type
join_path(
const ContainerT& v)
54 return yarp::conf::string::join<ContainerT>(v.begin(), v.end(),
typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
66inline std::string
get_string(
const std::string& key,
bool* found =
nullptr)
68 const char* result = std::getenv(key.c_str());
69 if (found !=
nullptr) {
70 *found = (result !=
nullptr);
72 if (result ==
nullptr) {
75 return std::string(result);
87inline std::string
get_string(
const std::string& key,
const std::string& defaultValue)
89 const char* result = std::getenv(key.c_str());
90 return result ? std::string{result} : defaultValue;
102inline std::string
get_string(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
106 return (found ?
ret :
get_string(altKey, altDefaultValue) + altAppend);
118inline std::vector<std::string>
get_path(
const std::string& key,
bool* found =
nullptr)
132inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& defaultValue)
148inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
151 std::vector<std::string> path =
get_path(key, &found);
153 path =
get_path(altKey, altDefaultValue);
154 if (!altAppend.empty()) {
155 for (
auto& dir : path) {
156 dir.append(altAppend);
172inline bool get_bool(
const std::string& key,
bool defaultValue =
false)
174 const char *strvalue = std::getenv(key.c_str());
178 return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
191inline T
get_numeric(
const std::string& key, T defaultValue =
static_cast<T
>(0))
193 const char *strvalue = std::getenv(key.c_str());
197 return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
209inline bool set_string(
const std::string& key,
const std::string& value)
212 auto ret = _putenv_s(key.c_str(), value.c_str());
214 auto ret = ::setenv(key.c_str(), value.c_str(), 1);
228inline bool set_path(
const std::string& key,
const std::vector<std::string>& value)
242inline bool set_bool(
const std::string& key,
bool value)
272inline bool unset(
const std::string& key)
275 auto ret = _putenv_s(key.c_str(),
"");
277 auto ret = ::unsetenv(key.c_str());
288inline bool is_set(
const std::string& key)
290 return std::getenv(key.c_str()) ? true :
false;
294#ifndef YARP_NO_DEPRECATED
305YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string() instead")
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
T get_numeric(const std::string &key, T defaultValue=static_cast< T >(0))
Read an numeric value from an environment variable.
bool set_bool(const std::string &key, bool value)
Set a bool to an environment variable (as 1 or 0).
ContainerT split_path(const typename ContainerT::value_type &s)
Utility to split a string containing a path separated by the path_separator, which depends on the sys...
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
bool unset(const std::string &key)
Remove an environment variable.
bool is_set(const std::string &key)
Check if an environment variable is set.
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
ContainerT::value_type join_path(const ContainerT &v)
Utility to join a vector of strings into a single string separated by the proper path_separator,...
static constexpr char path_separator
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
bool set_path(const std::string &key, const std::vector< std::string > &value)
Set a path to an environment variable.
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
void unsetEnvironment(const std::string &key)
Remove an environment variable.
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
std::string to_string(IntegerType x)