-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp.backup
148 lines (96 loc) · 4.65 KB
/
main.cpp.backup
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
#include <cstdlib>
#include <string>
#include "File.h"
#include "Directory.h"
#include "CompositePrinter.h"
#include "filesyscmds.h"
#include "SuffixPrintVisitor.h"
#include <iostream>
#include <algorithm>
using namespace std;
// TODO: use for_each() )
class OutputFunctor {
ostream& ostr;
SuffixPrintVisitor spv;
public:
OutputFunctor(ostream& o, const SuffixPrintVisitor& s) : ostr(o), spv(s) {}
void operator()(Node& n)
{
ostr << n.getName();
n.accept(spv);
ostr << endl;
}
};
int main(int argc, char** argv)
{
long file_size = 1000; // we hardcode the size
try {
File *ptop_f1 = new File(string("top-File1"), string("12-12-2013"), file_size);
File *ptop_f2 = new File(string("top-File2"), string("12-10-2013"), file_size);
Directory top(string("top-level-dir"), string("12-09-2013"));
top.adopt(ptop_f1);
top.adopt(ptop_f2);
/*
* We could equivalently do this
* Node *psubdir_mid = top.getChild(2);
* to get a pointer to Directory we just created, "subdir-mid"
*/
Directory *psubdir_mid = mkdir(&top, "subdir-mid");
Directory *psubdir_lower1 = mkdir(&top, "subdir-mid/subdir-lower1");
/* Node *psubdir_lower1 = psubdir_mid->getChild(0); // alternative to line above. */
File *psubdir_lower1_f1 = new File(string("subdir-lower1-File1"), string("12-12-2013"), file_size);
File *psubdir_lower1_f2 = new File(string("subdir-lower1-File2"), string("12-10-2013"), file_size);
psubdir_lower1->adopt(psubdir_lower1_f1);
psubdir_lower1->adopt(psubdir_lower1_f2);
File *psubdir_mid_f1 = new File(string("subdir_mid-File1"), string("12-12-2013"), file_size);
File *psubdir_mid_f2 = new File(string("subdir-mid_File2"), string("12-10-2013"), file_size);
psubdir_mid->adopt(psubdir_mid_f1);
psubdir_mid->adopt(psubdir_mid_f2);
mkdir(&top, "subdir-mid/subdir-lower2");
/*
* For illustration purposes only, we call
* psubdir_mid->getChild(3)
* instead of using the return value of mkdir. Note, the subdirectory subdir-lower2/ is the third child (we index starting
* at zero) of its immediate parent subdir-mid/.
*/
Node *psubdir_lower2 = psubdir_mid->getChild(3);
File *psubdir_lower2_f1 = new File(string("subdir-lower2-File1"), string("12-12-2013"), file_size);
File *psubdir_lower2_f2 = new File(string("subdir-lower2-File2"), string("12-10-2013"), file_size);
psubdir_lower2->adopt(psubdir_lower2_f1);
psubdir_lower2->adopt(psubdir_lower2_f2);
File *ptop_f3 = new File(string("top-File3"), string("12-12-2013"), file_size);
File *ptop_f4 = new File(string("top-File4"), string("12-10-2013"), file_size);
top.adopt(ptop_f3);
top.adopt(ptop_f4);
cout << "\n ===== top.traverse(CompositePrint(cout)) =========== \n" << endl;
CompositePrinter printer(cout);
top.traverse(printer);
cout << "\n ===== print using iterator: top.print() =========== \n" << endl;
top.print();
cout << "\n ====== using Directory::iterator external iterator to print. SuffixPrintVisitor used to append Node type. ==== \n" << endl;
Directory::iterator iter_current = top.begin();
Directory::iterator iter_end = top.end();
SuffixPrintVisitor spv;
OutputFunctor ofunctor(cout, spv);
for_each(iter_current, iter_end, ofunctor);
/*
for (;iter_current != iter_end; ++iter_current) {
cout << iter_current->getName();
iter_current->accept(spv);
cout << endl;
}
*/
cout << "\n ===== Testing Directory::const_iterator. Also using SuffixPrintVisitor ======\n";
const Directory& const_top = top;
Directory::const_iterator const_iter_current = const_top.begin();
Directory::const_iterator const_iter_end = const_top.end();
for (;const_iter_current != const_iter_end; ++const_iter_current) {
cout << iter_current->getName();
iter_current->accept(spv);
cout << endl;
}
} catch (std::exception& e) {
cout << e.what();
}
return 0;
}