From d37fd2fada5c7d1cbb78844a4a6540284256e72f Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Thu, 18 Feb 1999 14:22:23 +0000
Subject: [PATCH] wxSplitPath() bugs corrected and it's documented

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1716 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/function.tex | 21 +++++++++++
 src/common/filefn.cpp      | 71 +++++++++++++++++++++++---------------
 2 files changed, 64 insertions(+), 28 deletions(-)

diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex
index fe92c20c42..8829f12eb7 100644
--- a/docs/latex/wx/function.tex
+++ b/docs/latex/wx/function.tex
@@ -239,6 +239,27 @@ The {\it flags} parameter is reserved for future use.
 Sets the current working directory, returning TRUE if the operation succeeded.
 Under MS Windows, the current drive is also changed if {\it dir} contains a drive specification.
 
+\membersection{::wxSplitPath}\label{wxsplitfunction}
+
+\func{void}{wxSplitPath}{\param{const char *}{ fullname}, \param{const wxString *}{ path}, \param{const wxString *}{ name}, \param{const wxString *}{ ext}}
+
+This function splits a full file name into components: the path (including possible disk/drive
+specification under Windows), the base name and the extension. Any of the output parameters
+({\it path}, {\it name} or {\it ext}) may be NULL if you are not interested in the value of
+a particular component.
+
+wxSplitPath() will correctly handle filenames with both DOS and Unix path separators under
+Windows, however it will not consider backslashes as path separators under Unix (where backslash
+is a valid character in a filename).
+
+On entry, {\it fullname} should be non NULL (it may be empty though).
+
+On return, {\it path} contains the file path (without the trailing separator), {\it name}
+contains the file name and {\it ext} contains the file extension without leading dot. All
+three of them may be empty if the corresponding component is. The old contents of the
+strings pointed to by these parameters will be overwritten in any case (if the pointers
+are not NULL).
+
 \section{String functions}
 
 \membersection{::copystring}
diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp
index c36a08b281..a4e7efb765 100644
--- a/src/common/filefn.cpp
+++ b/src/common/filefn.cpp
@@ -1532,40 +1532,55 @@ void WXDLLEXPORT wxSplitPath(const char *pszFileName,
                              wxString *pstrName,
                              wxString *pstrExt)
 {
-  wxCHECK_RET( pszFileName, "NULL file name in wxSplitPath" );
+    // it can be empty, but it shouldn't be NULL
+    wxCHECK_RET( pszFileName, "NULL file name in wxSplitPath" );
 
-  const char *pDot = strrchr(pszFileName, wxFILE_SEP_EXT);
-  const char *pSepUnix = strrchr(pszFileName, wxFILE_SEP_PATH_UNIX);
-  const char *pSepDos = strrchr(pszFileName, wxFILE_SEP_PATH_DOS);
+    const char *pDot = strrchr(pszFileName, wxFILE_SEP_EXT);
 
-  // take the last of the two: nPosUnix containts the last slash in the
-  // filename
-  size_t nPosUnix = pSepUnix ? pSepUnix - pszFileName : 0;
-  size_t nPosDos = pSepDos ? pSepDos - pszFileName : 0;
-  if ( nPosDos > nPosUnix )
-    nPosUnix = nPosDos;
-
-  if ( pstrPath )
-    *pstrPath = wxString(pszFileName, nPosUnix);
+#ifdef __WXMSW__
+    // under Windows we understand both separators
+    const char *pSepUnix = strrchr(pszFileName, wxFILE_SEP_PATH_UNIX);
+    const char *pSepDos = strrchr(pszFileName, wxFILE_SEP_PATH_DOS);
+    const char *pLastSeparator = pSepUnix > pSepDos ? pSepUnix : pSepDos;
+#else // assume Unix
+    const char *pLastSeparator = strrchr(pszFileName, wxFILE_SEP_PATH_UNIX);
+
+    if ( pDot == pszFileName )
+    {
+        // under Unix files like .profile are treated in a special way
+        pDot = NULL;
+    }
+#endif // MSW/Unix
+    
+    if ( pDot < pLastSeparator )
+    {
+        // the dot is part of the path, not the start of the extension
+        pDot = NULL;
+    }
 
-  size_t nPosDot = 0;
-  if ( pDot )
-    nPosDot = pDot - pszFileName;
+    if ( pstrPath )
+    {
+        if ( pLastSeparator )    
+            *pstrPath = wxString(pszFileName, pLastSeparator - pszFileName);
+        else
+            pstrPath->Empty();
+    }
 
-  if ( nPosDot > nPosUnix ) {
-    // the file name looks like "path/name.ext"
-    if ( pstrName )
-      *pstrName = wxString(pszFileName + nPosUnix + 1, nPosDot - nPosUnix - 1);
-    if ( pstrExt )
-      *pstrExt = wxString(pszFileName + nPosDot + 1);
-  }
-  else {
-    // there is either no dot at all or there is a '/' after it
     if ( pstrName )
-      *pstrName = wxString(pszFileName + nPosUnix + 1);
+    {
+        const char *start = pLastSeparator ? pLastSeparator + 1 : pszFileName;
+        const char *end = pDot ? pDot : pszFileName + strlen(pszFileName);
+
+        *pstrName = wxString(start, end - start);
+    }
+
     if ( pstrExt )
-      pstrExt->Empty();
-  }
+    {
+        if ( pDot )
+            *pstrExt = wxString(pDot + 1);
+        else
+            pstrExt->Empty();
+    }
 }
 
 //------------------------------------------------------------------------
-- 
2.50.0

