From 90a77e64841dfcaf37103b6411987402739baa0b Mon Sep 17 00:00:00 2001
From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= <vslavik@fastmail.fm>
Date: Tue, 19 Jun 2007 20:02:49 +0000
Subject: [PATCH] make wxSetEnv compatible with ANSI and Unicode, deprecate
 passing NULL to it in favour of wxUnsetEnv()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46547 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/function.tex | 10 +++++++++-
 include/wx/utils.h         | 24 ++++++++++++++++++++++--
 src/mac/carbon/utils.cpp   |  8 +++++++-
 src/msdos/utilsdos.cpp     | 13 ++++++++++++-
 src/msw/utils.cpp          | 16 +++++++++++++---
 src/os2/utils.cpp          | 15 ++++++++++++---
 src/palmos/utils.cpp       |  5 +++++
 src/unix/utilsunx.cpp      | 17 ++++++++++++-----
 8 files changed, 92 insertions(+), 16 deletions(-)

diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 6560fad01a..b8e866eb9f 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -4686,13 +4686,17 @@ Returns \true if the variable exists, \false otherwise.
 
 \membersection{wxSetEnv}\label{wxsetenv}
 
-\func{bool}{wxSetEnv}{\param{const wxString\&}{ var}, \param{const wxChar *}{value}}
+\func{bool}{wxSetEnv}{\param{const wxString\&}{ var}, \param{const wxString\& }{value}}
 
 Sets the value of the environment variable {\it var} (adding it if necessary)
 to {\it value}.
 
 Returns \true on success.
 
+\wxheading{See also}
+
+\helpref{wxUnsetEnv}{wxunsetenv}
+
 
 \membersection{wxUnsetEnv}\label{wxunsetenv}
 
@@ -4703,3 +4707,7 @@ Removes the variable {\it var} from the environment.
 function.
 
 Returns \true on success.
+
+\wxheading{See also}
+
+\helpref{wxSetEnv}{wxsetenv}
diff --git a/include/wx/utils.h b/include/wx/utils.h
index 43c6a3c11a..a50c70ad8a 100644
--- a/include/wx/utils.h
+++ b/include/wx/utils.h
@@ -457,10 +457,30 @@ WXDLLIMPEXP_BASE bool wxHandleFatalExceptions(bool doit = true);
 WXDLLIMPEXP_BASE bool wxGetEnv(const wxString& var, wxString *value);
 
 // set the env var name to the given value, return true on success
-WXDLLIMPEXP_BASE bool wxSetEnv(const wxString& var, const wxChar *value);
+WXDLLIMPEXP_BASE bool wxSetEnv(const wxString& var, const wxString& value);
 
 // remove the env var from environment
-inline bool wxUnsetEnv(const wxString& var) { return wxSetEnv(var, NULL); }
+WXDLLIMPEXP_BASE bool wxUnsetEnv(const wxString& var);
+
+#if WXWIN_COMPATIBILITY_2_8
+inline bool wxSetEnv(const wxString& var, const char *value)
+    { return wxSetEnv(var, wxString(value)); }
+inline bool wxSetEnv(const wxString& var, const wchar_t *value)
+    { return wxSetEnv(var, wxString(value)); }
+template<typename T>
+inline bool wxSetEnv(const wxString& var, const wxCharTypeBuffer<T>& value)
+    { return wxSetEnv(var, wxString(value)); }
+inline bool wxSetEnv(const wxString& var, const wxCStrData& value)
+    { return wxSetEnv(var, wxString(value)); }
+
+// this one is for passing NULL directly - don't use it, use wxUnsetEnv instead
+wxDEPRECATED( inline bool wxSetEnv(const wxString& var, int value) );
+inline bool wxSetEnv(const wxString& var, int value)
+{
+    wxASSERT_MSG( value == 0, "using non-NULL integer as string?" );
+    return wxUnsetEnv(var);
+}
+#endif // WXWIN_COMPATIBILITY_2_8
 
 // ----------------------------------------------------------------------------
 // Network and username functions.
diff --git a/src/mac/carbon/utils.cpp b/src/mac/carbon/utils.cpp
index edf0c4a45c..9afe0b6d5b 100644
--- a/src/mac/carbon/utils.cpp
+++ b/src/mac/carbon/utils.cpp
@@ -306,7 +306,13 @@ 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)
+WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxString& value)
+{
+    // TODO : under classic there is no environement support, under X yes
+    return false;
+}
+
+WXDLLEXPORT bool wxUnsetEnv(const wxString& var)
 {
     // TODO : under classic there is no environement support, under X yes
     return false;
diff --git a/src/msdos/utilsdos.cpp b/src/msdos/utilsdos.cpp
index 3b9328c561..6f432f4f89 100644
--- a/src/msdos/utilsdos.cpp
+++ b/src/msdos/utilsdos.cpp
@@ -96,7 +96,7 @@ bool wxGetEnv(const wxString& var, wxString *value)
     return true;
 }
 
