00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanEnvironmentVariables.h"
00028
00029 #include "CeylanSystem.h"
00030
00031 #ifdef CEYLAN_USES_CONFIG_H
00032 #include "CeylanConfig.h"
00033 #endif // CEYLAN_USES_CONFIG_H
00034
00035
00036
00037 #include <cstdlib>
00038
00039
00040 using std::string ;
00041
00042 using namespace Ceylan::System ;
00043
00044
00045
00046 bool Ceylan::System::isEnvironmentVariableSet( const string & variableName )
00047 {
00048
00049 return ( ! getEnvironmentVariable( variableName ).empty() ) ;
00050
00051 }
00052
00053
00054
00055 const string Ceylan::System::getEnvironmentVariable(
00056 const string & variableName )
00057 {
00058
00059 #ifdef CEYLAN_USES_GETENV
00060
00061
00062
00063 const char * value = ::getenv( variableName.c_str() ) ;
00064
00065 if ( value != 0 )
00066 return string( value ) ;
00067
00068 return "" ;
00069
00070 #else // CEYLAN_USES_GETENV
00071
00072 #ifdef CEYLAN_USES__DUPENV_S
00073
00074 char * returnedString ;
00075 size_t returnedLength ;
00076
00077 if ( ::_dupenv_s( &returnedString, &returnedLength,
00078 variableName.c_str() ) != 0 )
00079 throw UtilsException(
00080 "Ceylan::System::getEnvironmentVariable failed: "
00081 + System::explainError() ) ;
00082 if ( returnedString == 0 )
00083 {
00084
00085
00086 return "" ;
00087
00088 }
00089 else
00090 {
00091
00092 string res( returnedString ) ;
00093 ::free( returnedString ) ;
00094 return res ;
00095
00096 }
00097
00098 #else // CEYLAN_USES__DUPENV_S
00099
00100 throw UtilsException( "Ceylan::System::getEnvironmentVariable: "
00101 "not available on this platform." ) ;
00102
00103 #endif // CEYLAN_USES__DUPENV_S
00104
00105 #endif // CEYLAN_USES_GETENV
00106
00107 }
00108
00109
00110
00111 void Ceylan::System::setEnvironmentVariable( const string & variableName,
00112 const string & variableValue )
00113 {
00114
00115
00116
00117 #ifdef CEYLAN_USES_PUTENV
00118
00119 const string envString = variableName + "=" + variableValue ;
00120 char * newEnv = new char[ envString.size() + 1 ] ;
00121
00122
00123
00124
00125
00126
00127
00128 envString.copy( newEnv, envString.size() );
00129 newEnv[ envString.size() ] = 0 ;
00130
00131
00132 if ( ::putenv( newEnv ) != 0 )
00133 {
00134
00135 throw Ceylan::UtilsException( "Ceylan::SetEnvironmentVariable: "
00136 "unable to set the value of environment variable ["
00137 + variableName + "] to [" + variableValue + "]: "
00138 + System::explainError() ) ;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 #else // CEYLAN_USES_PUTENV
00150
00151 #ifdef CEYLAN_USES__PUTENV_S
00152
00153
00154 if ( ::_putenv_s( variableName.c_str(), variableValue.c_str() ) != 0 )
00155 {
00156 throw Ceylan::UtilsException( "Ceylan::SetEnvironmentVariable: "
00157 "unable to set the value of environment variable ["
00158 + variableName + "] to [" + variableValue + "]: "
00159 + System::explainError() ) ;
00160 }
00161
00162 #else // CEYLAN_USES__PUTENV_S
00163
00164
00165 throw UtilsException( "Ceylan::System::setEnvironmentVariable: "
00166 "not available on this platform." ) ;
00167
00168 #endif // CEYLAN_USES__PUTENV_S
00169
00170 #endif // CEYLAN_USES_PUTENV
00171
00172 }
00173
00174
00175
00176 void Ceylan::System::unsetEnvironmentVariable( const string & variableName )
00177 {
00178
00179 #ifdef CEYLAN_USES_UNSETENV
00180
00181 ::unsetenv( variableName.c_str() ) ;
00182
00183 #else // CEYLAN_USES_UNSETENV
00184
00185 setEnvironmentVariable( variableName, "" ) ;
00186
00187 #endif // CEYLAN_USES_UNSETENV
00188
00189 }
00190