From 99dd9543a389e0465ba8678316c943866c3a8f93 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Mon, 20 Feb 2023 10:22:44 -0700 Subject: [PATCH] improved CLookup::Utility --- reflector/Lookup.cpp | 6 +- reflector/Lookup.h | 3 +- reflector/LookupDmr.cpp | 2 +- reflector/LookupNxdn.cpp | 2 +- reflector/LookupYsf.cpp | 5 +- reflector/Main.cpp | 187 ++++++++++++++++++--------------------- reflector/Makefile | 9 +- 7 files changed, 100 insertions(+), 114 deletions(-) diff --git a/reflector/Lookup.cpp b/reflector/Lookup.cpp index f70bfa3..27bc6af 100644 --- a/reflector/Lookup.cpp +++ b/reflector/Lookup.cpp @@ -115,12 +115,12 @@ bool CLookup::LoadContentFile(std::stringstream &ss) return rval; } -bool CLookup::Utility(Eaction action) +bool CLookup::Utility(Eaction action, Esource source) { std::stringstream ss; LoadParameters(); - auto rval = LoadContentHttp(ss); + auto rval = (Esource::http == source) ? LoadContentHttp(ss) : LoadContentFile(ss); if (rval) - std::cout << ss.str() << std::endl; + UpdateContent(ss, action); return rval; } diff --git a/reflector/Lookup.h b/reflector/Lookup.h index 541204e..d4638d0 100644 --- a/reflector/Lookup.h +++ b/reflector/Lookup.h @@ -25,6 +25,7 @@ #include "Configure.h" enum class Eaction { normal, parse, error_only }; +enum class Esource { http, file }; // compare function for std::map::find @@ -60,7 +61,7 @@ public: // locks void Lock(void) { m_Mutex.lock(); } void Unlock(void) { m_Mutex.unlock(); } - bool Utility(Eaction action); + bool Utility(Eaction action, Esource source); protected: std::time_t GetLastModTime(); diff --git a/reflector/LookupDmr.cpp b/reflector/LookupDmr.cpp index e239a62..a3c202c 100644 --- a/reflector/LookupDmr.cpp +++ b/reflector/LookupDmr.cpp @@ -95,7 +95,7 @@ void CLookupDmr::UpdateContent(std::stringstream &ss, Eaction action) } if (Eaction::error_only == action && failed) { - std::cout << "Bad syntax at line '" << line << "'\n"; + std::cout << line << '\n'; } } if (Eaction::normal == action) diff --git a/reflector/LookupNxdn.cpp b/reflector/LookupNxdn.cpp index a6a3abe..95b4f00 100644 --- a/reflector/LookupNxdn.cpp +++ b/reflector/LookupNxdn.cpp @@ -95,7 +95,7 @@ void CLookupNxdn::UpdateContent(std::stringstream &ss, Eaction action) } if (Eaction::error_only == action && failed) { - std::cout << "Bad syntax at line '" << line << "'\n"; + std::cout << line << '\n'; } } if (Eaction::normal == action) diff --git a/reflector/LookupYsf.cpp b/reflector/LookupYsf.cpp index 6964bb6..6150ea7 100644 --- a/reflector/LookupYsf.cpp +++ b/reflector/LookupYsf.cpp @@ -66,10 +66,11 @@ void CLookupYsf::UpdateContent(std::stringstream &ss, Eaction action) } else if (Eaction::error_only == action) { - std::cout << "YSF value '" << line << ";' is malformed" << std::endl; + std::cout << line << '\n'; } } - std::cout << "DMR Id database size now is " << m_map.size() << std::endl; + if (Eaction::normal == action) + std::cout << "DMR Id database size now is " << m_map.size() << std::endl; } void CLookupYsf::FindFrequencies(const CCallsign &cs, uint32_t &txfreq, uint32_t &rxfreq) diff --git a/reflector/Main.cpp b/reflector/Main.cpp index ac73d9e..c6af52f 100644 --- a/reflector/Main.cpp +++ b/reflector/Main.cpp @@ -79,8 +79,6 @@ int main(int argc, char *argv[]) #else // UTILITY is defined -#include - //////////////////////////////////////////////////////////////////////////////////////// // global objects @@ -92,130 +90,115 @@ CLookupYsf g_LYtr; static void usage(std::ostream &os, const char *name) { - os << "Usage: " << name << " [-d | -n | -y] [-p | -q] inifile\n"; - os << "Where:\n" - " -d - read the DMR Id http source (default)\n" - " -n - read the NXDN Id http source\n" - " -y - read the YSF Tx/Tx http source\n" - " -p - parse the input, removing bad lines\n" - " -q - parse the input, but only output problem lines in the http source\n" - " infile - an error-free urfd ini file (check it first with inicheck)\n" - "Without -p or -q, no parsing is done and the raw http source is output\n" - << std::endl; + os << "\nUsage: " << name << " DATABASE SOURCE ACTION INIFILE\n"; + os << "DATABASE (choose one)\n" + " dmr : The DmrId <==> Callsign databases.\n" + " nxdn : The NxdnId <==> Callsign databases.\n" + " ysf : The Callsign => Tx/Rx frequency database.\n" + "SOURCE (choose one)\n" + " file : The file specified by the FilePath ini parameter.\n" + " http : The URL specified by the URL ini paramater.\n" + "ACTION (choose one)\n" + " print : Print all lines from the SOURCE that are syntactically correct.\n" + " error : Print only the lines with failed syntax.\n" + "INIFILE : an error-free urfd ini file (check it first with inicheck).\n\n" + "Only the first character of DATABASE, SOURCE and ACTION is read.\n" + "Example: " << name << " y f e urfd.ini # Check your YSF Tx/Rx database file specifed in urfd.ini for syntax errors.\n\n"; } enum class Edb { none, dmr, nxdn, ysf }; int main(int argc, char *argv[]) { - Edb db = Edb::none; - Eaction action = Eaction::normal; - while (1) + Edb db; + Eaction action; + Esource source; + + if (5 != argc) { - auto c = getopt(argc, argv, "dnypq"); - if (c < 0) - { - if (1 == argc) - { - usage(std::cout, argv[0]); - return EXIT_SUCCESS; - } - break; - } - else - { - switch (c) - { - // define the input souce - case 'd': - if (Edb::none == db) - db = Edb::dmr; - else - { - std::cerr << "You can only select one database!\n"; - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - } - break; - - case 'n': - if (Edb::none == db) - db = Edb::nxdn; - else - { - std::cerr << "You can only select one database!\n"; - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - } - break; - case 'y': - if (Edb::none == db) - db = Edb::ysf; - else - { - std::cerr << "You can only select one database!\n"; - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - } - break; - - // define the action - case 'p': - if (Eaction::error_only == action) - { - std::cerr << "You can't specify both -p and -q!\n"; - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - } - else - action = Eaction::parse; - break; - - case 'q': - if (Eaction::parse == action) - { - std::cerr << "You can't specify both -p and -q!\n"; - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - } - else - action = Eaction::error_only; - break; - - // finally - default: - usage(std::cerr, argv[0]); - return EXIT_FAILURE; - break; - } - } + usage(std::cerr, argv[0]); + return EXIT_FAILURE; } - if (optind + 1 != argc) + switch (argv[1][0]) { - std::cerr << argv[0] << ": " << ((optind==argc) ? "No ini file specified!" : "Too many arguments!") << std::endl; - usage(std::cerr, argv[0]); - exit(EXIT_FAILURE); + case 'd': + case 'D': + db = Edb::dmr; + break; + case 'n': + case 'N': + db = Edb::nxdn; + break; + + case 'y': + case 'Y': + db = Edb::ysf; + break; + + default: + std::cout << "Unrecognized DATABASE: " << argv[1]; + db = Edb::none; + break; } - if (g_Conf.ReadData(argv[optind])) + switch (argv[2][0]) + { + case 'h': + case 'H': + source = Esource::http; + break; + + case 'f': + case 'F': + source = Esource::file; + break; + + default: + std::cerr << "Unrecognized SOURCE: " << argv[2] << std::endl; + db = Edb::none; + break; + } + + switch (argv[3][0]) + { + case 'p': + case 'P': + action = Eaction::parse; + break; + + case 'e': + case 'E': + action = Eaction::error_only; + break; + + default: + std::cerr << "Unrecognized ACTION: " << argv[3] << std::endl; + db = Edb::none; + break; + } + + if (db == Edb::none) + { + usage(std::cerr, argv[0]); return EXIT_FAILURE; + } - if (Edb::none == db) - db = Edb::dmr; + if (g_Conf.ReadData(argv[4])) + return EXIT_FAILURE; switch (db) { case Edb::dmr: - g_LDid.Utility(action); + g_LDid.Utility(action, source); break; case Edb::nxdn: - g_LNid.Utility(action); + g_LNid.Utility(action, source); break; case Edb::ysf: - g_LYtr.Utility(action); + g_LYtr.Utility(action, source); break; } diff --git a/reflector/Makefile b/reflector/Makefile index 0ce66d6..292bf49 100644 --- a/reflector/Makefile +++ b/reflector/Makefile @@ -37,17 +37,18 @@ LDFLAGS=-pthread -lcurl SRCS = $(wildcard *.cpp) OBJS = $(SRCS:.cpp=.o) DEPS = $(SRCS:.cpp=.d) +DBUTILOBJS = Configure.o CurlGet.o Lookup.o LookupDmr.o LookupNxdn.o LookupYsf.o YSFNode.o Callsign.o all : $(EXE) $(INICHECK) $(DBUTIL) $(EXE) : $(OBJS) $(CXX) $^ -o $@ $(LDFLAGS) -$(INICHECK) : Configure.cpp CurlGet.cpp - $(CXX) -DINICHECK $(CFLAGS) Configure.cpp CurlGet.cpp -o $(INICHECK) -lcurl +$(INICHECK) : Configure.cpp CurlGet.o + $(CXX) -DINICHECK $(CFLAGS) $< CurlGet.o -o $@ -lcurl -$(DBUTIL) : Main.cpp Configure.cpp CurlGet.cpp Lookup.cpp LookupDmr.cpp LookupNxdn.cpp LookupYsf.cpp YSFNode.cpp Callsign.cpp - $(CXX) -DUTILITY $(CFLAGS) -o dbutil Main.cpp Configure.cpp CurlGet.cpp Lookup.cpp LookupDmr.cpp LookupNxdn.cpp LookupYsf.cpp YSFNode.cpp Callsign.cpp -lcurl +$(DBUTIL) : Main.cpp $(DBUTILOBJS) + $(CXX) -DUTILITY $(CFLAGS) $< $(DBUTILOBJS) -o $@ -lcurl %.o : %.cpp $(CXX) $(CFLAGS) -c $< -o $@