From 691708e7fd4929e5d0835978e9a652e118c641f1 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 24 Aug 2022 14:09:59 +0200 Subject: Support reading mux config in JSON --- src/DabMux.cpp | 36 ++++++++++++++++++++---------------- src/utils.cpp | 5 +++++ src/utils.h | 1 + 3 files changed, 26 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/DabMux.cpp b/src/DabMux.cpp index f6a69bb..1a367da 100644 --- a/src/DabMux.cpp +++ b/src/DabMux.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -200,40 +201,29 @@ int main(int argc, char *argv[]) } #endif - - - int returnCode = 0; - ptree pt; std::vector > outputs; try { + string conf_file = ""; + if (argc == 2) { // Assume the only argument is a config file - string conf_file = argv[1]; + conf_file = argv[1]; if (conf_file == "-h") { printUsage(argv[0], stdout); throw MuxInitException("Nothing to do"); } - - try { - read_info(conf_file, pt); - - } - catch (runtime_error &e) { - throw MuxInitException(e.what()); - } } else if (argc > 1 && strncmp(argv[1], "-e", 2) == 0) { // use external config file try { - if (argc != 3) { printUsage(argv[0], stderr); throw MuxInitException(); } - string conf_file = argv[2]; + conf_file = argv[2]; read_info(conf_file, pt); } @@ -241,10 +231,24 @@ int main(int argc, char *argv[]) throw MuxInitException(e.what()); } } - else { + + if (conf_file.empty()) { + printUsage(argv[0], stderr); throw MuxInitException("No configuration file specified"); } + try { + if (stringEndsWith(conf_file, ".json")) { + read_json(conf_file, pt); + } + else { + read_info(conf_file, pt); + } + } + catch (runtime_error &e) { + throw MuxInitException(e.what()); + } + /* Enable Logging to syslog conditionally */ if (pt.get("general.syslog", false)) { etiLog.register_backend(std::make_shared()); diff --git a/src/utils.cpp b/src/utils.cpp index 71e2b5c..c9a2714 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -599,3 +599,8 @@ long hexparse(const std::string& input) return value; } +bool stringEndsWith(const std::string& fullString, const std::string& ending) +{ + return fullString.length() >= ending.length() and + fullString.compare(fullString.length() - ending.length(), ending.length(), ending) == 0; +} diff --git a/src/utils.h b/src/utils.h index cf64c69..b844676 100644 --- a/src/utils.h +++ b/src/utils.h @@ -75,3 +75,4 @@ void printSubchannels(const vec_sp_subchannel& subchannels); long hexparse(const std::string& input); +bool stringEndsWith(std::string const &fullString, std::string const &ending); -- cgit v1.2.3