-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cc
143 lines (127 loc) · 3.25 KB
/
controller.cc
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
#include "controller.h"
using namespace std;
Controller::Controller() : doc{new Tag{"document"}}, cur{doc}, clip{nullptr} {}
Controller::~Controller() {
delete clip;
delete doc;
// No need to delete cur because it's just a pointer to some tag within doc
}
void Controller::printCurrent() {
cout << "<" << cur->getType() << "> is now the current tag." << endl;
}
void Controller::print() { cout << *cur; }
void Controller::addChild(string option) {
if (option == "document") {
cerr << "Cannot create new <document> tag." << endl;
return;
}
Tag *newTag = new Tag{option};
newTag->setParent(cur);
cur->addChild(newTag);
cur = newTag;
printCurrent();
}
void Controller::deleteCur() {
if (cur == doc) {
cerr << "Document tag cannot be removed." << endl;
return;
}
Tag *tmp = cur;
cur = tmp->getParent();
cur->removeChild(tmp);
delete tmp;
printCurrent();
}
void Controller::parent() {
if (cur->getParent() == nullptr) {
cerr << "Current tag is already the upper level tag." << endl;
return;
}
cur = cur->getParent();
printCurrent();
}
void Controller::up() {
cur = doc;
printCurrent();
}
void Controller::setId(string option) {
cur->setId(option);
cout << "<" << cur->getType() << "> id set to '" << option << "'." << endl;
}
void Controller::setValue(string option) {
cur->setValue(option);
cout << "<" << cur->getType() << "> value set to '" << option << "'." << endl;
}
void Controller::findChild(string option) {
Tag *tmp = cur->findChild(option);
if (tmp == nullptr) {
cerr << "<" << option << "> not found." << endl;
return;
}
cur = tmp;
printCurrent();
}
void Controller::findChildId(string option) {
Tag *tmp = cur->findChildId(option);
if (tmp == nullptr) {
cerr << "Tag with id '" << option << "' not found." << endl;
return;
}
cur = tmp;
printCurrent();
}
void Controller::list() {
cout << "<" << cur->getType() << "> has the following children:" << endl;
for (Tag *child : *cur) {
cout << "- <" << child->getType() << ">" << endl;
}
}
void Controller::cut() {
if (cur == doc) {
cerr << "Document tag cannot be cut." << endl;
return;
}
// remove tag from parent
Tag *parent = cur->getParent();
parent->removeChild(cur);
// move tag to clipboard
if (clip == nullptr) {
// using move constructor
clip = new Tag{std::move(*cur)};
} else {
// using move assignment operator
*clip = std::move(*cur);
}
// delete original tag
delete cur;
cur = parent;
cout << "<" << clip->getType() << "> is now in the clipboard." << endl;
printCurrent();
}
void Controller::copy() {
if (cur == doc) {
cerr << "Document tag cannot be copied." << endl;
return;
}
// copy tag to clipboard
if (clip == nullptr) {
// using copy constructor
clip = new Tag{*cur};
} else {
// using copy assignment operator
*clip = *cur;
}
cout << "<" << clip->getType() << "> is now in the clipboard." << endl;
}
void Controller::paste() {
if (clip == nullptr) {
cerr << "The clipboard is empty." << endl;
return;
}
// copy tag from clipboard as a new child of the current tag
Tag *newTag = new Tag{*clip}; // using copy constructor
newTag->setParent(cur);
cur->addChild(newTag);
cur = newTag;
printCurrent();
}