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