From 14d63513822a9a91697508ecd37f95bac8af662e Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 6 Oct 2007 12:38:15 +0000
Subject: [PATCH] always return wxString from wxGetUserHome() instead of char
 */wxWCharBuffer depending on the build

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@49064 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/function.tex | 11 +++++---
 include/wx/utils.h         | 13 ++++-----
 src/mac/carbon/utils.cpp   |  4 +--
 src/msdos/utilsdos.cpp     | 10 +++----
 src/msw/utils.cpp          | 12 ++++----
 src/os2/utils.cpp          | 58 ++++++++++++++++----------------------
 src/palmos/utils.cpp       |  4 +--
 src/unix/utilsunx.cpp      | 21 +++++---------
 8 files changed, 58 insertions(+), 75 deletions(-)

diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index 62e622480a..5ab7cee273 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -1571,11 +1571,14 @@ that there isn't always a standard way to do a reliable check on the OS architec
 
 \membersection{::wxGetUserHome}\label{wxgetuserhome}
 
-\func{const wxChar *}{wxGetUserHome}{\param{const wxString\& }{user = ""}}
+\func{wxString}{wxGetUserHome}{\param{const wxString\& }{user = ""}}
 
-Returns the home directory for the given user. If the username is empty
-(default value), this function behaves like
-\helpref{wxGetHomeDir}{wxgethomedir}.
+Returns the home directory for the given user. If the \arg{user} is empty
+(default value), this function behaves like 
+\helpref{wxGetHomeDir}{wxgethomedir} i.e. returns the current user home
+directory.
+
+If the home directory couldn't be determined, an empty string is returned.
 
 \wxheading{Include files}
 
diff --git a/include/wx/utils.h b/include/wx/utils.h
index f63fae9c3c..6fe0851075 100644
--- a/include/wx/utils.h
+++ b/include/wx/utils.h
@@ -522,13 +522,10 @@ WXDLLIMPEXP_BASE wxString wxGetUserName();
 WXDLLIMPEXP_BASE wxString wxGetHomeDir();
 WXDLLIMPEXP_BASE const wxChar* wxGetHomeDir(wxString *pstr);
 
-// Get the user's home dir (caller must copy --- volatile)
-// returns NULL is no HOME dir is known
-#if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WINE__)
-WXDLLIMPEXP_BASE const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
-#else
-WXDLLIMPEXP_BASE wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
-#endif
+// Get the user's (by default use the current user name) home dir,
+// return empty string on error
+WXDLLIMPEXP_BASE wxString wxGetUserHome(const wxString& user = wxEmptyString);
+
 
 #if wxUSE_LONGLONG
     typedef wxLongLong wxDiskspaceSize_t;
@@ -550,7 +547,7 @@ typedef int (wxCMPFUNC_CONV *CMPFUNCDATA)(const void* pItem1, const void* pItem2
 
 
 WXDLLIMPEXP_BASE void wxQsort(void *const pbase, size_t total_elems,
-			                   size_t size, CMPFUNCDATA cmp, const void* user_data);
+                              size_t size, CMPFUNCDATA cmp, const void* user_data);
 
 
 #if wxUSE_GUI // GUI only things from now on
diff --git a/src/mac/carbon/utils.cpp b/src/mac/carbon/utils.cpp
index 55848f5799..71acf7553d 100644
--- a/src/mac/carbon/utils.cpp
+++ b/src/mac/carbon/utils.cpp
@@ -589,10 +589,10 @@ wxString wxGetOsDescription()
 }
 
 #ifndef __DARWIN__
-wxChar *wxGetUserHome (const wxString& user)
+wxString wxGetUserHome (const wxString& user)
 {
     // TODO
-    return NULL;
+    return wxString();
 }
 
 bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)
diff --git a/src/msdos/utilsdos.cpp b/src/msdos/utilsdos.cpp
index 6f432f4f89..ff4647668c 100644
--- a/src/msdos/utilsdos.cpp
+++ b/src/msdos/utilsdos.cpp
@@ -211,14 +211,14 @@ const wxChar* wxGetHomeDir(wxString *home)
     return strDir.c_str();
 }
 
-wxChar *wxGetUserHome(const wxString& user)
+wxString wxGetUserHome(const wxString& user)
 {
-    static wxString home;
+    wxString home;
 
     if (user.empty() || user == wxGetUserId())
-        return wx_const_cast(wxChar*, wxGetHomeDir(&home));
-    else
-        return _T("");
+        wxGetHomeDir(&home);
+
+    return home;
 }
 
 // returns %UserName%, $USER or just "user"
diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp
index baeeb9e5b7..fb792f42b3 100644
--- a/src/msw/utils.cpp
+++ b/src/msw/utils.cpp
@@ -467,14 +467,14 @@ const wxChar* wxGetHomeDir(wxString *pstr)
     return strDir.c_str();
 }
 
-wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
+wxString wxGetUserHome(const wxString& user)
 {
-    // VZ: the old code here never worked for user != "" anyhow! Moreover, it
-    //     returned sometimes a malloc()'d pointer, sometimes a pointer to a
-    //     static buffer and sometimes I don't even know what.
-    static wxString s_home;
+    wxString home;
 
-    return (wxChar *)wxGetHomeDir(&s_home);
+    if ( user.empty() || user == wxGetUserId() )
+        wxGetHomeDir(&home);
+
+    return home;
 }
 
 bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path),
