added wxFileName::GetVolumeString() (#9950)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55596 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-09-14 01:18:05 +00:00
parent 19abad600c
commit 35c2aa4f19
4 changed files with 74 additions and 19 deletions

View File

@@ -37,6 +37,12 @@ class WXDLLIMPEXP_FWD_BASE wxFile;
class WXDLLIMPEXP_FWD_BASE wxFFile; class WXDLLIMPEXP_FWD_BASE wxFFile;
#endif #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 // constants
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -75,6 +81,7 @@ enum wxPathNormalize
// what exactly should GetPath() return? // what exactly should GetPath() return?
enum enum
{ {
wxPATH_NO_SEPARATOR = 0x0000, // for symmetry with wxPATH_GET_SEPARATOR
wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable
wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator
}; };
@@ -484,7 +491,12 @@ public:
wxString *path, wxString *path,
wxPathFormat format = wxPATH_NATIVE); 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 #if wxUSE_LONGLONG
// returns the size of the given filename // returns the size of the given filename

View File

@@ -427,18 +427,25 @@ public:
wxString GetName() const; wxString GetName() const;
/** /**
Returns the path part of the filename (without the name or extension). The Returns the path part of the filename (without the name or extension).
possible flags values are:
The possible flags values are:
@b wxPATH_GET_VOLUME @b wxPATH_GET_VOLUME
Return the path with the volume (does nothing for the filename formats without Return the path with the volume (does nothing for the filename formats
volumes), otherwise the path without volume part is returned. without volumes), otherwise the path without volume part is returned.
@b wxPATH_GET_SEPARATOR @b wxPATH_GET_SEPARATOR
Return the path with the trailing separator, if this flag is not given there Return the path with the trailing separator, if this flag is not given
will be no separator at the end of the path. 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, wxString GetPath(int flags = wxPATH_GET_VOLUME,
wxPathFormat format = wxPATH_NATIVE) const; wxPathFormat format = wxPATH_NATIVE) const;
@@ -534,6 +541,25 @@ public:
*/ */
static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE); 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. Returns @true if an extension is present.
*/ */

View File

@@ -1953,6 +1953,23 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format )
return 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 // path splitting function
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -40,6 +40,7 @@
#include "wx/module.h" #include "wx/module.h"
#endif #endif
#include "wx/filename.h"
#include "wx/filefn.h" #include "wx/filefn.h"
#include "wx/imaglist.h" #include "wx/imaglist.h"
#include "wx/tokenzr.h" #include "wx/tokenzr.h"
@@ -108,7 +109,7 @@ bool wxIsDriveAvailable(const wxString& dirName);
size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids) size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
{ {
#if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__) #ifdef wxHAS_FILESYSTEM_VOLUMES
#ifdef __WXWINCE__ #ifdef __WXWINCE__
// No logical drives; return "\" // No logical drives; return "\"
@@ -164,9 +165,10 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI
{ {
if (ulDriveMap & ( 1 << i )) if (ulDriveMap & ( 1 << i ))
{ {
wxString path, name; const wxString path = wxFileName::GetVolumeString(
path.Printf(wxT("%c:\\"), 'A' + i); 'A' + i, wxPATH_GET_SEPARATOR);
name.Printf(wxT("%c:"), 'A' + i); const wxString name = wxFileName::GetVolumeString(
'A' + i, wxPATH_NO_SEPARATOR);
// Note: If _filesys is unsupported by some compilers, // Note: If _filesys is unsupported by some compilers,
// we can always replace it by DosQueryFSAttach // we can always replace it by DosQueryFSAttach
@@ -201,20 +203,18 @@ size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayI
} }
} }
#else // !__WIN32__, !__OS2__ #else // !__WIN32__, !__OS2__
int drive;
/* If we can switch to the drive, it exists. */ /* 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; const wxString
path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1)); path = wxFileName::GetVolumeString(drive, wxPATH_GET_SEPARATOR);
name.Printf(wxT("%c:"), (char) (drive + 'A' - 1));
if (wxIsDriveAvailable(path)) if (wxIsDriveAvailable(path))
{ {
paths.Add(path); paths.Add(path);
names.Add(name); names.Add(wxFileName::GetVolumeString(drive, wxPATH_NO_SEPARATOR));
icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive); icon_ids.Add(drive <= 2 ? wxFileIconsTable::floppy
: wxFileIconsTable::drive);
} }
} }
#endif // __WIN32__/!__WIN32__ #endif // __WIN32__/!__WIN32__