From 4873e6df45424d2132dc9685f76bcbe2d9dd447e Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 17 May 2012 11:43:16 +0200 Subject: Make persistent store (XML). --- src/tasktree.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) (limited to 'src/tasktree.cc') diff --git a/src/tasktree.cc b/src/tasktree.cc index fc84efa..2576600 100644 --- a/src/tasktree.cc +++ b/src/tasktree.cc @@ -27,10 +27,39 @@ */ #include "tasktree.h" +#include "xmlparser.h" + #include "debug.h" +#include "xml_encode_decode.h" + #define ROOT_PARENT_ID -1 +static inline std::string id2str(taskid_t id) +{ + char buf[32]; + sprintf(buf, "%u", id); + return buf; +} + +std::string node::toXML(std::string prefix) +{ + std::string xml; + + xml += prefix + "\n"; + xml += prefix + " " + xml_encode(data.title) + "\n"; + xml += prefix + " \n"; + NodeList::iterator ni = children.begin(); + while(ni != children.end()) { + xml += (*ni)->toXML(prefix + " "); + ni++; + } + xml += prefix + " \n"; + xml += prefix + "\n"; + + return xml; +} + static void concatTaskIdLists(TaskIdList& pre, TaskIdList& post) { pre.insert(pre.end(), post.begin(), post.end()); // for(TaskIdList::iterator it = post.begin(); @@ -187,7 +216,7 @@ task_t TaskTree::data(taskid_t id) try { node_t* node = id2node.at(id); task_t tmp = node->data; - t.id = tmp.id; + t.id = node->id; t.title = tmp.title; // printf("!!!!t.id and tmp.id in data: %d and %d\n", t.id, tmp.id); if(node->parent) t.parentid = node->parent->id; @@ -267,14 +296,18 @@ node_t* TaskTree::createNode(taskid_t id) { } void TaskTree::insertChild(node_t* parent, node_t* child) { - parent->children.push_back(child); + if(parent) parent->children.push_back(child); + else { + rootid = child->id; + root = child; + } child->parent = parent; } static void printNode(node_t* node, std::string prefix) { if(!node) return; task_t t = node->data; - printf("%s- %u - %s (%p)\n", prefix.c_str(), t.id, t.title.c_str(), node); + printf("%s- %u - %s (%p)\n", prefix.c_str(), node->id, t.title.c_str(), node); NodeList::iterator it; for(it = node->children.begin(); it != node->children.end(); it++) { @@ -287,6 +320,25 @@ void TaskTree::toStdOut() { printNode(root, ""); } +std::string TaskTree::toXML() +{ + node_t *root = id2node.at(rootid); + + std::string xml; + xml += "\n"; + xml += "\n"; + xml += root->toXML(" "); + xml += ""; + + return xml; +} + +void TaskTree::fromXML(std::string xml) +{ + XmlParser p(this); + p.parse(xml.c_str(), xml.size()); +} + #ifdef TEST_TASKTREE //Additional dependency files -- cgit v1.2.3