-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.cpp
278 lines (234 loc) · 5.88 KB
/
Object.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
#include <sstream>
#include <string.h>
#include "Object.hpp"
bool ActNut::Object::_printFullNames = false;
void ActNut::Object::printFullNames(bool value)
{
_printFullNames = value;
}
ActNut::Object::Object(const Object* parent, const char* type, const std::string& name)
: parent(parent), type(type), name(name)
{}
void ActNut::Object::addMember(Object* member)
{
this->members.push_back(member);
}
const ActNut::Object* ActNut::Object::getParent() const
{
return this->parent;
}
const std::string& ActNut::Object::getName() const
{
return this->name;
}
void ActNut::Object::setName(const std::string& name)
{
this->name = name;
}
uint32_t ActNut::Object::getNumType() const
{
return 0xFFFFFFFF;
}
std::string ActNut::Object::getFullName() const
{
if (this->parent)
return this->parent->getFullName() + "/" + this->name;
else
return "";
}
int ActNut::Object::getIndentLevel() const
{
if (this->parent)
return this->parent->getIndentLevel() + 1;
else
return 0;
}
std::string ActNut::Object::printIndent(int indentLevel) const
{
if (indentLevel == -1)
indentLevel = this->getIndentLevel();
std::ostringstream ss;
for (int i = 0; i < indentLevel; i++)
ss << " ";
return ss.str();
}
bool ActNut::Object::writeToFile(const std::string& filename) const
{
FileBuffer buf(filename, std::ios_base::out);
return this->writeValue(buf);
}
std::ostream& ActNut::operator<<(std::ostream& os, const Object& o)
{
if (ActNut::Object::_printFullNames == false)
{
if (o.getName().empty() == false && o.getName()[0])
os << o.getName() << ": ";
}
else
os << o.getFullName() << ": ";
o.print(os);
return os;
}
ActNut::Object* ActNut::Object::operator[](const char* key)
{
for (Object* it : this->members)
if (it->getName() == key)
return it;
return nullptr;
}
const ActNut::Object* ActNut::Object::operator[](const char* key) const
{
for (Object* it : this->members)
if (it->getName() == key)
return it;
return nullptr;
}
ActNut::Object* ActNut::Object::getChild(const char* _path)
{
char* path = strdup(_path);
char* path_cur = path;
char* path_next;
ActNut::Object* obj;
obj = this;
while (path_cur)
{
while (*path_cur == '/')
path_cur++;
path_next = strchr(path_cur, '/');
if (path_next)
{
*path_next = '\0';
path_next++;
}
obj = (*obj)[path_cur];
if (!obj)
break ;
path_cur = path_next;
}
free(path);
return obj;
}
const ActNut::Object* ActNut::Object::getChild(const char* _path) const
{
char* path = strdup(_path);
char* path_cur = path;
char* path_next;
const ActNut::Object* obj;
obj = this;
while (path_cur)
{
while (*path_cur == '/')
path_cur++;
path_next = strchr(path_cur, '/');
if (path_next)
{
*path_next = '\0';
path_next++;
}
obj = (*obj)[path_cur];
if (!obj)
break ;
path_cur = path_next;
}
free(path);
return obj;
}
const ActNut::Object& ActNut::Object::operator=(const std::string&)
{
Error::error(std::string(this->type) + " doesn't support operator=");
return *this;
}
ActNut::String::String(const Object* parent, const char* type, const std::string& name)
: Object(parent, type, name)
{}
bool ActNut::String::readValue(IBuffer& buf)
{
uint32_t length = buf.readInt();
char* str = new char[length];
buf.readBytes((uint8_t*)str, length);
this->value = std::string(str, length);
delete[] str;
return true;
}
void ActNut::String::print(std::ostream& os) const
{
os << '"' << this->value << '"';
}
bool ActNut::String::writeValue(IBuffer& buf) const
{
buf.writeInt(this->value.length());
return buf.writeBytes((const uint8_t*)this->value.c_str(), this->value.length());
}
const ActNut::Object& ActNut::String::operator=(const std::string& new_value)
{
this->value = new_value;
return *this;
}
bool ActNut::String::operator==(const std::string& other) const
{
return this->value == other;
}
ActNut::vector::~vector()
{
for (ActNut::Object* it : *this)
delete it;
}
bool ActNut::vector::readValue(IBuffer&)
{
throw std::logic_error("Calling readValue doesn't make sense on an Act::vector");
}
bool ActNut::vector::writeValue(IBuffer&) const
{
throw std::logic_error("Calling writeValue doesn't make sense on an Act::vector");
}
void ActNut::vector::print(std::ostream& os) const
{
os << "[";
if (this->size() != 0)
os << std::endl;
for (Object* it : *this)
os << printIndent() << *it << std::endl;
os << (this->size() != 0 ? printIndent(this->getIndentLevel() - 1) : "") << " ]" << std::endl;
}
ActNut::Object* ActNut::vector::operator[](size_type n)
{
return this->std::vector<Object*>::operator[](n);
}
const ActNut::Object* ActNut::vector::operator[](size_type n) const
{
return this->std::vector<Object*>::operator[](n);
}
ActNut::Object* ActNut::vector::operator[](const char* key)
{
// First, try by index
char* end;
long idx = strtol(key, &end, 10);
if (!*end && 0 < idx && idx < (long)this->size())
return (*this)[idx];
// Then, search in our registered members (but we probably don't have any, we are a vector)
Object* obj = this->Object::operator[](key);
if (obj != nullptr)
return obj;
// Finally, search if one of our elements has a key with this name
for (Object* it : *this)
if (it->getName() == key)
return it;
return nullptr;
}
const ActNut::Object* ActNut::vector::operator[](const char* key) const
{
// First, try by index
char* end;
long idx = strtol(key, &end, 10);
if (!*end && 0 < idx && idx < (long)this->size())
return (*this)[idx];
// Then, search in our registered members (but we probably don't have any, we are a vector)
const Object* obj = this->Object::operator[](key);
if (obj != nullptr)
return obj;
// Finally, search if one of our elements has a key with this name
for (Object* it : *this)
if (it->getName() == key)
return it;
return nullptr;
}