1#include "../../AssetsDef.h"
3#include "../../../Manager/AssetMgr.h"
9XValue::XValue(
const std::string& str) : std::string(str) {}
11XValue& XValue::operator=(
const std::string& str) {
16std::vector<float> XValue::toFloatVector()
const {
17 std::string buffer =
"";
18 std::vector<float> array;
20 for (
unsigned int i = 0; i < this->length(); i++) {
21 int acsii = (int) this->at(i);
22 buffer += (char) acsii;
24 if (acsii == 32 || i == this->length() - 1) {
25 float f = atof(buffer.c_str());
34std::vector<int> XValue::toIntegerVector()
const {
35 std::string buffer =
"";
36 std::vector<int> array;
38 for (
unsigned int i = 0; i < this->length(); i++) {
39 int acsii = (int) this->at(i);
40 buffer += (char) acsii;
42 if (acsii == 32 || i == this->length() - 1) {
43 float f = atoi(buffer.c_str());
52std::vector<std::string> XValue::toStringVector()
const {
53 std::string buffer =
"";
54 std::vector<std::string> array;
56 for (
unsigned int i = 0; i < this->length(); i++) {
57 int acsii = (int) this->at(i);
59 buffer += (char) acsii;
61 if (acsii == 32 || i == this->length() - 1) {
62 array.push_back(buffer);
74XAttrib::XAttrib(
const std::string& str) {
75 size_t index = str.find(
'=');
76 name = str.substr(0, index);
77 value = str.substr(index + 2, str.substr(index + 2).size() -
81XAttrib::XAttrib(
const char* _name,
const char* _value) {
82 this->name = std::string(_name);
83 this->value = std::string(_value);
86std::string XAttrib::toString()
const {
87 return (name +
"=\"" + value +
"\"");
92const XNode* XNode::_getNode(
const char* node_name)
const {
93 if (strcmp(node_name, this->name.c_str()) == 0)
96 for (
unsigned int i = 0; i < this->children.size(); i++) {
97 const XNode* res = this->children.at(i)._getNode(node_name);
98 if (res != NULL)
return res;
105XNode::_getNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
106 if (node_name == NULL || strcmp(node_name, this->name.c_str()) == 0) {
107 for (
unsigned int i = 0; i < this->attributes.size(); i++) {
108 if (attrib_name == NULL || strcmp(attrib_name, this->attributes.at(i).name.c_str()) == 0) {
109 if (attrib_value == NULL || strcmp(attrib_value, this->attributes.at(i).value.c_str()) == 0) {
116 for (
unsigned int i = 0; i < this->children.size(); i++) {
117 const XNode* res = this->children.at(i)._getNodeByAttribute(node_name, attrib_name, attrib_value);
118 if (res != NULL)
return res;
128 this->attributes = std::vector<XAttrib>();
129 this->children = std::vector<XNode>();
132void XNode::print() const
134 printf(
"%s: [%s] ", name.c_str(), value.c_str());
135 for (
unsigned int i = 0; i < this->attributes.size(); i++)
136 printf(
"%s ", this->attributes.at(i).toString().c_str());
140const XNode& XNode::getNode(
const char* name)
const {
141 const XNode* res = this->_getNode(name);
143 printf(
"Error: Node [%s] not found.", name);
149const XAttrib& XNode::getAttribute(
const char* name)
const {
150 for (
unsigned int i = 0; i < this->attributes.size(); i++) {
151 if (strcmp(this->attributes.at(i).name.c_str(), name) == 0)
return this->attributes.at(i);
154 printf(
"Warning: Attribute [%s] not found.", name);
158bool XNode::hasAttribute(
const char* name)
const {
159 for (
const auto & attribute : this->attributes) {
160 if (strcmp(attribute.name.c_str(), name) == 0)
return true;
165const XNode& XNode::getNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
166 const XNode* res = this->_getNodeByAttribute(node_name, attrib_name, attrib_value);
168 printf(
"Warning: Node with ");
169 if (node_name != NULL) printf(
"name \"%s\" and ", node_name);
170 printf(
"attribute [");
171 printf(
"%s=", attrib_name == NULL ?
"\"\"" : attrib_name);
172 printf(
"%s", attrib_value == NULL ?
"\"\"" : attrib_value);
173 printf(
"] not found.\n");
179bool XNode::hasNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
180 const XNode* res = this->_getNodeByAttribute(node_name, attrib_name, attrib_value);
181 return res !=
nullptr;
184const XNode& XNode::getChild(
const char* name)
const {
185 for (
unsigned int i = 0; i < this->children.size(); i++) {
186 if (strcmp(this->children[i].name.c_str(), name) == 0)
return this->children[i];
189 printf(
"Error: Node [%s] not found.\n", name);
193bool XNode::hasChild(
const char* name)
const {
194 for (
const auto & child : this->children) {
195 if (strcmp(child.name.c_str(), name) == 0)
return true;
202int XFILE::read(std::string& buffer) {
204 for (; file_index < file.length(); file_index++) {
206 char c = file[file_index];
214 }
else if (c ==
'<' && buffer.length() > 0) {
218 if (c ==
' ' && buffer.length() == 0)
227const XNode* XFILE::loadBuffer(std::string buffer) {
233 return this->getRoot();
236XFILE::XFILE(
const char* str) {
237 this->file = CSE::AssetMgr::LoadAssetFile(str);
240 printf(
"XML file [%s] not found!\n", str);
246const XNode* XFILE::getRoot() {
248 XNode* current = root;
252 while (ret = read(buffer), ret != XML_EOF)
254 if (buffer[0] ==
'?')
257 else if (buffer[0] ==
'/')
259 if (buffer.substr(1) != current->name)
260 printf(
"[WARNING]: Name mismatch: [%s] vs [%s]\n", buffer.substr(1).c_str(), current->name.c_str());
261 current = current->parent;
262 }
else if (ret == XML_VALUE) {
263 current->value = buffer;
265 else if (buffer.find(
' ') == std::string::npos)
269 node.sub_index = file_index;
270 node.parent = current;
271 current->children.push_back(node);
273 if (buffer.back() !=
'/')
274 current = ¤t->children.back();
276 current->children.back().name.pop_back();
279 char* str = (
char*) malloc(buffer.length() + 1);
281 strcpy(str, buffer.c_str());
283 char* token = strtok(str,
" ");
285 node.sub_index = file_index;
287 node.parent = current;
291 token = strtok(NULL,
" ");
295 if (token[strlen(token) - 1] ==
'/')
296 token[strlen(token) - 1] =
'\0';
299 node.attributes.push_back(attribute);
302 current->children.push_back(node);
303 if (buffer.back() !=
'/')
304 current = ¤t->children.back();