From 181dd701faf12eb62d2572dddca5af72cbd04022 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 26 Mar 2009 15:55:01 +0000 Subject: [PATCH] implement deprecated wxStripExtension() in terms of new wxFileName::StripExtension() (closes #10634) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59868 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/filefn.h | 3 ++- include/wx/filename.h | 3 +++ interface/wx/filename.h | 22 ++++++++++++++++++++++ src/common/filefn.cpp | 11 +---------- src/common/filename.cpp | 8 ++++++++ tests/filename/filenametest.cpp | 32 ++++++-------------------------- 6 files changed, 42 insertions(+), 37 deletions(-) diff --git a/include/wx/filefn.h b/include/wx/filefn.h index c64db53235..427eb1974b 100644 --- a/include/wx/filefn.h +++ b/include/wx/filefn.h @@ -555,10 +555,11 @@ wxDEPRECATED_BUT_USED_INTERNALLY( WXDLLIMPEXP_BASE void wxUnix2DosFilename(wchar_t *s) ); // Strip the extension, in situ +// Deprecated in favour of wxFileName::StripExtension() but notice that their +// behaviour is slightly different, see the manual wxDEPRECATED( WXDLLIMPEXP_BASE void wxStripExtension(char *buffer) ); wxDEPRECATED( WXDLLIMPEXP_BASE void wxStripExtension(wchar_t *buffer) ); wxDEPRECATED( WXDLLIMPEXP_BASE void wxStripExtension(wxString& buffer) ); - // DEPRECATED: construct a wxFileName, use ClearExt() and then GetFullPath() // Get a temporary filename wxDEPRECATED_BUT_USED_INTERNALLY( WXDLLIMPEXP_BASE wxChar* wxGetTempFileName(const wxString& prefix, wxChar *buf = NULL) ); diff --git a/include/wx/filename.h b/include/wx/filename.h index 7a7593c6d6..70ebc44ed7 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -519,6 +519,9 @@ public: wxString *path, wxPathFormat format = wxPATH_NATIVE); + // Strip the file extension + static wxString StripExtension(const wxString& fullpath); + #ifdef wxHAS_FILESYSTEM_VOLUMES // return the string representing a file system volume, or drive static wxString GetVolumeString(char drive, int flags = wxPATH_GET_SEPARATOR); diff --git a/interface/wx/filename.h b/interface/wx/filename.h index fcec353020..72bf9273e8 100644 --- a/interface/wx/filename.h +++ b/interface/wx/filename.h @@ -1137,6 +1137,28 @@ public: wxString* path, wxPathFormat format = wxPATH_NATIVE); + + /** + Strip the file extension. + + This function does more than just removing everything after the last + period from the string, for example it will return the string ".vimrc" + unchanged because the part after the period is not an extension but the + file name in this case. You can use wxString::BeforeLast() to really + get just the part before the last period (but notice that that function + returns empty string if period is not present at all unlike this + function which returns the @a fullname unchanged in this case). + + @param fullname + File path including name and, optionally, extension. + + @return + File path without extension + + @since 2.9.0 + */ + static wxString StripExtension(const wxString& fullname); + /** Sets the access and modification times to the current moment. */ diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index b732f23a1c..ca0d92a863 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -384,16 +384,7 @@ void wxStripExtension(wchar_t *buffer) { wxDoStripExtension(buffer); } void wxStripExtension(wxString& buffer) { - //RN: Be careful about the handling the case where - //buffer.length() == 0 - for(size_t i = buffer.length() - 1; i != wxString::npos; --i) - { - if (buffer.GetChar(i) == wxT('.')) - { - buffer = buffer.Left(i); - break; - } - } + buffer = wxFileName::StripExtension(buffer); } // Destructive removal of /./ and /../ stuff diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 7db00dc4a9..f16ae7127b 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -2261,6 +2261,14 @@ void wxFileName::SplitPath(const wxString& fullpath, } } +/* static */ +wxString wxFileName::StripExtension(const wxString& fullpath) +{ + wxFileName fn(fullpath); + fn.SetExt(""); + return fn.GetFullPath(); +} + // ---------------------------------------------------------------------------- // time functions // ---------------------------------------------------------------------------- diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 9132c0e3ea..d4a83c2ca3 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -454,35 +454,15 @@ void FileNameTestCase::TestReplace() fn.GetFullPath(wxPATH_UNIX) ); } -#if WXWIN_COMPATIBILITY_2_8 - -#ifdef __VISUALC__ - // disable warning about using deprecated wxStripExtension() - #pragma warning(disable:4996) -#endif - -wxString wxTestStripExtension(wxString szFile) -{ - wxStripExtension(szFile); - return szFile; -} - -#ifdef __VISUALC__ - #pragma warning(default:4996) -#endif - void FileNameTestCase::TestStrip() { - //test a crash - CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) ); - - //others - CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) ); - CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) ); - CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) ); - CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) ); + CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension(_T("")) ); + CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(_T(".")) ); + CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(_T(".vimrc")) ); + CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension(_T("bad")) ); + CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension(_T("good.wav")) ); + CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension(_T("good.wav.wav")) ); } -#endif // WXWIN_COMPATIBILITY_2_8 #ifdef __WINDOWS__