YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
65inline std::string get_string(const std::string& key, bool* found = nullptr)
66{
67 const char* result = std::getenv(key.c_str());
68 if (found != nullptr) {
69 *found = (result != nullptr);
70 }
71 if (result == nullptr) {
72 return {};
73 }
74 return std::string(result);
75}
76
85inline std::string get_string(const std::string& key, const std::string& defaultValue)
86{
87 const char* result = std::getenv(key.c_str());
88 return result ? std::string{result} : defaultValue;
89}
90
99inline std::string get_string(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
100{
101 bool found;
102 auto ret = get_string(key, &found);
103 return (found ? ret : get_string(altKey, altDefaultValue) + altAppend);
104}
105
114inline std::vector<std::string> get_path(const std::string& key, bool* found = nullptr)
115{
116 return split_path(get_string(key, found));
117}
118
127inline std::vector<std::string> get_path(const std::string& key, const std::string& defaultValue)
128{
129 return split_path(get_string(key, defaultValue));
130}
131
142inline std::vector<std::string> get_path(const std::string& key, const std::string& altKey, const std::string& altDefaultValue, const std::string& altAppend = {})
143{
144 bool found;
145 std::vector<std::string> path = get_path(key, &found);
146 if (!found) {
147 path = get_path(altKey, altDefaultValue);
148 if (!altAppend.empty()) {
149 for (auto& dir : path) {
150 dir.append(altAppend);
151 }
152 }
153 }
154 return path;
155}
156
165inline bool get_bool(const std::string& key, bool defaultValue = false)
166{
167 const char *strvalue = std::getenv(key.c_str());
168 if(!strvalue) {
169 return defaultValue;
170 }
171 return yarp::conf::numeric::from_string<bool>(strvalue, defaultValue);
172}
173
182template <typename T>
183inline T get_numeric(const std::string& key, T defaultValue = static_cast<T>(0))
184{
185 const char *strvalue = std::getenv(key.c_str());
186 if (!strvalue) {
187 return defaultValue;
188 }
189 return yarp::conf::numeric::from_string<T>(strvalue, defaultValue);
190}
191
200inline bool set_string(const std::string& key, const std::string& value)
201{
202#if defined(_MSC_VER)
203 auto ret = _putenv_s(key.c_str(), value.c_str());
204#else
205 auto ret = ::setenv(key.c_str(), value.c_str(), 1);
206#endif
207 return (ret == 0);
208}
209
218inline bool set_path(const std::string& key, const std::vector<std::string>& value)
219{
220 return set_string(key, join_path(value));
221}
222
231inline bool set_bool(const std::string& key, bool value)
232{
233 // Boolean environment variables are usually set as "1" or "0", and not
234 // as "true" or "false", therefore `to_string` is not used here.
235 return set_string(key, value ? "1" : "0");
236}
237
246template <typename T>
247inline bool set_numeric(const std::string& key, bool value)
248{
249 return set_string(key, yarp::conf::numeric::to_string(value));
250}
251
259inline bool unset(const std::string& key)
260{
261#if defined(_MSC_VER)
262 auto ret = _putenv_s(key.c_str(), "");
263#else
264 auto ret = ::unsetenv(key.c_str());
265#endif
266 return (ret == 0);
267}
268
275inline bool is_set(const std::string& key)
276{
277 return std::getenv(key.c_str()) ? true : false;
278}
279
280} // namespace yarp::conf::environment
281
282#endif // YARP_CONF_ENVIRONMENT_H
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...
Definition environment.h:39
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,...
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.
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.
Definition environment.h:65
bool set_numeric(const std::string &key, bool value)
Set a numeric value to an environment variable.
std::string to_string(IntegerType x)
Definition numeric.h:116