/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * projectserialiser.cc * * Sat May 12 10:11:22 CEST 2018 * Copyright 2018 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of DrumGizmo. * * DrumGizmo 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. * * DrumGizmo 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 DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "projectserialiser.h" #include #include #include /* MyProject /some/path */ class DomHelper { public: DomHelper(const QDomDocument& doc) : elem(doc.documentElement()) {} DomHelper(const QDomElement& elem) : elem(elem) {} DomHelper operator()(const QString& name) { auto node = elem.namedItem(name); if(!node.isElement()) { std::cout << "No such child-element: '" << name.toStdString() << "'\n"; QDomElement e; return DomHelper(e); } return DomHelper(node.toElement()); } QString operator[](const QString& name) { auto attrs = elem.attributes(); auto node = attrs.namedItem(name); if(!node.isAttr()) { std::cout << "No such attribute: '" << name.toStdString() << "'\n"; return ""; } return node.toAttr().value(); } QString text() { return elem.text(); } std::vector children() { std::vector children; return children; } private: const QDomElement& elem; }; QString ProjectSerialiser::serialise(const Project& project) { QDomDocument doc; auto header = doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); doc.appendChild(header); auto dgedit = doc.createElement("dgedit"); doc.appendChild(dgedit); dgedit.setAttribute("version", "1.0"); auto project_name = doc.createElement("project_name"); project_name.appendChild(doc.createTextNode(project.project_name)); dgedit.appendChild(project_name); auto raw_file_root = doc.createElement("raw_file_root"); raw_file_root.appendChild(doc.createTextNode(project.raw_file_root)); dgedit.appendChild(raw_file_root); return doc.toString(); } bool ProjectSerialiser::deserialise(const QString& data, Project& project) { QDomDocument doc; if(!doc.setContent(data)) { std::cout << "Could not parse XML\n"; return false; } project.reset(); DomHelper dom(doc); QString version = dom["version"]; if(version != "1.0") { std::cout << "Bad version: '" << version.toStdString() << "'\n"; return false; } project.project_name = dom("project_name").text(); project.raw_file_root = dom("raw_file_root").text(); return true; }