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
This commit is contained in:
Vadim Zeitlin
2007-10-06 12:38:15 +00:00
parent 987bb1c66b
commit 14d6351382
8 changed files with 58 additions and 75 deletions

View File

@@ -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} \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 Returns the home directory for the given user. If the \arg{user} is empty
(default value), this function behaves like (default value), this function behaves like
\helpref{wxGetHomeDir}{wxgethomedir}. \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} \wxheading{Include files}

View File

@@ -522,13 +522,10 @@ WXDLLIMPEXP_BASE wxString wxGetUserName();
WXDLLIMPEXP_BASE wxString wxGetHomeDir(); WXDLLIMPEXP_BASE wxString wxGetHomeDir();
WXDLLIMPEXP_BASE const wxChar* wxGetHomeDir(wxString *pstr); WXDLLIMPEXP_BASE const wxChar* wxGetHomeDir(wxString *pstr);
// Get the user's home dir (caller must copy --- volatile) // Get the user's (by default use the current user name) home dir,
// returns NULL is no HOME dir is known // return empty string on error
#if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WINE__) WXDLLIMPEXP_BASE wxString wxGetUserHome(const wxString& user = wxEmptyString);
WXDLLIMPEXP_BASE const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
#else
WXDLLIMPEXP_BASE wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
#endif
#if wxUSE_LONGLONG #if wxUSE_LONGLONG
typedef wxLongLong wxDiskspaceSize_t; 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, 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 #if wxUSE_GUI // GUI only things from now on

View File

@@ -589,10 +589,10 @@ wxString wxGetOsDescription()
} }
#ifndef __DARWIN__ #ifndef __DARWIN__
wxChar *wxGetUserHome (const wxString& user) wxString wxGetUserHome (const wxString& user)
{ {
// TODO // TODO
return NULL; return wxString();
} }
bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree) bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)

View File

@@ -211,14 +211,14 @@ const wxChar* wxGetHomeDir(wxString *home)
return strDir.c_str(); return strDir.c_str();
} }
wxChar *wxGetUserHome(const wxString& user) wxString wxGetUserHome(const wxString& user)
{ {
static wxString home; wxString home;
if (user.empty() || user == wxGetUserId()) if (user.empty() || user == wxGetUserId())
return wx_const_cast(wxChar*, wxGetHomeDir(&home)); wxGetHomeDir(&home);
else
return _T(""); return home;
} }
// returns %UserName%, $USER or just "user" // returns %UserName%, $USER or just "user"

View File

@@ -467,14 +467,14 @@ const wxChar* wxGetHomeDir(wxString *pstr)
return strDir.c_str(); 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 wxString home;
// 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;
return (wxChar *)wxGetHomeDir(&s_home); if ( user.empty() || user == wxGetUserId() )
wxGetHomeDir(&home);
return home;
} }
bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path), bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path),

View File

@@ -394,55 +394,45 @@ const wxChar* wxGetHomeDir(
return rStrDir.c_str(); return rStrDir.c_str();
} }
// Hack for OS/2 wxString wxGetUserHome ( const wxString &rUser )
wxChar* wxGetUserHome ( const wxString &rUser )
{ {
wxChar* zHome; wxChar* zHome;
wxString sUser1(rUser); wxString sUser(rUser);
wxString home;
wxChar *wxBuffer = new wxChar[256];
#ifndef __EMX__ #ifndef __EMX__
if (!sUser1.empty()) if (!sUser.empty())
{ {
wxChar zTmp[64]; const wxString currentUser = wxGetUserId();
if (wxGetUserId( zTmp // Guests belong in the temp dir
,sizeof(zTmp)/sizeof(char) if ( currentUser == "annonymous" )
))
{ {
// Guests belong in the temp dir zHome = wxGetenv(_T("TMP"));
if (wxStricmp(zTmp, _T("annonymous")) == 0) if ( !zHome )
{ zHome = wxGetenv(_T("TMPDIR"));
if ((zHome = wxGetenv(_T("TMP"))) != NULL || if ( !zHome )
(zHome = wxGetenv(_T("TMPDIR"))) != NULL || zHome = wxGetenv(_T("TEMP"));
(zHome = wxGetenv(_T("TEMP"))) != NULL)
delete[] wxBuffer; if ( zHome && *zHome )
return *zHome ? zHome : (wxChar*)_T("\\"); return zHome;
}
if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
sUser1 = wxEmptyString;
} }
if ( sUser == currentUser )
sUser.clear();
} }
#endif #endif
if (sUser1.empty()) if (sUser.empty())
{ {
if ((zHome = wxGetenv(_T("HOME"))) != NULL) if ((zHome = wxGetenv(_T("HOME"))) != NULL)
{ {
wxStrcpy(wxBuffer, zHome); home = zHome;
wxUnix2DosFilename(wxBuffer); home.Replace("/", "\\");
#if wxUSE_UNICODE
wxWCharBuffer retBuffer (wxBuffer);
delete[] wxBuffer;
return retBuffer;
#else
wxStrcpy(zHome, wxBuffer);
delete[] wxBuffer;
return zHome;
#endif
} }
} }
delete[] wxBuffer;
return (wxChar*)wxEmptyString; // No home known! return home;
} }
bool wxGetDiskSpace(const wxString& path, bool wxGetDiskSpace(const wxString& path,

View File

@@ -103,9 +103,9 @@ const wxChar* wxGetHomeDir(wxString *pstr)
return NULL; 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) bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree)

View File

@@ -695,7 +695,7 @@ long wxExecute(wxChar **argv, int flags, wxProcess *process)
const wxChar* wxGetHomeDir( wxString *home ) const wxChar* wxGetHomeDir( wxString *home )
{ {
*home = wxGetUserHome( wxEmptyString ); *home = wxGetUserHome();
wxString tmp; wxString tmp;
if ( home->empty() ) if ( home->empty() )
*home = wxT("/"); *home = wxT("/");
@@ -707,11 +707,7 @@ const wxChar* wxGetHomeDir( wxString *home )
return home->c_str(); return home->c_str();
} }
#if wxUSE_UNICODE wxString wxGetUserHome( const wxString &user )
const wxMB2WXbuf wxGetUserHome( const wxString &user )
#else // just for binary compatibility -- there is no 'const' here
char *wxGetUserHome( const wxString &user )
#endif
{ {
struct passwd *who = (struct passwd *) NULL; struct passwd *who = (struct passwd *) NULL;
@@ -721,20 +717,17 @@ char *wxGetUserHome( const wxString &user )
if ((ptr = wxGetenv(wxT("HOME"))) != NULL) if ((ptr = wxGetenv(wxT("HOME"))) != NULL)
{ {
#if wxUSE_UNICODE
wxWCharBuffer buffer( ptr );
return buffer;
#else
return ptr; 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)); who = getpwnam(wxSafeConvertWX2MB(ptr));
} }
// We now make sure the the user exists! // make sure the user exists!
if (who == NULL) if ( !who )
{ {
who = getpwuid(getuid()); who = getpwuid(getuid());
} }