]>
Commit | Line | Data |
---|---|---|
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> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_DIR_H_ | |
13 | #define _WX_DIR_H_ | |
14 | ||
15 | #include "wx/longlong.h" | |
16 | #include "wx/string.h" | |
17 | #include "wx/filefn.h" // for wxS_DIR_DEFAULT | |
18 | ||
19 | class WXDLLIMPEXP_FWD_BASE wxArrayString; | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
22 | // constants | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
25 | // These flags affect the behaviour of GetFirst/GetNext() and Traverse(). | |
26 | // They define what types are included in the list of items they produce. | |
27 | // Note that wxDIR_NO_FOLLOW is relevant only on Unix and ignored under systems | |
28 | // not supporting symbolic links. | |
29 | enum wxDirFlags | |
30 | { | |
31 | wxDIR_FILES = 0x0001, // include files | |
32 | wxDIR_DIRS = 0x0002, // include directories | |
33 | wxDIR_HIDDEN = 0x0004, // include hidden files | |
34 | wxDIR_DOTDOT = 0x0008, // include '.' and '..' | |
35 | wxDIR_NO_FOLLOW = 0x0010, // don't dereference any symlink | |
36 | ||
37 | // by default, enumerate everything except '.' and '..' | |
38 | wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN | |
39 | }; | |
40 | ||
41 | // these constants are possible return value of wxDirTraverser::OnDir() | |
42 | enum wxDirTraverseResult | |
43 | { | |
44 | wxDIR_IGNORE = -1, // ignore this directory but continue with others | |
45 | wxDIR_STOP, // stop traversing | |
46 | wxDIR_CONTINUE // continue into this directory | |
47 | }; | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // wxDirTraverser: helper class for wxDir::Traverse() | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | class WXDLLIMPEXP_BASE wxDirTraverser | |
54 | { | |
55 | public: | |
56 | /// a virtual dtor has been provided since this class has virtual members | |
57 | virtual ~wxDirTraverser() { } | |
58 | // called for each file found by wxDir::Traverse() | |
59 | // | |
60 | // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't | |
61 | // make sense) | |
62 | virtual wxDirTraverseResult OnFile(const wxString& filename) = 0; | |
63 | ||
64 | // called for each directory found by wxDir::Traverse() | |
65 | // | |
66 | // return one of the enum elements defined above | |
67 | virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0; | |
68 | ||
69 | // called for each directory which we couldn't open during our traversal | |
70 | // of the directory tree | |
71 | // | |
72 | // this method can also return either wxDIR_STOP, wxDIR_IGNORE or | |
73 | // wxDIR_CONTINUE but the latter is treated specially: it means to retry | |
74 | // opening the directory and so may lead to infinite loop if it is | |
75 | // returned unconditionally, be careful with this! | |
76 | // | |
77 | // the base class version always returns wxDIR_IGNORE | |
78 | virtual wxDirTraverseResult OnOpenError(const wxString& dirname); | |
79 | }; | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // wxDir: portable equivalent of {open/read/close}dir functions | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | class WXDLLIMPEXP_FWD_BASE wxDirData; | |
86 | ||
87 | class WXDLLIMPEXP_BASE wxDir | |
88 | { | |
89 | public: | |
90 | ||
91 | // ctors | |
92 | // ----- | |
93 | ||
94 | // default, use Open() | |
95 | wxDir() { m_data = NULL; } | |
96 | ||
97 | // opens the directory for enumeration, use IsOpened() to test success | |
98 | wxDir(const wxString& dir); | |
99 | ||
100 | // dtor calls Close() automatically | |
101 | ~wxDir() { Close(); } | |
102 | ||
103 | // open the directory for enumerating | |
104 | bool Open(const wxString& dir); | |
105 | ||
106 | // close the directory, Open() can be called again later | |
107 | void Close(); | |
108 | ||
109 | // returns true if the directory was successfully opened | |
110 | bool IsOpened() const; | |
111 | ||
112 | // get the full name of the directory (without '/' at the end) | |
113 | wxString GetName() const; | |
114 | ||
115 | // Same as GetName() but does include the trailing separator, unless the | |
116 | // string is empty (only for invalid directories). | |
117 | wxString GetNameWithSep() const; | |
118 | ||
119 | ||
120 | // file enumeration routines | |
121 | // ------------------------- | |
122 | ||
123 | // start enumerating all files matching filespec (or all files if it is | |
124 | // empty) and flags, return true on success | |
125 | bool GetFirst(wxString *filename, | |
126 | const wxString& filespec = wxEmptyString, | |
127 | int flags = wxDIR_DEFAULT) const; | |
128 | ||
129 | // get next file in the enumeration started with GetFirst() | |
130 | bool GetNext(wxString *filename) const; | |
131 | ||
132 | // return true if this directory has any files in it | |
133 | bool HasFiles(const wxString& spec = wxEmptyString) const; | |
134 | ||
135 | // return true if this directory has any subdirectories | |
136 | bool HasSubDirs(const wxString& spec = wxEmptyString) const; | |
137 | ||
138 | // enumerate all files in this directory and its subdirectories | |
139 | // | |
140 | // return the number of files found | |
141 | size_t Traverse(wxDirTraverser& sink, | |
142 | const wxString& filespec = wxEmptyString, | |
143 | int flags = wxDIR_DEFAULT) const; | |
144 | ||
145 | // simplest version of Traverse(): get the names of all files under this | |
146 | // directory into filenames array, return the number of files | |
147 | static size_t GetAllFiles(const wxString& dirname, | |
148 | wxArrayString *files, | |
149 | const wxString& filespec = wxEmptyString, | |
150 | int flags = wxDIR_DEFAULT); | |
151 | ||
152 | // check if there any files matching the given filespec under the given | |
153 | // directory (i.e. searches recursively), return the file path if found or | |
154 | // empty string otherwise | |
155 | static wxString FindFirst(const wxString& dirname, | |
156 | const wxString& filespec, | |
157 | int flags = wxDIR_DEFAULT); | |
158 | ||
159 | #if wxUSE_LONGLONG | |
160 | // returns the size of all directories recursively found in given path | |
161 | static wxULongLong GetTotalSize(const wxString &dir, wxArrayString *filesSkipped = NULL); | |
162 | #endif // wxUSE_LONGLONG | |
163 | ||
164 | ||
165 | // static utilities for directory management | |
166 | // (alias to wxFileName's functions for dirs) | |
167 | // ----------------------------------------- | |
168 | ||
169 | // test for existence of a directory with the given name | |
170 | static bool Exists(const wxString& dir); | |
171 | ||
172 | static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT, | |
173 | int flags = 0); | |
174 | ||
175 | static bool Remove(const wxString &dir, int flags = 0); | |
176 | ||
177 | ||
178 | private: | |
179 | friend class wxDirData; | |
180 | ||
181 | wxDirData *m_data; | |
182 | ||
183 | wxDECLARE_NO_COPY_CLASS(wxDir); | |
184 | }; | |
185 | ||
186 | #endif // _WX_DIR_H_ | |
187 |