Skip to content

Commit 2237665

Browse files
authored
Improve ofGetEnv with default value. (#7587)
#changelog #utils
1 parent fba7db2 commit 2237665

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

libs/openFrameworks/utils/ofUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,22 +1102,22 @@ ofTargetPlatform ofGetTargetPlatform(){
11021102
#endif
11031103
}
11041104

1105-
std::string ofGetEnv(const std::string & var){
1105+
std::string ofGetEnv(const std::string & var, const std::string defaultValue){
11061106
#ifdef TARGET_WIN32
11071107
const size_t BUFSIZE = 4096;
11081108
std::vector<char> pszOldVal(BUFSIZE, 0);
11091109
auto size = GetEnvironmentVariableA(var.c_str(), pszOldVal.data(), BUFSIZE);
11101110
if(size>0){
11111111
return std::string(pszOldVal.begin(), pszOldVal.begin()+size);
11121112
}else{
1113-
return "";
1113+
return defaultValue;
11141114
}
11151115
#else
11161116
auto value = getenv(var.c_str());
11171117
if(value){
11181118
return value;
11191119
}else{
1120-
return "";
1120+
return defaultValue;
11211121
}
11221122
#endif
11231123
}

libs/openFrameworks/utils/ofUtils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,10 @@ ofTargetPlatform ofGetTargetPlatform();
10711071
/// \brief Get the value of a given environment variable.
10721072
///
10731073
/// \note The available environment variables differ between operating systems.
1074-
/// \returns the environmnt variable's value or an empty string if not found.
1075-
std::string ofGetEnv(const std::string & var);
1074+
/// \param var the environment variable name.
1075+
/// \param defaultValue the value to return if the environment variable is not set. defaults to empty string.
1076+
/// \returns the environmnt variable's value or the provided default value if not found.
1077+
std::string ofGetEnv(const std::string & var, const std::string defaultValue = "");
10761078

10771079
/// \brief Iterate through each Unicode codepoint in a UTF8-encoded std::string.
10781080
///

tests/utils/utils/addons.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ofxUnitTests

tests/utils/utils/src/main.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "utils/ofUtils.h"
2+
#include "ofxUnitTests.h"
3+
4+
5+
class ofApp: public ofxUnitTestsApp{
6+
void run(){
7+
ofLogNotice() << "testing utils/ofUtils";
8+
ofLogNotice() << "testing ofGetEnv() on unset environment variable";
9+
ofxTest(ofGetEnv("DUMMY")=="", "it should return an empty string when called with no default value.");
10+
ofxTest(ofGetEnv("DUMMY","default")=="default", "it should return the deefault when it is provided.");
11+
ofLogNotice() << "testing ofGetEnv() on set environment variable (PATH)";
12+
ofxTest(ofGetEnv("PATH")!="", "it should return a (non empty) string when called with no default value.");
13+
ofxTest(ofGetEnv("DUMMY","default")!="", "it should return a (non empty) string when a default value is provided.");
14+
ofxTest(ofGetEnv("DUMMY","default")!="defautl", "it should not return the default value.");
15+
}
16+
};
17+
18+
19+
#include "app/ofAppNoWindow.h"
20+
#include "app/ofAppRunner.h"
21+
//========================================================================
22+
int main( ){
23+
ofInit();
24+
auto window = std::make_shared<ofAppNoWindow>();
25+
auto app = std::make_shared<ofApp>();
26+
ofRunApp(window, app);
27+
return ofRunMainLoop();
28+
}

0 commit comments

Comments
 (0)