Merge branch 'restructure-tree'
[TortoiseGit.git] / src / Utils / DirFileEnum.cpp
blob79d1a43c221c7bac8eac6268afdf21610e5aeaaa
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2005 - 2006 - Jon Foster
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #include "StdAfx.h"
19 #include "DirFileEnum.h"
22 CSimpleFileFind::CSimpleFileFind(const CString &sPath, LPCTSTR pPattern) :
23 m_dError(ERROR_SUCCESS),
24 m_bFirst(TRUE),
25 m_sPathPrefix(sPath)
27 // Add a trailing \ to m_sPathPrefix if it is missing.
28 // Do not add one to "C:" since "C:" and "C:\" are different.
30 int len = m_sPathPrefix.GetLength();
31 if (len != 0) {
32 TCHAR ch = sPath[len-1];
33 if (ch != '\\' && (ch != ':' || len != 2)) {
34 m_sPathPrefix += "\\";
39 m_hFindFile = ::FindFirstFile((LPCTSTR)(m_sPathPrefix + pPattern), &m_FindFileData);
40 if (m_hFindFile == INVALID_HANDLE_VALUE) {
41 m_dError = ::GetLastError();
45 CSimpleFileFind::~CSimpleFileFind()
47 if (m_hFindFile != INVALID_HANDLE_VALUE) {
48 ::FindClose(m_hFindFile);
52 BOOL CSimpleFileFind::FindNextFile()
54 if (m_dError) {
55 return FALSE;
58 if (m_bFirst) {
59 m_bFirst = FALSE;
60 return TRUE;
63 if (!::FindNextFile(m_hFindFile, &m_FindFileData)) {
64 m_dError = ::GetLastError();
65 return FALSE;
68 return TRUE;
71 BOOL CSimpleFileFind::FindNextFileNoDots()
73 BOOL result;
74 do {
75 result = FindNextFile();
76 } while (result && IsDots());
78 return result;
81 BOOL CSimpleFileFind::FindNextFileNoDirectories()
83 BOOL result;
84 do {
85 result = FindNextFile();
86 } while (result && IsDirectory());
88 return result;
93 * Implementation notes:
95 * This is a depth-first traversal. Directories are visited before
96 * their contents.
98 * We keep a stack of directories. The deepest directory is at the top
99 * of the stack, the originally-requested directory is at the bottom.
100 * If we come across a directory, we first return it to the user, then
101 * recurse into it. The finder at the bottom of the stack always points
102 * to the file or directory last returned to the user (except immediately
103 * after creation, when the finder points to the first valid thing we need
104 * to return, but we haven't actually returned anything yet - hence the
105 * m_bIsNew variable).
107 * Errors reading a directory are assumed to be end-of-directory, and
108 * are otherwise ignored.
110 * The "." and ".." psedo-directories are ignored for obvious reasons.
114 CDirFileEnum::CDirStackEntry::CDirStackEntry(CDirStackEntry * seNext,
115 const CString& sDirName)
116 : CSimpleFileFind(sDirName),
117 m_seNext(seNext)
121 CDirFileEnum::CDirStackEntry::~CDirStackEntry()
125 inline void CDirFileEnum::PopStack()
127 CDirStackEntry * seToDelete = m_seStack;
128 m_seStack = seToDelete->m_seNext;
129 delete seToDelete;
132 inline void CDirFileEnum::PushStack(const CString& sDirName)
134 m_seStack = new CDirStackEntry(m_seStack,sDirName);
137 CDirFileEnum::CDirFileEnum(const CString& sDirName) :
138 m_seStack(NULL),
139 m_bIsNew(TRUE)
141 PushStack(sDirName);
144 CDirFileEnum::~CDirFileEnum()
146 while (m_seStack != NULL) {
147 PopStack();
151 BOOL CDirFileEnum::NextFile(CString &sResult, bool* pbIsDirectory)
153 if (m_bIsNew) {
154 // Special-case first time - haven't found anything yet,
155 // so don't do recurse-into-directory check.
156 m_bIsNew = FALSE;
157 } else if (m_seStack && m_seStack->IsDirectory()) {
158 PushStack(m_seStack->GetFilePath());
161 while (m_seStack && !m_seStack->FindNextFileNoDots()) {
162 // No more files in this directory, try parent.
163 PopStack();
166 if (m_seStack)
168 sResult = m_seStack->GetFilePath();
169 if(pbIsDirectory != NULL)
171 *pbIsDirectory = m_seStack->IsDirectory();
173 return TRUE;
174 } else {
175 return FALSE;