CSEngine
Loading...
Searching...
No Matches
XFILE Class Reference

Public Member Functions

 XFILE (const char *str)
 
const XNodegetRoot ()
 
const XNodeloadBuffer (std::string buffer)
 

Detailed Description

Definition at line 77 of file XML.h.

Constructor & Destructor Documentation

◆ XFILE() [1/2]

XFILE::XFILE ( )
inline

Definition at line 87 of file XML.h.

87{}

◆ XFILE() [2/2]

XFILE::XFILE ( const char *  str)

Definition at line 235 of file XML.cpp.

235 { // read the file into the node heirchy
236 this->file = CSE::AssetMgr::LoadAssetFile(str);
237 if (file.empty()) {
238#ifndef __ANDROID__
239 printf("XML file [%s] not found!\n", str);
240#endif
241 throw -1;
242 }
243}

◆ ~XFILE()

XFILE::~XFILE ( )

Definition at line 312 of file XML.cpp.

312 {
313// fclose(this->file);
314}

Member Function Documentation

◆ getRoot()

const XNode * XFILE::getRoot ( )

Definition at line 245 of file XML.cpp.

245 {
246 XNode* root = new XNode();
247 XNode* current = root;
248
249 std::string buffer;
250 int ret = 0;
251 while (ret = read(buffer), ret != XML_EOF) // read the next node/value
252 {
253 if (buffer[0] == '?') // comment
254 continue;
255
256 else if (buffer[0] == '/') // end of node
257 {
258 if (buffer.substr(1) != current->name) // confirm the node names match
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) { // if this is a value string, we simply set it to the current node
262 current->value = buffer;
263 }
264 else if (buffer.find(' ') == std::string::npos) // this node has no attributes, simply append it to the children
265 {
266 XNode node = XNode();
267 node.name = buffer;
268 node.sub_index = file_index;
269 node.parent = current;
270 current->children.push_back(node);
271
272 if (buffer.back() != '/') // this node does not instant terminate, set it to the current node
273 current = &current->children.back();
274 else // this node does instant terminate, make sure you remove the '/' at the end
275 current->children.back().name.pop_back();
276 } else // this node does have attributes
277 {
278 char* str = (char*) malloc(buffer.length() + 1); // allocate and initalize the buffer
279 if (!str) throw -1;
280 strcpy(str, buffer.c_str());
281
282 char* token = strtok(str, " "); // start tokenizing
283 XNode node = XNode();
284 node.sub_index = file_index;
285 node.name = token; // the first token is the name
286 node.parent = current;
287
288 while (true) // parse the attribute tokens
289 {
290 token = strtok(NULL, " "); // get the next token
291 if (token == NULL)
292 break;
293
294 if (token[strlen(token) - 1] == '/') // if the current token ends with a '/', then remove it
295 token[strlen(token) - 1] = '\0';
296
297 XAttrib attribute = XAttrib(std::string(token));
298 node.attributes.push_back(attribute);
299 }
300
301 current->children.push_back(node);
302 if (buffer.back() != '/') // if this node does not instant-terminate, set it to the current node
303 current = &current->children.back();
304 free(str);
305 }
306// current->file_end = file_index;
307 }
308
309 return root;
310}
Definition XML.h:29
Definition XML.h:43

◆ loadBuffer()

const XNode * XFILE::loadBuffer ( std::string  buffer)

Definition at line 226 of file XML.cpp.

226 {
227 this->file = buffer;
228 if (file.empty()) {
229 throw -1;
230 }
231
232 return this->getRoot();
233}

The documentation for this class was generated from the following files: