forked from qianxliu/search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
41 lines (34 loc) · 807 Bytes
/
main.cpp
File metadata and controls
41 lines (34 loc) · 807 Bytes
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
#include <bits/stdc++.h>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
bool match(string &&p1, string &p2)
{
return p1.find(p2) != string::npos;
}
// c plus plus has a recursive directory iterator, but it is not elegant and useless.
void find(string &&p, string &pattern)
{
for (auto &entry : fs::directory_iterator(p, fs::directory_options::skip_permission_denied))
{
if (!fs::is_directory(entry.path()))
{
if (match(entry.path().string(), pattern))
{
cout << entry << '\n';
}
}
else
find(entry.path().string(), pattern);
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
cout << "args: path[str] and a file match[str]\n";
return 0;
}
string pattern = argv[2];
find(argv[1], pattern);
}