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
135
136
137
138
139
140
141
142
143
144
145
|
#include "dgxmlparser.h"
#include <unordered_map>
#include <pugixml.hpp>
#include "nolocale.h"
bool probeDrumkitFile(const std::string& filename)
{
DrumkitDOM d;
return parseDrumkitFile(filename, d);
}
bool probeInstrumentFile(const std::string& filename)
{
InstrumentDOM d;
return parseInstrumentFile(filename, d);
}
static bool assign(double& dest, std::string val)
{
dest = atof_nol(val.c_str());
return dest > 0;
}
static bool assign(std::string& dest, std::string val)
{
dest = val;
return true;
}
static bool assign(size_t& dest, std::string val)
{
dest = atoi(val.c_str());
return dest > 0;
}
template<typename T>
static bool attrcpy(T& dest, const pugi::xml_node& src, const std::string attr)
{
const char* val = src.attribute(attr.c_str()).as_string(nullptr);
if(!val) return false;
return assign(dest, std::string(val));
}
bool parseDrumkitFile(const std::string& filename, DrumkitDOM& dom)
{
bool res = true;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
res &= !result.status;
pugi::xml_node drumkit = doc.child("drumkit");
res &= attrcpy(dom.description, drumkit, "description");
res &= attrcpy(dom.name, drumkit, "name");
pugi::xml_node channels = doc.child("drumkit").child("channels");
for(pugi::xml_node channel: channels.children("channel"))
{
dom.channels.emplace_back();
res &= attrcpy(dom.channels.back().name, channel, "name");
}
pugi::xml_node instruments = doc.child("drumkit").child("instruments");
for(pugi::xml_node instrument: instruments.children("instrument"))
{
dom.instruments.emplace_back();
res &= attrcpy(dom.instruments.back().name, instrument, "name");
res &= attrcpy(dom.instruments.back().file, instrument, "file");
res &= attrcpy(dom.instruments.back().group, instrument, "group");
for(pugi::xml_node cmap: instrument.children("channelmap"))
{
dom.instruments.back().channel_map.emplace_back();
res &= attrcpy(dom.instruments.back().channel_map.back().in, cmap, "in");
res &= attrcpy(dom.instruments.back().channel_map.back().out, cmap, "out");
}
}
return res;
}
bool parseInstrumentFile(const std::string& filename, InstrumentDOM& dom)
{
bool res = true;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
res &= !result.status;
pugi::xml_node instrument = doc.child("instrument");
res &= attrcpy(dom.name, instrument, "name");
pugi::xml_node samples = doc.child("instrument").child("samples");
for(pugi::xml_node sample: samples.children("sample"))
{
dom.samples.emplace_back();
res &= attrcpy(dom.samples.back().name, sample, "name");
res &= attrcpy(dom.samples.back().power, sample, "power");
for(pugi::xml_node audiofile: sample.children("audiofile"))
{
dom.samples.back().audiofiles.emplace_back();
res &= attrcpy(dom.samples.back().audiofiles.back().instrument_channel, audiofile, "channel");
res &= attrcpy(dom.samples.back().audiofiles.back().file, audiofile, "file");
res &= attrcpy(dom.samples.back().audiofiles.back().filechannel, audiofile, "filechannel");
}
}
return res;
}
|