YARP
Yet Another Robot Platform
environment.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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/system.h>
14 
15 #include <string>
16 #include <cstdlib>
17 
18 namespace yarp {
19 namespace conf {
20 namespace environment {
21 
31 inline std::string getEnvironment(const char* key, bool* found = nullptr)
32 {
33  const char* result = std::getenv(key);
34  if (found != nullptr) {
35  *found = (result != nullptr);
36  }
37  if (result == nullptr) {
38  return {};
39  }
40  return std::string(result);
41 }
42 
51 inline void setEnvironment(const std::string& key, const std::string& val)
52 {
53 #if defined(_MSC_VER)
54  _putenv_s(key.c_str(), val.c_str());
55 #else
56  ::setenv(key.c_str(), val.c_str(), 1);
57 #endif
58 }
59 
67 inline void unsetEnvironment(const std::string& key)
68 {
69 #if defined(_MSC_VER)
70  _putenv_s(key.c_str(), "");
71 #else
72  ::unsetenv(key.c_str());
73 #endif
74 }
75 
76 
77 } // namespace filesystem
78 } // namespace conf
79 } // namespace yarp
80 
81 #endif // YARP_CONF_ENVIRONMENT_H
yarp::conf::environment::getEnvironment
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
Definition: environment.h:31
yarp::os::getenv
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:34
yarp::conf::environment::unsetEnvironment
void unsetEnvironment(const std::string &key)
Remove an environment variable.
Definition: environment.h:67
system.h
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::conf::environment::setEnvironment
void setEnvironment(const std::string &key, const std::string &val)
Set or change an environment variable.
Definition: environment.h:51