|
10 | 10 |
|
11 | 11 | #include <dest/util/glob.h> |
12 | 12 | #include <tinydir/tinydir.h> |
| 13 | +#include <stack> |
13 | 14 |
|
14 | 15 | namespace dest { |
15 | 16 | namespace util { |
16 | 17 |
|
17 | | - std::vector<std::string> findFilesInDir(const std::string &directory, const std::string &extension, bool stripExtension) |
| 18 | + std::vector<std::string> findFilesInDir(const std::string &directory, const std::string &extension, bool stripExtension, bool recursive) |
18 | 19 | { |
19 | 20 | std::vector<std::string> files; |
20 | 21 |
|
| 22 | + std::stack<std::string> dirsLeft; |
| 23 | + dirsLeft.push(directory); |
| 24 | + |
21 | 25 | tinydir_dir dir; |
22 | | - if (tinydir_open(&dir, directory.c_str()) != 0) |
23 | | - return files; |
24 | | - |
25 | | - while (dir.has_next) { |
26 | | - tinydir_file file; |
27 | | - |
28 | | - if (tinydir_readfile(&dir, &file) != 0 || file.is_dir) { |
29 | | - tinydir_next(&dir); |
30 | | - continue; |
31 | | - } |
32 | | - |
33 | | - if (extension != file.extension) { |
34 | | - tinydir_next(&dir); |
| 26 | + while (!dirsLeft.empty()) { |
| 27 | + std::string dirPath = dirsLeft.top(); dirsLeft.pop(); |
| 28 | + |
| 29 | + // Try to open directory |
| 30 | + if (tinydir_open(&dir, dirPath.c_str()) != 0) |
35 | 31 | continue; |
36 | 32 |
|
| 33 | + while (dir.has_next) { |
| 34 | + tinydir_file file; |
| 35 | + |
| 36 | + if (tinydir_readfile(&dir, &file) != 0) { |
| 37 | + tinydir_next(&dir); |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if (file.is_dir) { |
| 42 | + if (recursive && file.name != std::string(".") && file.name != std::string("..")) { |
| 43 | + dirsLeft.push(file.path); |
| 44 | + } |
| 45 | + tinydir_next(&dir); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + if (extension != file.extension) { |
| 50 | + tinydir_next(&dir); |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + std::string path(file.path); |
| 55 | + |
| 56 | + if (stripExtension) { |
| 57 | + size_t lastindex = path.find_last_of("."); |
| 58 | + std::string raw = path.substr(0, lastindex); |
| 59 | + files.push_back(raw); |
| 60 | + } |
| 61 | + else { |
| 62 | + files.push_back(path); |
| 63 | + } |
| 64 | + |
| 65 | + tinydir_next(&dir); |
37 | 66 | } |
38 | | - |
39 | | - std::string path(file.path); |
40 | | - |
41 | | - if (stripExtension) { |
42 | | - size_t lastindex = path.find_last_of("."); |
43 | | - std::string raw = path.substr(0, lastindex); |
44 | | - files.push_back(raw); |
45 | | - } else { |
46 | | - files.push_back(path); |
47 | | - } |
48 | | - |
49 | | - tinydir_next(&dir); |
| 67 | + |
| 68 | + tinydir_close(&dir); |
50 | 69 | } |
51 | 70 |
|
52 | | - tinydir_close(&dir); |
53 | 71 | return files; |
54 | 72 |
|
55 | 73 | } |
|
0 commit comments