blob: 3b0d320bdf4d56715d5da1f02fd62e758d0eef7a (
plain) (
blame)
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
|
/*
* FolderScanner.cpp
* -----------------
* Purpose: Class for finding files in folders.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "FolderScanner.h"
#include <tchar.h>
OPENMPT_NAMESPACE_BEGIN
FolderScanner::FolderScanner(const mpt::PathString &path, FlagSet<ScanType> type, mpt::PathString filter)
: m_paths(1, path)
, m_filter(std::move(filter))
, m_hFind(INVALID_HANDLE_VALUE)
, m_type(type)
{
MemsetZero(m_wfd);
}
FolderScanner::~FolderScanner()
{
FindClose(m_hFind);
}
bool FolderScanner::Next(mpt::PathString &file)
{
bool found = false;
do
{
if(m_hFind == INVALID_HANDLE_VALUE)
{
if(m_paths.empty())
{
return false;
}
m_currentPath = m_paths.back();
m_paths.pop_back();
m_currentPath.EnsureTrailingSlash();
m_hFind = FindFirstFile((m_currentPath + m_filter).AsNative().c_str(), &m_wfd);
}
BOOL nextFile = FALSE;
if(m_hFind != INVALID_HANDLE_VALUE)
{
do
{
file = m_currentPath + mpt::PathString::FromNative(m_wfd.cFileName);
if(m_wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(_tcscmp(m_wfd.cFileName, _T("..")) && _tcscmp(m_wfd.cFileName, _T(".")))
{
if(m_type[kFindInSubDirectories])
{
// Add sub directory
m_paths.push_back(file);
}
if(m_type[kOnlyDirectories])
{
found = true;
}
}
} else if(m_type[kOnlyFiles])
{
found = true;
}
} while((nextFile = FindNextFile(m_hFind, &m_wfd)) != FALSE && !found);
}
if(nextFile == FALSE)
{
// Done with this directory, advance to next
if(m_hFind != INVALID_HANDLE_VALUE)
{
FindClose(m_hFind);
}
m_hFind = INVALID_HANDLE_VALUE;
}
} while(!found);
return true;
}
OPENMPT_NAMESPACE_END
|