-
Notifications
You must be signed in to change notification settings - Fork 211
/
tree.cc
144 lines (122 loc) · 2.76 KB
/
tree.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
#include "tree.h"
#include "databuffer.h"
Tree::Tree(std::string tag)
{
this->tag = tag;
}
Tree::Tree(std::string tag, std::map < std::string, std::string > attributes)
{
this->tag = tag;
this->attributes = attributes;
}
Tree::~Tree()
{
}
void Tree::addChild(Tree t)
{
children.push_back(t);
}
void Tree::setAttributes(std::map < std::string, std::string > attributes)
{
this->attributes = attributes;
}
void Tree::readAttributes(DataBuffer * data, int size)
{
int count = (size - 2 + (size % 2)) / 2;
while (count--) {
std::string key = data->readString();
std::string value = data->readString();
attributes[key] = value;
}
}
void Tree::writeAttributes(DataBuffer * data)
{
for (std::map < std::string, std::string >::iterator iter = attributes.begin(); iter != attributes.end(); iter++) {
data->putString(iter->first);
data->putString(iter->second);
}
}
void Tree::setData(const std::string d)
{
data = d;
}
void Tree::setChildren(std::vector < Tree > c)
{
children = c;
}
std::vector < Tree > Tree::getChildren() const
{
return children;
}
std::map < std::string, std::string > &Tree::getAttributes()
{
return attributes;
}
bool Tree::hasAttributeValue(std::string at, std::string val) const
{
if (hasAttribute(at)) {
return (attributes.at(at) == val);
}
return false;
}
bool Tree::hasAttribute(const std::string & at) const
{
return (attributes.find(at) != attributes.end());
}
std::string Tree::getAtr(const std::string & at) const
{
if (hasAttribute(at))
return (attributes.at(at));
return "";
}
bool Tree::getChild(std::string tag, Tree & t) const
{
for (unsigned int i = 0; i < children.size(); i++) {
if (children[i].getTag() == tag) {
t = children[i];
return true;
}
if (children[i].getChild(tag, t))
return true;
}
return false;
}
bool Tree::hasChild(std::string tag) const
{
for (unsigned int i = 0; i < children.size(); i++) {
if (children[i].getTag() == tag)
return true;
if (children[i].hasChild(tag))
return true;
}
return false;
}
std::string Tree::escapeStrings(std::string s) {
std::string ret;
for (auto c: s) {
if (c < 32 || c > 126) {
ret += "\\";
ret += ('0' + (c / 64));
ret += ('0' + (c / 8)%8);
ret += ('0' + (c % 8));
}
else
ret += c;
}
return ret;
}
std::string Tree::toString(int sp)
{
std::string ret;
std::string spacing(sp, ' ');
ret += spacing + "Tag: " + tag + "\n";
for (std::map < std::string, std::string >::iterator iter = attributes.begin(); iter != attributes.end(); iter++) {
ret += spacing + "at[" + iter->first + "]=" + iter->second + "\n";
}
std::string piece = data.substr(0,10) + " ...";
ret += spacing + "Data: " + escapeStrings(piece) + "\n";
for (unsigned int i = 0; i < children.size(); i++) {
ret += children[i].toString(sp + 1);
}
return ret;
}