diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-07-25 21:24:38 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-08-12 11:13:57 +0200 |
commit | f2fa9543a11fd5551aa619160d5a02b886fb1bbb (patch) | |
tree | 6c6adcd10be04aca81b283f82920dbd0b880467e | |
parent | 7543b64a995c1a2d3eb598c049919729016ba252 (diff) |
Rewrite MidiMapParser to use pugixml.
-rw-r--r-- | src/audioinputenginemidi.cc | 2 | ||||
-rw-r--r-- | src/configparser.cc | 2 | ||||
-rw-r--r-- | src/midimapparser.cc | 28 | ||||
-rw-r--r-- | src/midimapparser.h | 5 | ||||
-rw-r--r-- | test/Makefile.am | 6 | ||||
-rw-r--r-- | test/midimapparsertest.cc | 4 |
6 files changed, 31 insertions, 16 deletions
diff --git a/src/audioinputenginemidi.cc b/src/audioinputenginemidi.cc index 3ac2a96..6e76f19 100644 --- a/src/audioinputenginemidi.cc +++ b/src/audioinputenginemidi.cc @@ -67,7 +67,7 @@ bool AudioInputEngineMidi::loadMidiMap(const std::string& file, } MidiMapParser midimap_parser; - if(midimap_parser.parseFile(f)) + if(!midimap_parser.parseFile(f)) { return false; } diff --git a/src/configparser.cc b/src/configparser.cc index 0227d15..30b981f 100644 --- a/src/configparser.cc +++ b/src/configparser.cc @@ -39,8 +39,6 @@ bool ConfigParser::parseString(const std::string& xml) return false; } - //TODO: handle xml version - pugi::xml_node config_node = doc.child("config"); for(pugi::xml_node value_node : config_node.children("value")) { diff --git a/src/midimapparser.cc b/src/midimapparser.cc index cc97280..059dfec 100644 --- a/src/midimapparser.cc +++ b/src/midimapparser.cc @@ -26,14 +26,32 @@ */ #include "midimapparser.h" -void MidiMapParser::startTag(const std::string& name, const attr_t& attr) +#include <pugixml.hpp> +#include <hugin.hpp> + +bool MidiMapParser::parseFile(const std::string& filename) { - if(name == "map") + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load_file(filename.data()); + if(result.status) + { + ERR(midimapparser, "XML parse error: %d", (int)result.offset); + return false; + } + + pugi::xml_node midimap_node = doc.child("midimap"); + for(pugi::xml_node map_node : midimap_node.children("map")) { - if((attr.find("note") != attr.end()) && - (attr.find("instr") != attr.end())) + constexpr int bad_value = 10000; + auto note = map_node.attribute("note").as_int(bad_value); + auto instr = map_node.attribute("instr").as_string(); + if(std::string(instr) == "" || note == bad_value) { - midimap[std::stoi(attr.at("note"))] = attr.at("instr"); + continue; } + + midimap[note] = instr; } + + return true; } diff --git a/src/midimapparser.h b/src/midimapparser.h index 8ec76c0..d2f2ddd 100644 --- a/src/midimapparser.h +++ b/src/midimapparser.h @@ -26,14 +26,13 @@ */ #pragma once -#include "saxparser.h" #include "midimapper.h" class MidiMapParser - : public SAXParser { public: - void startTag(const std::string& name, const attr_t& attr) override; + //! Returns false on error, true on success. + bool parseFile(const std::string& filename); midimap_t midimap; }; diff --git a/test/Makefile.am b/test/Makefile.am index a58fba2..7dcfd52 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -230,12 +230,12 @@ configparsertest_SOURCES = \ midimapparsertest_CXXFLAGS = -DOUTPUT=\"midimapparsertest\" \ -I$(top_srcdir)/src -I$(top_srcdir)/include -I$(top_srcdir)/hugin \ - -I$(top_srcdir)/pugixml/src $(EXPAT_CFLAGS) -midimapparsertest_LDFLAGS = $(EXPAT_LIBS) + -I$(top_srcdir)/pugixml/src +midimapparsertest_LDFLAGS = midimapparsertest_SOURCES = \ $(top_srcdir)/hugin/hugin.c \ $(top_srcdir)/src/midimapparser.cc \ - $(top_srcdir)/src/saxparser.cc \ + $(top_srcdir)/pugixml/src/pugixml.cpp \ scopedfile.cc \ midimapparsertest.cc \ dgtest.cc diff --git a/test/midimapparsertest.cc b/test/midimapparsertest.cc index 9af415c..006944c 100644 --- a/test/midimapparsertest.cc +++ b/test/midimapparsertest.cc @@ -53,7 +53,7 @@ public: "</midimap>"); MidiMapParser parser; - DGUNIT_ASSERT(!parser.parseFile(scoped_file.filename())); + DGUNIT_ASSERT(parser.parseFile(scoped_file.filename())); DGUNIT_ASSERT(parser.midimap.find(54) != parser.midimap.end()); DGUNIT_ASSERT(parser.midimap.find(60) != parser.midimap.end()); @@ -81,7 +81,7 @@ public: "</midimap>"); MidiMapParser parser; - DGUNIT_ASSERT(parser.parseFile(scoped_file.filename())); + DGUNIT_ASSERT(!parser.parseFile(scoped_file.filename())); } }; |