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 
22 namespace yarp {
23 namespace conf {
24 namespace environment {
25 
26 #if defined(_WIN32)
27 static constexpr char path_separator = ';';
28 #else
29 static constexpr char path_separator = ':';
30 #endif
31 
32 
40 template <typename ContainerT = std::vector<std::string>>
41 inline ContainerT split_path(const typename ContainerT::value_type& s)
42 {
43  return yarp::conf::string::split<ContainerT>(s, typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
44 }
45 
53 template <typename ContainerT = std::vector<std::string>>
54 inline typename ContainerT::value_type join_path(const ContainerT& v)
55 {
56  return yarp::conf::string::join<ContainerT>(v.begin(), v.end(), typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
57 }
58 
68 inline std::string get_string(const std::string& key, bool* found = nullptr)
69 {
70  const char* result = std::getenv(key.c_str());
71  if (found != nullptr) {
72  *found = (result != nullptr);
73  }
74  if (result == nullptr) {
75  return {};
76  }
77  return std::string(result);
78 }
79 
89 inline std::string get_string(const std::string& key, const std::string& defaultValue)
90 {
91  const char* result = std::getenv(key.c_str());
92  return result ? std::string{result} : defaultValue;
93 }
94 
104 inline std::string get_string(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
105 {
106  bool found;
107  auto ret = get_string(key, &found);
108  return (found ? ret : get_string(altKey, altDefaultValue) + altAppend);
109 }
110 
120 inline std::vector<std::string> get_path(const std::string& key, bool* found = nullptr)
121 {
122  return split_path(get_string(key, found));
123 }
124 
134 inline std::vector<std::string> get_path(const std::string& key, const std::string& defaultValue)
135 {
136  return split_path(get_string(key, defaultValue));
137 }
138 
150 inline std::vector<std::string> get_path(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
151 {
152  bool found;
153  std::vector<std::string> path = get_path(key, &found);
154  if (!found) {
155  path = get_path(altKey, altDefaultValue);
156  if (!altAppend.empty()) {
157  for (auto& dir : path) {
158  dir.append(altAppend);
159  }
160  }
161  }
162  return path;
163 }
164 
174 inline bool get_bool(const std::string& key, bool defaultValue = false)
175 {
176  const char *strvalue = std::getenv(key.c_str());
177  if(!strvalue) {
178  return defaultValue;
179  }
180  return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
181 }
182 
192 template <typename T>
193 inline T get_numeric(const std::string& key, T defaultValue = static_cast<T>(0))
194 {
195  const char *strvalue = std::getenv(key.c_str());
196  if (!strvalue) {
197  return defaultValue;
198  }
199  return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
200 }
201 
211 inline bool set_string(const std::string& key, const std::string& value)
212 {
213 #if defined(_MSC_VER)
214  auto ret = _putenv_s(key.c_str(), value.c_str());
215 #else
216  auto ret = ::setenv(key.c_str(), value.c_str(), 1);
217 #endif
218  return (ret == 0);
219 }
220 
230 inline bool set_path(const std::string& key, const std::vector<std::string>& value)
231 {
232  return set_string(key, join_path(value));
233 }
234 
244 inline bool set_bool(const std::string& key, bool value)
245 {
246  // Boolean environment variables are usually set as "1" or "0", and not
247  // as "true" or "false", therefore `to_string` is not used here.
248  return set_string(key, value ? "1" : "0");
249 }
250 
260 template <typename T>
261 inline bool set_numeric(const std::string& key, bool value)
262 {
263  return set_string(key, yarp::conf::numeric::to_string(value));
264 }
265 
274 inline bool unset(const std::string& key)
275 {
276 #if defined(_MSC_VER)
277  auto ret = _putenv_s(key.c_str(), "");
278 #else
279  auto ret = ::unsetenv(key.c_str());
280 #endif
281  return (ret == 0);
282 }
283 
290 inline bool is_set(const std::string& key)
291 {
292  return std::getenv(key.c_str()) ? true : false;
293 }
294 
295 
296 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
307 YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string() instead")
308 inline std::string
309 getEnvironment(const char* key, bool* found = nullptr)
310 {
311  return get_string(key, found);
312 }
313 
323 YARP_DEPRECATED_MSG("Use yarp::conf::environment::set_string() instead")
324 inline void setEnvironment(const std::string& key, const std::string& value)
325 {
326  set_string(key, value);
327 }
328 
337 YARP_DEPRECATED_MSG("Use yarp::conf::environment::unset() instead")
338 inline void unsetEnvironment(const std::string& key)
339 {
340  unset(key);
341 }
342 
343 
344 #endif // YARP_NO_DEPRECATED
345 
346 } // namespace environment
347 } // namespace conf
348 } // namespace yarp
349 
350 #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:2885
T get_numeric(const std::string &key, T defaultValue=static_cast< T >(0))
Read an numeric value from an environment variable.
Definition: environment.h:193
bool set_bool(const std::string &key, bool value)
Set a bool to an environment variable (as 1 or 0).
Definition: environment.h:244
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:41
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
Definition: environment.h:324
bool unset(const std::string &key)
Remove an environment variable.
Definition: environment.h:274
bool is_set(const std::string &key)
Check if an environment variable is set.
Definition: environment.h:290
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
Definition: environment.h:174
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:54
static constexpr char path_separator
Definition: environment.h:29
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
Definition: environment.h:211
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:309
bool set_path(const std::string &key, const std::vector< std::string > &value)
Set a path to an environment variable.
Definition: environment.h:230
void unsetEnvironment(const std::string &key)
Remove an environment variable.
Definition: environment.h:338
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
Definition: environment.h:261
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
Definition: environment.h:120
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
The main, catch-all namespace for YARP.
Definition: dirs.h:16