]>
Commit | Line | Data |
---|---|---|
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 | ||
1944c6bd | 15 | #ifndef WX_PRECOMP |
2da2f941 | 16 | #include "wx/string.h" |
1944c6bd VZ |
17 | #endif |
18 | ||
2da2f941 MB |
19 | class WXDLLIMPEXP_BASE wxArrayString; |
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 | |
27 | enum | |
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() |
39 | enum 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 | 50 | class WXDLLIMPEXP_BASE wxDirTraverser |
35332784 VZ |
51 | { |
52 | public: | |
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 | |
67 | // of the directory tyree | |
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 | ||
bddd7a8d | 82 | class WXDLLIMPEXP_BASE wxDirData; |
fbfb3fb3 | 83 | |
bddd7a8d | 84 | class WXDLLIMPEXP_BASE wxDir |
1944c6bd VZ |
85 | { |
86 | public: | |
87 | // test for existence of a directory with the given name | |
88 | static bool Exists(const wxString& dir); | |
89 | ||
90 | // ctors | |
91 | // ----- | |
92 | ||
93 | // default, use Open() | |
94 | wxDir() { m_data = NULL; } | |
95 | ||
96 | // opens the directory for enumeration, use IsOpened() to test success | |
97 | wxDir(const wxString& dir); | |
98 | ||
99 | // dtor cleans up the associated ressources | |
100 | ~wxDir(); | |
101 | ||
102 | // open the directory for enumerating | |
103 | bool Open(const wxString& dir); | |
104 | ||
68379eaf | 105 | // returns true if the directory was successfully opened |
1944c6bd VZ |
106 | bool IsOpened() const; |
107 | ||
35332784 VZ |
108 | // get the full name of the directory (without '/' at the end) |
109 | wxString GetName() const; | |
110 | ||
1944c6bd VZ |
111 | // file enumeration routines |
112 | // ------------------------- | |
113 | ||
114 | // start enumerating all files matching filespec (or all files if it is | |
68379eaf | 115 | // empty) and flags, return true on success |
1944c6bd VZ |
116 | bool GetFirst(wxString *filename, |
117 | const wxString& filespec = wxEmptyString, | |
118 | int flags = wxDIR_DEFAULT) const; | |
119 | ||
d9ff0f91 | 120 | // get next file in the enumeration started with GetFirst() |
1944c6bd VZ |
121 | bool GetNext(wxString *filename) const; |
122 | ||
d9ff0f91 | 123 | // return true if this directory has any files in it |
1357a7dd | 124 | bool HasFiles(const wxString& spec = wxEmptyString); |
d9ff0f91 VZ |
125 | |
126 | // return true if this directory has any subdirectories | |
1357a7dd | 127 | bool HasSubDirs(const wxString& spec = wxEmptyString); |
d9ff0f91 | 128 | |
35332784 | 129 | // enumerate all files in this directory and its subdirectories |
1944c6bd | 130 | // |
35332784 VZ |
131 | // return the number of files found |
132 | size_t Traverse(wxDirTraverser& sink, | |
133 | const wxString& filespec = wxEmptyString, | |
134 | int flags = wxDIR_DEFAULT) const; | |
135 | ||
136 | // simplest version of Traverse(): get the names of all files under this | |
137 | // directory into filenames array, return the number of files | |
138 | static size_t GetAllFiles(const wxString& dirname, | |
139 | wxArrayString *files, | |
140 | const wxString& filespec = wxEmptyString, | |
141 | int flags = wxDIR_DEFAULT); | |
1944c6bd VZ |
142 | |
143 | private: | |
886dd7d2 | 144 | friend class wxDirData; |
1944c6bd VZ |
145 | |
146 | wxDirData *m_data; | |
22f3361e VZ |
147 | |
148 | DECLARE_NO_COPY_CLASS(wxDir) | |
1944c6bd VZ |
149 | }; |
150 | ||
151 | #endif // _WX_DIR_H_ | |
886dd7d2 | 152 |