diff options
-rw-r--r-- | src/dgxmlparser.cc | 53 | ||||
-rw-r--r-- | test/dgxmlparsertest.cc | 34 |
2 files changed, 56 insertions, 31 deletions
diff --git a/src/dgxmlparser.cc b/src/dgxmlparser.cc index 016ad60..cc76b3e 100644 --- a/src/dgxmlparser.cc +++ b/src/dgxmlparser.cc @@ -58,25 +58,40 @@ static bool assign(std::string& dest, const std::string& val) return true; } -static bool assign(size_t& dest, const std::string& val) +static bool assign(std::size_t& dest, const std::string& val) { int tmp = atoi(val.c_str()); if(tmp < 0) return false; dest = tmp; - return std::to_string(dest) == val; + return std::to_string(dest) == val; +} + +static bool assign(main_state_t& dest, const std::string& val) +{ + if(val != "true" && val != "false") + { + return false; + } + dest = (val == "true") ? main_state_t::is_main : main_state_t::is_not_main; + return true; } template<typename T> -static bool attrcpy(T& dest, const pugi::xml_node& src, const std::string& attr) +static bool attrcpy(T& dest, const pugi::xml_node& src, const std::string& attr, bool opt = false) { - const char* val = src.attribute(attr.c_str()).as_string(nullptr); + const char* val = src.attribute(attr.c_str()).as_string(nullptr); if(!val) { - ERR("Attribute %s not found in %s, offset %s\n", attr.c_str(), src.path().c_str(), (int) src.offset_debug()); - return false; + if(!opt) + { + ERR(dgxmlparser, "Attribute %s not found in %s, offset %d\n", + attr.data(), src.path().data(), (int)src.offset_debug()); + } + return opt; } if(!assign(dest, std::string(val))) { - ERR("Attribute %s could not be assigned, offset %s\n", attr.c_str(), (int) src.offset_debug()); + ERR(dgxmlparser, "Attribute %s could not be assigned, offset %d\n", + attr.data(), (int)src.offset_debug()); return false; } @@ -96,11 +111,12 @@ bool parseDrumkitFile(const std::string& filename, DrumkitDOM& dom) return false; } - //TODO: handle xml version - + //TODO: handle xml version + pugi::xml_node drumkit = doc.child("drumkit"); res &= attrcpy(dom.description, drumkit, "description"); res &= attrcpy(dom.name, drumkit, "name"); + res &= attrcpy(dom.samplerate, drumkit, "samplerate"); pugi::xml_node channels = doc.child("drumkit").child("channels"); for(pugi::xml_node channel: channels.children("channel")) @@ -110,20 +126,21 @@ bool parseDrumkitFile(const std::string& filename, DrumkitDOM& dom) } pugi::xml_node instruments = doc.child("drumkit").child("instruments"); - for(pugi::xml_node instrument: instruments.children("instrument")) + 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"); + res &= attrcpy(dom.instruments.back().group, instrument, "group", true); 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 + dom.instruments.back().channel_map.back().main = main_state_t::unset; + res &= attrcpy(dom.instruments.back().channel_map.back().main, cmap, "main", true); } } @@ -135,14 +152,22 @@ 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()); - + pugi::xml_parse_result result = doc.load_file(filename.data()); res &= !result.status; //TODO: handle version pugi::xml_node instrument = doc.child("instrument"); res &= attrcpy(dom.name, instrument, "name"); + pugi::xml_node channels = instrument.child("channels"); + for(pugi::xml_node channel : channels.children("channel")) + { + dom.instrument_channels.emplace_back(); + res &= attrcpy(dom.instrument_channels.back().name, channel, "name"); + dom.instrument_channels.back().main = main_state_t::unset; + res &= attrcpy(dom.instrument_channels.back().main, channel, "main", true); + } + pugi::xml_node samples = doc.child("instrument").child("samples"); for(pugi::xml_node sample: samples.children("sample")) { diff --git a/test/dgxmlparsertest.cc b/test/dgxmlparsertest.cc index c5bb984..cee034b 100644 --- a/test/dgxmlparsertest.cc +++ b/test/dgxmlparsertest.cc @@ -52,9 +52,9 @@ public: "<?xml version='1.0' encoding='UTF-8'?>\n" \ "<instrument version=\"2.0\" name=\"Snare\">\n" \ " <channels>\n" \ - " <channel name=\"AmbLeft\" main=\"true\">\">\n" \ - " <channel name=\"AmbRight\" main=\"false\">\">\n" \ - " <channel name=\"SnareBottom\">\">\n" \ + " <channel name=\"AmbLeft\" main=\"true\"/>\n" \ + " <channel name=\"AmbRight\" main=\"false\"/>\n" \ + " <channel name=\"SnareBottom\"/>\n" \ " </channels>\n" \ " <samples>\n" \ " <sample name=\"Snare-1\" power=\"0.00985718\">\n" \ @@ -95,11 +95,11 @@ public: CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), s.audiofiles[2].instrument_channel); CPPUNIT_ASSERT_EQUAL(std::string("1-Snare.wav"), s.audiofiles[2].file); - CPPUNIT_ASSERT_EQUAL(std::size_t(3), s.audiofiles[2].filechannel); + CPPUNIT_ASSERT_EQUAL(std::size_t(12), s.audiofiles[2].filechannel); CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), s.audiofiles[3].instrument_channel); CPPUNIT_ASSERT_EQUAL(std::string("1-Snare.wav"), s.audiofiles[3].file); - CPPUNIT_ASSERT_EQUAL(std::size_t(4), s.audiofiles[3].filechannel); + CPPUNIT_ASSERT_EQUAL(std::size_t(13), s.audiofiles[3].filechannel); } { @@ -117,11 +117,11 @@ public: CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), s.audiofiles[2].instrument_channel); CPPUNIT_ASSERT_EQUAL(std::string("2-Snare.wav"), s.audiofiles[2].file); - CPPUNIT_ASSERT_EQUAL(std::size_t(3), s.audiofiles[2].filechannel); + CPPUNIT_ASSERT_EQUAL(std::size_t(12), s.audiofiles[2].filechannel); CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), s.audiofiles[3].instrument_channel); CPPUNIT_ASSERT_EQUAL(std::string("2-Snare.wav"), s.audiofiles[3].file); - CPPUNIT_ASSERT_EQUAL(std::size_t(4), s.audiofiles[3].filechannel); + CPPUNIT_ASSERT_EQUAL(std::size_t(13), s.audiofiles[3].filechannel); } CPPUNIT_ASSERT_EQUAL(std::size_t(3), dom.instrument_channels.size()); @@ -177,13 +177,13 @@ public: CPPUNIT_ASSERT_EQUAL(std::string("AmbLeft-in"), instr.channel_map[0].in); CPPUNIT_ASSERT_EQUAL(std::string("AmbRight-in"), instr.channel_map[1].in); - CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom-in"), instr.channel_map[2].in); - CPPUNIT_ASSERT_EQUAL(std::string("SnareTop-in"), instr.channel_map[3].in); + CPPUNIT_ASSERT_EQUAL(std::string("SnareTop-in"), instr.channel_map[2].in); + CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom-in"), instr.channel_map[3].in); CPPUNIT_ASSERT_EQUAL(std::string("AmbLeft"), instr.channel_map[0].out); CPPUNIT_ASSERT_EQUAL(std::string("AmbRight"), instr.channel_map[1].out); - CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), instr.channel_map[2].out); - CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), instr.channel_map[3].out); + CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), instr.channel_map[2].out); + CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), instr.channel_map[3].out); CPPUNIT_ASSERT(main_state_t::is_main == instr.channel_map[0].main); CPPUNIT_ASSERT(main_state_t::is_main == instr.channel_map[1].main); @@ -199,13 +199,13 @@ public: CPPUNIT_ASSERT_EQUAL(std::string("AmbLeft2-in"), instr.channel_map[0].in); CPPUNIT_ASSERT_EQUAL(std::string("AmbRight2-in"), instr.channel_map[1].in); - CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom2-in"), instr.channel_map[2].in); - CPPUNIT_ASSERT_EQUAL(std::string("SnareTop2-in"), instr.channel_map[3].in); + CPPUNIT_ASSERT_EQUAL(std::string("SnareTop2-in"), instr.channel_map[2].in); + CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom2-in"), instr.channel_map[3].in); CPPUNIT_ASSERT_EQUAL(std::string("AmbLeft"), instr.channel_map[0].out); CPPUNIT_ASSERT_EQUAL(std::string("AmbRight"), instr.channel_map[1].out); - CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), instr.channel_map[2].out); - CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), instr.channel_map[3].out); + CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), instr.channel_map[2].out); + CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), instr.channel_map[3].out); CPPUNIT_ASSERT(main_state_t::is_not_main == instr.channel_map[0].main); CPPUNIT_ASSERT(main_state_t::is_not_main == instr.channel_map[1].main); @@ -216,8 +216,8 @@ public: CPPUNIT_ASSERT_EQUAL(std::size_t(4), dom.channels.size()); CPPUNIT_ASSERT_EQUAL(std::string("AmbLeft"), dom.channels[0].name); CPPUNIT_ASSERT_EQUAL(std::string("AmbRight"), dom.channels[1].name); - CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), dom.channels[2].name); - CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), dom.channels[3].name); + CPPUNIT_ASSERT_EQUAL(std::string("SnareTop"), dom.channels[2].name); + CPPUNIT_ASSERT_EQUAL(std::string("SnareBottom"), dom.channels[3].name); } }; |