-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.cpp
413 lines (353 loc) · 9.76 KB
/
archive.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
403
404
405
406
407
408
409
410
411
412
413
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "brother.h"
#include "archive.h"
archive::~archive(){
for(auto b : brothers){
if(b != nullptr){
delete b;
}
}
}
void archive::readFile(std::ifstream& in){
// Temp storage variables for reading
std::string name, year, bg, tmp;
std::getline(in, tmp); // Discards header row
while(!in.eof()){
std::getline(in, year, ','); // Grabs: semester initiated
std::getline(in, name, ','); // name
std::getline(in, bg, ','); // big brother
std::getline(in, tmp); // littles
addBrother(year, name, bg, tmp);
}
in.close();
}
brother* archive::searchBrother(const std::string& n) const{
for(int i = 0; i < brothers.size(); ++i){
if(brothers[i]->getName().find(n) != std::string::npos)
return brothers[i];
}
return nullptr;
}
void archive::printBanner() const{
// Header row for bros
std::cout << std::left << std::setw(6) << "Year" << std::setw(25)
<< "Name" << std::setw(25) << "Big Bro" << std::right
<< std::setw(25) << "Little" << std::endl;
std::cout << std::left << std::setw(6) << "-----" << std::setw(25)
<< "----" << std::setw(25) << "-------" << std::right
<< std::setw(25) << "------" << std::endl;
}
void archive::printClass(const std::string& c) const{
bool empty = true;
std::istringstream strs(c);
std::string sem, yr, n;
strs >> sem;
strs >> yr;
if(sem == "fa" || sem == "Fa" || sem == "Fall" || sem == "fall")
sem = "Fa";
else if(sem == "sp" || sem == "Sp" || sem == "Spring" || sem == "spring")
sem = "Sp";
if(yr.length() > 2)
yr = yr.substr(2,4);
n = sem + ' ' + yr;
for(int i = n.length() == 5 ? 0 : brothers.size();
i < brothers.size(); ++i){
if(n == brothers[i]->getYear()){
if(empty){
printBanner();
empty = false;
}
printInfo(brothers[i]);
}
}
if(!empty)
return;
else
empty = false;
if((n.find("Rab") != std::string::npos) ||
(n.find("rab") != std::string::npos)){
printFamily(searchBrother("Tony Geronimos"));
} else if((n.find("Mo") != std::string::npos) ||
(n.find("mo") != std::string::npos)){
printFamily(searchBrother("Jeff Chamlis"));
} else if((n.find("Zeb") != std::string::npos) ||
(n.find("zeb") != std::string::npos)){
printFamily(searchBrother("Troy Paolantonio"));
} else if((n.find("Panda") != std::string::npos) ||
(n.find("panda") != std::string::npos) ||
(n.find("platinum") != std::string::npos) ||
(n.find("Platinum") != std::string::npos)){
printFamily(searchBrother("Michael Weintraub"));
} else if((n.find("Possum") != std::string::npos) ||
(n.find("possum") != std::string::npos)){
printFamily(searchBrother("Jennifer Harris"));
} else if((n.find("Whale") != std::string::npos) ||
(n.find("whale") != std::string::npos) ||
(n.find("dolphin") != std::string::npos) ||
(n.find("Dolphin") != std::string::npos)){
printFamily(searchBrother("Josh Willoughby"));
}else
empty = true;
if(empty)
std::cout << "No brothers found" << std::endl;
}
void archive::printInfo(brother* b) const{
if(b == nullptr || !isBro(b)){
std::cout << "Brother not found" << std::endl;
return;
}
std::cout << std::left << std::setw(6) << b->getYear();
//if(COLOR)
//famColor(b);
std::cout << std::setw(25) << b->getName();
//if(COLOR) // important
//std::cout << "\033[0m";
std::cout << std::setw(25) << (b->Big() == nullptr ? "" :
b->getBig());
if(b->Littles().size() == 0)
std::cout << std::endl;
for(int i = 0; i < b->Littles().size(); ++i){
std::cout << std::right;
if(i == 0)
std::cout << std::setw(25);
else
std::cout << std::setw(81);
std::cout << b->Littles()[i]->getName() << std::endl;
}
}
void archive::printFamily(brother* b) const{
if(!isBro(b)){
std::cout << "Brother not found" << std::endl;
return;
} else
printTree(b, 0);
}
void archive::printLine(brother* b) const{
if(!isBro(b))
return;
if(b->Big() != nullptr){
printLine(b->Big());
std::cout << "\t ^" << std::endl
<< "\t |" << std::endl;
}
std::cout << b->getYear() << " - " << b->getName();
// b->famLabel();
std::cout << std::endl;
}
void archive::printList() const{
if(brothers.size() > 0)
printBanner();
// Prints out brothers info
for(int i = 0; i < brothers.size(); ++i){
printInfo(brothers[i]);
}
}
void archive::printTree(brother* current, const int& id) const{
bool fLil = false;
// Prints indented name
std::cout << indent(id-3) << current->getName();
//current->famLabel();
std::cout << " (" << current->getYear() << ')' << std::endl;
for(auto lil : current->Littles()){
if(!fLil){
std::cout << indent(id) << " ^\n"
<< indent(id) << " |\n"
<< indent(id) << " --\n"
<< indent(id) << " |"
<< std::endl;
fLil = true;
printTree(lil, id+INDENT);
} else{
std::cout << std::endl << indent(id-3);
std::cout << current->getName() << " ("
<< current->getYear() << ')' << std::endl;
std::cout << indent(id);
std::cout << " ^" << std::endl;
std::cout << indent(id)
<< " |" << std::endl;
printTree(lil, id+INDENT);
}
}
}
std::string adFormat(brother* b){
std::string name;
std::string fname = b->getFName();
for(int i = 0; i < 7; ++i){
if(i < fname.length())
name.push_back(fname[i]);
else
name.push_back(' ');
}
name.push_back(' ');
name.push_back(b->getLName()[0]);
return name;
}
int dfs(brother* b, int level){
if(b->Littles().size() == 0)
return level;
int child;
int deepest = level;
for(int i = 0; i < b->Littles().size(); ++i){
child = dfs(b->Littles()[i], level+1);
if(child > deepest) deepest = child;
}
return deepest;
}
std::string dfs(brother* b, int level, int target, int lbound, int rbound, std::string names){
if(level == target){
int bound_length = rbound - lbound;
int extra_space = bound_length - adFormat(b).length();
if(extra_space < 0) extra_space = 0;
for(int i = names.length(); i < (extra_space / 2) + lbound; ++i)
names += ' ';
names += adFormat(b);
for(int i = names.length(); i < rbound; ++i)
names += ' ';
//std::cout << "LB: " << lbound << ", RB: " << rbound << "; " << adFormat(b) << std::endl;
}
if(b->Littles().size() == 0)
return names;
int child_bound = (rbound - lbound) / b->Littles().size();
for(int i = 0; i < b->Littles().size(); ++i){
names = dfs(b->Littles()[i], level+1, target, (lbound + (child_bound*i)), (lbound + (child_bound * (i+1))), names);
}
return names;
}
void archive::printATree(brother* b, const int& pos) const{
int deepest_level = dfs(b, 1);
for(int i = 0; i < deepest_level; ++i){
std::string names = dfs(b, 0, i, 0, 120, "");
std::cout << names << std::endl;
}
}
/* PRIVATE HELPER FUNCTIONS */
// Checks if brother exists in brothers
bool archive::isBro(const std::string& name){
if(name.size() == 0)
return false;
for(int i = 0; i < brothers.size(); ++i){
if(name == brothers[i]->getName()){
big = i;
return true;
}
}
return false;
}
bool archive::isBro(const brother* b) const{
if(b == nullptr)
return false;
for(auto bro : brothers){
if(b->getName() == bro->getName())
return true;
}
return false;
}
// Checks if brother exists as a little
bool archive::isLittle(const std::string& name){
for(int i = 0; i < brothers.size(); ++i){
if(brothers[i]->isLittle(name, lil)){
big = i;
return true;
}
}
return false;
}
std::vector<std::string> archive::getLils(std::string& LL){
std::istringstream ss(LL);
std::vector<std::string> littles;
std::string little;
while(std::getline(ss, little, ','))
littles.push_back(little);
return littles;
}
std::string archive::format(std::string& x){
std::string tmp;
bool isSpace = false;
bool isChar = false;
for(int ch = 0; ch < x.size(); ++ch){
// Handles names with more than 1 space in between
if(isChar && ch+1 < x.size()){
if(isSpace && isspace(x[ch]) && isalnum(x[ch+1]))
isSpace = false;
}
// Checks if end of name
if(isChar && isSpace && (!isalnum(x[ch])
&& x[ch] != '-' && x[ch] != '.'))
break;
// Checks for name inbetween name
if(isChar && isspace(x[ch])){
isSpace = true;
tmp += x[ch];
}
else if(isChar || isalnum(x[ch])
|| x[ch] == '-' || x[ch] == '.'){
isChar = true;
tmp += x[ch];
}
}
return tmp;
}
std::string archive::indent(const int& id) const{
std::string tmp;
for(int i = 0; i < id; ++i)
tmp += ' ';
return tmp;
}
void archive::famColor(brother* b) const{
if(b->relatedTo("Jennifer Harris"))
std::cout << "\033[1;32m";
if(b->relatedTo("Jeff Chamlis"))
std::cout << "\033[1;35m";
if(b->relatedTo("Troy Paolantonio")){
if(b->relatedTo("Briana Meder"))
std::cout << "\033[1;30m";
else
std::cout << "\033[1;37m";
}
if(b->relatedTo("Tony Geronimos"))
std::cout << "\033[1;33m";
if(b->relatedTo("Josh Willoughby"))
std::cout << "\033[1;34m";
if(b->relatedTo("Michael Weintraub"))
std::cout << "\033[1;31m";
}
void archive::addBrother(std::string& year, std::string& name,
std::string& bg, std::string& tmp){
year = format(year);
name = format(name);
bg = format(bg);
std::vector<std::string> littles = getLils(tmp);
if(year.size() == 0)
return;
// New Brother
brother* newB = new brother(year, name);
// Sets big
if(isBro(bg))
{ // Sets big, sets bigs little to this
if(!brothers[big]->replaceLittle(newB))
brothers[big]->pushLittle(newB);
}
else if(bg.size() > 0) // Catches if big is not bro
newB->setBig(new brother(bg));
// Adds littles to bro
for(auto little : littles){
little = format(little);
if(little.size() == 0)
continue;
// Checks if little is already a bro
if(isBro(little)){
if(!newB->replaceLittle(brothers[big]))
newB->pushLittle(brothers[big]);
}
else{
newB->pushLittle(new brother(little));
}
}
brothers.push_back(newB);
}