Changeset View
Changeset View
Standalone View
Standalone View
source/ps/DataTreeXML.h
- This file was added.
/* Copyright (C) 2020 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#ifndef INCLUDED_DATATREEXML | |||||
#define INCLUDED_DATATREEXML | |||||
#include "DataTree.h" | |||||
#include <libxml/tree.h> | |||||
class DataTreeXMLImpl | |||||
{ | |||||
protected: | |||||
struct NodeWrapper | |||||
{ | |||||
NodeWrapper(xmlNodePtr p) : node(p) {} | |||||
NodeWrapper begin() | |||||
{ | |||||
if (!node->children || node->children->type == XML_ELEMENT_NODE) | |||||
return node->children; | |||||
return ++NodeWrapper(node->children); | |||||
} | |||||
NodeWrapper end() { return nullptr; }; | |||||
NodeWrapper begin() const | |||||
{ | |||||
if (!node->children || node->children->type == XML_ELEMENT_NODE) | |||||
return node->children; | |||||
return ++NodeWrapper(node->children); | |||||
} | |||||
NodeWrapper end() const { return nullptr; }; | |||||
NodeWrapper& operator++() | |||||
{ | |||||
do | |||||
node = node->next; | |||||
while (node && node->type != XML_ELEMENT_NODE); | |||||
return *this; | |||||
} | |||||
bool operator!=(const NodeWrapper& o) const { return node != o.node; } | |||||
operator xmlNodePtr() const { return node; } | |||||
NodeWrapper& operator*() { return *this; } | |||||
xmlNodePtr operator->() const { return node; } | |||||
xmlNodePtr node; | |||||
}; | |||||
using nodetype = NodeWrapper; | |||||
using doctype = xmlDocPtr; | |||||
protected: | |||||
DataTreeXMLImpl() = default; | |||||
DataTreeXMLImpl(const DataTreeXMLImpl&) = delete; | |||||
DataTreeXMLImpl& operator=(const DataTreeXMLImpl&) = delete; | |||||
DataTreeXMLImpl(DataTreeXMLImpl&& o) | |||||
{ | |||||
std::swap(doc, o.doc); | |||||
} | |||||
DataTreeXMLImpl& operator=(DataTreeXMLImpl&& o) | |||||
{ | |||||
std::swap(doc, o.doc); | |||||
return *this; | |||||
} | |||||
~DataTreeXMLImpl() | |||||
{ | |||||
//if (doc) | |||||
// xmlSaveFile(file.string8().c_str(), doc); | |||||
if (doc) | |||||
xmlFreeDoc(doc); | |||||
} | |||||
bool LoadFile(const PIVFS& vfs, const VfsPath& filename); | |||||
bool CheckForInclude() const; | |||||
bool IsIncludeNode(const nodetype& node) const; | |||||
std::vector<VfsPath> GetIncludeFiles(const nodetype& node) const; | |||||
bool IsReplaceNode(const nodetype& node) const; | |||||
nodetype GetRootNode(const doctype& doc) const; | |||||
nodetype GetSameChild(const nodetype& node, const nodetype& similar_to) const; | |||||
void AddChild(nodetype& node, nodetype& child); | |||||
void DeleteNode(nodetype& node); | |||||
void UpdateNode(nodetype& node_to_update, const nodetype& reference); | |||||
void PostMergeProcessing(nodetype& node); | |||||
doctype GetDoc() { return doc; } | |||||
doctype GetDoc() const { return doc; } | |||||
public: | |||||
void SetDoc(doctype d) | |||||
{ | |||||
if (doc) | |||||
xmlFreeDoc(doc); | |||||
doc = d; | |||||
} | |||||
protected: | |||||
doctype doc = nullptr; | |||||
VfsPath file; | |||||
}; | |||||
using DataTreeXML = DataTree<DataTreeXMLImpl>; | |||||
#endif // INCLUDED_DATATREEXML |
Wildfire Games · Phabricator