forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_finder.cpp
166 lines (152 loc) · 6.1 KB
/
file_finder.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "file_finder.h"
#include <cstring> // for strcmp
#include <stack> // for stack (obviously)
#include <algorithm>
// FILE I/O
#include <sys/stat.h>
#ifdef _MSC_VER
#include "wdirent.h"
#include <direct.h>
#else
#include <dirent.h>
#endif
/**
Searches the supplied root directory for the extension provided. Can recursively search downward through the directory tree
to get all files. It will then return a vector of file paths that fulfill the search requirements.
USAGE:
extension should be preceeded by the '.' character to denote it as an extension, if it is not then it will return all files
and folder names that contain the extension string.
root_path can point to any folder relative to the execution directory.
Searching from the execution directory -- Supply "." as the root_path
Searching from the parent directory of the execution directory -- Supply ".." as the root_path
Searching child directories within the execution directory -- Supply "<directory path>" or "./<directory path>", both
will provide correct results.
recursive_search will:
if true, continue searching all child directories from the supplied root_path
if false, search only the supplied root_path, and stop when the directory contents have been worked through.
*/
std::vector<std::string> file_finder::get_files_from_path(std::string extension, std::string root_path, bool recursive_search)
{
std::vector<std::string> files;
// test for empty root path
if (root_path.empty())
{
root_path = ".";
}
std::stack<std::string> directories, tempstack;
directories.push(root_path);
std::string path = "";
while (!directories.empty())
{
path = directories.top();
directories.pop();
DIR *root = opendir(path.c_str());
if (root)
{
struct dirent *root_file;
struct stat _buff;
DIR *subdir;
while ((root_file = readdir(root)))
{
// check to see if it is a folder!
if (stat(root_file->d_name, &_buff) != 0x4)
{
// ignore '.' and '..' folder names, which are current and parent folder relative paths
if ((strcmp(root_file->d_name, ".") != 0) && (strcmp(root_file->d_name, "..") != 0))
{
std::string subpath = path + "/" + root_file->d_name;
if (recursive_search)
{
subdir = opendir(subpath.c_str());
if (subdir)
{
tempstack.push(subpath);
closedir(subdir);
}
}
}
}
// check to see if it is a file with the appropriate extension
std::string tmp = root_file->d_name;
if (tmp.find(extension.c_str()) != std::string::npos)
{
// file with extension found! add to files list with full path
std::string fullpath = path + "/" + tmp;
files.push_back(fullpath);
}
}
}
closedir(root);
// Directories are added to tempstack in A->Z order, which makes them pop from Z->A. This Makes sure that directories are
// searched in the proper order and that the final output is in the proper order.
while (!tempstack.empty()){
directories.push(tempstack.top());
tempstack.pop();
}
}
return files;
}
std::vector<std::string> file_finder::get_directories_with(std::vector<std::string> extensions, std::string root_path, bool recursive_search)
{
std::vector<std::string> found;
const int num_extensions = extensions.size();
// test for empty root path
if (root_path.empty())
{
root_path = ".";
}
std::stack<std::string> directories, tempstack;
directories.push(root_path);
std::string path = "";
while (!directories.empty())
{
path = directories.top();
directories.pop();
DIR *root = opendir(path.c_str());
if (root)
{
struct dirent *root_file;
struct stat _buff;
DIR *subdir;
bool foundit = false;
while ((root_file = readdir(root)))
{
// check to see if it is a folder!
if (stat(root_file->d_name, &_buff) != 0x4)
{
// ignore '.' and '..' folder names, which are current and parent folder relative paths
if ((strcmp(root_file->d_name, ".") != 0) && (strcmp(root_file->d_name, "..") != 0))
{
std::string subpath = path + "/" + root_file->d_name;
if (recursive_search)
{
subdir = opendir(subpath.c_str());
if (subdir)
{
tempstack.push(subpath);
closedir(subdir);
}
}
}
}
// check to see if it is a file with the appropriate extension
std::string tmp = root_file->d_name;
for(int i=0;i < num_extensions && foundit == false; i++) {
if (tmp.find(extensions[i].c_str()) != std::string::npos)
{
found.push_back(path);
foundit = true;
}
}
}
closedir(root);
}
// Directories are added to tempstack in A->Z order, which makes them pop from Z->A. This Makes sure that directories are
// searched in the proper order and that the final output is in the proper order.
while (!tempstack.empty()){
directories.push(tempstack.top());
tempstack.pop();
}
}
return found;
}