summaryrefslogtreecommitdiffstats
path: root/gfx/vr/service/openvr/src/envvartools_public.cpp
blob: 4fb4817927973945e84c4f7ef5d91c7b2e38aa62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//========= Copyright Valve Corporation ============//
#include "envvartools_public.h"
#include "strtools_public.h"
#include <stdlib.h>
#include <string>
#include <cctype>

#if defined(_WIN32)
#include <windows.h>

#undef GetEnvironmentVariable
#undef SetEnvironmentVariable
#endif


std::string GetEnvironmentVariable( const char *pchVarName )
{
#if defined(_WIN32)
	char rchValue[32767]; // max size for an env var on Windows
	DWORD cChars = GetEnvironmentVariableA( pchVarName, rchValue, sizeof( rchValue ) );
	if( cChars == 0 )
		return "";
	else
		return rchValue;
#elif defined(POSIX)
	char *pchValue = getenv( pchVarName );
	if( pchValue )
		return pchValue;
	else
		return "";
#else
#error "Unsupported Platform"
#endif
}

bool GetEnvironmentVariableAsBool( const char *pchVarName, bool bDefault )
{
	std::string sValue = GetEnvironmentVariable( pchVarName );

	if ( sValue.empty() )
	{
		return bDefault;
	}

	sValue = StringToLower( sValue );
	std::string sYesValues[] = { "y", "yes", "true" };
	std::string sNoValues[] = { "n", "no", "false" };

	for ( std::string &sMatch : sYesValues )
	{
		if ( sMatch == sValue )
		{
			return true;
		}
	}

	for ( std::string &sMatch : sNoValues )
	{
		if ( sMatch == sValue )
		{
			return false;
		}
	}

	if ( std::isdigit( sValue.at(0) ) )
	{
		return atoi( sValue.c_str() ) != 0;
	}

	fprintf( stderr,
			 "GetEnvironmentVariableAsBool(%s): Unable to parse value '%s', using default %d\n",
			 pchVarName, sValue.c_str(), bDefault );
	return bDefault;
}

bool SetEnvironmentVariable( const char *pchVarName, const char *pchVarValue )
{
#if defined(_WIN32)
	return 0 != SetEnvironmentVariableA( pchVarName, pchVarValue );
#elif defined(POSIX)
	if( pchVarValue == NULL )
		return 0 == unsetenv( pchVarName );
	else
		return 0 == setenv( pchVarName, pchVarValue, 1 );
#else
#error "Unsupported Platform"
#endif
}