-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.cpp
54 lines (45 loc) · 1.53 KB
/
env.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdlib>
#include <string>
#include "env.hpp"
double getEnvDouble(std::string const& envName, double const& defaultValue){
// Returns environment variable as double.
// (from https://stackoverflow.com/questions/5866134/how-to-read-linux-environment-variables-in-c)
const char* val = std::getenv(envName.c_str());
if ( val == 0 ){
return defaultValue;
}
else {
return std::atof(val);
}
}
int getEnvInt(std::string const& envName, int const& defaultValue) {
// Returns environment variable as integer.
// (from https://stackoverflow.com/questions/5866134/how-to-read-linux-environment-variables-in-c)
const char* val = std::getenv(envName.c_str());
if ( val == 0 ){
return defaultValue;
}
else {
// return std::atoi(val);
return (int) std::atof(val);
}
}
bool getEnvBool(std::string const& envName, bool const& defaultValue) {
// Returns environment variable as boolean.
// WARNING: Be EXTRA CAREFUL with this function, only use "0" and "1" as
// environment variables.
// (from https://stackoverflow.com/questions/5866134/how-to-read-linux-environment-variables-in-c)
return (bool) getEnvInt(envName, defaultValue);
}
std::string getEnvString(std::string const& envName,
std::string const& defaultValue) {
// Returns environment variable as string.
// (from https://stackoverflow.com/questions/5866134/how-to-read-linux-environment-variables-in-c)
const char* val = std::getenv(envName.c_str());
if ( val == 0 ){
return defaultValue;
}
else {
return std::string(val);
}
}