File tree Expand file tree Collapse file tree
libs/openFrameworks/utils Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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// /
Original file line number Diff line number Diff line change 1+ ofxUnitTests
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments