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 236 of file XML.cpp.

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

◆ ~XFILE()

XFILE::~XFILE ( )

Definition at line 313 of file XML.cpp.

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

Member Function Documentation

◆ getRoot()

const XNode * XFILE::getRoot ( )

Definition at line 246 of file XML.cpp.

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

◆ loadBuffer()

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

Definition at line 227 of file XML.cpp.

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

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