-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
402 lines (326 loc) · 9.92 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "Tree.h"
#include <time.h>
#include <iostream>
#include <fstream>
Tree::Tree()
{
getcwd(this->cwd, sizeof(cwd));
std::string root = "ROOT";
std::string type = "dir";
this->root = new Node(this, NULL, root , type);
//current Dir root
this->current_Dir = this->root;
//size
this->numberOfNodes++;
//last node id root
this->lastNodeAdded = 0;
}
//destructor
Tree :: ~Tree()
{
delete this->root;
}
////SETTERS////
//set new value in root
void Tree::setRoot(Node* root)
{
this->root = root;
}
void Tree::setCurrentDir(Node* newDir)
{
this->current_Dir = newDir;
}
////SPECIAL METHODS////
bool Tree::isFatherFolder(Node* father)
{
if ((father->getType()).compare("fichero") == 0 || (father->getType()).compare("f") == 0 || (father->getType()).compare("file") == 0)
{
return 0;
}
return 1;
}
bool Tree::isAlreadyOnFather(Node* father, Node* son)
{
if (father->getNumberOfOffsprings() == 0) return 0;
std::vector<Node*>* offsprings = father->getOffsprings();
for (Node* node: *offsprings)
{
//if ((strcmp(node->getName(), son->getName())) == 0) return 1;
if (node->getName() == son->getName()) return 1;
}
return 0;
}
//Check if father is a folder: DONE
//Check if son's name is in father other sons: DONE
Node* Tree::addChild(Node* son, Node* father)
{
if (!isFatherFolder(father))
{
std::cout << "The father is not a folder." << std::endl;
return NULL;
}
//If father is a folder, we check if the son's name is already on it:
if (isAlreadyOnFather(father, son))
{
std::cout << "There is already something with the same name." << std::endl;
return NULL;
}
//If there are no repeated names, we add it:
(*son).setTree(this);
(*son).setFather(father);
(*son).setLevel(father->getLevel() + 1);
(*son).setDateLastModif(std::time(0));
(*father).setNewOffSpring(son);
(*father).setDateLastModif(std::time(0));
(*father).setNumberOffsprings(1);
this->numberOfNodes++;
this->lastNodeAdded = son->getId();
return son;
}
//
Node* Tree::findNode(Node* father, int id)
{
if (father->getNumberOfOffsprings() == 0) return NULL;
std::vector<Node*>* offsprings = father->getOffsprings();
for (Node* node: *offsprings)
{
if (node->getId() == id) return node;
}
return 0;
}
//update node information
void Tree :: updateChild(Node* node)
{
std::string option;
off_t bytes;
std::string name;
bool changed = false;
std::cout << "Do you want to modify the name? y/n" << std::endl;
std::cin >> option;
fflush(stdin);
if (option == "y")
{
std::cout << "Set new name:" << std::endl;
std::cin >> name;
fflush(stdin);
char charName[name.size() + 1];
name.copy(charName, name.size() + 1);
charName[name.size()] = '\0';
node->setName(charName);
changed = true;
}
// std::cout << "Do you want to modify the size? y/n" << std::endl;
// std::cin >> option;
// if (option == "y")
// {
// std::cout << "Set new size:" << std::endl;
// std::cin >> bytes;
// fflush(stdin);
// node->setByteSize(bytes);
// changed = true;
// }
if (changed) node->setDateLastModif(std::time(0));
}
//delete node from tree
void Tree::removeChild(Node* node)
{
if(node->getId() == 0)
{
std::cout << "You cant delete root." << std::endl;
}
else
{
if(node->getNumberOfOffsprings() != 0)
{
std::cout << "You cant delete this, it's not empty." << std::endl;
}
else
{
Node* father = node->getNodeFather();
for(int i = 0; i < father->getNumberOfOffsprings(); i++)
{
if (father->getOffsprings()->at(i)->getId() == node->getId())
{
father->getOffsprings()->erase(father->getOffsprings()->begin() + i);
break;
}
}
node->setFather(NULL);
father->setNumberOffsprings(-1);
this->numberOfNodes--;
//node->blockOccupied.clear();
}
}
}
Node* Tree::findNodeByName(std::string name)
{
if (this->getCurrentDir()->getNumberOfOffsprings() == 0) return NULL;
std::vector<Node*>* offsprings = this->getCurrentDir()->getOffsprings();
for (Node* node: *offsprings)
{
if (std::string(node->getName()) == name) return node;
}
return NULL;
}
/////SAVE TREE/////
void Tree::WriteBinaryFile()
{
std::ofstream fs;
std::string string_cwd = std::string(this->cwd);
string_cwd += "/tree.dat";
fs.open(string_cwd, std::ios::out | std::ios::binary | std::ios::trunc);
if (!fs.is_open()) std::cout << "Cannot open file." << std::endl;
else saveTree(fs);
fs.close();
}
void Tree::saveTreeRecursive(Node* nodeToSave, std::ofstream &of)
{
int id = nodeToSave->getId();
int idFather = nodeToSave->getNodeFather()->getId();
std::string name = nodeToSave->getName();
char nameChar[20];
strcpy(nameChar, name.c_str());
nameChar[19] = '\0';
char node_type_char[20];
std::string node_type = "";
if (nodeToSave->getIsDirectory())
{
node_type = "Folder";
strcpy(node_type_char, node_type.c_str());
}
else
{
node_type = "File";
strcpy(node_type_char, node_type.c_str());
}
off_t size = nodeToSave->getByteSize();
time_t lastModification = nodeToSave->getDateLastModif();
////New Adition////
std::vector<int>* blockOcuppied = nodeToSave->getBlockLocations();
typename std::vector<int>::size_type vectorSize = blockOcuppied->size();
////New Adition////
of.write((char*)&idFather, sizeof(idFather));
of.write((char*)&id, sizeof(id));
of.write((char*)&nameChar, sizeof(nameChar));
of.write((char*)&node_type_char, sizeof(node_type_char));
of.write((char*)&size, sizeof(size));
of.write((char*)&lastModification, sizeof(lastModification));
of.write((char*)&vectorSize, sizeof(vectorSize)); //tamaño del vector de structs
of.write((char*)blockOcuppied->data(), blockOcuppied->size() * sizeof(int));
std::vector<Node*>* elements = nodeToSave->getOffsprings();
for (int i = 0; i < nodeToSave->getNumberOfOffsprings(); i++)
{
Node* nodeToSave = elements->at(i);
saveTreeRecursive(nodeToSave, of);
}
}
Node* Tree::searchByIdRecursive(Node* nodeToSearch, int id)
{
if (nodeToSearch->getId() == id)
{
return nodeToSearch;
}
else
{
Node* result = NULL;
std::vector<Node*>* elements = nodeToSearch->getOffsprings();
for (int i = 0; i < nodeToSearch->getNumberOfOffsprings(); i++)
{
Node* nodeToSearch = elements->at(i);
result = searchByIdRecursive(nodeToSearch, id);
if (result != NULL) return result;
}
return result;
}
}
Node* Tree::searchById(int id)
{
Node* result = NULL;
result = searchByIdRecursive(this->getRoot(), id);
return result;
}
void Tree::saveTree(std::ofstream &of)
{
int totalNodes = this->numberOfNodes;
int lastNodeAdded = this->lastNodeAdded;
of.write((char*)&totalNodes, sizeof(totalNodes));
of.write((char*)&lastNodeAdded, sizeof(lastNodeAdded));
std::vector<Node*>* elements = this->getRoot()->getOffsprings();
for (int i = 0; i < this->getRoot()->getNumberOfOffsprings(); i++)
{
Node* nodeToSave = elements->at(i);
saveTreeRecursive(nodeToSave, of);
}
of.close();
}
void Tree::loadTree()
{
std::ifstream binaryFile;
int size = 0;
binaryFile.open("tree.dat", std::ios::in | std::ios::binary);
binaryFile.seekg(0, std::ios::end);
size = (int)binaryFile.tellg();
binaryFile.seekg(0, std::ios::beg);
int numberNodes = 0;
int lastNode = -1;
binaryFile.read((char*)&numberNodes, sizeof(numberNodes));
binaryFile.read((char*)&lastNode, sizeof(lastNode));
while(binaryFile.tellg() < size)
{
//saco datos y creo nodo
int idFather = -1;
binaryFile.read((char*)&idFather, sizeof(idFather));
int id = -1;
binaryFile.read((char*)&id, sizeof(id));
char name [20];
binaryFile.read((char*)&name, sizeof(name));
std::string nameString = std::string(name);
char node_type [20];
binaryFile.read((char*)&node_type, sizeof(node_type));
std::string node_typeString = std::string(node_type);
off_t size = -1;
binaryFile.read((char*)&size, sizeof(size));
time_t lastModification;
binaryFile.read((char*)&lastModification, sizeof(lastModification));
typename std::vector<int>::size_type vectorSize = -1;
binaryFile.read((char*)&vectorSize, sizeof(vectorSize));
std::vector<int> blockOccupied;
blockOccupied.resize(vectorSize);
binaryFile.read((char*)blockOccupied.data(), blockOccupied.size()*sizeof(int));
//busco padre
Node* father = searchById(idFather);
if (father == NULL) std::cout << "Father not found, error in data" << std::endl;
else //asigno
{
Node* nodeToInsert = new Node(this, father, nameString, node_typeString);
this->addChild(nodeToInsert, father);
nodeToInsert->setID(id);
nodeToInsert->setByteSize(size);
nodeToInsert->setDateLastModif(lastModification);
//asignar cosas
nodeToInsert->setBlockOccupied(blockOccupied); /////
nodeToInsert->setNumBlocksOccupied();
}
}
binaryFile.close();
}
////GETTERS////
//returns root node * from tree
Node* Tree :: getRoot()
{
return this->root;
}
//return current dir
Node* Tree::getCurrentDir()
{
return this->current_Dir;
}
//returns size of structure
int Tree::getNumberOfNodes()
{
return this->numberOfNodes;
}
//returns last node id
int Tree::getLastNodeId() {
return this->lastNodeAdded;
}