-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
187 lines (142 loc) · 3.85 KB
/
tree.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include "tree.h"
#include "utils.h"
#include "worker.h"
int current = 0;
int total = -1;
void Tree::addChild(Tree *child) {
this->children.push_back(child);
}
void Tree::addParent(Tree *parent) {
this->parents.push_back(parent);
}
void Tree::display(std::ostream &out, int indent) const {
out << std::string(indent * 4, ' ') << "|- " << this->name << std::endl;
std::vector<Tree *>::const_iterator first = this->children.cbegin();
std::vector<Tree *>::const_iterator last = this->children.cend();
while (first != last) {
(*first)->display(out, indent + 1);
++first;
}
}
void Tree::process() const {
if (total == -1) {
total = this->size();
}
std::vector<Tree *>::const_iterator first = this->children.cbegin();
std::vector<Tree *>::const_iterator last = this->children.cend();
//std::cout << "\033[32;0m" << "[" << "%] Building " << this->name << "\033[0m" << std::endl;
//std::cout << "\033[36;0m" << "Dependencies" << "\033[0m" << std::endl;
while (first < last) {
(*first)->process();
++first;
}
current++;
std::cout << "[" << std::setw(3) << std::right << (current * 100) / total << std::left << "%]" << "\033[32;2m" << " Building " << this->name << "\033[0m" << std::endl;
//std::cout << "\033[36;0m" << "Execution" << "\033[0m" << std::endl;
this->execute();
if (current == total) {
current = 0;
total = -1;
}
}
void Tree::execute() const {
std::string s = ssystem(this->cmd);
std::istringstream iss(s);
std::string line;
while (std::getline(iss, line)) {
line = trim(line);
if (line.length() > 0) {
//std::cout << "\033[22;104m\033[97m" << " OUT " << "\033[0m" << " " << line << std::endl;
}
}
/*FILE* pipe = popen(this->cmd.c_str(), "r");
if (!pipe) {
std::cerr << "Can't execute the command" << std::endl;
}*/
/*char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;*/
//system(this->cmd.c_str());
}
int Tree::size() const {
int size = 0;
std::vector<Tree *>::const_iterator first = this->children.cbegin();
std::vector<Tree *>::const_iterator last = this->children.cend();
while (first < last) {
size += (*first)->size();
++first;
}
size++;
return size;
}
std::ostream &operator<<(std::ostream &out, const Tree *t) {
t->display(out, 0);
return out;
}
std::set<Tree *> Tree::getLeafs() {
std::set<Tree *> leafs;
std::vector<Tree *>::const_iterator first = this->children.cbegin();
std::vector<Tree *>::const_iterator last = this->children.cend();
if (first == last) {
leafs.insert(this);
} else {
while (first < last) {
std::set<Tree *> child_leafs = (*first)->getLeafs();
leafs.insert(child_leafs.begin(), child_leafs.end());
++first;
}
}
return leafs;
}
// <name>|<dependencies>|<cmd>
std::string Tree::serialize(int avancement) const {
std::stringstream ss;
ss << this->name << "\035";
std::vector<Tree *>::const_iterator first = this->children.cbegin();
std::vector<Tree *>::const_iterator last = this->children.cend();
while (first < last) {
debug("Serialization : " + (*first)->name);
ss << (*first)->name;
++first;
if (first != last) {
ss << ";";
}
}
ss << "\035";
ss << this->cmd;
ss << "\035";
ss << avancement;
return ss.str();
}
#if 0
void deserialize(std::string s) {
std::istringstream iss(s);
std::string temp;
std::string name;
std::vector<std::string> dependencies;
std::string cmds;
// name
std::getline(iss, name, '|');
// dependencies
std::getline(iss, temp, '|');
std::istringstream iss_temp(s);
while (std::getline(iss_temp, temp, ';')) {
dependencies.push_back(temp);
}
// cmds
std::getline(iss, cmds, '|');
std::cout << name << std::endl;
std::cout << dependencies << std::endl;
std::cout << cmds << std::endl;
}
#endif