YARP
Yet Another Robot Platform
environment.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 
10 #ifndef YARP_CONF_ENVIRONMENT_H
11 #define YARP_CONF_ENVIRONMENT_H
12 
13 #include <yarp/conf/api.h>
14 #include <yarp/conf/system.h>
15 #include <yarp/conf/numeric.h>
16 #include <yarp/conf/string.h>
17 
18 #include <cstdlib>
19 #include <numeric>
20 #include <regex>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24 
25 namespace yarp {
26 namespace conf {
27 namespace environment {
28 
29 #if defined(_WIN32)
30 static constexpr char path_separator = ';';
31 #else
32 static constexpr char path_separator = ':';
33 #endif
34 
35 
43 template <typename ContainerT = std::vector<std::string>>
44 inline ContainerT split_path(const typename ContainerT::value_type& s)
45 {
46  return yarp::conf::string::split<ContainerT>(s, typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
47 }
48 
56 template <typename ContainerT = std::vector<std::string>>
57 inline typename ContainerT::value_type join_path(const ContainerT& v)
58 {
59  return yarp::conf::string::join<ContainerT>(v.begin(), v.end(), typename ContainerT::value_type::value_type{yarp::conf::environment::path_separator});
60 }
61 
71 inline std::string get_string(const std::string& key, bool* found = nullptr)
72 {
73  const char* result = std::getenv(key.c_str());
74  if (found != nullptr) {
75  *found = (result != nullptr);
76  }
77  if (result == nullptr) {
78  return {};
79  }
80  return std::string(result);
81 }
82 
92 inline std::string get_string(const std::string& key, const std::string& defaultValue)
93 {
94  const char* result = std::getenv(key.c_str());
95  return result ? std::string{result} : defaultValue;
96 }
97 
107 inline std::string get_string(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
108 {
109  bool found;
110  auto ret = get_string(key, &found);
111  return (found ? ret : get_string(altKey, altDefaultValue) + altAppend);
112 }
113 
123 inline std::vector<std::string> get_path(const std::string& key, bool* found = nullptr)
124 {
125  return split_path(get_string(key, found));
126 }
127 
137 inline std::vector<std::string> get_path(const std::string& key, const std::string& defaultValue)
138 {
139  return split_path(get_string(key, defaultValue));
140 }
141 
153 inline std::vector<std::string> get_path(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
154 {
155  bool found;
156  std::vector<std::string> path = get_path(key, &found);
157  if (!found) {
158  path = get_path(altKey, altDefaultValue);
159  if (!altAppend.empty()) {
160  for (auto& dir : path) {
161  dir.append(altAppend);
162  }
163  }
164  }
165  return path;
166 }
167 
177 inline bool get_bool(const std::string& key, bool defaultValue = false)
178 {
179  const char *strvalue = std::getenv(key.c_str());
180  if(!strvalue) {
181  return defaultValue;
182  }
183  return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
184 }
185 
195 template <typename T>
196 inline T get_numeric(const std::string& key, T defaultValue = static_cast<T>(0))
197 {
198  const char *strvalue = std::getenv(key.c_str());
199  if (!strvalue) {
200  return defaultValue;
201  }
202  return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
203 }
204 
214 inline bool set_string(const std::string& key, const std::string& value)
215 {
216 #if defined(_MSC_VER)
217  auto ret = _putenv_s(key.c_str(), value.c_str());
218 #else
219  auto ret = ::setenv(key.c_str(), value.c_str(), 1);
220 #endif
221  return (ret == 0);
222 }
223 
233 inline bool set_path(const std::string& key, const std::vector<std::string>& value)
234 {
235  return set_string(key, join_path(value));
236 }
237 
247 inline bool set_bool(const std::string& key, bool value)
248 {
249  // Boolean environment variables are usually set as "1" or "0", and not
250  // as "true" or "false", therefore `to_string` is not used here.
251  return set_string(key, value ? "1" : "0");
252 }
253 
263 template <typename T>
264 inline bool set_numeric(const std::string& key, bool value)
265 {
266  return set_string(key, yarp::conf::numeric::to_string(value));
267 }
268 
277 inline bool unset(const std::string& key)
278 {
279 #if defined(_MSC_VER)
280  auto ret = _putenv_s(key.c_str(), "");
281 #else
282  auto ret = ::unsetenv(key.c_str());
283 #endif
284  return (ret == 0);
285 }
286 
293 inline bool is_set(const std::string& key)
294 {
295  return std::getenv(key.c_str()) ? true : false;
296 }
297 
298 
299 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
310 YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string() instead")
311 inline std::string
312 getEnvironment(const char* key, bool* found = nullptr)
313 {
314  return get_string(key, found);
315 }
316 
326 YARP_DEPRECATED_MSG("Use yarp::conf::environment::set_string() instead")
327 inline void setEnvironment(const std::string& key, const std::string& value)
328 {
329  set_string(key, value);
330 }
331 
340 YARP_DEPRECATED_MSG("Use yarp::conf::environment::unset() instead")
341 inline void unsetEnvironment(const std::string& key)
342 {
343  unset(key);
344 }
345 
346 
347 #endif // YARP_NO_DEPRECATED
348 
349 } // namespace environment
350 } // namespace conf
351 } // namespace yarp
352 
353 #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:2883
T get_numeric(const std::string &key, T defaultValue=static_cast< T >(0))
Read an numeric value from an environment variable.
Definition: environment.h:196
bool set_bool(const std::string &key, bool value)
Set a bool to an environment variable (as 1 or 0).
Definition: environment.h:247
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:44
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
Definition: environment.h:327
bool unset(const std::string &key)
Remove an environment variable.
Definition: environment.h:277
bool is_set(const std::string &key)
Check if an environment variable is set.
Definition: environment.h:293
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
Definition: environment.h:177
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:57
static constexpr char path_separator
Definition: environment.h:32
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
Definition: environment.h:214
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:312
bool set_path(const std::string &key, const std::vector< std::string > &value)
Set a path to an environment variable.
Definition: environment.h:233
void unsetEnvironment(const std::string &key)
Remove an environment variable.
Definition: environment.h:341
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:71
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
Definition: environment.h:264
std::vector< std::string > get_path(const std::string &key, bool *found=nullptr)
Read a path from an environment variable.
Definition: environment.h:123
std::string to_string(Integer x)
Definition: numeric.h:118
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:34
The main, catch-all namespace for YARP.
Definition: environment.h:25