tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

envvartools_public.cpp (1863B)


      1 //========= Copyright Valve Corporation ============//
      2 #include "envvartools_public.h"
      3 #include "strtools_public.h"
      4 #include <stdlib.h>
      5 #include <string>
      6 #include <cctype>
      7 
      8 #if defined(_WIN32)
      9 #include <windows.h>
     10 
     11 #undef GetEnvironmentVariable
     12 #undef SetEnvironmentVariable
     13 #endif
     14 
     15 
     16 std::string GetEnvironmentVariable( const char *pchVarName )
     17 {
     18 #if defined(_WIN32)
     19 char rchValue[32767]; // max size for an env var on Windows
     20 DWORD cChars = GetEnvironmentVariableA( pchVarName, rchValue, sizeof( rchValue ) );
     21 if( cChars == 0 )
     22 	return "";
     23 else
     24 	return rchValue;
     25 #elif defined(POSIX)
     26 char *pchValue = getenv( pchVarName );
     27 if( pchValue )
     28 	return pchValue;
     29 else
     30 	return "";
     31 #else
     32 #error "Unsupported Platform"
     33 #endif
     34 }
     35 
     36 bool GetEnvironmentVariableAsBool( const char *pchVarName, bool bDefault )
     37 {
     38 std::string sValue = GetEnvironmentVariable( pchVarName );
     39 
     40 if ( sValue.empty() )
     41 {
     42 	return bDefault;
     43 }
     44 
     45 sValue = StringToLower( sValue );
     46 std::string sYesValues[] = { "y", "yes", "true" };
     47 std::string sNoValues[] = { "n", "no", "false" };
     48 
     49 for ( std::string &sMatch : sYesValues )
     50 {
     51 	if ( sMatch == sValue )
     52 	{
     53 		return true;
     54 	}
     55 }
     56 
     57 for ( std::string &sMatch : sNoValues )
     58 {
     59 	if ( sMatch == sValue )
     60 	{
     61 		return false;
     62 	}
     63 }
     64 
     65 if ( std::isdigit( sValue.at(0) ) )
     66 {
     67 	return atoi( sValue.c_str() ) != 0;
     68 }
     69 
     70 fprintf( stderr,
     71 		 "GetEnvironmentVariableAsBool(%s): Unable to parse value '%s', using default %d\n",
     72 		 pchVarName, sValue.c_str(), bDefault );
     73 return bDefault;
     74 }
     75 
     76 bool SetEnvironmentVariable( const char *pchVarName, const char *pchVarValue )
     77 {
     78 #if defined(_WIN32)
     79 return 0 != SetEnvironmentVariableA( pchVarName, pchVarValue );
     80 #elif defined(POSIX)
     81 if( pchVarValue == NULL )
     82 	return 0 == unsetenv( pchVarName );
     83 else
     84 	return 0 == setenv( pchVarName, pchVarValue, 1 );
     85 #else
     86 #error "Unsupported Platform"
     87 #endif
     88 }