From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Wed, 17 Jan 2001 16:08:15 +0000 (+0000)
Subject: fixed wxSplitPath() bug and added tests for it
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/42b1f941b010bee7e5e82b12c1096dce094553d4

fixed wxSplitPath() bug and added tests for it


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

diff --git a/samples/console/console.cpp b/samples/console/console.cpp
index cdd4e8f68f..3bf7de87f0 100644
--- a/samples/console/console.cpp
+++ b/samples/console/console.cpp
@@ -37,14 +37,14 @@
 
 //#define TEST_ARRAYS
 //#define TEST_CMDLINE
-#define TEST_DATETIME
+//#define TEST_DATETIME
 //#define TEST_DIR
 //#define TEST_DLLLOADER
 //#define TEST_ENVIRON
 //#define TEST_EXECUTE
 //#define TEST_FILE
 //#define TEST_FILECONF
-//#define TEST_FILENAME
+#define TEST_FILENAME
 //#define TEST_FTP
 //#define TEST_HASH
 //#define TEST_LIST
@@ -596,18 +596,20 @@ static void TestFileConfRead()
 
 #include <wx/filename.h>
 
+static const wxChar *filenames[] =
+{
+    _T("/usr/bin/ls"),
+    _T("/usr/bin/"),
+    _T("~/.zshrc"),
+    _T("../../foo"),
+    _T("~/foo.bar"),
+    _T("/tmp/wxwin.tar.bz"),
+};
+
 static void TestFileNameConstruction()
 {
     puts("*** testing wxFileName construction ***");
 
-    static const wxChar *filenames[] =
-    {
-        _T("/usr/bin/ls"),
-        _T("/usr/bin/"),
-        _T("~/.zshrc"),
-        _T("../../foo"),
-    };
-
     for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
     {
         wxFileName fn(filenames[n], wxPATH_UNIX);
@@ -626,6 +628,21 @@ static void TestFileNameConstruction()
     puts("");
 }
 
+static void TestFileNameSplit()
+{
+    puts("*** testing wxFileName splitting ***");
+
+    for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+    {
+        wxString path, name, ext;
+        wxFileName::SplitPath(filenames[n], &path, &name, &ext);
+        printf("%s -> path = '%s', name = '%s', ext = '%s'\n",
+               filenames[n], path.c_str(), name.c_str(), ext.c_str());
+    }
+
+    puts("");
+}
+
 static void TestFileNameComparison()
 {
     // TODO!
@@ -4000,9 +4017,10 @@ int main(int argc, char **argv)
 #endif // TEST_FILE
 
 #ifdef TEST_FILENAME
-    TestFileNameConstruction();
+    TestFileNameSplit();
     if ( 0 )
     {
+        TestFileNameConstruction();
         TestFileNameCwd();
         TestFileNameComparison();
         TestFileNameOperations();
diff --git a/src/common/filename.cpp b/src/common/filename.cpp
index 91318e43bf..3bb8694534 100644
--- a/src/common/filename.cpp
+++ b/src/common/filename.cpp
@@ -535,9 +535,12 @@ void wxFileName::SplitPath(const wxString& fullpath,
 
     if ( pstrName )
     {
+        // take all characters starting from the one after the last slash and
+        // up to, but excluding, the last dot
         size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
-        size_t count = posLastDot == wxString::npos ? wxString::npos
-                                                    : posLastDot - posLastSlash;
+        size_t count = posLastDot == wxString::npos
+                        ? wxString::npos
+                        : posLastDot - posLastSlash - 1;
 
         *pstrName = fullpath.Mid(nStart, count);
     }