-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDirTreeNode.cpp
executable file
·187 lines (178 loc) · 5.26 KB
/
DirTreeNode.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 "DirTreeNode.h"
#include "defs.h"
#include "settings.h"
DirTreeNode* DirTreeNode::lastInserted = NULL;
DirTreeNode::DirTreeNode(const char* fName, const unsigned long long fSize, const unsigned fTime, const int fType) {
this->name = strdup(fName);
this->type = fType;
this->size = fSize;
this->time = fTime;
this->forced = false;
this->do_not_list_as_dir = false;
lastInserted = (DirTreeNode *)this;
next = NULL;
firstChild = NULL;
}
DirTreeNode::~DirTreeNode() {
for (DirTreeNode *p = firstChild; p != NULL; ) {
DirTreeNode *q = p->getNext();
delete p;
p = q;
}
free(name);
}
void DirTreeNode::setNext(DirTreeNode *next) {
this->next = next;
}
void DirTreeNode::setForced(bool bln) {
this->forced = bln;
}
DirTreeNode* DirTreeNode::getNext() {
return next;
}
char* DirTreeNode::getName() {
return name;
}
// modifies firstChild, if new or replacing
bool DirTreeNode::insert(char* fName, const unsigned long long fSize, const unsigned fTime, const int fType) {
if (fName == NULL) { // we are replacing, so free all children (if this was not created implicitly)
if (this->forced == false) {
for (DirTreeNode *p = firstChild; p != NULL; ) {
DirTreeNode *q = p->getNext();
delete p;
p = q;
}
firstChild = NULL;
}
this->size = fSize;
this->time = fTime;
this->type = fType;
this->forced = false;
lastInserted = this;
return true;
}
// rem will be pointer to remainder of path, or NULL if no remainder
char* rem = (char*) strchr(fName, '\\');
int fNameLen;
if (rem != NULL) {
if (rem[1] == '\0') {
fNameLen = (int)strlen(fName);
rem = NULL;
} else {
fNameLen = (int)(rem - fName) + 1;
rem++;
}
} else {
fNameLen = (int)strlen(fName);
}
bool exists = false;
DirTreeNode *q = firstChild;
for (DirTreeNode *p = firstChild; p != NULL; q = p, p = p->getNext()) {
if (strncmp(p->getName(), fName, fNameLen) == 0 && strlen(p->getName()) == fNameLen) {
exists = p->insert(rem, fSize, fTime, fType);
if (exists == false) return false; // error while inserting
exists = true;
break;
}
}
if (exists == false) { // new element
if (rem != NULL) { // has remainder (directory which was not added before)
char z = fName[fNameLen];
fName[fNameLen] = '\0';
DirTreeNode *a = new DirTreeNode(fName, 0, fTime, FILE_TYPE_DIRECTORY);
if (q == NULL) {
firstChild = a;
} else {
q->setNext(a);
}
fName[fNameLen] = z;
a->insert(rem, fSize, fTime, fType);
a->setForced(true); // created implicitly
} else {
DirTreeNode *a = new DirTreeNode(fName, fSize, fTime, fType);
if (q == NULL) {
firstChild = a;
} else {
q->setNext(a);
}
}
}
return true;
}
// modifies firstChild, if new or replacing
bool DirTreeNode::remove(const char* fName) {
if (fName == NULL) { // we found it, so return true, the deletion will take place later
return true;
}
// rem will be pointer to remainder of path, or NULL if no remainder
char* rem = (char*) strchr(fName, '\\');
int fNameLen;
if (rem != NULL) {
if (rem[1] == '\0') {
fNameLen = (int)strlen(fName);
rem = NULL;
} else {
fNameLen = (int)(rem - fName) + 1;
rem++;
}
} else {
fNameLen = (int)strlen(fName);
}
bool exists = false;
DirTreeNode *q = firstChild;
for (DirTreeNode *p = firstChild; p != NULL; q = p, p = p->getNext()) {
if (strncmp(p->getName(), fName, fNameLen) == 0 && strlen(p->getName()) == fNameLen) {
exists = p->remove(rem);
if (exists == false) return false; // do not remove this
exists = false;
// we are going to remove p
if (p == firstChild) {
firstChild = p->getNext();
} else {
q->setNext(p->getNext());
}
delete p;
break;
}
}
if (exists == false) return false;
return true;
}
void DirTreeNode::writeOut(FILE* fout, char* curPath) {
int year = (this->time >> 25) + 1980;
int month = (this->time & 0x1FFFFFF) >> 21;
int day = (this->time & 0x1FFFFF) >> 16;
int hour = (this->time & 0xFFFF) >> 11;
int minute = (this->time & 0x7FF) >> 5;
int second = (this->time & 0x1F) * 2;
// do not write c:\, g:\, etc. as directories
if (this->name[1] == ':' && this->name[2] == '\\' && this->name[3] == '\0') ; // nothing
else {
if (this->name[strlen(this->name) - 1] == '\\') fprintf(fout, "%s", curPath);
fprintf(fout, settings.getLineFormat(),
this->name, this->size, year, month, day, hour, minute, second);
}
if (firstChild != NULL) {
int lenOrig = strlen(curPath);
strcat(curPath, this->name);
// first non directories
for (DirTreeNode *p = firstChild; p != NULL; p = p->getNext()) {
if (p->do_not_list_as_dir || p->getName()[strlen(p->getName()) - 1] != '\\') {
if (p->getName()[strlen(p->getName()) - 1] == '\\') p->getName()[strlen(p->getName()) - 1] = '\0';
p->writeOut(fout, curPath);
}
}
// now directories
for (DirTreeNode *p = firstChild; p != NULL; p = p->getNext()) {
if (!p->do_not_list_as_dir && p->getName()[strlen(p->getName()) - 1] == '\\')
p->writeOut(fout, curPath);
}
curPath[lenOrig] = '\0';
}
}
void DirTreeNode::finalize() {
this->forced = false;
for (DirTreeNode *p = firstChild; p != NULL; p = p->getNext()) {
p->finalize();
}
}