246 {
248 XNode* current = root;
249
250 std::string buffer;
251 int ret = 0;
252 while (ret = read(buffer), ret != XML_EOF)
253 {
254 if (buffer[0] == '?')
255 continue;
256
257 else if (buffer[0] == '/')
258 {
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;
264 }
265 else if (buffer.find(' ') == std::string::npos)
266 {
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() != '/')
274 current = ¤t->children.back();
275 else
276 current->children.back().name.pop_back();
277 } else
278 {
279 char* str = (char*) malloc(buffer.length() + 1);
280 if (!str) throw -1;
281 strcpy(str, buffer.c_str());
282
283 char* token = strtok(str, " ");
285 node.sub_index = file_index;
286 node.name = token;
287 node.parent = current;
288
289 while (true)
290 {
291 token = strtok(NULL, " ");
292 if (token == NULL)
293 break;
294
295 if (token[strlen(token) - 1] == '/')
296 token[strlen(token) - 1] = '\0';
297
299 node.attributes.push_back(attribute);
300 }
301
302 current->children.push_back(node);
303 if (buffer.back() != '/')
304 current = ¤t->children.back();
305 free(str);
306 }
307
308 }
309
310 return root;
311}