-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
250 lines (217 loc) · 4.38 KB
/
Node.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
#include "Node.h"
bool Node::isFolder(std::string name)
{
if (name.compare("fichero") == 0 || name.compare("f") == 0 || name.compare("file") == 0 || name.compare("File") == 0)
{
return 0;
}
return 1;
}
Node::Node(Tree* tree, Node* nodeFather, std::string name, std::string node_type)
{
//This note is ROOT
if (nodeFather == NULL)
{
//Check if ROOT already exists
if (tree->getRoot() == NULL)
{
this->tree = tree;
this->father = NULL;
this->setName("ROOT");
this->Id = 0;
this->idcont = 1;
this->numberOfOffsprings = 0;
this->level = 0;
this->isDirectory = 1;
this->byteSize = 4096;
this->dateLastModif = std::time(0);
}
else
{
std::cout << "Error: ROOT already exists." << std::endl;
}
}
//This note is not root
else
{
this->tree = tree;
this->father = nodeFather;
this->name = name;
this->Id = this->idcont;
this->idcont += 1;
this->numberOfOffsprings = 0;
this->level = nodeFather->getLevel() + 1;
if (isFolder(node_type))
{
this->byteSize = 4096;
this->isDirectory = 1;
}
else
{
this->byteSize = 0;
this->isDirectory = 0;
}
this->dateLastModif = std::time(0);
}
}
//destructor
Node :: ~Node()
{
for (int i = 0; i < numberOfOffsprings; i++)
{
delete offsprings.at(i);
}
}
////GETTERS////
//returns type folder or archive
std::string Node::getType()
{
if (this->isDirectory == 0)
{
return "File";
}
else
{
return "Folder";
}
}
//return pointer Node father
Node* Node::getNodeFather()
{
return this->father;
}
//return id unic
int Node::getId()
{
return this->Id;
}
//return offSpring
std::vector<Node*>* Node::getOffsprings()
{
return &this->offsprings;
}
//return name
std::string Node::getName()
{
return this->name;
}
//return size in bytes
off_t Node::getByteSize()
{
//modificar por punter
return this->byteSize;
}
//return last modification date
time_t Node::getDateLastModif()
{
return this->dateLastModif;
}
int Node::getLevel()
{
return this->level;
}
bool Node::getIsDirectory()
{
return this->isDirectory;
}
//get number of offsprings
int Node::getNumberOfOffsprings()
{
return this->numberOfOffsprings;
}
std::vector<int>* Node::getBlockLocations()
{
return &this->blockOccupied;
}
int Node::getNumBlocksOccupied()
{
return this->numBlocksOccupied;
}
////SETTERS/////
void Node::setTree(Tree* tree)
{
this->tree = tree;
}
void Node::setIsDirectory(bool isDirectory)
{
this->isDirectory = isDirectory;
}
//set type
void Node::setType(std::string fichDire)
{
if (fichDire.compare("fichero") == 0 || fichDire.compare("f") == 0 || fichDire.compare("file") == 0)
{
this->isDirectory = 0;
}
else if (fichDire.compare("directorio") == 0 || fichDire.compare("dir") == 0 || fichDire.compare("folder") == 0)
{
this->isDirectory = 1;
}
}
//set pointer node father
void Node::setFather(Node* father)
{
this->father = father;
}
void Node::setNewOffSpring(Node* son)
{
std::vector<Node*>* sons = this->getOffsprings();
sons->push_back(son);
//this->offsprings.push_back(son);
}
//set ++ offspring or -- offsprings number
void Node :: setNumberOffsprings(int number)
{
if (number == -1)
{
this->numberOfOffsprings--;
}
else if (number == 1)
{
this->numberOfOffsprings++;
}
}
//set the new id Name of the node, max 25 characters
void Node::setName(std::string name)
{
this->name = name;
}
//TODO: check tam if file is not a directory
void Node::setByteSize(off_t tam)
{
if (this->getIsDirectory())
{
this->byteSize = 4096;
}
else
{
this->byteSize = tam;
}
}
//sets last time modification
void Node::setDateLastModif(time_t date)
{
this->dateLastModif = date;
}
//set level of file or directory in tree
void Node::setLevel(int level)
{
this->level = level;
}
void Node::setID(int id)
{
this->Id = id;
this->idcont = id + 1;
}
void Node::setBlock(int block)
{
this->blockOccupied.push_back(block);
}
void Node::setNumBlocksOccupied()
{
this->numBlocksOccupied = this->blockOccupied.size();
}
void Node::setBlockOccupied(std::vector<int> &blocks)
{
this->blockOccupied = blocks;
}