From 9e8d860761774a8e54ba9caa8a1dc38380332a9a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 31 Dec 2000 01:15:33 +0000 Subject: [PATCH] implemented wxFileName::SplitPath(), wxSplitPath() now just calls it git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9027 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/filename.h | 27 ++++++++++- samples/console/console.cpp | 10 +++- src/common/filefn.cpp | 52 +-------------------- src/common/filename.cpp | 91 +++++++++++++++++++++++++++++++------ 4 files changed, 114 insertions(+), 66 deletions(-) diff --git a/include/wx/filename.h b/include/wx/filename.h index 9622124d8e..76f9e2169e 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -17,9 +17,23 @@ #endif #ifndef WX_PRECOMP - #include "wx/string.h" + #include "wx/string.h" #endif +/* + TODO: + + 1. support for drives under Windows + 2. more file operations: + a) chmod() + b) [acm]time() - get and set + c) file size + d) file permissions with readable accessors for most common bits + such as IsReadable() &c + e) rename()? + 3. SameFileAs() function to compare inodes under Unix + */ + // ridiculously enough, this will replace DirExists with wxDirExists etc #include "wx/filefn.h" @@ -224,8 +238,19 @@ public: // Construct full path with name and ext wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const; + // various helpers + + // get the canonical path format for this platform static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE ); + // split a fullpath into path, (base) name and ext (all of the pointers + // can be NULL) + static void SplitPath(const wxString& fullpath, + wxString *path, + wxString *name, + wxString *ext, + wxPathFormat format = wxPATH_NATIVE); + private: // the path components of the file wxArrayString m_dirs; diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 0cc41b2408..bf00e4a65c 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -610,10 +610,10 @@ static void TestFileNameConstruction() for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) { - wxFileName fn(filenames[n]); + wxFileName fn(filenames[n], wxPATH_UNIX); printf("Filename: '%s'\t", fn.GetFullPath().c_str()); - if ( !fn.Normalize() ) + if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), wxPATH_UNIX) ) { puts("ERROR (couldn't be normalized)"); } @@ -3964,6 +3964,12 @@ int main(int argc, char **argv) #ifdef TEST_FILENAME TestFileNameConstruction(); + if ( 0 ) + { + TestFileNameCwd(); + TestFileNameComparison(); + TestFileNameOperations(); + } #endif // TEST_FILENAME #ifdef TEST_THREADS diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index 1046ffd340..c6aa8b4b48 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -32,6 +32,7 @@ #include "wx/utils.h" #include "wx/intl.h" #include "wx/ffile.h" +#include "wx/filename.h" // there are just too many of those... #ifdef __VISUALC__ @@ -1788,56 +1789,7 @@ void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, // it can be empty, but it shouldn't be NULL wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") ); - const wxChar *pDot = wxStrrchr(pszFileName, wxFILE_SEP_EXT); - -#ifdef __WXMSW__ - // under Windows we understand both separators - const wxChar *pSepUnix = wxStrrchr(pszFileName, wxFILE_SEP_PATH_UNIX); - const wxChar *pSepDos = wxStrrchr(pszFileName, wxFILE_SEP_PATH_DOS); - const wxChar *pLastSeparator = pSepUnix > pSepDos ? pSepUnix : pSepDos; -#else // assume Unix - const wxChar *pLastSeparator = wxStrrchr(pszFileName, wxFILE_SEP_PATH_UNIX); - - if ( pDot ) - { - if ( (pDot == pszFileName) || (*(pDot - 1) == wxFILE_SEP_PATH_UNIX) ) - { - // under Unix, dot may be (and commonly is) the first character of the - // filename, don't treat the entire filename as extension in this case - pDot = NULL; - } - } -#endif // MSW/Unix - - if ( pDot && (pDot < pLastSeparator) ) - { - // the dot is part of the path, not the start of the extension - pDot = NULL; - } - - if ( pstrPath ) - { - if ( pLastSeparator ) - *pstrPath = wxString(pszFileName, pLastSeparator - pszFileName); - else - pstrPath->Empty(); - } - - if ( pstrName ) - { - const wxChar *start = pLastSeparator ? pLastSeparator + 1 : pszFileName; - const wxChar *end = pDot ? pDot : pszFileName + wxStrlen(pszFileName); - - *pstrName = wxString(start, end - start); - } - - if ( pstrExt ) - { - if ( pDot ) - *pstrExt = wxString(pDot + 1); - else - pstrExt->Empty(); - } + wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); } time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 51100b0829..e0c45c36ff 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -38,13 +38,6 @@ #include "wx/config.h" // for wxExpandEnvVars #include "wx/utils.h" -// ---------------------------------------------------------------------------- -// constants -// ---------------------------------------------------------------------------- - -// the character separating the extension from the base name -#define EXT_SEP _T('.') - // ============================================================================ // implementation // ============================================================================ @@ -89,7 +82,7 @@ void wxFileName::Assign(const wxString& fullpath, wxPathFormat format) { wxString path, name, ext; - wxSplitPath(fullpath, &path, &name, &ext); + SplitPath(fullpath, &path, &name, &ext, format); Assign(path, name, ext, format); } @@ -99,7 +92,7 @@ void wxFileName::Assign(const wxString& path, wxPathFormat format) { wxString name, ext; - wxSplitPath(fullname, NULL /* no path */, &name, &ext); + SplitPath(fullname, NULL /* no path */, &name, &ext, format); Assign(path, name, ext, format); } @@ -379,7 +372,7 @@ wxString wxFileName::GetPathSeparators(wxPathFormat format) { case wxPATH_DOS: // accept both as native APIs do - seps = _T("/\\"); + seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS; break; default: @@ -387,11 +380,11 @@ wxString wxFileName::GetPathSeparators(wxPathFormat format) // fall through case wxPATH_UNIX: - seps = _T("/"); + seps = wxFILE_SEP_PATH_UNIX; break; case wxPATH_MAC: - seps = _T(":"); + seps = wxFILE_SEP_PATH_MAC; break; } @@ -444,7 +437,7 @@ wxString wxFileName::GetFullName() const wxString fullname = m_name; if ( !m_ext.empty() ) { - fullname << EXT_SEP << m_ext; + fullname << wxFILE_SEP_EXT << m_ext; } return fullname; @@ -486,3 +479,75 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format ) return format; } +// ---------------------------------------------------------------------------- +// path splitting function +// ---------------------------------------------------------------------------- + +void wxFileName::SplitPath(const wxString& fullpath, + wxString *pstrPath, + wxString *pstrName, + wxString *pstrExt, + wxPathFormat format) +{ + format = GetFormat(format); + + // find the positions of the last dot and last path separator in the path + size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); + size_t posLastSlash = fullpath.find_last_of(GetPathSeparators(format)); + + if ( (posLastDot != wxString::npos) && (format == wxPATH_UNIX) ) + { + if ( (posLastDot == 0) || + (fullpath[posLastDot - 1] == wxFILE_SEP_PATH_UNIX) ) + { + // under Unix, dot may be (and commonly is) the first character of + // the filename, don't treat the entire filename as extension in + // this case + posLastDot = wxString::npos; + } + } + + if ( (posLastDot != wxString::npos) && (posLastDot < posLastSlash) ) + { + // the dot is part of the path, not the start of the extension + posLastDot = wxString::npos; + } + + // now fill in the variables provided by user + if ( pstrPath ) + { + if ( posLastSlash == wxString::npos ) + { + // no path at all + pstrPath->Empty(); + } + else + { + // take all until the separator + *pstrPath = fullpath.Left(posLastSlash); + } + } + + if ( pstrName ) + { + size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; + size_t count = posLastDot == wxString::npos ? wxString::npos + : posLastDot - posLastSlash; + + *pstrName = fullpath.Mid(nStart, count); + } + + if ( pstrExt ) + { + if ( posLastDot == wxString::npos ) + { + // no extension + pstrExt->Empty(); + } + else + { + // take everything after the dot + *pstrExt = fullpath.Mid(posLastDot + 1); + } + } +} -- 2.45.2