Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/openFrameworks/utils/ofUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,22 +1102,22 @@ ofTargetPlatform ofGetTargetPlatform(){
#endif
}

std::string ofGetEnv(const std::string & var){
std::string ofGetEnv(const std::string & var, const std::string defaultValue){
#ifdef TARGET_WIN32
const size_t BUFSIZE = 4096;
std::vector<char> pszOldVal(BUFSIZE, 0);
auto size = GetEnvironmentVariableA(var.c_str(), pszOldVal.data(), BUFSIZE);
if(size>0){
return std::string(pszOldVal.begin(), pszOldVal.begin()+size);
}else{
return "";
return defaultValue;
}
#else
auto value = getenv(var.c_str());
if(value){
return value;
}else{
return "";
return defaultValue;
}
#endif
}
6 changes: 4 additions & 2 deletions libs/openFrameworks/utils/ofUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,10 @@ ofTargetPlatform ofGetTargetPlatform();
/// \brief Get the value of a given environment variable.
///
/// \note The available environment variables differ between operating systems.
/// \returns the environmnt variable's value or an empty string if not found.
std::string ofGetEnv(const std::string & var);
/// \param var the environment variable name.
/// \param defaultValue the value to return if the environment variable is not set. defaults to empty string.
/// \returns the environmnt variable's value or the provided default value if not found.
std::string ofGetEnv(const std::string & var, const std::string defaultValue = "");

/// \brief Iterate through each Unicode codepoint in a UTF8-encoded std::string.
///
Expand Down
1 change: 1 addition & 0 deletions tests/utils/utils/addons.make
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ofxUnitTests
28 changes: 28 additions & 0 deletions tests/utils/utils/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "utils/ofUtils.h"
#include "ofxUnitTests.h"


class ofApp: public ofxUnitTestsApp{
void run(){
ofLogNotice() << "testing utils/ofUtils";
ofLogNotice() << "testing ofGetEnv() on unset environment variable";
ofxTest(ofGetEnv("DUMMY")=="", "it should return an empty string when called with no default value.");
ofxTest(ofGetEnv("DUMMY","default")=="default", "it should return the deefault when it is provided.");
ofLogNotice() << "testing ofGetEnv() on set environment variable (PATH)";
ofxTest(ofGetEnv("PATH")!="", "it should return a (non empty) string when called with no default value.");
ofxTest(ofGetEnv("DUMMY","default")!="", "it should return a (non empty) string when a default value is provided.");
ofxTest(ofGetEnv("DUMMY","default")!="defautl", "it should not return the default value.");
}
};


#include "app/ofAppNoWindow.h"
#include "app/ofAppRunner.h"
//========================================================================
int main( ){
ofInit();
auto window = std::make_shared<ofAppNoWindow>();
auto app = std::make_shared<ofApp>();
ofRunApp(window, app);
return ofRunMainLoop();
}