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});
65inline std::string
get_string(
const std::string& key,
bool* found =
nullptr)
67 const char* result = std::getenv(key.c_str());
68 if (found !=
nullptr) {
69 *found = (result !=
nullptr);
71 if (result ==
nullptr) {
74 return std::string(result);
85inline std::string
get_string(
const std::string& key,
const std::string& defaultValue)
87 const char* result = std::getenv(key.c_str());
88 return result ? std::string{result} : defaultValue;
99inline std::string
get_string(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
103 return (found ? ret :
get_string(altKey, altDefaultValue) + altAppend);
114inline std::vector<std::string>
get_path(
const std::string& key,
bool* found =
nullptr)
127inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& defaultValue)
142inline std::vector<std::string>
get_path(
const std::string& key,
const std::string& altKey,
const std::string& altDefaultValue,
const std::string& altAppend = {})
145 std::vector<std::string> path =
get_path(key, &found);
147 path =
get_path(altKey, altDefaultValue);
148 if (!altAppend.empty()) {
149 for (
auto& dir : path) {
150 dir.append(altAppend);
165inline bool get_bool(
const std::string& key,
bool defaultValue =
false)
167 const char *strvalue = std::getenv(key.c_str());
171 return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
183inline T
get_numeric(
const std::string& key, T defaultValue =
static_cast<T
>(0))
185 const char *strvalue = std::getenv(key.c_str());
189 return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
200inline bool set_string(
const std::string& key,
const std::string& value)
203 auto ret = _putenv_s(key.c_str(), value.c_str());
205 auto ret = ::setenv(key.c_str(), value.c_str(), 1);
218inline bool set_path(
const std::string& key,
const std::vector<std::string>& value)
231inline bool set_bool(
const std::string& key,
bool value)
259inline bool unset(
const std::string& key)
262 auto ret = _putenv_s(key.c_str(),
"");
264 auto ret = ::unsetenv(key.c_str());
275inline bool is_set(
const std::string& key)
277 return std::getenv(key.c_str()) ? true :
false;
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...
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.
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.
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)