-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTree.cpp
66 lines (60 loc) · 1.67 KB
/
FileTree.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
#include "FileTree.hpp"
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include <vector>
void Directory::insert(std::string &&path)
{
auto idx = path.find('/');
if (idx == std::string::npos)
children.push_back(std::make_shared<File>(path));
else if (idx == path.length() - 1)
children.push_back(std::make_shared<Directory>(path.substr(0, path.length() - 1)));
else {
std::string p = path.substr(0, idx);
for (const auto& n : children) {
if (n->is_directory() && n->name == p) {
static_cast<Directory&>(*n).insert(path.substr(idx + 1));
return;
}
}
auto d = std::make_shared<Directory>(p);
d->insert(path.substr(idx + 1));
children.push_back(d);
}
}
std::set<std::string> Directory::get()
{
std::set<std::string> result;
result.insert(name + "/");
for (auto &n : children) {
if (n->is_directory())
for (const auto &o : static_cast<Directory&>(*n).get())
result.insert(name + "/" + o);
else
result.insert(name + "/" + n->name);
}
return result;
}
std::set<std::string> Directory::get(const std::string &subdir, bool recursive)
{
std::set<std::string> result;
if (subdir.empty()) {
for (auto &n : children) {
if (recursive && n->is_directory())
for (const auto &o : static_cast<Directory&>(*n).get())
result.insert(o);
else if (!n->is_directory())
result.insert(n->name);
}
return result;
}
std::string child = subdir.substr(0, subdir.find('/')),
sub = child.length() + 1 < subdir.length() ? subdir.substr(child.length() + 1) : "";
for (auto &n : children) {
if (n->is_directory() && n->name == child)
return static_cast<Directory&>(*n).get(sub, recursive);
}
return std::set<std::string>{};
}