1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Purpose: wxDir documentation
4 %% Author: Vadim Zeitlin
8 %% Copyright: (c) Vadim Zeitlin
9 %% License: wxWindows license
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 \section{\class{wxDir
}}\label{wxdir
}
14 wxDir is a portable equivalent of Unix
{open/read/close
}dir functions which
15 allow enumerating of the files in a directory. wxDir allows enumerate files as
18 wxDir also provides a flexible way to enumerate files recursively using
19 \helpref{Traverse
}{wxdirtraverse
} or a simpler
20 \helpref{GetAllFiles
}{wxdirgetallfiles
} function.
25 wxDir dir(wxGetCwd());
27 if ( !dir.IsOpened() )
29 // deal with the error here - wxDir would already log an error message
30 // explaining the exact reason of the failure
34 puts("Enumerating object files in current directory:");
38 bool cont = dir.GetFirst(&filename, filespec, flags);
41 printf("
%s\n", filename.c_str());
43 cont = dir.GetNext(&filename);
47 \wxheading{Derived from
}
53 These flags define what kind of filenames is included in the list of files
54 enumerated by GetFirst/GetNext
60 wxDIR_FILES =
0x0001, // include files
61 wxDIR_DIRS =
0x0002, // include directories
62 wxDIR_HIDDEN =
0x0004, // include hidden files
63 wxDIR_DOTDOT =
0x0008, // include '.' and '..'
65 // by default, enumerate everything except '.' and '..'
66 wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
71 \wxheading{Include files
}
75 \latexignore{\rtfignore{\wxheading{Members
}}}
77 \membersection{wxDir::Exists
}\label{wxdirexists
}
79 \func{static bool
}{Exists
}{\param{const wxString\&
}{dir
}}
81 Test for existence of a directory with the given name
83 \membersection{wxDir::wxDir
}\label{wxdirwxdir
}
87 Default constructor, use
\helpref{Open()
}{wxdiropen
} afterwards.
89 \func{}{wxDir
}{\param{const wxString\&
}{dir
}}
91 Opens the directory for enumeration, use
\helpref{IsOpened()
}{wxdirisopened
}
94 \membersection{wxDir::
\destruct{wxDir
}}\label{wxdirdtor
}
96 \func{}{\destruct{wxDir
}}{\void}
98 Destructor cleans up the associated ressources. It is not virtual and so this
99 class is not meant to be used polymorphically.
101 \membersection{wxDir::Open
}\label{wxdiropen
}
103 \func{bool
}{Open
}{\param{const wxString\&
}{dir
}}
105 Open the directory for enumerating, returns TRUE on success or FALSE if an
108 \membersection{wxDir::IsOpened
}\label{wxdirisopened
}
110 \constfunc{bool
}{IsOpened
}{\void}
112 Returns TRUE if the directory was successfully opened by a previous call to
113 \helpref{Open
}{wxdiropen
}.
115 \membersection{wxDir::GetFirst
}\label{wxdirgetfirst
}
117 \constfunc{bool
}{GetFirst
}{\param{wxString*
}{filename
},
\param{const wxString\&
}{filespec = wxEmptyString
},
\param{int
}{flags = wxDIR
\_DEFAULT}}
119 Start enumerating all files matching
{\it filespec
} (or all files if it is
120 empty) and flags, return TRUE on success.
122 \membersection{wxDir::GetNext
}\label{wxdirgetnext
}
124 \constfunc{bool
}{GetNext
}{\param{wxString*
}{filename
}}
126 Continue enumerating files satisfying the criteria specified by the last call
127 to
\helpref{GetFirst
}{wxdirgetfirst
}.
129 \membersection{wxDir::HasFiles
}\label{wxdirhasfiles
}
131 \func{bool
}{HasFiles
}{\param{const wxString&
}{filespec = wxEmptyString
}}
133 Returns
{\tt TRUE
} if the directory contains any files matching the given
134 {\it filespec
}. If
{\it filespec
} is empty, look for any files at all. In any
135 case, even hidden files are taken into account.
137 \membersection{wxDir::HasSubDirs
}\label{wxdirhassubdirs
}
139 \func{bool
}{HasSubDirs
}{\param{const wxString&
}{dirspec = wxEmptyString
}}
141 Returns
{\tt TRUE
} if the directory contains any subdirectories (if a non
142 empty
{\it filespec
} is given, only check for directories matching it).
143 The hidden subdirectories are taken into account as well.
145 \membersection{wxDir::Traverse
}\label{wxdirtraverse
}
147 \func{size
\_t}{Traverse
}{\param{wxDirTraverser&
}{sink
},
\param{const wxString&
}{filespec = wxEmptyString
},
\param{int
}{flags = wxDIR
\_DEFAULT}}
149 Enumerate all files and directories under the given directory recursively
150 calling the element of the provided
\helpref{wxDirTraverser
}{wxdirtraverser
}
151 object for each of them.
153 More precisely, the function will really recurse into subdirectories if
154 {\it flags
} contains
{\tt wxDIR
\_DIRS} flag. It will ignore the files (but
155 still possibly recurse into subdirectories) if
{\tt wxDIR
\_FILES} flag is
158 For each found directory,
\helpref{sink.OnDir()
}{wxdirtraverserondir
} is called
159 and
\helpref{sink.OnFile()
}{wxdirtraverseronfile
} is called for every file.
160 Depending on the return value, the enumeration may continue or stop.
162 The function returns the total number of files found or
{\tt (size
\_t)-
1} on
165 See also:
\helpref{GetAllFiles
}{wxdirgetallfiles
}
167 \membersection{wxDir::GetAllFiles
}\label{wxdirgetallfiles
}
169 \func{static size
\_t}{GetAllFiles
}{\param{const wxString&
}{dirname
},
\param{wxArrayString *
}{files
},
\param{const wxString&
}{filespec = wxEmptyString
},
\param{int
}{flags = wxDIR
\_DEFAULT}}
171 The function appends the names of all the files under directory
{\it dirname
}
172 to the array
{\it files
} (note that its old contents is preserved). Only files
173 matching the
{\it filespec
} are taken, with empty spec matching all the files.
175 The
{\it flags
} parameter should always include
{\tt wxDIR
\_FILES} or the array
176 would be unchanged and should include
{\tt wxDIR
\_DIRS} flag to recurse into
177 subdirectories (both flags are included in the value by default).
179 See also:
\helpref{Traverse
}{wxdirtraverse
}
181 \section{\class{wxDirTraverser
}}\label{wxdirtraverser
}
183 wxDirTraverser is an abstract interface which must be implemented by objects
184 passed to
\helpref{Traverse
}{wxdirtraverse
} function.
186 Example of use (this works almost like
\helpref{GetAllFiles
}{wxdirgetallfiles
}):
189 class wxDirTraverserSimple : public wxDirTraverser
192 wxDirTraverserSimple(wxArrayString& files) : m_files(files)
{ }
194 virtual wxDirTraverseResult OnFile(const wxString& filename)
196 m_files.Add(filename);
197 return wxDIR_CONTINUE;
200 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
202 return wxDIR_CONTINUE;
206 wxArrayString& m_files;
209 // get the names of all files in the array
211 wxDirTraverserSimple traverser(files);
214 dir.Traverse(traverser);
217 \wxheading{Derived from
}
221 \wxheading{Constants
}
223 The elements of
{\tt wxDirTraverseResult
} are the possible return values of the
228 enum wxDirTraverseResult
230 wxDIR_IGNORE = -
1, // ignore this directory but continue with others
231 wxDIR_STOP, // stop traversing
232 wxDIR_CONTINUE // continue into this directory
237 \wxheading{Include files
}
241 \latexignore{\rtfignore{\wxheading{Members
}}}
243 \membersection{wxDirTraverser::OnFile
}\label{wxdirtraverseronfile
}
245 \func{virtual wxDirTraverseResult
}{OnFile
}{\param{const wxString&
}{filename
}}
247 This function is called for each file. It may return
{\tt wxDIR
\_STOP} to abort
248 traversing (for example, if the file being searched is found) or
249 {\tt wxDIR
\_CONTINUE} to proceed.
251 \membersection{wxDirTraverser::OnDir
}\label{wxdirtraverserondir
}
253 \func{virtual wxDirTraverseResult
}{OnDir
}{\param{const wxString&
}{dirname
}}
255 This function is called for each directory. It may return
{\tt wxSIR
\_STOP}
256 to abort traversing completely,
{\tt wxDIR
\_IGNORE} to skip this directory but
257 continue with others or
{\tt wxDIR
\_CONTINUE} to enumerate all files and
258 subdirectories in this directory.