-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfiletree.cpp
More file actions
109 lines (95 loc) · 3.05 KB
/
filetree.cpp
File metadata and controls
109 lines (95 loc) · 3.05 KB
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
#include <iostream>
#include <string>
#include <cmath>
#include "filetree.h"
#include "util.h"
FileTree::FileTree(){
_root = new TreeNode("/", false);
}
bool FileTree::insert_node(const std::string &path, const bool isFile){
TreeNode * parent = nullptr;
bool isFound = find_node(path, &parent);
if(isFound) return false;
std::vector<std::string> path_folders = split(path, '/');
TreeNode* match = _root;
TreeNode* cur = _root->firstSon;
for (size_t i = 0; i < path_folders.size(); ++i) {
// Try to get the node
while (cur && cur->value_ != path_folders[i]) {
cur = cur->nextSibling;
}
// Found the node, try next layer
if (cur) {
match = cur;
cur = cur->firstSon;
continue;
}
// If the last layer, add node directly
if (i + 1 == path_folders.size()) {
// create this node of file
auto node = new TreeNode(path_folders[i], true);
node->parent = match;
if (!match->firstSon) {
match->firstSon = node;
} else {
auto sibling = match->firstSon;
while (sibling->nextSibling) {
sibling = sibling->nextSibling;
}
sibling->nextSibling = node;
}
return true;
}
// If not the last layer, add missing dir node
auto dir = new TreeNode(path_folders[i], false);
dir->parent = match;
if (!match->firstSon) {
match->firstSon = dir;
} else {
auto sibling = match->firstSon;
while (sibling->nextSibling) {
sibling = sibling->nextSibling;
}
sibling->nextSibling = dir;
}
match = dir;
cur = match->firstSon;
}
return true;
}
bool FileTree::find_node(const std::string &path, TreeNode **last_node)const{
std::vector<std::string> path_folders = split(path, '/');
TreeNode *node = _root->firstSon;
*last_node = _root;
for(const auto &name: path_folders){
while(node && node->value_ != name)
node = node->nextSibling;
if(!node)
return false;
*last_node = node;
node = node->firstSon;
}
return (*last_node)->isFile;
}
void FileTree::list(TreeNode *node, std::map<std::string, std::pair<int, int>>& meta){
static int chunkSize = 2 * 1024 * 1024;
if(node){
if (node->isFile) {
auto full_path_of_file = list_node_path(node);
std::cout << full_path_of_file << "\t" << meta[full_path_of_file].first << "\t" << (int)ceil(1.0 * meta[full_path_of_file].second/chunkSize) << std::endl;
}
// std::cout << node->value_ << "\t" << std::endl;
list(node->firstSon, meta);
list(node->nextSibling, meta);
}
}
void FileTree::list(std::map<std::string, std::pair<int, int>>& meta){
list(_root, meta);
}
const std::string FileTree::list_node_path(TreeNode* node) {
while (node->parent) {
return list_node_path(node->parent) + "/" + node->value_;
}
// Don't return the root value, which is '/'
return (node->value_ != "/") ? node->value_ : "";
}