-bool wxSetEnv(const wxString& variable, const wxChar *value)
+static bool wxDoSetEnv(const wxString& variable, const char *value)
 {
     wxString s = variable;
     if ( value )
@@ -112,6 +112,17 @@ bool wxSetEnv(const wxString& variable, const wxChar *value)
     return putenv(buf) == 0;
 }
 
+bool wxSetEnv(const wxString& variable, const wxString& value)
+{
+    return wxDoSetEnv(variable, value.mb_str());
+}
+
+bool wxUnsetEnv(const wxString& variable)
+{
+    return wxDoSetEnv(variable, NULL);
+}
+
+
 //----------------------------------------------------------------------------
 // Hostname, username, home directory
 //----------------------------------------------------------------------------
diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp
index 14bfb8e2b4..df8a474ea7 100644
--- a/src/msw/utils.cpp
+++ b/src/msw/utils.cpp
@@ -625,8 +625,8 @@ bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
 #endif // WinCE/32
 }
 
-bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
-              const wxChar *WXUNUSED_IN_WINCE(value))
+bool wxDoSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
+                const wxChar *WXUNUSED_IN_WINCE(value))
 {
     // some compilers have putenv() or _putenv() or _wputenv() but it's better
     // to always use Win32 function directly instead of dealing with them
@@ -634,7 +634,7 @@ bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
     // no environment variables under CE
     return false;
 #else
-    if ( !::SetEnvironmentVariable(var, value) )
+    if ( !::SetEnvironmentVariable(var.wx_str(), value) )
     {
         wxLogLastError(_T("SetEnvironmentVariable"));
 
@@ -645,6 +645,16 @@ bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
 #endif
 }
 
+bool wxSetEnv(const wxString& variable, const wxString& value)
+{
+    return wxDoSetEnv(variable, value.wx_str());
+}
+
+bool wxUnsetEnv(const wxString& variable)
+{
+    return wxDoSetEnv(variable, NULL);
+}
+
 // ----------------------------------------------------------------------------
 // process management
 // ----------------------------------------------------------------------------
diff --git a/src/os2/utils.cpp b/src/os2/utils.cpp
index 5576bc9b82..00aafc22cb 100644
--- a/src/os2/utils.cpp
+++ b/src/os2/utils.cpp
@@ -238,11 +238,10 @@ bool wxGetEnv(const wxString& var, wxString *value)
     return true;
 }
 
-bool wxSetEnv(const wxString& variable, const wxChar *value)
+bool wxSetEnv(const wxString& variable, const char *value)
 {
 #if defined(HAVE_SETENV)
-    return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
-                                           : NULL, 1 /* overwrite */) == 0;
+    return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0;
 #elif defined(HAVE_PUTENV)
     wxString s = variable;
     if ( value )
@@ -263,6 +262,16 @@ bool wxSetEnv(const wxString& variable, const wxChar *value)
 #endif
 }
 
+bool wxSetEnv(const wxString& variable, const wxString& value)
+{
+    return wxDoSetEnv(variable, value.mb_str());
+}
+
+bool wxUnsetEnv(const wxString& variable)
+{
+    return wxDoSetEnv(variable, NULL);
+}
+
 void wxMilliSleep(
   unsigned long                     ulMilliseconds
 )
diff --git a/src/palmos/utils.cpp b/src/palmos/utils.cpp
index f9f5806ec8..c8d8d84908 100644
--- a/src/palmos/utils.cpp
+++ b/src/palmos/utils.cpp
@@ -127,6 +127,11 @@ bool wxSetEnv(const wxString& var, const wxChar *value)
     return false;
 }
 
+bool wxUnsetEnv(const wxString& var)
+{
+    return false;
+}
+
 // ----------------------------------------------------------------------------
 // process management
 // ----------------------------------------------------------------------------
diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp
index 25b1558c73..88e0e5101f 100644
--- a/src/unix/utilsunx.cpp
+++ b/src/unix/utilsunx.cpp
@@ -1064,13 +1064,10 @@ bool wxGetEnv(const wxString& var, wxString *value)
     return true;
 }
 
-bool wxSetEnv(const wxString& variable, const wxChar *value)
+static bool wxDoSetEnv(const wxString& variable, const char *value)
 {
 #if defined(HAVE_SETENV)
-    return setenv(variable.mb_str(),
-                  value ? (const char *)wxString(value).mb_str()
-                        : NULL,
-                  1 /* overwrite */) == 0;
+    return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0;
 #elif defined(HAVE_PUTENV)
     wxString s = variable;
     if ( value )
@@ -1089,6 +1086,16 @@ bool wxSetEnv(const wxString& variable, const wxChar *value)
 #endif
 }
 
+bool wxSetEnv(const wxString& variable, const wxString& value)
+{
+    return wxDoSetEnv(variable, value.mb_str());
+}
+
+bool wxUnsetEnv(const wxString& variable)
+{
+    return wxDoSetEnv(variable, NULL);
+}
+
 // ----------------------------------------------------------------------------
 // signal handling
 // ----------------------------------------------------------------------------
-- 
2.50.0

