-
Notifications
You must be signed in to change notification settings - Fork 0
/
docus.cpp
226 lines (197 loc) · 6.05 KB
/
docus.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
#include "docus.h"
Document::Document(const std::vector<std::string> &data) : filename(data[0]) {
/*
vector<Holder*> 'holders' contains Holder* pointers, and each of it points an individual holder.
To set a new holder, we push a new Holder* pointer and dynamically allocate it.
*/
for (int i = 0; i < data.size(); ++i) {
if (data[i] == "<TESTHOLDER>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</TESTHOLDER>") {
hold.push_back(data[i++]);
}
Holder* p = new TestHolder(hold);
holders.push_back(p);
continue;
}
if (data[i] == "<SHAPE_HOLDER>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</SHAPE_HOLDER>") {
hold.push_back(data[i++]);
}
Holder* p = new ShapeHolder(hold);
holders.push_back(p);
continue;
}
if (data[i] == "<STRINGHOLDER>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</STRINGHOLDER>") {
hold.push_back(data[i++]);
}
Holder* p = new StringHolder(hold);
holders.push_back(p);
continue;
}
if (data[i] == "<LINE>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</LINE>") {
hold.push_back(data[i++]);
}
Holder* p = new LineHolder(hold);
holders.push_back(p);
continue;
}
if (data[i] == "<HISTOGRAM>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</HISTOGRAM>") {
hold.push_back(data[i++]);
}
Holder* p = new HistogramHolder(hold);
holders.push_back(p);
continue;
}
if (data[i] == "<TABLE>") {
std::vector<std::string> hold;
++i;
while (data[i] != "</TABLE>") {
hold.push_back(data[i++]);
}
Holder* p = new TableHolder(hold);
holders.push_back(p);
continue;
}
}
}
Document::~Document() {
// Deallocate every pointers in 'holders' and pop it.
for (int i = (int) holders.size() - 1; i >= 0; --i) {
delete holders[i];
holders.pop_back();
}
}
void Document::print() const {
for (Holder* p : holders) {
p->print();
}
}
void Document::print_infos() const {
int i = 0;
for (Holder* p : holders) {
std::cout << "The holder #" << ++i;
Holder::TYPE type = p->get_type();
switch (type) {
case Holder::TEST_HOLDER:
std::cout << " is a TestHolder"; break;
case Holder::SHAPE_HOLDER:
std::cout << " is a ShapeHolder"; break;
case Holder::STRING_HOLDER:
std::cout << " is a StringHolder"; break;
case Holder::LINE_HOLDER:
std::cout << " is a LineHolder"; break;
case Holder::HISTOGRAM_HOLDER:
std::cout << " is a HistogramHolder"; break;
case Holder::TABLE_HOLDER:
std::cout << " is a TableHolder"; break;
default:
std::cout << " RAISED AN ERROR : UNKNOWN HOLDER"; break;
}
std::cout << " named " << p->get_title() << ".\n";
} std::cout << std::endl;
}
void Document::push_tes() {
Holder* p = new TestHolder;
holders.push_back(p);
}
void Document::push_str() {
Holder* p = new StringHolder;
holders.push_back(p);
}
void Document::push_tab(int i, int j) {
Holder* p = new TableHolder(i, j);
holders.push_back(p);
}
void Document::push_lin() {
Holder* p = new LineHolder;
holders.push_back(p);
}
void Document::push_his() {
Holder* p = new HistogramHolder;
holders.push_back(p);
}
void Document::push_tes_infrontof(int idx) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::push_tes_infrontof");
}
Holder* p = new TestHolder;
holders.insert(holders.begin() + idx, p);
}
void Document::push_str_infrontof(int idx) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::push_str_infrontof");
}
Holder* p = new StringHolder;
holders.insert(holders.begin() + idx, p);
}
void Document::push_tab_infrontof(int idx, int i, int j) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::push_tab_infrontof");
}
Holder* p = new TableHolder;
holders.insert(holders.begin() + idx, p);
}
void Document::push_lin_infrontof(int idx) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::push_lin_infrontof");
}
Holder* p = new LineHolder;
holders.insert(holders.begin() + idx, p);
}
void Document::push_his_infrontof(int idx) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::push_his_infrontof");
}
Holder* p = new HistogramHolder;
holders.insert(holders.begin() + idx, p);
}
void Document::pop_holder() {
if (holders.empty()) {
throw mints::input_out_of_range("at Document::pop_holder");
}
if (holders.back() != nullptr) {
delete holders.back();
}
holders.pop_back();
}
void Document::remove_holder(int idx) {
if (idx >= holders.size()) {
throw mints::input_out_of_range("at Document::remove_holder");
}
Holder*& to_be_del = holders[idx];
if (to_be_del != nullptr) {
delete to_be_del;
}
holders.erase(holders.begin() + idx);
}
std::string Document::save() const {
std::stringstream ss;
ss << filename << "\n\n";
for (Holder* p : holders) {
ss << p->to_txt_data();
}
std::string str; ss >> str;
return str;
}
Holder *Document::at(int idx) {
return holders.at(idx);
}
unsigned int Document::size() const {
return (unsigned int) holders.size();
}
std::string Document::get_filename() const {
return filename;
}