-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileFinder.cpp
143 lines (123 loc) · 3.27 KB
/
FileFinder.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
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: FILEFINDER.CPP
** COMPONENT: Windows C++ Library.
** DESCRIPTION: CFileFinder class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "FileFinder.hpp"
/******************************************************************************
** Method: Constructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CFileFinder::CFileFinder()
{
}
/******************************************************************************
** Method: Destructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CFileFinder::~CFileFinder()
{
}
/******************************************************************************
** Method: Find.
**
** Description: Find all files in the path matching the mask.
**
** Parameters: pszPath The root path to search.
** pszMask The file mask.
** bRecurse Recurse into subdirectories flag.
** oTree The file tree to fill in.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CFileFinder::Find(const tchar* pszPath, const tchar* pszMask, bool bRecurse, CFileTree& oTree)
{
ASSERT(pszPath != NULL);
ASSERT(pszMask != NULL);
// Clear the existing tree.
oTree.Clear();
// Allocate the root node.
CFileTreeNode* pRoot = new CFileTreeNode();
// Initialise the node and tree.
pRoot->m_oData.m_strPath = pszPath;
oTree.Root(pRoot);
// Enumerate start directory.
Find(*pRoot, pszMask, bRecurse);
}
/******************************************************************************
** Method: Find.
**
** Description: Find all files in the path of the given directory node.
**
** Parameters: oNode The directory node to search.
** pszMask The file mask.
** bRecurse Recurse into subdirectories flag.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CFileFinder::Find(CFileTreeNode& oNode, const tchar* pszMask, bool bRecurse)
{
HANDLE hFind;
WIN32_FIND_DATA oInfo;
CPath strFind = oNode.m_oData.m_strPath / pszMask;
// Find the files...
hFind = ::FindFirstFile(strFind, &oInfo);
// At least 1 file found?
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
CString strFileName = oInfo.cFileName;
// Is a directory?
if (oInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strFileName != TXT(".")) && (strFileName != TXT("..")))
oNode.m_oData.m_astrDirs.Add(strFileName);
}
// Is a file.
else
{
oNode.m_oData.m_astrFiles.Add(strFileName);
}
}
while (::FindNextFile(hFind, &oInfo));
// Cleanup.
::FindClose(hFind);
}
// Recurse into subdirectories?
if (bRecurse)
{
for (size_t i = 0; i < oNode.m_oData.m_astrDirs.Size(); ++i)
{
CFileTreeNode* pNode = new CFileTreeNode();
// Add new node to the tree.
pNode->m_oData.m_strPath = oNode.m_oData.m_strPath / oNode.m_oData.m_astrDirs[i];
oNode.AddNode(pNode);
// Enumerate sub-folder.
Find(*pNode, pszMask, true);
}
}
}