Debian Codegraph @

CodeLite/clFilesCollector.cpp

raw
1 #include "clFilesCollector.h"
2
3 #include "file_logger.h"
4 #include "fileutils.h"
5
6 #include <queue>
7 #include <vector>
8 #include <wx/dir.h>
9 #include <wx/filename.h>
10 #include <wx/tokenzr.h>
11
12 clFilesScanner::clFilesScanner() {}
13
14 clFilesScanner::~clFilesScanner() {}
15
16 size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxFileName>& filesOutput, const wxString& filespec,
17 const wxString& excludeFilespec, const wxString& excludeFoldersSpec)
18
19 {
20 filesOutput.clear();
21 auto cb = [&filesOutput](const wxString& fullpath) -> bool {
22 filesOutput.push_back(fullpath);
23 return true;
24 };
25 return Scan(rootFolder, filespec, excludeFilespec, excludeFoldersSpec, std::move(cb));
26 }
27
28 size_t clFilesScanner::Scan(const wxString& rootFolder, wxArrayString& filesOutput, const wxString& filespec,
29 const wxString& excludeFilespec, const wxString& excludeFoldersSpec)
30
31 {
32 filesOutput.clear();
33 auto cb = [&filesOutput](const wxString& fullpath) -> bool {
34 filesOutput.push_back(fullpath);
35 return true;
36 };
37 return Scan(rootFolder, filespec, excludeFilespec, excludeFoldersSpec, std::move(cb));
38 }
39
40 namespace
41 {
42 bool IsRelPathContainedInSpec(const wxString& rootPath, const wxString& fullPath, const wxStringSet_t& specSet)
43 {
44 wxFileName fp(fullPath);
45 fp.MakeRelativeTo(rootPath);
46
47 wxArrayString fpDirs = fp.GetDirs();
48 fpDirs.Add(fp.GetFullName()); // Add the last (filename) part into the path array
49
50 const wxString pathSeparators = fp.GetPathSeparators();
51 for(const wxString& spec : specSet) {
52 // Check if spec matches the beginning of the full path
53 if(fp.GetFullPath().StartsWith(spec)) {
54 return true;
55 }
56 // First check if spec is a path-elem (without path separators)
57 // Then check if path-elem if found in the array of full path-elements
58 if(!spec.Contains(pathSeparators) && (fpDirs.Index(spec) != wxNOT_FOUND)) {
59 return true;
60 }
61 }
62 return false;
63 }
64 } // namespace
65
66 size_t clFilesScanner::Scan(const wxString& rootFolder, std::vector<wxString>& filesOutput, const wxString& filespec,
67 const wxString& excludeFilespec, const wxStringSet_t& excludeFolders)
68 {
69 filesOutput.clear();
70 if(!wxFileName::DirExists(rootFolder)) {
71 clDEBUG() << "clFilesScanner: No such dir:" << rootFolder << clEndl;
72 return 0;
73 }
74
75 wxArrayString specArr = ::wxStringTokenize(filespec.Lower(), ";,|", wxTOKEN_STRTOK);
76 wxArrayString excludeSpecArr = ::wxStringTokenize(excludeFilespec.Lower(), ";,|", wxTOKEN_STRTOK);
77 std::queue<wxString> Q;
78 std::unordered_set<wxString> Visited;
79 Q.push(rootFolder);
80 Visited.insert(rootFolder);
81
82 while(!Q.empty()) {
83 wxString dirpath = Q.front();
84 Q.pop();
85
86 wxDir dir(dirpath);
87 if(!dir.IsOpened()) {
88 continue;
89 }
90
91 wxString filename;
92 bool cont = dir.GetFirst(&filename);
93 while(cont) {
94 // Check to see if this is a folder
95 wxString fullpath;
96 fullpath << dir.GetNameWithSep() << filename;
97 bool isDirectory = wxFileName::DirExists(fullpath);
98 // Use FileUtils::RealPath() here to cope with symlinks on Linux
99 bool isExcludeDir =
100 isDirectory &&
101 (
102 #if defined(__FreeBSD__)
103 ((FileUtils::IsSymlink(fullpath) && excludeFolders.count(FileUtils::RealPath(fullpath)))
104 #else
105 (excludeFolders.count(FileUtils::RealPath(fullpath))
106 #endif
107 || IsRelPathContainedInSpec(rootFolder, fullpath, excludeFolders)));
108 if(isDirectory && !isExcludeDir) {
109 // Traverse into this folder
110 wxString realPath = FileUtils::RealPath(fullpath);
111 if(Visited.insert(realPath).second) {
112 Q.push(fullpath);
113 }
114
115 } else if(!isDirectory && FileUtils::WildMatch(excludeSpecArr, filename)) {
116 // Do nothing
117 } else if(!isDirectory && FileUtils::WildMatch(specArr, filename)) {
118 // Include this file
119 filesOutput.push_back(fullpath);
120 }
121 cont = dir.GetNext(&filename);
122 }
123 }
124 return filesOutput.size();
125 }
126
127 size_t clFilesScanner::Scan(const wxString& rootFolder, const wxString& filespec, const wxString& excludeFilespec,
128 const wxString& excludeFoldersSpec, std::function<bool(const wxString&)>&& collect_cb)
129 {
130 if(!wxFileName::DirExists(rootFolder)) {
131 clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl;
132 return 0;
133 }
134
135 wxArrayString specArr = ::wxStringTokenize(filespec.Lower(), ";,|", wxTOKEN_STRTOK);
136 wxArrayString excludeSpecArr = ::wxStringTokenize(excludeFilespec.Lower(), ";,|", wxTOKEN_STRTOK);
137 wxArrayString excludeFoldersSpecArr = ::wxStringTokenize(excludeFoldersSpec.Lower(), ";,|", wxTOKEN_STRTOK);
138 std::queue<wxString> Q;
139 std::unordered_set<wxString> Visited;
140
141 Q.push(FileUtils::RealPath(rootFolder));
142 Visited.insert(FileUtils::RealPath(rootFolder));
143
144 size_t nCount = 0;
145 while(!Q.empty()) {
146 wxString dirpath = Q.front();
147 Q.pop();
148
149 wxDir dir(dirpath);
150 if(!dir.IsOpened()) {
151 continue;
152 }
153
154 wxString filename;
155 bool cont = dir.GetFirst(&filename);
156 while(cont) {
157 // Check to see if this is a folder
158 wxString fullpath;
159 fullpath << dir.GetNameWithSep() << filename;
160 bool isDirectory = wxFileName::DirExists(fullpath);
161 bool isFile = !isDirectory;
162 // Use FileUtils::RealPath() here to cope with symlinks on Linux
163 if(isDirectory /* a folder */ &&
164 !FileUtils::WildMatch(excludeFoldersSpecArr, filename) /* does not match the exclude folder spec */) {
165 // Traverse into this folder
166 wxString real_path = FileUtils::RealPath(fullpath);
167 if(Visited.count(real_path) == 0) {
168 Visited.insert(real_path);
169 Q.push(fullpath);
170 }
171 } else if(isFile && /* a file */
172 !FileUtils::WildMatch(excludeSpecArr, filename) /* does not match the exclude file spec */ &&
173 FileUtils::WildMatch(specArr, filename) /* matches the file spec array */) {
174 // Include this file
175 if(!collect_cb(fullpath)) {
176 // requested to stop
177 return nCount;
178 } else {
179 ++nCount;
180 }
181 }
182 cont = dir.GetNext(&filename);
183 }
184 }
185 return nCount;
186 }
187
188 size_t clFilesScanner::ScanNoRecurse(const wxString& rootFolder, clFilesScanner::EntryData::Vec_t& results,
189 const wxString& matchSpec)
190 {
191 results.clear();
192 if(!wxFileName::DirExists(rootFolder)) {
193 clDEBUG() << "clFilesScanner::ScanNoRecurse(): No such dir:" << rootFolder << clEndl;
194 return 0;
195 }
196 wxArrayString specArr = ::wxStringTokenize(matchSpec.Lower(), ";,|", wxTOKEN_STRTOK);
197 wxDir dir(rootFolder);
198 if(!dir.IsOpened()) {
199 clDEBUG() << "Failed to open root dir:" << rootFolder;
200 return 0;
201 }
202 wxString dirWithSep = dir.GetNameWithSep();
203
204 wxString filename;
205 bool cont = dir.GetFirst(&filename);
206 while(cont) {
207 if(FileUtils::WildMatch(specArr, filename)) {
208 wxString fullpath;
209 fullpath << dirWithSep << filename;
210 EntryData ed;
211 if(FileUtils::IsDirectory(fullpath)) {
212 ed.flags |= kIsFolder;
213 } else {
214 ed.flags |= kIsFile;
215 }
216 if(FileUtils::IsSymlink(fullpath)) {
217 ed.flags |= kIsSymlink;
218 }
219 if(FileUtils::IsHidden(fullpath)) {
220 ed.flags |= kIsHidden;
221 }
222 ed.fullpath = fullpath;
223 results.push_back(ed);
224 }
225 cont = dir.GetNext(&filename);
226 }
227 return results.size();
228 }
229
230 #ifdef __WXMSW__
231 #define DIR_SEPARATOR "\\"
232 #else
233 #define DIR_SEPARATOR "/"
234 #endif
235
236 void clFilesScanner::ScanWithCallbacks(const wxString& rootFolder, std::function<bool(const wxString&)>&& on_folder_cb,
237 std::function<void(const wxArrayString&)>&& on_file_cb, size_t search_flags)
238 {
239 if(!wxFileName::DirExists(rootFolder)) {
240 clDEBUG() << "clFilesScanner: No such directory:" << rootFolder << clEndl;
241 return;
242 }
243
244 std::vector<wxString> Q;
245 std::unordered_set<wxString> Visited;
246
247 Q.push_back(FileUtils::RealPath(rootFolder));
248 Visited.insert(FileUtils::RealPath(rootFolder));
249
250 while(!Q.empty()) {
251 wxString dirpath = Q.front();
252 Q.erase(Q.begin());
253
254 wxDir dir(dirpath);
255 if(!dir.IsOpened()) {
256 continue;
257 }
258
259 // container for the files found
260 wxArrayString files;
261 files.reserve(1000);
262
263 wxString filename;
264 bool cont = dir.GetFirst(&filename);
265 while(cont) {
266 // Check to see if this is a folder
267 wxString fullpath;
268 fullpath << dirpath << DIR_SEPARATOR << filename;
269
270 bool isDirectory = wxFileName::DirExists(fullpath);
271 bool isFile = !isDirectory;
272 if(isDirectory) {
273 // A hidden folder?
274 if((search_flags & SF_EXCLUDE_HIDDEN_DIRS) && FileUtils::IsHidden(fullpath)) {
275 cont = dir.GetNext(&filename);
276 continue;
277 }
278
279 // A symlink?
280 if((search_flags & SF_DONT_FOLLOW_SYMLINKS) && FileUtils::IsSymlink(fullpath)) {
281 cont = dir.GetNext(&filename);
282 continue;
283 }
284
285 if(on_folder_cb(fullpath)) {
286 // Traverse into this folder
287 wxString real_path = FileUtils::RealPath(fullpath);
288 if(Visited.insert(real_path).second) {
289 Q.push_back(fullpath);
290 }
291 }
292 } else if(isFile) {
293 files.Add(fullpath);
294 }
295 cont = dir.GetNext(&filename);
296 }
297
298 // notify about this batch of files
299 on_file_cb(files);
300 }
301 }