diff --git a/CMakeLists.txt b/CMakeLists.txt index 16c20b47a..4c58f7cd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,6 +468,10 @@ OPTION(OSG_PROVIDE_READFILE "Set to ON for include/osgDB/ReadFile to provide the OPTION(OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION "Set to ON to use the ref_ptr<> T* operator() output conversion. " ON) OPTION(OSG_USE_REF_PTR_SAFE_DEREFERENCE "Set to ON to throw an exception whenever ref_ptr<> is dereferenced or called. " OFF) +OPTION(OSG_ENVVAR_SUPPORTED "Set to ON to build OpenSceneGraph with the OSG_ENVVAR_SUPPOERTED #define enabled to enable use of getenv() related functions." ON) + + + # Map the OPENGL_PROFILE to OSG_GL*_AVAILABLE settings SET(OPENGL_PROFILE "GL2" CACHE STRING "OpenGL Profile to use, choose from GL1, GL2, GL3, GLES1, GLES2, GLES3") diff --git a/include/osg/EnvVar b/include/osg/EnvVar new file mode 100644 index 000000000..5a8cb043a --- /dev/null +++ b/include/osg/EnvVar @@ -0,0 +1,101 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2017 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_EnvVar +#define OSG_EnvVar 1 + +#include + +#ifdef OSG_ENVVAR_SUPPORTED + #include +#endif + +namespace osg { + +template +bool getEnvVar(const char* name, T& value) +{ +#ifdef OSG_ENVVAR_SUPPORTED + const char* ptr = getenv(name); + if (!ptr) return false; + + std::istringstream str(ptr); + str >> value; + return !str.fail(); +#else + return false; +#endif +} + +template<> +bool getEnvVar(const char* name, std::string& value) +{ +#ifdef OSG_ENVVAR_SUPPORTED + const char* ptr = getenv(name); + if (!ptr) return false; + + value = ptr; + return true; +#else + return false; +#endif +} + +template +bool getEnvVar(const char* name, T1& value1, T2& value2) +{ +#ifdef OSG_ENVVAR_SUPPORTED + const char* ptr = getenv(name); + if (!ptr) return false; + + std::istringstream str(ptr); + str >> value1 >> value2; + return !str.fail(); +#else + return false; +#endif +} + +template +bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3) +{ +#ifdef OSG_ENVVAR_SUPPORTED + const char* ptr = getenv(name); + if (!ptr) return false; + + std::istringstream str(ptr); + str >> value1 >> value2 >> value3; + return !str.fail(); +#else + return false; +#endif +} + +template +bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3, T4& value4) +{ +#ifdef OSG_ENVVAR_SUPPORTED + const char* ptr = getenv(name); + if (!ptr) return false; + + std::istringstream str(ptr); + str >> value1 >> value2 >> value3 >> value4; + return !str.fail(); +#else + return false; +#endif +} + +} + +# endif diff --git a/src/osg/Config.in b/src/osg/Config.in index 3d52f9ca3..8f4c23359 100644 --- a/src/osg/Config.in +++ b/src/osg/Config.in @@ -34,5 +34,6 @@ #cmakedefine OSG_DISABLE_MSVC_WARNINGS #cmakedefine OSG_PROVIDE_READFILE #cmakedefine OSG_USE_DEPRECATED_API +#cmakedefine OSG_ENVVAR_SUPPORTED #endif