]>
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
16 | #pragma interface "dir.h" | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/string.h" | |
21 | #endif | |
22 | ||
23 | class WXDLLIMPEXP_BASE wxArrayString; | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // constants | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | // these flags define what kind of filenames is included in the list of files | |
30 | // enumerated by GetFirst/GetNext | |
31 | enum | |
32 | { | |
33 | wxDIR_FILES = 0x0001, // include files | |
34 | wxDIR_DIRS = 0x0002, // include directories | |
35 | wxDIR_HIDDEN = 0x0004, // include hidden files | |
36 | wxDIR_DOTDOT = 0x0008, // include '.' and '..' | |
37 | ||
38 | // by default, enumerate everything except '.' and '..' | |
39 | wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN | |
40 | }; | |
41 | ||
42 | // these constants are possible return value of wxDirTraverser::OnDir() | |
43 | enum wxDirTraverseResult | |
44 | { | |
45 | wxDIR_IGNORE = -1, // ignore this directory but continue with others | |
46 | wxDIR_STOP, // stop traversing | |
47 | wxDIR_CONTINUE // continue into this directory | |
48 | }; | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxDirTraverser: helper class for wxDir::Traverse() | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | class WXDLLIMPEXP_BASE wxDirTraverser | |
55 | { | |
56 | public: | |
57 | /// a virtual dtor has been provided since this class has virtual members | |
58 | virtual ~wxDirTraverser() { } | |
59 | // called for each file found by wxDir::Traverse() | |
60 | // | |
61 | // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't | |
62 | // make sense) | |
63 | virtual wxDirTraverseResult OnFile(const wxString& filename) = 0; | |
64 | ||
65 | // called for each directory found by wxDir::Traverse() | |
66 | // | |
67 | // return one of the enum elements defined above | |
68 | virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0; | |
69 | ||
70 | // called for each directory which we couldn't open during our traversal | |
71 | // of the directory tyree | |
72 | // | |
73 | // this method can also return either wxDIR_STOP, wxDIR_IGNORE or | |
74 | // wxDIR_CONTINUE but the latter is treated specially: it means to retry | |
75 | // opening the directory and so may lead to infinite loop if it is | |
76 | // returned unconditionally, be careful with this! | |
77 | // | |
78 | // the base class version always returns wxDIR_IGNORE | |
79 | virtual wxDirTraverseResult OnOpenError(const wxString& dirname); | |
80 | }; | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxDir: portable equivalent of {open/read/close}dir functions | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | class WXDLLIMPEXP_BASE wxDirData; | |
87 | ||
88 | class WXDLLIMPEXP_BASE wxDir | |
89 | { | |
90 | public: | |
91 | // test for existence of a directory with the given name | |
92 | static bool Exists(const wxString& dir); | |
93 | ||
94 | // ctors | |
95 | // ----- | |
96 | ||
97 | // default, use Open() | |
98 | wxDir() { m_data = NULL; } | |
99 | ||
100 | // opens the directory for enumeration, use IsOpened() to test success | |
101 | wxDir(const wxString& dir); | |
102 | ||
103 | // dtor cleans up the associated ressources | |
104 | ~wxDir(); | |
105 | ||
106 | // open the directory for enumerating | |
107 | bool Open(const wxString& dir); | |
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 | // file enumeration routines | |
116 | // ------------------------- | |
117 | ||
118 | // start enumerating all files matching filespec (or all files if it is | |
119 | // empty) and flags, return true on success | |
120 | bool GetFirst(wxString *filename, | |
121 | const wxString& filespec = wxEmptyString, | |
122 | int flags = wxDIR_DEFAULT) const; | |
123 | ||
124 | // get next file in the enumeration started with GetFirst() | |
125 | bool GetNext(wxString *filename) const; | |
126 | ||
127 | // return true if this directory has any files in it | |
128 | bool HasFiles(const wxString& spec = wxEmptyString); | |
129 | ||
130 | // return true if this directory has any subdirectories | |
131 | bool HasSubDirs(const wxString& spec = wxEmptyString); | |
132 | ||
133 | // enumerate all files in this directory and its subdirectories | |
134 | // | |
135 | // return the number of files found | |
136 | size_t Traverse(wxDirTraverser& sink, | |
137 | const wxString& filespec = wxEmptyString, | |
138 | int flags = wxDIR_DEFAULT) const; | |
139 | ||
140 | // simplest version of Traverse(): get the names of all files under this | |
141 | // directory into filenames array, return the number of files | |
142 | static size_t GetAllFiles(const wxString& dirname, | |
143 | wxArrayString *files, | |
144 | const wxString& filespec = wxEmptyString, | |
145 | int flags = wxDIR_DEFAULT); | |
146 | ||
147 | private: | |
148 | friend class wxDirData; | |
149 | ||
150 | wxDirData *m_data; | |
151 | ||
152 | DECLARE_NO_COPY_CLASS(wxDir) | |
153 | }; | |
154 | ||
155 | #endif // _WX_DIR_H_ | |
156 |