Merge pull request #586 from jpcima/convert-to-xml
Allow the preprocessor to output as xml
This commit is contained in:
commit
ae39e573bf
2 changed files with 39 additions and 5 deletions
|
|
@ -9,4 +9,4 @@ if(TARGET sfizz::jack AND TARGET Qt5::Widgets)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(sfizz_preprocessor Preprocessor.cpp)
|
add_executable(sfizz_preprocessor Preprocessor.cpp)
|
||||||
target_link_libraries(sfizz_preprocessor PRIVATE sfizz::parser sfizz::cxxopts)
|
target_link_libraries(sfizz_preprocessor PRIVATE sfizz::parser sfizz::pugixml sfizz::cxxopts)
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,18 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "parser/Parser.h"
|
#include "parser/Parser.h"
|
||||||
|
#include <pugixml.hpp>
|
||||||
#include <cxxopts.hpp>
|
#include <cxxopts.hpp>
|
||||||
#include <absl/strings/string_view.h>
|
#include <absl/strings/string_view.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
enum Mode { OutputSFZ, OutputXML };
|
||||||
|
Mode g_mode = OutputSFZ;
|
||||||
|
|
||||||
|
pugi::xml_document g_xml_doc;
|
||||||
|
}
|
||||||
|
|
||||||
class MyParserListener : public sfz::Parser::Listener {
|
class MyParserListener : public sfz::Parser::Listener {
|
||||||
public:
|
public:
|
||||||
explicit MyParserListener(sfz::Parser& parser)
|
explicit MyParserListener(sfz::Parser& parser)
|
||||||
|
|
@ -27,10 +35,20 @@ public:
|
||||||
protected:
|
protected:
|
||||||
void onParseFullBlock(const std::string& header, const std::vector<sfz::Opcode>& opcodes) override
|
void onParseFullBlock(const std::string& header, const std::vector<sfz::Opcode>& opcodes) override
|
||||||
{
|
{
|
||||||
std::cout << '\n';
|
if (g_mode == OutputSFZ) {
|
||||||
std::cout << '<' << header << '>' << '\n';
|
std::cout << '\n';
|
||||||
for (const sfz::Opcode& opc : opcodes)
|
std::cout << '<' << header << '>' << '\n';
|
||||||
std::cout << opc.opcode << '=' << opc.value << '\n';
|
for (const sfz::Opcode& opc : opcodes)
|
||||||
|
std::cout << opc.opcode << '=' << opc.value << '\n';
|
||||||
|
}
|
||||||
|
else if (g_mode == OutputXML) {
|
||||||
|
pugi::xml_node block_node = g_xml_doc.append_child(header.c_str());
|
||||||
|
for (const sfz::Opcode& opc : opcodes) {
|
||||||
|
pugi::xml_node opcode_node = block_node.append_child("opcode");
|
||||||
|
opcode_node.append_attribute("name").set_value(opc.opcode.c_str());
|
||||||
|
opcode_node.append_attribute("value").set_value(opc.value.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onParseError(const sfz::SourceRange& range, const std::string& message) override
|
void onParseError(const sfz::SourceRange& range, const std::string& message) override
|
||||||
|
|
@ -58,6 +76,7 @@ int main(int argc, char *argv[])
|
||||||
options.add_options()
|
options.add_options()
|
||||||
("D,define", "Add external definition", cxxopts::value<std::vector<std::string>>())
|
("D,define", "Add external definition", cxxopts::value<std::vector<std::string>>())
|
||||||
("i,input", "Input SFZ file", cxxopts::value<std::string>())
|
("i,input", "Input SFZ file", cxxopts::value<std::string>())
|
||||||
|
("m,mode", "Mode of operation (sfz, xml)", cxxopts::value<std::string>())
|
||||||
("h,help", "Print usage");
|
("h,help", "Print usage");
|
||||||
|
|
||||||
options.parse_positional({"input"});
|
options.parse_positional({"input"});
|
||||||
|
|
@ -81,6 +100,18 @@ int main(int argc, char *argv[])
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result.count("mode")) {
|
||||||
|
const std::string& modeString = result["mode"].as<std::string>();
|
||||||
|
if (modeString == "sfz")
|
||||||
|
g_mode = OutputSFZ;
|
||||||
|
else if (modeString == "xml")
|
||||||
|
g_mode = OutputXML;
|
||||||
|
else {
|
||||||
|
std::cerr << "Unknown mode of operation: " << modeString << "\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fs::path sfzFilePath { result["input"].as<std::string>() };
|
const fs::path sfzFilePath { result["input"].as<std::string>() };
|
||||||
|
|
||||||
sfz::Parser parser;
|
sfz::Parser parser;
|
||||||
|
|
@ -106,5 +137,8 @@ int main(int argc, char *argv[])
|
||||||
if (parser.getErrorCount() > 0)
|
if (parser.getErrorCount() > 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
if (g_mode == OutputXML)
|
||||||
|
g_xml_doc.save(std::cout);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue