From 35c2aa4f19cce4f5a6bd0199333017453e569a2d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 14 Sep 2008 01:18:05 +0000 Subject: [PATCH] added wxFileName::GetVolumeString() (#9950) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55596 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/filename.h | 14 +++++++++++++- interface/wx/filename.h | 38 ++++++++++++++++++++++++++++++++------ src/common/filename.cpp | 17 +++++++++++++++++ src/generic/dirctrlg.cpp | 24 ++++++++++++------------ 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/include/wx/filename.h b/include/wx/filename.h index 93b12c5107..347b0ab621 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -37,6 +37,12 @@ class WXDLLIMPEXP_FWD_BASE wxFile; class WXDLLIMPEXP_FWD_BASE wxFFile; #endif +// this symbol is defined for the platforms where file systems use volumes in +// paths +#if defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__) + #define wxHAS_FILESYSTEM_VOLUMES +#endif + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -75,6 +81,7 @@ enum wxPathNormalize // what exactly should GetPath() return? enum { + wxPATH_NO_SEPARATOR = 0x0000, // for symmetry with wxPATH_GET_SEPARATOR wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator }; @@ -484,7 +491,12 @@ public: wxString *path, wxPathFormat format = wxPATH_NATIVE); - // Filesize +#ifdef wxHAS_FILESYSTEM_VOLUMES + // return the string representing a file system volume, or drive + static wxString GetVolumeString(char drive, int flags = wxPATH_GET_SEPARATOR); +#endif // wxHAS_FILESYSTEM_VOLUMES + + // File size #if wxUSE_LONGLONG // returns the size of the given filename diff --git a/interface/wx/filename.h b/interface/wx/filename.h index 3308fb84ad..39e27ceba2 100644 --- a/interface/wx/filename.h +++ b/interface/wx/filename.h @@ -427,18 +427,25 @@ public: wxString GetName() const; /** - Returns the path part of the filename (without the name or extension). The - possible flags values are: + Returns the path part of the filename (without the name or extension). + + The possible flags values are: @b wxPATH_GET_VOLUME - Return the path with the volume (does nothing for the filename formats without - volumes), otherwise the path without volume part is returned. + Return the path with the volume (does nothing for the filename formats + without volumes), otherwise the path without volume part is returned. @b wxPATH_GET_SEPARATOR - Return the path with the trailing separator, if this flag is not given there - will be no separator at the end of the path. + Return the path with the trailing separator, if this flag is not given + there will be no separator at the end of the path. + + @b wxPATH_NO_SEPARATOR + + Don't include the trailing separator in the returned string. This is + the default (the value of this flag is 0) and exists only for symmetry + with wxPATH_GET_SEPARATOR. */ wxString GetPath(int flags = wxPATH_GET_VOLUME, wxPathFormat format = wxPATH_NATIVE) const; @@ -534,6 +541,25 @@ public: */ static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE); + /** + This function builds a volume path string, for example "C:\\". + + Implemented for the platforms which use drive letters, i.e. DOS, MSW + and OS/2 only. + + @since 2.9.0 + + @param drive + The drive letter, 'A' through 'Z' or 'a' through 'z'. + + @param flags + @c wxPATH_NO_SEPARATOR or @c wxPATH_GET_SEPARATOR to omit or include + the trailing path separator, the default is to include it. + + @return Volume path string. + */ + static wxString GetVolumeString(char drive, int flags = wxPATH_GET_SEPARATOR); + /** Returns @true if an extension is present. */ diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 1e3444bc87..9285be0e5d 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -1953,6 +1953,23 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format ) return format; } +#ifdef wxHAS_FILESYSTEM_VOLUMES + +/* static */ +wxString wxFileName::GetVolumeString(char drive, int flags) +{ + wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); + + wxString vol(drive); + vol += wxFILE_SEP_DSK; + if ( flags & wxPATH_GET_SEPARATOR ) + vol += wxFILE_SEP_PATH; + + return vol; +} + +#endif // wxHAS_FILESYSTEM_VOLUMES + // ---------------------------------------------------------------------------- // path splitting function // ---------------------------------------------------------------------------- diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp index f75a597988..8ad7f390a0 100644 --- a/src/generic/dirctrlg.cpp +++ b/src/generic/dirctrlg.cpp @@ -40,6 +40,7 @@ #include "wx/module.h" #endif +#include "wx/filename.h" #include "wx/filefn.h" #include "wx/imaglist.h" #include "wx/tokenzr.h" @@ -108,7 +109,7 @@ bool wxIsDriveAvailable(const wxString& dirName); size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids) { -#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__) +#ifdef wxHAS_FILESYSTEM_VOLUMES #ifdef __WXWINCE__ // No logical drives; return "\" @@ -164,9 +165,10 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI { if (ulDriveMap & ( 1 << i )) { - wxString path, name; - path.Printf(wxT("%c:\\"), 'A' + i); - name.Printf(wxT("%c:"), 'A' + i); + const wxString path = wxFileName::GetVolumeString( + 'A' + i, wxPATH_GET_SEPARATOR); + const wxString name = wxFileName::GetVolumeString( + 'A' + i, wxPATH_NO_SEPARATOR); // Note: If _filesys is unsupported by some compilers, // we can always replace it by DosQueryFSAttach @@ -201,20 +203,18 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI } } #else // !__WIN32__, !__OS2__ - int drive; - /* If we can switch to the drive, it exists. */ - for( drive = 1; drive <= 26; drive++ ) + for ( char drive = 'A'; drive <= 'Z'; drive++ ) { - wxString path, name; - path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1)); - name.Printf(wxT("%c:"), (char) (drive + 'A' - 1)); + const wxString + path = wxFileName::GetVolumeString(drive, wxPATH_GET_SEPARATOR); if (wxIsDriveAvailable(path)) { paths.Add(path); - names.Add(name); - icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive); + names.Add(wxFileName::GetVolumeString(drive, wxPATH_NO_SEPARATOR)); + icon_ids.Add(drive <= 2 ? wxFileIconsTable::floppy + : wxFileIconsTable::drive); } } #endif // __WIN32__/!__WIN32__ -- 2.45.2