2017-11-01 21:32:47 +08:00
|
|
|
/* -*-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 <osg/Config>
|
|
|
|
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
2017-11-02 01:38:33 +08:00
|
|
|
#include <stdlib.h>
|
2017-11-01 21:32:47 +08:00
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
inline unsigned int getClampedLength(const char* str, unsigned int maxNumChars=4096)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
while(i<maxNumChars && str[i]!=0) { ++i; }
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:38:33 +08:00
|
|
|
inline std::string getEnvVar(const char* name)
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (ptr) value.assign(ptr, getClampedLength(ptr));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-01 21:32:47 +08:00
|
|
|
template<typename T>
|
2017-11-01 22:16:54 +08:00
|
|
|
inline bool getEnvVar(const char* name, T& value)
|
2017-11-01 21:32:47 +08:00
|
|
|
{
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (!ptr) return false;
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
std::istringstream str(std::string(ptr, getClampedLength(ptr)));
|
2017-11-01 21:32:47 +08:00
|
|
|
str >> value;
|
|
|
|
return !str.fail();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2017-11-01 22:16:54 +08:00
|
|
|
inline bool getEnvVar(const char* name, std::string& value)
|
2017-11-01 21:32:47 +08:00
|
|
|
{
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (!ptr) return false;
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
value.assign(ptr, getClampedLength(ptr));
|
2017-11-01 21:32:47 +08:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1, typename T2>
|
2017-11-01 22:16:54 +08:00
|
|
|
inline bool getEnvVar(const char* name, T1& value1, T2& value2)
|
2017-11-01 21:32:47 +08:00
|
|
|
{
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (!ptr) return false;
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
std::istringstream str(std::string(ptr, getClampedLength(ptr)));
|
2017-11-01 21:32:47 +08:00
|
|
|
str >> value1 >> value2;
|
|
|
|
return !str.fail();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1, typename T2, typename T3>
|
2017-11-01 22:16:54 +08:00
|
|
|
inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3)
|
2017-11-01 21:32:47 +08:00
|
|
|
{
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (!ptr) return false;
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
std::istringstream str(std::string(ptr, getClampedLength(ptr)));
|
2017-11-01 21:32:47 +08:00
|
|
|
str >> value1 >> value2 >> value3;
|
|
|
|
return !str.fail();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1, typename T2, typename T3, typename T4>
|
2017-11-01 22:16:54 +08:00
|
|
|
inline bool getEnvVar(const char* name, T1& value1, T2& value2, T3& value3, T4& value4)
|
2017-11-01 21:32:47 +08:00
|
|
|
{
|
|
|
|
#ifdef OSG_ENVVAR_SUPPORTED
|
|
|
|
const char* ptr = getenv(name);
|
|
|
|
if (!ptr) return false;
|
|
|
|
|
2017-11-02 00:43:32 +08:00
|
|
|
std::istringstream str(std::string(ptr, getClampedLength(ptr)));
|
2017-11-01 21:32:47 +08:00
|
|
|
str >> value1 >> value2 >> value3 >> value4;
|
|
|
|
return !str.fail();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# endif
|