Replaced getenv usage with safer osg::getEnvVar

This commit is contained in:
Robert Osfield 2017-11-02 10:43:41 +00:00
parent aa744edacc
commit ce69f18ec7
3 changed files with 36 additions and 32 deletions

View File

@ -17,6 +17,7 @@
#include <osg/ApplicationUsage>
#include <osg/Object>
#include <osg/Math>
#include <osg/EnvVar>
#include <osg/ref_ptr>
#include <sstream>
@ -317,10 +318,16 @@ void ApplicationUsage::writeEnvironmentSettings(std::ostream& output)
std::string::size_type len = citr->first.find_first_of("\n\r\t ");
if (len == std::string::npos) len = citr->first.size();
line.replace(optionPos,len,citr->first.substr(0,len));
const char *cp = getenv(citr->first.substr(0, len).c_str());
if (!cp) cp = "[not set]";
else if (!*cp) cp = "[set]";
line += std::string(cp) + "\n";
std::string value;
if (getEnvVar(citr->first.substr(0, len).c_str(), value))
{
line += "[set]\n";
}
else
{
line += "[not set]\n";
}
output << line;
}

View File

@ -16,6 +16,7 @@
#include <osg/CullSettings>
#include <osg/ArgumentParser>
#include <osg/ApplicationUsage>
#include <osg/EnvVar>
#include <osg/Notify>
@ -97,25 +98,21 @@ void CullSettings::readEnvironmentalVariables()
{
OSG_INFO<<"CullSettings::readEnvironmentalVariables()"<<std::endl;
char *ptr;
if ((ptr = getenv("OSG_COMPUTE_NEAR_FAR_MODE")) != 0)
std::string value;
if (getEnvVar("OSG_COMPUTE_NEAR_FAR_MODE", value))
{
if (strcmp(ptr,"DO_NOT_COMPUTE_NEAR_FAR")==0) _computeNearFar = DO_NOT_COMPUTE_NEAR_FAR;
else if (strcmp(ptr,"COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES")==0) _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
else if (strcmp(ptr,"COMPUTE_NEAR_FAR_USING_PRIMITIVES")==0) _computeNearFar = COMPUTE_NEAR_FAR_USING_PRIMITIVES;
if (value=="DO_NOT_COMPUTE_NEAR_FAR") _computeNearFar = DO_NOT_COMPUTE_NEAR_FAR;
else if (value=="COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES") _computeNearFar = COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES;
else if (value=="COMPUTE_NEAR_FAR_USING_PRIMITIVES") _computeNearFar = COMPUTE_NEAR_FAR_USING_PRIMITIVES;
OSG_INFO<<"Set compute near far mode to "<<_computeNearFar<<std::endl;
}
if ((ptr = getenv("OSG_NEAR_FAR_RATIO")) != 0)
if (getEnvVar("OSG_NEAR_FAR_RATIO", _nearFarRatio))
{
_nearFarRatio = osg::asciiToDouble(ptr);
OSG_INFO<<"Set near/far ratio to "<<_nearFarRatio<<std::endl;
}
}
void CullSettings::readCommandLine(ArgumentParser& arguments)

View File

@ -583,10 +583,9 @@ void DisplaySettings::readEnvironmentalVariables()
for( unsigned int n = 0; n < sizeof( variable ) / sizeof( variable[0] ); n++ )
{
const char* env = getenv( variable[n] );
if ( !env ) continue;
std::string str(env);
std::string str;
if (getEnvVar(variable[n], str))
{
if(str.find("OFF")!=std::string::npos) *mask[n] = 0;
if(str.find("~DEFAULT")!=std::string::npos) *mask[n] ^= DEFAULT_IMPLICIT_BUFFER_ATTACHMENT;
@ -602,6 +601,7 @@ void DisplaySettings::readEnvironmentalVariables()
else if(str.find("STENCIL")!=std::string::npos) *mask[n] |= (int)IMPLICIT_STENCIL_BUFFER_ATTACHMENT;
}
}
}
if (getEnvVar("OSG_GL_VERSION", value) || getEnvVar("OSG_GL_CONTEXT_VERSION", value))
{
@ -1110,7 +1110,7 @@ void DisplaySettings::setValue(const std::string& name, const std::string& value
_valueMap[name] = value;
}
bool DisplaySettings::getValue(const std::string& name, std::string& value, bool use_getenv_fallback) const
bool DisplaySettings::getValue(const std::string& name, std::string& value, bool use_env_fallback) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_valueMapMutex);
@ -1122,12 +1122,12 @@ bool DisplaySettings::getValue(const std::string& name, std::string& value, bool
return true;
}
if (!use_getenv_fallback) return false;
if (!use_env_fallback) return false;
const char* str = getenv(name.c_str());
if (str)
std::string str;
if (getEnvVar(name.c_str(), str))
{
OSG_INFO<<"DisplaySettings::getValue("<<name<<") found getenv value = ["<<value<<"]"<<std::endl;
OSG_INFO<<"DisplaySettings::getValue("<<name<<") found getEnvVar value = ["<<value<<"]"<<std::endl;
_valueMap[name] = value = str;
return true;