-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDirFileEnum.cpp
220 lines (193 loc) · 6 KB
/
DirFileEnum.cpp
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
// 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.
//
#include "stdafx.h"
#include <Shlwapi.h>
#include "DirFileEnum.h"
#pragma comment(lib, "shlwapi.lib")
CSimpleFileFind::CSimpleFileFind(const std::wstring& sPath, LPCWSTR pPattern, FINDEX_INFO_LEVELS infoLevel)
: m_dError(ERROR_SUCCESS)
, m_bFirst(true)
, m_sPathPrefix(sPath)
{
std::wstring sPattern = pPattern ? pPattern : L"";
if (pPattern == nullptr || wcscmp(pPattern, L"*.*") == 0)
{
auto slashPos = sPath.find_last_of(L"\\/");
if (slashPos != std::wstring::npos)
{
auto lastPart = sPath.substr(slashPos + 1);
if (lastPart.find_first_of(L"*?") != std::wstring::npos)
{
// the path contains a pattern
sPattern = lastPart;
m_sPathPrefix = sPath.substr(0, slashPos);
}
}
}
m_findFileData = {};
if (PathIsDirectory(m_sPathPrefix.c_str()))
{
// Add a trailing \ to m_sPathPrefix if it is missing.
// Do not add one to "C:" since "C:" and "C:\" are different.
{
auto len = m_sPathPrefix.size();
if (len != 0)
{
wchar_t ch = m_sPathPrefix[len - 1];
if (ch != '\\' && (ch != ':' || len != 2))
{
m_sPathPrefix += '\\';
}
}
}
m_hFindFile = ::FindFirstFileEx(std::wstring(m_sPathPrefix + sPattern).c_str(), infoLevel, &m_findFileData, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH);
m_bFile = FALSE;
}
else
{
m_hFindFile = ::FindFirstFile(m_sPathPrefix.c_str(), &m_findFileData);
m_bFile = TRUE;
}
if (m_hFindFile == INVALID_HANDLE_VALUE)
{
m_dError = ::GetLastError();
}
}
CSimpleFileFind::~CSimpleFileFind()
{
if (m_hFindFile != INVALID_HANDLE_VALUE)
{
::FindClose(m_hFindFile);
}
}
bool CSimpleFileFind::FindNextFile()
{
if (m_dError)
{
return false;
}
if (m_bFirst)
{
m_bFirst = false;
return (m_hFindFile != INVALID_HANDLE_VALUE);
}
if (!::FindNextFile(m_hFindFile, &m_findFileData))
{
m_dError = ::GetLastError();
return false;
}
return true;
}
bool CSimpleFileFind::FindNextFileNoDots(DWORD attrToIgnore)
{
bool result;
do
{
result = FindNextFile();
} while (result && (IsDots() || ((GetAttributes() & attrToIgnore) != 0)));
return result;
}
bool CSimpleFileFind::FindNextFileNoDirectories()
{
bool result;
do
{
result = FindNextFile();
} while (result && IsDirectory());
return result;
}
/*
* Implementation notes:
*
* This is a depth-first traversal. Directories are visited before
* their contents.
*
* We keep a stack of directories. The deepest directory is at the top
* of the stack, the originally-requested directory is at the bottom.
* If we come across a directory, we first return it to the user, then
* recurse into it. The finder at the bottom of the stack always points
* to the file or directory last returned to the user (except immediately
* after creation, when the finder points to the first valid thing we need
* to return, but we haven't actually returned anything yet - hence the
* m_bIsNew variable).
*
* Errors reading a directory are assumed to be end-of-directory, and
* are otherwise ignored.
*
* The "." and ".." psedo-directories are ignored for obvious reasons.
*/
CDirFileEnum::CDirStackEntry::CDirStackEntry(CDirStackEntry* seNext, const std::wstring& sDirName)
: CSimpleFileFind(sDirName)
, m_seNext(seNext)
{
}
CDirFileEnum::CDirStackEntry::~CDirStackEntry()
{
}
inline void CDirFileEnum::PopStack()
{
CDirStackEntry* seToDelete = m_seStack;
m_seStack = seToDelete->m_seNext;
delete seToDelete;
}
inline void CDirFileEnum::PushStack(const std::wstring& sDirName)
{
m_seStack = new CDirStackEntry(m_seStack, sDirName);
}
CDirFileEnum::CDirFileEnum(const std::wstring& sDirName)
: m_seStack(nullptr)
, m_bIsNew(true)
, m_attrToIgnore(0)
{
PushStack(sDirName);
}
CDirFileEnum::~CDirFileEnum()
{
while (m_seStack != nullptr)
{
PopStack();
}
}
bool CDirFileEnum::NextFile(std::wstring& sResult, bool* pbIsDirectory, bool recurse)
{
if (m_bIsNew)
{
// Special-case first time - haven't found anything yet,
// so don't do recurse-into-directory check.
m_bIsNew = false;
}
else if (m_seStack && m_seStack->IsDirectory() && recurse && ((m_seStack->GetAttributes() & m_attrToIgnore) == 0))
{
PushStack(m_seStack->GetFilePath());
}
while (m_seStack && !m_seStack->FindNextFileNoDots(m_attrToIgnore))
{
// No more files in this directory, try parent.
PopStack();
}
if (m_seStack)
{
sResult = m_seStack->GetFilePath();
if (pbIsDirectory != nullptr)
{
*pbIsDirectory = m_seStack->IsDirectory();
}
return true;
}
else
{
return false;
}
}