YARP
Yet Another Robot Platform
environment.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6
7#ifndef YARP_CONF_ENVIRONMENT_H
8#define YARP_CONF_ENVIRONMENT_H
9
10#include <yarp/conf/api.h>
11#include <yarp/conf/system.h>
12#include <yarp/conf/numeric.h>
13#include <yarp/conf/string.h>
14
15#include <cstdlib>
16#include <numeric>
17#include <regex>
18#include <string>
19#include <type_traits>
20#include <vector>
21
23
24#if defined(_WIN32)
25static constexpr char path_separator = ';';
26#else
27static constexpr char path_separator = ':';
28#endif
29
30
38template <typename ContainerT = std::vector<std::string>>
39inline ContainerT split_path(const typename ContainerT::value_type& s)
40{
41 return yarp::conf::string::split<ContainerT>(s, typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
42}
43
51template <typename ContainerT = std::vector<std::string>>
52inline typename ContainerT::value_type join_path(const ContainerT& v)
53{
54 return yarp::conf::string::join<ContainerT>(v.begin(), v.end(), typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
55}
56
66inline std::string get_string(const std::string& key, bool* found = nullptr)
67{
68 const char* result = std::getenv(key.c_str());
69 if (found != nullptr) {
70 *found = (result != nullptr);
71 }
72 if (result == nullptr) {
73 return {};
74 }
75 return std::string(result);
76}
77
87inline std::string get_string(const std::string& key, const std::string& defaultValue)
88{
89 const char* result = std::getenv(key.c_str());
90 return result ? std::string{result} : defaultValue;
91}
92
102inline std::string get_string(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
103{
104 bool found;
105 auto ret = get_string(key, &found);
106 return (found ? ret : get_string(altKey, altDefaultValue) + altAppend);
107}
108
118inline std::vector<std::string> get_path(const std::string& key, bool* found = nullptr)
119{
120 return split_path(get_string(key, found));
121}
122
132inline std::vector<std::string> get_path(const std::string& key, const std::string& defaultValue)
133{
134 return split_path(get_string(key, defaultValue));
135}
136
148inline std::vector<std::string> get_path(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
149{
150 bool found;
151 std::vector<std::string> path = get_path(key, &found);
152 if (!found) {
153 path = get_path(altKey, altDefaultValue);
154 if (!altAppend.empty()) {
155 for (auto& dir : path) {
156 dir.append(altAppend);
157 }
158 }
159 }
160 return path;
161}
162
172inline bool get_bool(const std::string& key, bool defaultValue = false)
173{
174 const char *strvalue = std::getenv(key.c_str());
175 if(!strvalue) {
176 return defaultValue;
177 }
178 return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
179}
180
190template <typename T>
191inline T get_numeric(const std::string& key, T defaultValue = static_cast<T>(0))
192{
193 const char *strvalue = std::getenv(key.c_str());
194 if (!strvalue) {
195 return defaultValue;
196 }
197 return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
198}
199
209inline bool set_string(const std::string& key, const std::string& value)
210{
211#if defined(_MSC_VER)
212 auto ret = _putenv_s(key.c_str(), value.c_str());
213#else
214 auto ret = ::setenv(key.c_str(), value.c_str(), 1);
215#endif
216 return (ret == 0);
217}
218
228inline bool set_path(const std::string& key, const std::vector<std::string>& value)
229{
230 return set_string(key, join_path(value));
231}
232
242inline bool set_bool(const std::string& key, bool value)
243{
244 // Boolean environment variables are usually set as "1" or "0", and not
245 // as "true" or "false", therefore `to_string` is not used here.
246 return set_string(key, value ? "1" : "0");
247}
248
258template <typename T>
259inline bool set_numeric(const std::string& key, bool value)
260{
261 return set_string(key, yarp::conf::numeric::to_string(value));
262}
263
272inline bool unset(const std::string& key)
273{
274#if defined(_MSC_VER)
275 auto ret = _putenv_s(key.c_str(), "");
276#else
277 auto ret = ::unsetenv(key.c_str());
278#endif
279 return (ret == 0);
280}
281
288inline bool is_set(const std::string& key)
289{
290 return std::getenv(key.c_str()) ? true : false;
291}
292
293
294#ifndef YARP_NO_DEPRECATED // Since YARP 3.5
305YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string() instead")
306inline std::string
307getEnvironment(const char* key, bool* found = nullptr)
308{
309 return get_string(key, found);
310}
311
321YARP_DEPRECATED_MSG("Use yarp::conf::environment::set_string() instead")
322inline void setEnvironment(const std::string& key, const std::string& value)
323{
324 set_string(key, value);
325}
326
335YARP_DEPRECATED_MSG("Use yarp::conf::environment::unset() instead")
336inline void unsetEnvironment(const std::string& key)
337{
338 unset(key);
339}
340
341
342#endif // YARP_NO_DEPRECATED
343
344} // namespace yarp::conf::environment
345
346#endif // YARP_CONF_ENVIRONMENT_H
bool ret
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2895
STL namespace.
T get_numeric(const std::string &key, T defaultValue=static_cast< T >(0))
Read an numeric value from an environment variable.
Definition: environment.h:191
bool set_bool(const std::string &key, bool value)
Set a bool to an environment variable (as 1 or 0).
Definition: environment.h:242
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...
Definition: environment.h:39
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
Definition: environment.h:322
bool unset(const std::string &key)
Remove an environment variable.
Definition: environment.h:272
bool is_set(const std::string &key)
Check if an environment variable is set.
Definition: environment.h:288
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
Definition: environment.h:172
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,...
Definition: environment.h:52
static constexpr char path_separator
Definition: environment.h:27
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
Definition: environment.h:209
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:307
bool set_path(const std::string &key, const std::vector< std::string > &value)
Set a path to an environment variable.
Definition: environment.h:228
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
Definition: environment.h:118
void unsetEnvironment(const std::string &key)
Remove an environment variable.
Definition: environment.h:336
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:66
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
Definition: environment.h:259
std::string to_string(IntegerType x)
Definition: numeric.h:115
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:31