2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: dir.h
|
2008-04-20 21:40:12 -04:00
|
|
|
// Purpose: interface of wxDir and wxDirTraverser
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
/**
|
|
|
|
Possible return values of wxDirTraverser callback functions.
|
|
|
|
*/
|
|
|
|
enum wxDirTraverseResult
|
|
|
|
{
|
|
|
|
wxDIR_IGNORE = -1, ///< Ignore this directory but continue with others.
|
|
|
|
wxDIR_STOP, ///< Stop traversing.
|
|
|
|
wxDIR_CONTINUE ///< Continue into this directory.
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxDirTraverser
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
wxDirTraverser is an abstract interface which must be implemented by
|
|
|
|
objects passed to wxDir::Traverse() function.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
Example of use (this works almost like wxDir::GetAllFiles()):
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
class wxDirTraverserSimple : public wxDirTraverser
|
2008-04-20 21:40:12 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
|
|
|
|
|
|
|
|
virtual wxDirTraverseResult OnFile(const wxString& filename)
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
2008-04-20 21:40:12 -04:00
|
|
|
m_files.Add(filename);
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
|
|
|
|
{
|
|
|
|
return wxDIR_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxArrayString& m_files;
|
|
|
|
};
|
|
|
|
|
|
|
|
// get the names of all files in the array
|
|
|
|
wxArrayString files;
|
|
|
|
wxDirTraverserSimple traverser(files);
|
|
|
|
|
|
|
|
wxDir dir(dirname);
|
|
|
|
dir.Traverse(traverser);
|
2008-03-08 08:52:38 -05:00
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{file}
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxDirTraverser
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
This function is called for each directory. It may return ::wxDIR_STOP
|
|
|
|
to abort traversing completely, ::wxDIR_IGNORE to skip this directory
|
|
|
|
but continue with others or ::wxDIR_CONTINUE to enumerate all files and
|
2008-03-08 08:52:38 -05:00
|
|
|
subdirectories in this directory.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
|
|
|
This is a pure virtual function and must be implemented in the derived
|
|
|
|
class.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-29 06:52:37 -04:00
|
|
|
virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
This function is called for each file. It may return ::wxDIR_STOP to
|
|
|
|
abort traversing (for example, if the file being searched is found) or
|
|
|
|
::wxDIR_CONTINUE to proceed.
|
|
|
|
|
|
|
|
This is a pure virtual function and must be implemented in the derived
|
|
|
|
class.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-09-29 06:52:37 -04:00
|
|
|
virtual wxDirTraverseResult OnFile(const wxString& filename) = 0;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
This function is called for each directory which we failed to open for
|
2008-04-20 21:40:12 -04:00
|
|
|
enumerating. It may return ::wxDIR_STOP to abort traversing completely,
|
|
|
|
::wxDIR_IGNORE to skip this directory but continue with others or
|
|
|
|
::wxDIR_CONTINUE to retry opening this directory once again.
|
|
|
|
|
|
|
|
The base class version always returns ::wxDIR_IGNORE.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
virtual wxDirTraverseResult OnOpenError(const wxString& openerrorname);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
/**
|
2012-10-23 19:57:32 -04:00
|
|
|
These flags affect the behaviour of GetFirst/GetNext() and Traverse(),
|
|
|
|
determining what types are included in the list of items they produce.
|
2008-04-20 21:40:12 -04:00
|
|
|
*/
|
2009-03-01 10:47:49 -05:00
|
|
|
enum wxDirFlags
|
2008-04-20 21:40:12 -04:00
|
|
|
{
|
|
|
|
wxDIR_FILES = 0x0001, ///< Includes files.
|
|
|
|
wxDIR_DIRS = 0x0002, ///< Includes directories.
|
|
|
|
wxDIR_HIDDEN = 0x0004, ///< Includes hidden files.
|
|
|
|
wxDIR_DOTDOT = 0x0008, ///< Includes "." and "..".
|
|
|
|
|
2012-10-23 19:57:32 -04:00
|
|
|
/**
|
|
|
|
Don't follow symbolic links during the directory traversal.
|
|
|
|
|
|
|
|
This flag is ignored under systems not supporting symbolic links (i.e.
|
|
|
|
non-Unix ones).
|
|
|
|
|
|
|
|
Notice that this flag is @e not included in wxDIR_DEFAULT and so the
|
|
|
|
default behaviour of wxDir::Traverse() is to follow symbolic links,
|
|
|
|
even if they lead outside of the directory being traversed.
|
|
|
|
|
|
|
|
@since 2.9.5
|
|
|
|
*/
|
|
|
|
wxDIR_NO_FOLLOW = 0x0010,
|
|
|
|
|
|
|
|
/**
|
|
|
|
Default directory traversal flags include both files and directories,
|
|
|
|
even hidden.
|
|
|
|
|
|
|
|
Notice that by default wxDIR_NO_FOLLOW is @e not included, meaning that
|
|
|
|
symbolic links are followed by default. If this is not desired, you
|
|
|
|
must pass that flag explicitly.
|
|
|
|
*/
|
2008-04-20 21:40:12 -04:00
|
|
|
wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxDir
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
wxDir is a portable equivalent of Unix open/read/closedir functions which
|
2008-04-20 21:40:12 -04:00
|
|
|
allow enumerating of the files in a directory. wxDir allows to enumerate
|
|
|
|
files as well as directories.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
|
|
|
wxDir also provides a flexible way to enumerate files recursively using
|
2008-04-20 21:40:12 -04:00
|
|
|
Traverse() or a simpler GetAllFiles() function.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Example of use:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@code
|
|
|
|
wxDir dir(wxGetCwd());
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
if ( !dir.IsOpened() )
|
|
|
|
{
|
|
|
|
// deal with the error here - wxDir would already log an error message
|
|
|
|
// explaining the exact reason of the failure
|
|
|
|
return;
|
|
|
|
}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
puts("Enumerating object files in current directory:");
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
wxString filename;
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
bool cont = dir.GetFirst(&filename, filespec, flags);
|
|
|
|
while ( cont )
|
|
|
|
{
|
|
|
|
printf("%s\n", filename.c_str());
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
cont = dir.GetNext(&filename);
|
|
|
|
}
|
2008-03-08 08:52:38 -05:00
|
|
|
@endcode
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{file}
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxDir
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
Default constructor, use Open() afterwards.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
wxDir();
|
2008-04-20 21:40:12 -04:00
|
|
|
/**
|
|
|
|
Opens the directory for enumeration, use IsOpened() to test for errors.
|
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxDir(const wxString& dir);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2012-07-24 16:45:30 -04:00
|
|
|
Destructor cleans up the associated resources by calling Close().
|
|
|
|
|
|
|
|
It is not virtual and so this class is not meant to be used
|
|
|
|
polymorphically.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
~wxDir();
|
|
|
|
|
2012-07-24 16:45:30 -04:00
|
|
|
/**
|
|
|
|
Close the directory.
|
|
|
|
|
|
|
|
The object can't be used after closing it, but Open() may be called
|
|
|
|
again to reopen it later.
|
|
|
|
|
|
|
|
@since 2.9.5
|
|
|
|
*/
|
|
|
|
void Close();
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
Test for existence of a directory with the given name.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
static bool Exists(const wxString& dir);
|
|
|
|
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
The function returns the path of the first file matching the given
|
|
|
|
@a filespec or an empty string if there are no files matching it.
|
|
|
|
|
|
|
|
The @a flags parameter may or may not include ::wxDIR_FILES, the
|
2008-03-09 08:33:59 -04:00
|
|
|
function always behaves as if it were specified. By default, @a flags
|
2008-04-20 21:40:12 -04:00
|
|
|
includes ::wxDIR_DIRS and so the function recurses into the
|
|
|
|
subdirectories but if this flag is not specified, the function
|
|
|
|
restricts the search only to the directory @a dirname itself.
|
2009-03-01 10:47:49 -05:00
|
|
|
See ::wxDirFlags for the list of the possible flags.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
|
|
|
@see Traverse()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
static wxString FindFirst(const wxString& dirname,
|
|
|
|
const wxString& filespec,
|
|
|
|
int flags = wxDIR_DEFAULT);
|
|
|
|
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
The function appends the names of all the files under directory
|
|
|
|
@a dirname to the array @a files (note that its old content is
|
|
|
|
preserved). Only files matching the @a filespec are taken, with empty
|
|
|
|
spec matching all the files.
|
|
|
|
|
|
|
|
The @a flags parameter should always include ::wxDIR_FILES or the array
|
|
|
|
would be unchanged and should include ::wxDIR_DIRS flag to recurse into
|
2008-03-08 08:52:38 -05:00
|
|
|
subdirectories (both flags are included in the value by default).
|
2009-03-01 10:47:49 -05:00
|
|
|
See ::wxDirFlags for the list of the possible flags.
|
2010-04-19 14:54:26 -04:00
|
|
|
|
|
|
|
@return Returns the total number of files found while traversing
|
|
|
|
the directory @a dirname (i.e. the number of entries appended
|
|
|
|
to the @a files array).
|
2008-04-20 21:40:12 -04:00
|
|
|
|
|
|
|
@see Traverse()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-04-20 21:40:12 -04:00
|
|
|
static size_t GetAllFiles(const wxString& dirname, wxArrayString* files,
|
2008-03-08 08:52:38 -05:00
|
|
|
const wxString& filespec = wxEmptyString,
|
|
|
|
int flags = wxDIR_DEFAULT);
|
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Start enumerating all files matching @a filespec (or all files if it is
|
2008-03-08 08:52:38 -05:00
|
|
|
empty) and @e flags, return @true on success.
|
2009-03-01 10:47:49 -05:00
|
|
|
See ::wxDirFlags for the list of the possible flags.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool GetFirst(wxString* filename,
|
|
|
|
const wxString& filespec = wxEmptyString,
|
2008-03-09 12:24:26 -04:00
|
|
|
int flags = wxDIR_DEFAULT) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2012-05-04 16:35:31 -04:00
|
|
|
Returns the name of the directory itself.
|
|
|
|
|
|
|
|
The returned string does not have the trailing path separator (slash or
|
|
|
|
backslash).
|
|
|
|
|
|
|
|
Notice that in spite of this the last character of the returned string
|
|
|
|
can still be the path separator if this directory is the root one.
|
2013-08-21 11:26:34 -04:00
|
|
|
Because of this, don't append @c wxFILE_SEP_PATH to the returned value
|
2012-05-04 16:35:31 -04:00
|
|
|
if you do need a slash-terminated directory name but use
|
|
|
|
GetNameWithSep() instead to avoid having duplicate consecutive slashes.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxString GetName() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2012-05-04 16:35:31 -04:00
|
|
|
/**
|
|
|
|
Returns the name of the directory with the path separator appended.
|
|
|
|
|
2013-08-21 11:26:34 -04:00
|
|
|
The last character of the returned string is always @c wxFILE_SEP_PATH
|
2012-05-04 16:35:31 -04:00
|
|
|
unless the string is empty, indicating that this directory is invalid.
|
|
|
|
|
|
|
|
@see GetName()
|
|
|
|
|
|
|
|
@since 2.9.4
|
|
|
|
*/
|
|
|
|
wxString GetNameWithSep() const;
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
Continue enumerating files which satisfy the criteria specified by the
|
|
|
|
last call to GetFirst().
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool GetNext(wxString* filename) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the size (in bytes) of all files recursively found in @c dir or
|
|
|
|
@c wxInvalidSize in case of error.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
2011-03-22 10:17:38 -04:00
|
|
|
In case it happens that while traversing folders a file's size cannot
|
2008-04-20 21:40:12 -04:00
|
|
|
be read, that file is added to the @a filesSkipped array, if not @NULL,
|
|
|
|
and then skipped. This usually happens with some special folders which
|
|
|
|
are locked by the operating system or by another process. Remember that
|
|
|
|
when the size of @a filesSkipped is not zero, then the returned value
|
|
|
|
is not 100% accurate and, if the skipped files were big, it could be
|
2008-03-08 08:52:38 -05:00
|
|
|
far from real size of the directory.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
|
|
|
@see wxFileName::GetHumanReadableSize(), wxGetDiskSpace()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
static wxULongLong GetTotalSize(const wxString& dir,
|
2008-03-09 08:33:59 -04:00
|
|
|
wxArrayString* filesSkipped = NULL);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-03-08 09:43:31 -05:00
|
|
|
Returns @true if the directory contains any files matching the given
|
2008-04-20 21:40:12 -04:00
|
|
|
@a filespec. If @a filespec is empty, look for any files at all. In any
|
2008-03-08 08:52:38 -05:00
|
|
|
case, even hidden files are taken into account.
|
|
|
|
*/
|
2008-04-22 12:29:43 -04:00
|
|
|
bool HasFiles(const wxString& filespec = wxEmptyString) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the directory contains any subdirectories (if a non
|
2008-04-20 21:40:12 -04:00
|
|
|
empty @a filespec is given, only check for directories matching it).
|
2008-03-08 08:52:38 -05:00
|
|
|
The hidden subdirectories are taken into account as well.
|
|
|
|
*/
|
2008-04-22 12:29:43 -04:00
|
|
|
bool HasSubDirs(const wxString& dirspec = wxEmptyString) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
Returns @true if the directory was successfully opened by a previous
|
|
|
|
call to Open().
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOpened() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2010-06-19 07:07:16 -04:00
|
|
|
/**
|
|
|
|
Creates a directory.
|
|
|
|
|
|
|
|
This is just an alias for wxFileName::Mkdir(); refer to that function
|
|
|
|
for more info.
|
|
|
|
*/
|
|
|
|
static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT,
|
|
|
|
int flags = 0);
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-04-20 21:40:12 -04:00
|
|
|
Open the directory for enumerating, returns @true on success or @false
|
|
|
|
if an error occurred.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool Open(const wxString& dir);
|
|
|
|
|
2010-06-19 07:07:16 -04:00
|
|
|
/**
|
|
|
|
Removes a directory.
|
|
|
|
|
|
|
|
This is just an alias for wxFileName::Rmdir(); refer to that function
|
|
|
|
for more info.
|
|
|
|
*/
|
|
|
|
static bool Remove(const wxString &dir, int flags = 0);
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2012-05-17 12:10:22 -04:00
|
|
|
Enumerate all files and directories under the given directory.
|
|
|
|
|
|
|
|
If @a flags contains ::wxDIR_DIRS this enumeration is recursive, i.e.
|
|
|
|
all the subdirectories of the given one and the files inside them will
|
|
|
|
be traversed. Otherwise only the files in this directory itself are.
|
|
|
|
|
|
|
|
If @a flags doesn't contain ::wxDIR_FILES then only subdirectories are
|
|
|
|
examined but not normal files. It doesn't make sense to not specify
|
|
|
|
either ::wxDIR_DIRS or ::wxDIR_FILES and usually both of them should be
|
|
|
|
specified, as is the case by default.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
2010-04-19 14:54:26 -04:00
|
|
|
For each directory found, @ref wxDirTraverser::OnDir() "sink.OnDir()"
|
2008-04-20 21:40:12 -04:00
|
|
|
is called and @ref wxDirTraverser::OnFile() "sink.OnFile()" is called
|
|
|
|
for every file. Depending on the return value, the enumeration may
|
2012-05-17 12:10:22 -04:00
|
|
|
continue or stop. If entering a subdirectory fails, @ref
|
|
|
|
wxDirTraverser::OnOpenError() "sink.OnOpenError()" is called.
|
2008-04-20 21:40:12 -04:00
|
|
|
|
|
|
|
The function returns the total number of files found or @c "(size_t)-1"
|
|
|
|
on error.
|
|
|
|
|
2012-05-17 12:10:22 -04:00
|
|
|
See ::wxDirFlags for the full list of the possible flags.
|
|
|
|
|
2008-04-20 21:40:12 -04:00
|
|
|
@see GetAllFiles()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
size_t Traverse(wxDirTraverser& sink,
|
|
|
|
const wxString& filespec = wxEmptyString,
|
2008-09-27 07:21:10 -04:00
|
|
|
int flags = wxDIR_DEFAULT) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|