10#include "Teuchos_EnvVariables.hpp"
35bool environmentVariableNameIsValid(
const char name[]) {
36 return name !=
nullptr && std::strchr(name,
'=') ==
nullptr;
39void throwEnvironmentVariablePlatformError(
const char operation[],
41 const std::string& detail) {
42 std::ostringstream oss;
43 oss <<
"Teuchos environment variable " << operation <<
" failed";
45 oss <<
" for \"" << name <<
"\"";
47 oss <<
": " << detail;
56void setEnvironmentVariableImpl(
const char* name,
const char* value,
int overwrite) {
57 if(!environmentVariableNameIsValid(name) || value ==
nullptr) {
63 const errno_t dupStatus = _dupenv_s(std::addressof(buf), std::addressof(bufSize), name);
65 std::ostringstream oss;
66 oss <<
"_dupenv_s returned " << dupStatus;
67 throwEnvironmentVariablePlatformError(
"set", name, oss.str());
74 const errno_t putStatus = _putenv_s(name, value);
76 std::ostringstream oss;
77 oss <<
"_putenv_s returned " << putStatus;
78 throwEnvironmentVariablePlatformError(
"set", name, oss.str());
82void unsetEnvironmentVariableImpl(
const char* name) {
83 if(!environmentVariableNameIsValid(name)) {
86 const errno_t putStatus = _putenv_s(name,
"");
88 std::ostringstream oss;
89 oss <<
"_putenv_s returned " << putStatus;
90 throwEnvironmentVariablePlatformError(
"unset", name, oss.str());
94void setEnvironmentVariableImpl(
const char* name,
const char* value,
int overwrite) {
95 if(!environmentVariableNameIsValid(name) || value ==
nullptr) {
98 if(setenv(name, value, overwrite) != 0) {
99 const int err = errno;
100 std::ostringstream oss;
101 oss <<
"setenv returned -1, errno=" << err;
103 oss <<
" (" << std::strerror(err) <<
")";
105 throwEnvironmentVariablePlatformError(
"set", name, oss.str());
109void unsetEnvironmentVariableImpl(
const char* name) {
110 if(!environmentVariableNameIsValid(name)) {
113 if(unsetenv(name) != 0) {
114 const int err = errno;
115 std::ostringstream oss;
116 oss <<
"unsetenv returned -1, errno=" << err;
118 oss <<
" (" << std::strerror(err) <<
")";
120 throwEnvironmentVariablePlatformError(
"unset", name, oss.str());
126const char* getEnvironmentVariableValueImpl(
const char* name) {
129 const errno_t dupStatus = _dupenv_s(std::addressof(buf), std::addressof(bufSize), name);
131 std::ostringstream oss;
132 oss <<
"_dupenv_s returned " << dupStatus;
133 throwEnvironmentVariablePlatformError(
"get", name, oss.str());
138 thread_local std::string valueStorage;
139 valueStorage.assign(buf);
141 return valueStorage.c_str();
144const char* getEnvironmentVariableValueImpl(
const char* name) {
150 return std::getenv(name);
165const char* getEnvironmentVariableValueFromView(std::string_view environmentVariableName) {
166 const std::string nameString(environmentVariableName);
180enum EnvironmentVariableState {
181 EnvironmentVariableIsSet_ON,
182 EnvironmentVariableIsSet_OFF,
183 EnvironmentVariableIsSet,
184 EnvironmentVariableIsNotSet
187EnvironmentVariableState
188environmentVariableState(
const std::string &environmentVariableValue) {
190 if (v ==
"1" || v ==
"YES" || v ==
"TRUE" || v ==
"ON")
192 return EnvironmentVariableIsSet_ON;
193 else if (v ==
"0" || v ==
"NO" || v ==
"FALSE" || v ==
"OFF")
195 return EnvironmentVariableIsSet_OFF;
197 return EnvironmentVariableIsSet;
200void setEnvironmentVariableMap(
201 const char environmentVariableName[],
202 std::map<std::string, std::map<std::string, bool>> &valsMap,
203 const bool defaultValue) {
209 valsMap[environmentVariableName] =
213 if (varVal ==
nullptr) {
220 const string varStr(varVal);
224 for (
auto const &name : names) {
225 auto state = environmentVariableState(name);
226 if (state == EnvironmentVariableIsSet_ON) {
229 valsMap[environmentVariableName][
"DEFAULT"] =
true;
230 }
else if (state == EnvironmentVariableIsSet_OFF) {
233 valsMap[environmentVariableName][
"DEFAULT"] =
false;
237 valsMap[environmentVariableName][name] =
true;
245T getEnvironmentVariable(std::string_view environmentVariableName,
246 const T defaultValue) {
247 const char* varVal = getEnvironmentVariableValueFromView(environmentVariableName);
248 if (varVal ==
nullptr) {
251 std::stringstream ss(varVal);
256 !ss, std::out_of_range,
257 "Environment variable \""
258 << environmentVariableName <<
"\" has a value " << varVal
259 <<
" that cannot be parsed as a " <<
typeid(T).name() <<
".");
267bool getEnvironmentVariable<bool>(
268 std::string_view environmentVariableName,
const bool defaultValue) {
269 const char* varVal = getEnvironmentVariableValueFromView(environmentVariableName);
270 bool retVal = defaultValue;
271 if (varVal !=
nullptr) {
272 auto state = environmentVariableState(std::string(varVal));
273 if (state == EnvironmentVariableIsSet_ON)
275 else if (state == EnvironmentVariableIsSet_OFF)
295 if (
val <
static_cast<long long>(0)) {
297 return std::numeric_limits<size_t>::max();
299 if (
sizeof(
long long) >
sizeof(
size_t)) {
304 static_cast<long long>(std::numeric_limits<size_t>::max());
307 "Environment variable \""
309 <<
" larger than the largest size_t value " <<
maxSizeT <<
".");
311 return static_cast<size_t>(
val);
315bool idempotentlyGetNamedEnvironmentVariableAsBool(
316 const char name[],
bool &initialized,
const char environmentVariableName[],
317 const bool defaultValue) {
318 static std::map<std::string, std::map<std::string, bool>> namedVariableMap_;
320 setEnvironmentVariableMap(environmentVariableName, namedVariableMap_,
324 auto thisEnvironmentVariableMap = namedVariableMap_[environmentVariableName];
325 auto thisEnvironmentVariable = thisEnvironmentVariableMap.find(name);
326 if (thisEnvironmentVariable != thisEnvironmentVariableMap.end())
327 return thisEnvironmentVariable->second;
328 return thisEnvironmentVariableMap[
"DEFAULT"];
343template std::string getEnvironmentVariable<std::string>(std::string_view,
const std::string);
345template std::string idempotentlyGetEnvironmentVariable<std::string>(std::string&,
bool&, std::string_view,
const std::string);
346template int idempotentlyGetEnvironmentVariable<int>(
int&,
bool&, std::string_view,
const int);
347template unsigned long idempotentlyGetEnvironmentVariable<unsigned long>(
unsigned long&,
bool&, std::string_view,
const unsigned long);
348template bool idempotentlyGetEnvironmentVariable<bool>(
bool&,
bool&, std::string_view,
const bool);
349template unsigned long long idempotentlyGetEnvironmentVariable<unsigned long long>(
unsigned long long&,
bool&, std::string_view,
const unsigned long long);
A std::string utilities class for Teuchos.
Standard test and throw macros.
Smart reference counting pointer class for automatic garbage collection.
static std::string allCaps(const std::string &str)
Converts a std::string to all upper case.
static Array< std::string > splitString(const std::string_view s, const char sep=',')
Split an input std::string using a seperator char sep.
This class creates a basic std::map object for platforms where the std::map is deficient,...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
void unsetEnvironmentVariable(const char name[])
Remove a process environment variable.
T idempotentlyGetEnvironmentVariable(T &value, bool &initialized, std::string_view environmentVariableName, const T defaultValue)
Read a variable from the environment. Example usage:
size_t getEnvironmentVariable< size_t >(std::string_view environmentVariableName, const size_t defaultValue)
const char * getEnvironmentVariableValue(const char name[])
Read a process environment variable by name.
void setEnvironmentVariable(const char name[], const char value[], int overwrite)
Set a process environment variable.