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);
58 buffer += (char) acsii;
60 if (acsii == 32 || i == this->length() - 1) {
61 array.push_back(buffer);
73XAttrib::XAttrib(
const std::string& str) {
74 size_t index = str.find(
'=');
75 name = str.substr(0, index);
76 value = str.substr(index + 2, str.substr(index + 2).size() -
80XAttrib::XAttrib(
const char* _name,
const char* _value) {
81 this->name = std::string(_name);
82 this->value = std::string(_value);
85std::string XAttrib::toString()
const {
86 return (name +
"=\"" + value +
"\"");
91const XNode* XNode::_getNode(
const char* node_name)
const {
92 if (strcmp(node_name, this->name.c_str()) == 0)
95 for (
unsigned int i = 0; i < this->children.size(); i++) {
96 const XNode* res = this->children.at(i)._getNode(node_name);
97 if (res != NULL)
return res;
104XNode::_getNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
105 if (node_name == NULL || strcmp(node_name, this->name.c_str()) == 0) {
106 for (
unsigned int i = 0; i < this->attributes.size(); i++) {
107 if (attrib_name == NULL || strcmp(attrib_name, this->attributes.at(i).name.c_str()) == 0) {
108 if (attrib_value == NULL || strcmp(attrib_value, this->attributes.at(i).value.c_str()) == 0) {
115 for (
unsigned int i = 0; i < this->children.size(); i++) {
116 const XNode* res = this->children.at(i)._getNodeByAttribute(node_name, attrib_name, attrib_value);
117 if (res != NULL)
return res;
127 this->attributes = std::vector<XAttrib>();
128 this->children = std::vector<XNode>();
131void XNode::print() const
133 printf(
"%s: [%s] ", name.c_str(), value.c_str());
134 for (
unsigned int i = 0; i < this->attributes.size(); i++)
135 printf(
"%s ", this->attributes.at(i).toString().c_str());
139const XNode& XNode::getNode(
const char* name)
const {
140 const XNode* res = this->_getNode(name);
142 printf(
"Error: Node [%s] not found.", name);
148const XAttrib& XNode::getAttribute(
const char* name)
const {
149 for (
unsigned int i = 0; i < this->attributes.size(); i++) {
150 if (strcmp(this->attributes.at(i).name.c_str(), name) == 0)
return this->attributes.at(i);
153 printf(
"Warning: Attribute [%s] not found.", name);
157bool XNode::hasAttribute(
const char* name)
const {
158 for (
const auto & attribute : this->attributes) {
159 if (strcmp(attribute.name.c_str(), name) == 0)
return true;
164const XNode& XNode::getNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
165 const XNode* res = this->_getNodeByAttribute(node_name, attrib_name, attrib_value);
167 printf(
"Warning: Node with ");
168 if (node_name != NULL) printf(
"name \"%s\" and ", node_name);
169 printf(
"attribute [");
170 printf(
"%s=", attrib_name == NULL ?
"\"\"" : attrib_name);
171 printf(
"%s", attrib_value == NULL ?
"\"\"" : attrib_value);
172 printf(
"] not found.\n");
178bool XNode::hasNodeByAttribute(
const char* node_name,
const char* attrib_name,
const char* attrib_value)
const {
179 const XNode* res = this->_getNodeByAttribute(node_name, attrib_name, attrib_value);
180 return res !=
nullptr;
183const XNode& XNode::getChild(
const char* name)
const {
184 for (
unsigned int i = 0; i < this->children.size(); i++) {
185 if (strcmp(this->children[i].name.c_str(), name) == 0)
return this->children[i];
188 printf(
"Error: Node [%s] not found.\n", name);
192bool XNode::hasChild(
const char* name)
const {
193 for (
const auto & child : this->children) {
194 if (strcmp(child.name.c_str(), name) == 0)
return true;
201int XFILE::read(std::string& buffer) {
203 for (; file_index < file.length(); file_index++) {
205 char c = file[file_index];
213 }
else if (c ==
'<' && buffer.length() > 0) {
217 if (c ==
' ' && buffer.length() == 0)
226const XNode* XFILE::loadBuffer(std::string buffer) {
232 return this->getRoot();
235XFILE::XFILE(
const char* str) {
236 this->file = CSE::AssetMgr::LoadAssetFile(str);
239 printf(
"XML file [%s] not found!\n", str);
245const XNode* XFILE::getRoot() {
247 XNode* current = root;
251 while (ret = read(buffer), ret != XML_EOF)
253 if (buffer[0] ==
'?')
256 else if (buffer[0] ==
'/')
258 if (buffer.substr(1) != current->name)
259 printf(
"[WARNING]: Name mismatch: [%s] vs [%s]\n", buffer.substr(1).c_str(), current->name.c_str());
260 current = current->parent;
261 }
else if (ret == XML_VALUE) {
262 current->value = buffer;
264 else if (buffer.find(
' ') == std::string::npos)
268 node.sub_index = file_index;
269 node.parent = current;
270 current->children.push_back(node);
272 if (buffer.back() !=
'/')
273 current = ¤t->children.back();
275 current->children.back().name.pop_back();
278 char* str = (
char*) malloc(buffer.length() + 1);
280 strcpy(str, buffer.c_str());
282 char* token = strtok(str,
" ");
284 node.sub_index = file_index;
286 node.parent = current;
290 token = strtok(NULL,
" ");
294 if (token[strlen(token) - 1] ==
'/')
295 token[strlen(token) - 1] =
'\0';
298 node.attributes.push_back(attribute);
301 current->children.push_back(node);
302 if (buffer.back() !=
'/')
303 current = ¤t->children.back();