From: Vadim Zeitlin Date: Wed, 1 Jul 2009 09:07:38 +0000 (+0000) Subject: interpret ~ specially only when it is the first character of the path (closes #10948... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/be5be16a2326d8bfaa109d522212adffca4dcf5d interpret ~ specially only when it is the first character of the path (closes #10948, see #10941) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61271 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/filename.cpp b/src/common/filename.cpp index b0274b5863..ecd14faf6f 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -412,8 +412,7 @@ void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) // !! Fall through !! case wxPATH_UNIX: - // the paths of the form "~" or "~username" are absolute - m_relative = leadingChar != wxT('/') && leadingChar != _T('~'); + m_relative = leadingChar != wxT('/'); break; case wxPATH_DOS: @@ -1237,7 +1236,7 @@ bool wxFileName::Normalize(int flags, } // handle ~ stuff under Unix only - if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) + if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) && m_relative ) { if ( !dirs.IsEmpty() ) { @@ -1246,16 +1245,6 @@ bool wxFileName::Normalize(int flags, { // to make the path absolute use the home directory curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); - - // if we are expanding the tilde, then this path - // *should* be already relative (since we checked for - // the tilde only in the first char of the first dir); - // if m_relative==false, it's because it was initialized - // from a string which started with /~; in that case - // we reach this point but then need m_relative=true - // for relative->absolute expansion later - m_relative = true; - dirs.RemoveAt(0u); } } @@ -1284,7 +1273,7 @@ bool wxFileName::Normalize(int flags, // if we used e.g. tilde expansion previously and wxGetUserHome didn't // return for some reason an absolute path, then curDir maybe not be absolute! - if ( curDir.IsAbsolute(format) ) + if ( !curDir.m_relative ) { // we have prepended an absolute path and thus we are now an absolute // file name too @@ -1499,6 +1488,18 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, bool wxFileName::IsAbsolute(wxPathFormat format) const { + // unix paths beginning with ~ are reported as being absolute + if ( format == wxPATH_UNIX ) + { + if ( !m_dirs.IsEmpty() ) + { + wxString dir = m_dirs[0u]; + + if (!dir.empty() && dir[0u] == _T('~')) + return true; + } + } + // if our path doesn't start with a path separator, it's not an absolute // path if ( m_relative ) @@ -1507,7 +1508,7 @@ bool wxFileName::IsAbsolute(wxPathFormat format) const if ( !GetVolumeSeparator(format).empty() ) { // this format has volumes and an absolute path must have one, it's not - // enough to have the full path to bean absolute file under Windows + // enough to have the full path to be an absolute file under Windows if ( GetVolume().empty() ) return false; } @@ -1804,13 +1805,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const case wxPATH_UNIX: if ( !m_relative ) { - // normally the absolute file names start with a slash - // with one exception: the ones like "~/foo.bar" don't - // have it - if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') ) - { - fullpath += wxFILE_SEP_PATH_UNIX; - } + fullpath += wxFILE_SEP_PATH_UNIX; } break; diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index ee65dc83e3..ce964f54b9 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -65,6 +65,8 @@ static struct TestFileNameInfo { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX }, { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX }, { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX }, + { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX }, + { "~user/", "", "~user", "", "", true, wxPATH_UNIX }, { "/foo", "", "/", "foo", "", true, wxPATH_UNIX }, { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX }, { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX }, @@ -314,10 +316,10 @@ void FileNameTestCase::TestNormalize() // test wxPATH_NORM_DOTS { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, - // test wxPATH_NORM_TILDE - // NB: do the tilde expansion also under Windows to test if it works there too + // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially + // when it is the first character in the file name { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX }, - { "/~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, + { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX }, { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, // test wxPATH_NORM_CASE