From 75d85549c6d2a5284593e20c21d61fc5d6200bca Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 8 Jun 2020 18:24:49 +0200 Subject: Add 'insert before id' to create and move commands. --- src/nodetree.cc | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'src/nodetree.cc') diff --git a/src/nodetree.cc b/src/nodetree.cc index 38c20e1..eac919e 100644 --- a/src/nodetree.cc +++ b/src/nodetree.cc @@ -39,9 +39,7 @@ static inline std::string id2str(nodeid_t id) { - char buf[32]; - sprintf(buf, "%u", id); - return buf; + return std::to_string(id); } std::string Node::toXML(std::string prefix) @@ -110,7 +108,8 @@ nodeid_t NodeTree::createId() static nodeid_t rootid = -1; -NodeIdList NodeTree::insertAsChild(nodeid_t parentid, nodeid_t id, node_t data) +NodeIdList NodeTree::insertAsChild(nodeid_t parentid, nodeid_t id, + node_t data, nodeid_t insertBeforeId) throw (std::exception) { NodeIdList affectedNodes; @@ -132,7 +131,7 @@ NodeIdList NodeTree::insertAsChild(nodeid_t parentid, nodeid_t id, node_t data) Node* child = createNode(id); //DEBUG(nodetree, "!!!!!!!id in insert: %d\n", data.id); child->data = data; - insertChild(parent, child); + insertChild(parent, child, insertBeforeId); //affectedNodes.push_back(parentid); affectedNodes.push_back(id); @@ -203,7 +202,7 @@ static bool findNode(Node *needle, Node *haystack) return false; } -NodeIdList NodeTree::move(nodeid_t id, nodeid_t toid) +NodeIdList NodeTree::move(nodeid_t id, nodeid_t toid, nodeid_t beforeId) throw (std::exception) { NodeIdList affectedNodes; @@ -225,7 +224,22 @@ NodeIdList NodeTree::move(nodeid_t id, nodeid_t toid) } child->parent->children.remove(child); - newparent->children.push_back(child); + bool inserted{false}; + for(auto it = newparent->children.begin(); + it != newparent->children.end(); + ++it) + { + if((*it)->id == beforeId) + { + newparent->children.insert(it, child); + inserted = true; + } + } + if(!inserted) + { + // beforeId was not found in parent - inserting last instead. + newparent->children.push_back(child); + } child->parent = newparent; @@ -370,11 +384,27 @@ Node* NodeTree::createNode(nodeid_t id) return node; } -void NodeTree::insertChild(Node* parent, Node* child) +void NodeTree::insertChild(Node* parent, Node* child, nodeid_t insertBeforeId) { if(parent) { - parent->children.push_back(child); + bool inserted{false}; + for(auto it = parent->children.begin(); + it != parent->children.end(); + ++it) + { + if((*it)->id == insertBeforeId) + { + parent->children.insert(it, child); + inserted = true; + } + } + if(!inserted) + { + // beforeId was not found in parent - inserting last instead. + parent->children.push_back(child); + } + } else { @@ -391,7 +421,7 @@ static void printNode(Node* node, std::string prefix) return; } node_t t = node->data; - DEBUG(nodetree, "%s- %u - %s (%p)\n", prefix.c_str(), node->id, + DEBUG(nodetree, "%s- %u - %s (%p)\n", prefix.c_str(), (int)node->id, t.attributes["title"].c_str(), node); NodeList::iterator it; @@ -420,8 +450,8 @@ std::string NodeTree::toXML() return xml; } -void NodeTree::fromXML(std::string xml) +void NodeTree::fromXML(const std::string& xml) { XmlParser p(this); - p.parse(xml.c_str(), xml.size()); + p.parse(xml.data(), xml.size()); } -- cgit v1.2.3