-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDirFileEnum.h
345 lines (305 loc) · 9.6 KB
/
DirFileEnum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2017-2018, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#pragma once
#include <string>
/**
* Enumerates over a directory tree, non-recursively.
* Advantages over CFileFind:
* 1) Return values are not broken. An error return from
* CFileFind::FindNext() indicates that the *next*
* call to CFileFind::FindNext() will fail.
* A failure from CSimpleFileFind means *that* call
* failed, which is what I'd expect.
* 2) Error handling. If you use CFileFind, you are
* required to call ::GetLastError() yourself, there
* is no abstraction.
* 3) Support for ignoring the "." and ".." directories
* automatically.
* 4) No dynamic memory allocation.
*/
class CSimpleFileFind
{
private:
/**
* Windows FindFirstFile() handle.
*/
HANDLE m_hFindFile;
/**
* Windows error code - if all is well, ERROR_SUCCESS.
* At end of directory, ERROR_NO_MORE_FILES.
*/
DWORD m_dError;
/**
* Flag indicating that FindNextFile() has not yet been
* called.
*/
BOOL m_bFirst;
/**
* Flag indicating whether CSimpleFileFind was started for a file
*/
BOOL m_bFile;
protected:
/**
* The prefix for files in this directory.
* Ends with a "\", unless it's a drive letter only
* ("C:" is different from "C:\", and "C:filename" is
* legal anyway.)
*/
std::wstring m_sPathPrefix;
/**
* The file data returned by FindFirstFile()/FindNextFile().
*/
WIN32_FIND_DATA m_findFileData;
public:
/**
* Constructor.
*
* \param sPath The path to search in.
* \param sPattern The filename pattern - default all files.
*/
CSimpleFileFind(const std::wstring& sPath, LPCWSTR pPattern = L"*.*", FINDEX_INFO_LEVELS infoLevel = FindExInfoBasic);
~CSimpleFileFind();
/**
* Advance to the next file.
* Note that the state of this object is undefined until
* this method is called the first time.
*
* \return TRUE if a file was found, FALSE on error or
* end-of-directory (use IsError() and IsPastEnd() to
* disambiguate).
*/
bool FindNextFile();
/**
* Advance to the next file, ignoring the "." and ".."
* pseudo-directories (if seen).
*
* Behaves like FindNextFile(), apart from ignoring "."
* and "..".
*
* \return TRUE if a file was found, FALSE on error or
* end-of-directory.
*/
bool FindNextFileNoDots(DWORD attrToIgnore);
/**
* Advance to the next file, ignoring all directories.
*
* Behaves like FindNextFile(), apart from ignoring
* directories.
*
* \return TRUE if a file was found, FALSE on error or
* end-of-directory.
*/
bool FindNextFileNoDirectories();
/**
* Get the Windows error code.
* Only useful when IsError() returns true.
*
* \return Windows error code.
*/
inline DWORD GetError() const
{
return m_dError;
}
/**
* Get the file/directory attributes.
*
* \return item attributes.
*/
inline DWORD GetAttributes() const
{
return m_findFileData.dwFileAttributes;
}
/**
* Check if the current file data is valid.
* (I.e. there has not been an error and we are not past
* the end of the directory).
*
* \return TRUE iff the current file data is valid.
*/
inline bool IsValid() const
{
return (m_dError == ERROR_SUCCESS);
}
/**
* Check if we have passed the end of the directory.
*
* \return TRUE iff we have passed the end of the directory.
*/
inline BOOL IsPastEnd() const
{
return (m_dError == ERROR_NO_MORE_FILES);
}
/**
* Check if there has been an unexpected error - i.e.
* any error other than passing the end of the directory.
*
* \return TRUE iff there has been an unexpected error.
*/
inline bool IsError() const
{
return (m_dError != ERROR_SUCCESS) && (m_dError != ERROR_NO_MORE_FILES);
}
/**
* Check if the current file is a directory.
*
* \return TRUE iff the current file is a directory.
*/
inline bool IsDirectory() const
{
return !!(m_findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
/**
* Get the current file name (excluding the path).
*
* \return the current file name.
*/
inline std::wstring GetFileName() const
{
return m_findFileData.cFileName;
}
const WIN32_FIND_DATA* GetFileFindData() const { return &m_findFileData; }
/*
* Get the current file name, including the path.
*
* \return the current file path.
*/
inline std::wstring GetFilePath() const
{
if (m_bFile)
return m_sPathPrefix;
return m_sPathPrefix + m_findFileData.cFileName;
}
/**
* Get the last write time of the file
*
* \return the last write time
*/
FILETIME GetLastWriteTime() const
{
return m_findFileData.ftLastWriteTime;
}
/**
* Get the creation time of the file
*
* \return the creation time
*/
FILETIME GetCreateTime() const
{
return m_findFileData.ftCreationTime;
}
/**
* Check if the current file is the "." or ".."
* pseudo-directory.
*
* \return TRUE iff the current file is the "." or ".."
* pseudo-directory.
*/
inline bool IsDots() const
{
return IsDirectory() && m_findFileData.cFileName[0] == L'.' && ((m_findFileData.cFileName[1] == 0) || (m_findFileData.cFileName[1] == L'.' && m_findFileData.cFileName[2] == 0));
}
};
/**
* Enumerates over a directory tree, recursively.
*/
class CDirFileEnum
{
private:
class CDirStackEntry : public CSimpleFileFind
{
public:
CDirStackEntry(CDirStackEntry* seNext, const std::wstring& sDirName);
~CDirStackEntry();
CDirStackEntry* m_seNext;
};
CDirStackEntry* m_seStack;
bool m_bIsNew;
DWORD m_attrToIgnore;
inline void PopStack();
inline void PushStack(const std::wstring& sDirName);
public:
/**
* Iterate through the specified directory and all subdirectories.
* It does not matter whether or not the specified directory ends
* with a slash. Both relative and absolute paths are allowed,
* the results of this iterator will be consistent with the style
* passed to this constructor.
*
* @param dirName The directory to search in.
*/
CDirFileEnum(const std::wstring& dirName);
/**
* Destructor. Frees all resources.
*/
~CDirFileEnum();
/**
* Get the next file from this iterator.
*
* \param result On successful return, holds the full path to the found
* file. (If this function returns FALSE, the value of
* result is unspecified).
* \param pbIsDirectory Pointer to a bool variable which will hold
* TRUE if the \c result path is a directory, FALSE
* if it's a file. Pass nullptr if you don't need that information.
* \param recurse true if recursing into subdirectories is requested.
* \return TRUE iff a file was found, false at end of the iteration.
*/
bool NextFile(std::wstring& result, bool* pbIsDirectory, bool recurse = true);
/**
* Get the file info structure.
*
* \return The WIN32_FIND_DATA structure of the file or directory
*/
const WIN32_FIND_DATA* GetFileInfo() const { return m_seStack->GetFileFindData(); }
/**
* Set a mask of file attributes to ignore. Files or directories that
* have any one of those attributes set won't be returned.
* Useful if you want to ignore e.g. all system or hidden files: pass
* FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN in that case.
*
* \param attr the file attribute mask
*/
void SetAttributesToIgnore(DWORD attr) { m_attrToIgnore = attr; }
/**
* Get the last write time of the file
*
* \return the last write time
*/
FILETIME GetLastWriteTime() const
{
if (m_seStack)
return m_seStack->GetLastWriteTime();
FILETIME ft = {0};
return ft;
}
/**
* Get the creation time of the file
*
* \return the creation time
*/
FILETIME GetCreateTime() const
{
if (m_seStack)
return m_seStack->GetCreateTime();
FILETIME ft = {0};
return ft;
}
DWORD GetError() const
{
if (m_seStack)
return m_seStack->GetError();
return 0;
}
};