summaryrefslogtreecommitdiff
path: root/src/dgxmlparser.cc
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2018-06-08 19:36:45 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2018-08-12 11:11:39 +0200
commit903b9eed60096cf0389f155a4574d978efdf6a3e (patch)
tree190b655535d1cd1f6d1e937766fe1a70ccab451e /src/dgxmlparser.cc
parente7876ee41c2a51ac5716e61e8af1573b63146f98 (diff)
dgxmlparser: initial xml parser for DrumGizmo xml
Diffstat (limited to 'src/dgxmlparser.cc')
-rw-r--r--src/dgxmlparser.cc145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/dgxmlparser.cc b/src/dgxmlparser.cc
new file mode 100644
index 0000000..9193101
--- /dev/null
+++ b/src/dgxmlparser.cc
@@ -0,0 +1,145 @@
+/* -*- Mode: c++ -*- */
+/***************************************************************************
+ * dgxmlparser.cc
+ *
+ * Fri Jun 8 22:04:31 CEST 2018
+ * Copyright 2018 Jonas Suhr Christensen
+ * jsc@umbraculum.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 Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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 "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;
+
+ //TODO: handle xml version
+
+ 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");
+ //TODO:handle main
+ }
+ }
+
+ 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;
+ //TODO: handle version
+
+ 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;
+}