diff --git a/src/os2/utils.cpp b/src/os2/utils.cpp
index 142878fd26..e3bc440f27 100644
--- a/src/os2/utils.cpp
+++ b/src/os2/utils.cpp
@@ -394,55 +394,45 @@ const wxChar* wxGetHomeDir(
     return rStrDir.c_str();
 }
 
-// Hack for OS/2
-wxChar* wxGetUserHome ( const wxString &rUser )
+wxString wxGetUserHome ( const wxString &rUser )
 {
     wxChar*    zHome;
-    wxString   sUser1(rUser);
+    wxString   sUser(rUser);
+
+    wxString home;
 
-    wxChar *wxBuffer = new wxChar[256];
 #ifndef __EMX__
-    if (!sUser1.empty())
+    if (!sUser.empty())
     {
-        wxChar                      zTmp[64];
+        const wxString currentUser = wxGetUserId();
 
-        if (wxGetUserId( zTmp
-                        ,sizeof(zTmp)/sizeof(char)
-                       ))
+        // Guests belong in the temp dir
+        if ( currentUser == "annonymous" )
         {
-            // Guests belong in the temp dir
-            if (wxStricmp(zTmp, _T("annonymous")) == 0)
-            {
-                if ((zHome = wxGetenv(_T("TMP"))) != NULL    ||
-                    (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
-                    (zHome = wxGetenv(_T("TEMP"))) != NULL)
-                    delete[] wxBuffer;
-                    return *zHome ? zHome : (wxChar*)_T("\\");
-            }
-            if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
-                sUser1 = wxEmptyString;
+            zHome = wxGetenv(_T("TMP"));
+            if ( !zHome )
+                zHome = wxGetenv(_T("TMPDIR"));
+            if ( !zHome )
+                zHome = wxGetenv(_T("TEMP"));
+
+            if ( zHome && *zHome )
+                return zHome;
         }
+
+        if ( sUser == currentUser )
+            sUser.clear();
     }
 #endif
-    if (sUser1.empty())
+    if (sUser.empty())
     {
         if ((zHome = wxGetenv(_T("HOME"))) != NULL)
         {
-            wxStrcpy(wxBuffer, zHome);
-            wxUnix2DosFilename(wxBuffer);
-#if wxUSE_UNICODE
-            wxWCharBuffer retBuffer (wxBuffer);
-            delete[] wxBuffer;
-            return retBuffer;
-#else
-            wxStrcpy(zHome, wxBuffer);
-            delete[] wxBuffer;
-            return zHome;
-#endif
+            home = zHome;
+            home.Replace("/", "\\");
         }
     }
-    delete[] wxBuffer;
-    return (wxChar*)wxEmptyString; // No home known!
+
+    return home;
 }
 
 bool wxGetDiskSpace(const wxString& path,
diff --git a/src/palmos/utils.cpp b/src/palmos/utils.cpp
index c8d8d84908..e0da4651b1 100644
--- a/src/palmos/utils.cpp
+++ b/src/palmos/utils.cpp
@@ -103,9 +103,9 @@ const wxChar* wxGetHomeDir(wxString *pstr)
     return NULL;
 }
 
-wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
+wxString wxGetUserHome(const wxString& WXUNUSED(user))
 {
-    return NULL;
+    return wxString();
 }
 
 bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)
diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp
index 429cf3e2f3..1cdeb6e670 100644
--- a/src/unix/utilsunx.cpp
+++ b/src/unix/utilsunx.cpp
@@ -695,7 +695,7 @@ long wxExecute(wxChar **argv, int flags, wxProcess *process)
 
 const wxChar* wxGetHomeDir( wxString *home  )
 {
-    *home = wxGetUserHome( wxEmptyString );
+    *home = wxGetUserHome();
     wxString tmp;
     if ( home->empty() )
         *home = wxT("/");
@@ -707,11 +707,7 @@ const wxChar* wxGetHomeDir( wxString *home  )
     return home->c_str();
 }
 
-#if wxUSE_UNICODE
-const wxMB2WXbuf wxGetUserHome( const wxString &user )
-#else // just for binary compatibility -- there is no 'const' here
-char *wxGetUserHome( const wxString &user )
-#endif
+wxString wxGetUserHome( const wxString &user )
 {
     struct passwd *who = (struct passwd *) NULL;
 
@@ -721,20 +717,17 @@ char *wxGetUserHome( const wxString &user )
 
         if ((ptr = wxGetenv(wxT("HOME"))) != NULL)
         {
-#if wxUSE_UNICODE
-            wxWCharBuffer buffer( ptr );
-            return buffer;
-#else
             return ptr;
-#endif
         }
-        if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
+
+        if ((ptr = wxGetenv(wxT("USER"))) != NULL ||
+             (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
         {
             who = getpwnam(wxSafeConvertWX2MB(ptr));
         }
 
-        // We now make sure the the user exists!
-        if (who == NULL)
+        // make sure the user exists!
+        if ( !who )
         {
             who = getpwuid(getuid());
         }
-- 
2.45.2

