From: Vadim Zeitlin Date: Tue, 23 Jan 2001 00:24:32 +0000 (+0000) Subject: fixed bug in parsing filenames without paths, added more/better tests X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/8e7dda219e5c048104b18ea3f4254a683e3e4aa8 fixed bug in parsing filenames without paths, added more/better tests git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9145 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 3bf7de87f0..395b6d6f2f 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -596,14 +596,22 @@ static void TestFileConfRead() #include -static const wxChar *filenames[] = +static struct FileNameInfo { - _T("/usr/bin/ls"), - _T("/usr/bin/"), - _T("~/.zshrc"), - _T("../../foo"), - _T("~/foo.bar"), - _T("/tmp/wxwin.tar.bz"), + const wxChar *fullname; + const wxChar *path; + const wxChar *name; + const wxChar *ext; +} filenames[] = +{ + { _T("/usr/bin/ls"), _T("/usr/bin"), _T("ls"), _T("") }, + { _T("/usr/bin/"), _T("/usr/bin"), _T(""), _T("") }, + { _T("~/.zshrc"), _T("~"), _T(".zshrc"), _T("") }, + { _T("../../foo"), _T("../.."), _T("foo"), _T("") }, + { _T("foo.bar"), _T(""), _T("foo"), _T("bar") }, + { _T("~/foo.bar"), _T("~"), _T("foo"), _T("bar") }, + { _T("Mahogany-0.60/foo.bar"), _T("Mahogany-0.60"), _T("foo"), _T("bar") }, + { _T("/tmp/wxwin.tar.bz"), _T("/tmp"), _T("wxwin.tar"), _T("bz") }, }; static void TestFileNameConstruction() @@ -612,7 +620,7 @@ static void TestFileNameConstruction() for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) { - wxFileName fn(filenames[n], wxPATH_UNIX); + wxFileName fn(filenames[n].fullname, wxPATH_UNIX); printf("Filename: '%s'\t", fn.GetFullPath().c_str()); if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), wxPATH_UNIX) ) @@ -634,10 +642,19 @@ static void TestFileNameSplit() for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) { + const FileNameInfo &fni = 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()); + wxFileName::SplitPath(fni.fullname, &path, &name, &ext); + + printf("%s -> path = '%s', name = '%s', ext = '%s'", + fni.fullname, path.c_str(), name.c_str(), ext.c_str()); + if ( path != fni.path ) + printf(" (ERROR: path = '%s')", fni.path); + if ( name != fni.name ) + printf(" (ERROR: name = '%s')", fni.name); + if ( ext != fni.ext ) + printf(" (ERROR: ext = '%s')", fni.ext); + puts(""); } puts(""); diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 3bb8694534..086598ef3b 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -512,7 +512,10 @@ void wxFileName::SplitPath(const wxString& fullpath, } } - if ( (posLastDot != wxString::npos) && (posLastDot < posLastSlash) ) + // if we do have a dot and a slash, check that the dot is in the name part + if ( (posLastDot != wxString::npos) && + (posLastSlash != wxString::npos) && + (posLastDot < posLastSlash) ) { // the dot is part of the path, not the start of the extension posLastDot = wxString::npos; @@ -538,9 +541,20 @@ void wxFileName::SplitPath(const wxString& fullpath, // 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 - 1; + size_t count; + if ( posLastDot == wxString::npos ) + { + // take all until the end + count = wxString::npos; + } + else if ( posLastSlash == wxString::npos ) + { + count = posLastDot; + } + else // have both dot and slash + { + count = posLastDot - posLastSlash - 1; + } *pstrName = fullpath.Mid(nStart, count); }