blob: 4b8247f38a69a1458cd0f6616a3216177859ec64 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/* -*- 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 <vector>
#include <iostream>
#include <QDomDocument>
/*
<?xml version="1.0" encoding="UTF-8"?>
<dgedit version="1.0">
<project_name>MyProject</project_name>
<raw_file_root>/some/path</raw_file_root>
</dgedit>
*/
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<DomHelper> children()
{
std::vector<DomHelper> 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;
}
|