<wx/utils.h>
-\membersection{wxGetEnv}\label{wxgetenv}
+\membersection{wxGetenv}\label{wxgetenvmacro}
\func{wxChar *}{wxGetEnv}{\param{const wxString\&}{ var}}
-Returns the current value of the environment variable {\it var} or {\tt NULL}
-if it doesn't exist.
+This is a macro defined as {\tt getenv()} or its wide char version in Unicode
+mode.
+
+Note that under Win32 it may not return correct value for the variables set
+with \helpref{wxSetEnv}{wxsetenv}, use \helpref{wxGetEnv}{wxgetenv} function
+instead.
+
+\membersection{wxGetEnv}\label{wxgetenv}
+
+\func{bool}{wxGetEnv}{\param{const wxString\&}{ var}, \param{wxString *}{value}}
+
+Returns the current value of the environment variable {\it var} in {\it value}.
+{\it value} may be {\tt NULL} if you just want to know if the variable exists
+and are not interested in its value.
+
+Returns {\tt TRUE} if the variable exists, {\tt FALSE} otherwise.
\membersection{wxSetEnv}\label{wxsetenv}
// Environment variables
// ----------------------------------------------------------------------------
-// wxGetenv is declared in wxchar.h, but define a wrapper/synonym for it for
-// consistency with wxSetEnv
-inline const wxChar *wxGetEnv(const wxString& var) { return wxGetenv(var); }
+// returns TRUE if variable exists (value may be NULL if you just want to check
+// for this)
+WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value);
// set the env var name to the given value, return TRUE on success
WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value);
#include <wx/utils.h>
+static wxString MyGetEnv(const wxString& var)
+{
+ wxString val;
+ if ( !wxGetEnv(var, &val) )
+ val = _T("<empty>");
+ else
+ val = wxString(_T('\'')) + val + _T('\'');
+
+ return val;
+}
+
static void TestEnvironment()
{
const wxChar *var = _T("wxTestVar");
puts("*** testing environment access functions ***");
- printf("Initially getenv(%s) = '%s'\n", var, wxGetenv(var));
+ printf("Initially getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
wxSetEnv(var, _T("value for wxTestVar"));
- printf("After wxSetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var));
+ printf("After wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
wxSetEnv(var, _T("another value"));
- printf("After 2nd wxSetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var));
+ printf("After 2nd wxSetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
wxUnsetEnv(var);
- printf("After wxUnsetEnv: getenv(%s) = '%s'\n", var, wxGetenv(var));
+ printf("After wxUnsetEnv: getenv(%s) = %s\n", var, MyGetEnv(var).c_str());
+ printf("PATH = %s\n", MyGetEnv(_T("PATH")));
}
#endif // TEST_ENVIRON
#endif // Win32/16
}
+// ----------------------------------------------------------------------------
+// env vars
+// ----------------------------------------------------------------------------
+
+bool wxGetEnv(const wxString& var, wxString *value)
+{
+ // first get the size of the buffer
+ DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
+ if ( !dwRet )
+ {
+ // this means that there is no such variable
+ return FALSE;
+ }
+
+ if ( value )
+ {
+ (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet);
+ value->UngetWriteBuf();
+ }
+
+ return TRUE;
+}
+
bool wxSetEnv(const wxString& var, const wxChar *value)
{
// some compilers have putenv() or _putenv() or _wputenv() but it's better
// env vars
// ----------------------------------------------------------------------------
+bool wxGetEnv(const wxString& var, wxString *value);
+{
+ // wxGetenv is defined as getenv()
+ wxChar *p = wxGetenv(var);
+ if ( !p )
+ return FALSE;
+
+ if ( value )
+ {
+ *value = p;
+ }
+
+ return TRUE;
+}
+
bool wxSetEnv(const wxString& variable, const wxChar *value)
{
#if defined(HAVE_SETENV)