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
This commit is contained in:
Vadim Zeitlin
2001-01-23 00:24:32 +00:00
parent 5e68f8f373
commit 8e7dda219e
2 changed files with 46 additions and 15 deletions

View File

@@ -596,14 +596,22 @@ static void TestFileConfRead()
#include <wx/filename.h> #include <wx/filename.h>
static const wxChar *filenames[] = static struct FileNameInfo
{ {
_T("/usr/bin/ls"), const wxChar *fullname;
_T("/usr/bin/"), const wxChar *path;
_T("~/.zshrc"), const wxChar *name;
_T("../../foo"), const wxChar *ext;
_T("~/foo.bar"), } filenames[] =
_T("/tmp/wxwin.tar.bz"), {
{ _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() static void TestFileNameConstruction()
@@ -612,7 +620,7 @@ static void TestFileNameConstruction()
for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) 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()); printf("Filename: '%s'\t", fn.GetFullPath().c_str());
if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), wxPATH_UNIX) ) 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++ ) for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
{ {
const FileNameInfo &fni = filenames[n];
wxString path, name, ext; wxString path, name, ext;
wxFileName::SplitPath(filenames[n], &path, &name, &ext); wxFileName::SplitPath(fni.fullname, &path, &name, &ext);
printf("%s -> path = '%s', name = '%s', ext = '%s'\n",
filenames[n], path.c_str(), name.c_str(), ext.c_str()); 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(""); puts("");

View File

@@ -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 // the dot is part of the path, not the start of the extension
posLastDot = wxString::npos; 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 // take all characters starting from the one after the last slash and
// up to, but excluding, the last dot // up to, but excluding, the last dot
size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
size_t count = posLastDot == wxString::npos size_t count;
? wxString::npos if ( posLastDot == wxString::npos )
: posLastDot - posLastSlash - 1; {
// 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); *pstrName = fullpath.Mid(nStart, count);
} }