]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxDir::GetNameWithSep() and use it to avoid consecutive slashes.
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 4 May 2012 20:35:31 +0000 (20:35 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 4 May 2012 20:35:31 +0000 (20:35 +0000)
It is wrong to use dir.GetName()+"/" to obtain a slash-terminated directory
name as this results in (usually harmless but at best ugly) double slashes at
at the beginning of the string for the root directory. Add GetNameWithSep() to
obtain the correct result in all cases.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71355 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/dir.h
interface/wx/dir.h
src/common/dircmn.cpp
tests/file/dir.cpp

index 197a1565e0f8dc77de64759521a6502756f84526..40788167be94edb3d2a8909caab7908883d19ec1 100644 (file)
@@ -496,6 +496,7 @@ All:
 - Added wxFileName::Exists().
 - Implement wxThread::SetConcurrency() for POSIX systems (Igor Korot).
 - Fix deadlock due to too many events in Unix console apps (Lukasz Michalski).
+- Added wxDir::GetNameWithSep().
 
 All (GUI):
 
index 94c5d453c0a229fe99cbd5eb995f32a8cf86f56b..0781b13c13d470d8879f364a883d75d492f1bd73 100644 (file)
@@ -106,6 +106,10 @@ public:
     // get the full name of the directory (without '/' at the end)
     wxString GetName() const;
 
+    // Same as GetName() but does include the trailing separator, unless the
+    // string is empty (only for invalid directories).
+    wxString GetNameWithSep() const;
+
 
     // file enumeration routines
     // -------------------------
index ecf8ffb724c2b75306bf2a3e13851dd61a5aacf6..42a8288b24c04ff071907239389829fc345650fa 100644 (file)
@@ -218,11 +218,31 @@ public:
                   int flags = wxDIR_DEFAULT) const;
 
     /**
-        Returns the name of the directory itself. The returned string does not
-        have the trailing path separator (slash or backslash).
+        Returns the name of the directory itself.
+
+        The returned string does not have the trailing path separator (slash or
+        backslash).
+
+        Notice that in spite of this the last character of the returned string
+        can still be the path separator if this directory is the root one.
+        Because of this, don't append ::wxFILE_SEP_PATH to the returned value
+        if you do need a slash-terminated directory name but use
+        GetNameWithSep() instead to avoid having duplicate consecutive slashes.
     */
     wxString GetName() const;
 
+    /**
+        Returns the name of the directory with the path separator appended.
+
+        The last character of the returned string is always ::wxFILE_SEP_PATH
+        unless the string is empty, indicating that this directory is invalid.
+
+        @see GetName()
+
+        @since 2.9.4
+     */
+    wxString GetNameWithSep() const;
+
     /**
         Continue enumerating files which satisfy the criteria specified by the
         last call to GetFirst().
index f5b0c9fe8a824b4c2f958312847dba67d8806976..1638cff0ac40a0b1637718a324555fe1c40e9a87 100644 (file)
@@ -72,6 +72,28 @@ bool wxDir::HasSubDirs(const wxString& spec) const
 
 #endif // !Unix
 
+// ----------------------------------------------------------------------------
+// wxDir::GetNameWithSep()
+// ----------------------------------------------------------------------------
+
+wxString wxDir::GetNameWithSep() const
+{
+    // Note that for historical reasons (i.e. because GetName() was there
+    // first) we implement this one in terms of GetName() even though it might
+    // actually make more sense to reverse this logic.
+
+    wxString name = GetName();
+    if ( !name.empty() )
+    {
+        // Notice that even though GetName() isn't supposed to return the
+        // separator, it can still be present for the root directory name.
+        if ( name.Last() != wxFILE_SEP_PATH )
+            name += wxFILE_SEP_PATH;
+    }
+
+    return name;
+}
+
 // ----------------------------------------------------------------------------
 // wxDir::Traverse()
 // ----------------------------------------------------------------------------
@@ -87,8 +109,7 @@ size_t wxDir::Traverse(wxDirTraverser& sink,
     size_t nFiles = 0;
 
     // the name of this dir with path delimiter at the end
-    wxString prefix = GetName();
-    prefix += wxFILE_SEP_PATH;
+    const wxString prefix = GetNameWithSep();
 
     // first, recurse into subdirs
     if ( flags & wxDIR_DIRS )
index c5151d243e6d937f4c6ae8631ad3a3e3fb568927..81d66ad53e862c4fb7305653777fc8a906cea588 100644 (file)
@@ -235,9 +235,13 @@ void DirTestCase::GetName()
 
     CPPUNIT_ASSERT( d.Open(".") );
     CPPUNIT_ASSERT( d.GetName().Last() != wxFILE_SEP_PATH );
+    CPPUNIT_ASSERT( d.GetNameWithSep().Last() == wxFILE_SEP_PATH );
+    CPPUNIT_ASSERT_EQUAL( d.GetName() + wxFILE_SEP_PATH,
+                          d.GetNameWithSep() );
 
 #ifdef __UNIX__
     CPPUNIT_ASSERT( d.Open("/") );
     CPPUNIT_ASSERT_EQUAL( "/", d.GetName() );
+    CPPUNIT_ASSERT_EQUAL( "/", d.GetNameWithSep() );
 #endif
 }