]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dir.h
Improve error messages from wxImage::LoadFile().
[wxWidgets.git] / include / wx / dir.h
CommitLineData
1944c6bd
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/dir.h
3// Purpose: wxDir is a class for enumerating the files in a directory
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 08.12.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
1944c6bd
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_DIR_H_
13#define _WX_DIR_H_
14
2bd50191 15#include "wx/longlong.h"
20ceebaa 16#include "wx/string.h"
774ef7e3 17#include "wx/filefn.h" // for wxS_DIR_DEFAULT
1944c6bd 18
b5dbe15d 19class WXDLLIMPEXP_FWD_BASE wxArrayString;
2da2f941 20
1944c6bd
VZ
21// ----------------------------------------------------------------------------
22// constants
23// ----------------------------------------------------------------------------
24
25// these flags define what kind of filenames is included in the list of files
26// enumerated by GetFirst/GetNext
e8f3bf98 27enum wxDirFlags
1944c6bd
VZ
28{
29 wxDIR_FILES = 0x0001, // include files
30 wxDIR_DIRS = 0x0002, // include directories
31 wxDIR_HIDDEN = 0x0004, // include hidden files
32 wxDIR_DOTDOT = 0x0008, // include '.' and '..'
33
34 // by default, enumerate everything except '.' and '..'
35 wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
36};
37
35332784
VZ
38// these constants are possible return value of wxDirTraverser::OnDir()
39enum wxDirTraverseResult
40{
41 wxDIR_IGNORE = -1, // ignore this directory but continue with others
42 wxDIR_STOP, // stop traversing
43 wxDIR_CONTINUE // continue into this directory
44};
45
46// ----------------------------------------------------------------------------
47// wxDirTraverser: helper class for wxDir::Traverse()
48// ----------------------------------------------------------------------------
49
bddd7a8d 50class WXDLLIMPEXP_BASE wxDirTraverser
35332784
VZ
51{
52public:
ed62f740
VZ
53 /// a virtual dtor has been provided since this class has virtual members
54 virtual ~wxDirTraverser() { }
35332784
VZ
55 // called for each file found by wxDir::Traverse()
56 //
350777b6
VZ
57 // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't
58 // make sense)
35332784
VZ
59 virtual wxDirTraverseResult OnFile(const wxString& filename) = 0;
60
61 // called for each directory found by wxDir::Traverse()
62 //
63 // return one of the enum elements defined above
64 virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0;
350777b6
VZ
65
66 // called for each directory which we couldn't open during our traversal
2a8c8b35 67 // of the directory tree
350777b6
VZ
68 //
69 // this method can also return either wxDIR_STOP, wxDIR_IGNORE or
70 // wxDIR_CONTINUE but the latter is treated specially: it means to retry
71 // opening the directory and so may lead to infinite loop if it is
72 // returned unconditionally, be careful with this!
73 //
74 // the base class version always returns wxDIR_IGNORE
75 virtual wxDirTraverseResult OnOpenError(const wxString& dirname);
35332784
VZ
76};
77
1944c6bd
VZ
78// ----------------------------------------------------------------------------
79// wxDir: portable equivalent of {open/read/close}dir functions
80// ----------------------------------------------------------------------------
81
b5dbe15d 82class WXDLLIMPEXP_FWD_BASE wxDirData;
fbfb3fb3 83
bddd7a8d 84class WXDLLIMPEXP_BASE wxDir
1944c6bd
VZ
85{
86public:
1944c6bd
VZ
87
88 // ctors
89 // -----
90
91 // default, use Open()
92 wxDir() { m_data = NULL; }
93
94 // opens the directory for enumeration, use IsOpened() to test success
95 wxDir(const wxString& dir);
96
2a8c8b35 97 // dtor cleans up the associated resources
1944c6bd
VZ
98 ~wxDir();
99
100 // open the directory for enumerating
101 bool Open(const wxString& dir);
102
68379eaf 103 // returns true if the directory was successfully opened
1944c6bd
VZ
104 bool IsOpened() const;
105
35332784
VZ
106 // get the full name of the directory (without '/' at the end)
107 wxString GetName() const;
ce00f59b 108
35332784 109
1944c6bd
VZ
110 // file enumeration routines
111 // -------------------------
112
113 // start enumerating all files matching filespec (or all files if it is
68379eaf 114 // empty) and flags, return true on success
1944c6bd
VZ
115 bool GetFirst(wxString *filename,
116 const wxString& filespec = wxEmptyString,
117 int flags = wxDIR_DEFAULT) const;
118
d9ff0f91 119 // get next file in the enumeration started with GetFirst()
1944c6bd
VZ
120 bool GetNext(wxString *filename) const;
121
d9ff0f91 122 // return true if this directory has any files in it
106dcc2c 123 bool HasFiles(const wxString& spec = wxEmptyString) const;
d9ff0f91
VZ
124
125 // return true if this directory has any subdirectories
106dcc2c 126 bool HasSubDirs(const wxString& spec = wxEmptyString) const;
d9ff0f91 127
35332784 128 // enumerate all files in this directory and its subdirectories
1944c6bd 129 //
35332784
VZ
130 // return the number of files found
131 size_t Traverse(wxDirTraverser& sink,
132 const wxString& filespec = wxEmptyString,
133 int flags = wxDIR_DEFAULT) const;
134
135 // simplest version of Traverse(): get the names of all files under this
136 // directory into filenames array, return the number of files
137 static size_t GetAllFiles(const wxString& dirname,
138 wxArrayString *files,
139 const wxString& filespec = wxEmptyString,
140 int flags = wxDIR_DEFAULT);
1944c6bd 141
d1af8e2d
VZ
142 // check if there any files matching the given filespec under the given
143 // directory (i.e. searches recursively), return the file path if found or
144 // empty string otherwise
145 static wxString FindFirst(const wxString& dirname,
146 const wxString& filespec,
147 int flags = wxDIR_DEFAULT);
148
bd08f2f7 149#if wxUSE_LONGLONG
23b8a262
JS
150 // returns the size of all directories recursively found in given path
151 static wxULongLong GetTotalSize(const wxString &dir, wxArrayString *filesSkipped = NULL);
bd08f2f7 152#endif // wxUSE_LONGLONG
23b8a262 153
d38315df
FM
154
155 // static utilities for directory management
156 // (alias to wxFileName's functions for dirs)
157 // -----------------------------------------
ce00f59b 158
d38315df
FM
159 // test for existence of a directory with the given name
160 static bool Exists(const wxString& dir);
161
162 static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT,
163 int flags = 0);
164
165 static bool Remove(const wxString &dir, int flags = 0);
ce00f59b 166
d38315df 167
1944c6bd 168private:
886dd7d2 169 friend class wxDirData;
1944c6bd
VZ
170
171 wxDirData *m_data;
22f3361e 172
c0c133e1 173 wxDECLARE_NO_COPY_CLASS(wxDir);
1944c6bd
VZ
174};
175
176#endif // _WX_DIR_H_
886dd7d2 177