From adaf56ca2ec0b262245173126295e83780391cca Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 14 Sep 2021 15:14:08 +0200 Subject: [PATCH] WIP --- slsDetectorSoftware/src/CmdParser.cpp | 127 ++++++------------- slsDetectorSoftware/src/CmdParser.h | 4 +- slsDetectorSoftware/tests/test-CmdParser.cpp | 116 +++++++++++++---- slsSupportLib/include/sls/string_utils.h | 3 + slsSupportLib/src/string_utils.cpp | 10 ++ slsSupportLib/tests/test-string_utils.cpp | 29 +++++ tests/CMakeLists.txt | 20 +-- 7 files changed, 188 insertions(+), 121 deletions(-) diff --git a/slsDetectorSoftware/src/CmdParser.cpp b/slsDetectorSoftware/src/CmdParser.cpp index dcd0a2bd4..3c59ebb26 100644 --- a/slsDetectorSoftware/src/CmdParser.cpp +++ b/slsDetectorSoftware/src/CmdParser.cpp @@ -1,6 +1,7 @@ #include "CmdParser.h" #include "sls/sls_detector_defs.h" +#include "sls/string_utils.h" #include #include #include @@ -41,8 +42,11 @@ void CmdParser::Parse(std::string s) { Reset(); //Are we looking at -h --help? - + bool h = replace_first(&s, "--help", " "); + h = h || replace_first(&s, "-h", " "); + help_ = h; + // Extract the position indicies auto pos = s.find_first_not_of("0123456789:- "); if (pos!=0){ auto pre = s.substr(0, pos); @@ -51,106 +55,59 @@ void CmdParser::Parse(std::string s) { DecodeIdAndPosition(pre.c_str()); } - - //Load string + //Command and args should now be all that's left in the string std::istringstream iss(s); auto it = std::istream_iterator(iss); command_ = *it++; //First arg is the comand to run arguments_ = std::vector(it, std::istream_iterator()); - auto old_size = arguments_.size(); - arguments_.erase(std::remove_if(begin(arguments_), end(arguments_), - [](const std::string &item) { - return (item == "-h" || - item == "--help"); - }), - end(arguments_)); - if (old_size - arguments_.size() > 0) - help_ = true; - // if (!arguments_.empty()) { - // command_ = arguments_[0]; - // arguments_.erase(begin(arguments_)); - // } + //allow comma sep for (auto& arg : arguments_){ if (arg.back() == ',') arg.pop_back(); } - - // DecodeIdAndPosition(command_.c_str()); } -void CmdParser::DecodeIdAndPosition(const char *c) { - bool contains_id = std::strchr(c, '-') != nullptr; - bool contains_pos = std::strchr(c, ':') != nullptr; - - // if (!isdigit(c[0])){ - // // The first char is not a digit which means we have a command. - // // or at least a candidate, calling could still fail - // command_ = c; - // return; - // } - - if (contains_id && contains_pos) { - int r = sscanf(c, "%d-%d:", &multi_id_, &detector_id_); - if (r != 2) { - throw(sls::RuntimeError( - "Cannot decode client or detector id from: \"" + - std::string(c) + "\"\n")); - } - // command_ = tmp; - } else if (contains_id && !contains_pos) { - int r = sscanf(c, "%d-", &multi_id_); - if (r != 1) { - throw(sls::RuntimeError("Cannot decode client id from: \"" + - std::string(c) + "\"\n")); - } - // command_ = tmp; - } else if (!contains_id && contains_pos) { - int r = sscanf(c, "%d:", &detector_id_); - if (r != 1) { - throw(sls::RuntimeError("Cannot decode detector id from: \"" + - std::string(c) + "\"\n")); - } - // command_ = tmp; - } else { - // command_ = c; +void CmdParser::DecodeIdAndPosition(std::string pre){ + if(pre.empty()) + return; + + //Get the detector id + auto pos = pre.find_first_of("-"); + if (pos != std::string::npos){ + multi_id_ = std::stoi(pre); + pre.erase(0,pos+1); } + + //if there is nothing left to parse we need to return + if(pre.empty()){ + return; + } + + //now lets see if there is a : + pos = pre.find_first_of(":"); + if (pos == std::string::npos){ + //no : we only have the multi id + detector_id_ = std::stoi(pre); + + }else if(pos == 0){ + //do nothing, there is no multi id specified + pre.erase(0,1); + }else{ + // the : is somewhere in the middle + detector_id_ = std::stoi(pre); + pre.erase(0,pos+1); + } + + //now if the string is not empty we also have a receiver id + if(!pre.empty()){ + receiver_id_ = std::stoi(pre); + } + } -// void CmdParser::DecodeIdAndPosition(const char *c) { -// bool contains_id = std::strchr(c, '-') != nullptr; -// bool contains_pos = std::strchr(c, ':') != nullptr; -// char tmp[100]; - -// if (contains_id && contains_pos) { -// int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp); -// if (r != 3) { -// throw(sls::RuntimeError( -// "Cannot decode client or detector id from: \"" + -// std::string(c) + "\"\n")); -// } -// command_ = tmp; -// } else if (contains_id && !contains_pos) { -// int r = sscanf(c, "%d-%s", &multi_id_, tmp); -// if (r != 2) { -// throw(sls::RuntimeError("Cannot decode client id from: \"" + -// std::string(c) + "\"\n")); -// } -// command_ = tmp; -// } else if (!contains_id && contains_pos) { -// int r = sscanf(c, "%d:%s", &detector_id_, tmp); -// if (r != 2) { -// throw(sls::RuntimeError("Cannot decode detector id from: \"" + -// std::string(c) + "\"\n")); -// } -// command_ = tmp; -// } else { -// command_ = c; -// } -// } - std::vector CmdParser::argv() const { std::vector vec; if (!command_.empty()) { diff --git a/slsDetectorSoftware/src/CmdParser.h b/slsDetectorSoftware/src/CmdParser.h index a72254134..adf60974b 100644 --- a/slsDetectorSoftware/src/CmdParser.h +++ b/slsDetectorSoftware/src/CmdParser.h @@ -27,6 +27,7 @@ class CmdParser { int multi_id() const noexcept { return multi_id_; }; int detector_id() const noexcept { return detector_id_; }; + int receiver_id() const noexcept { return receiver_id_; }; int n_arguments() const noexcept { return arguments_.size(); } const std::string &command() const noexcept { return command_; } void setCommand(std::string cmd) { command_ = cmd; } @@ -40,10 +41,11 @@ class CmdParser { std::string cli_line() const; private: - void DecodeIdAndPosition(const char *c); + void DecodeIdAndPosition(std::string pre); void Reset(); // reset all private variables int multi_id_ = 0; int detector_id_ = -1; + int receiver_id_ = -1; bool help_ = false; std::string command_; std::string executable_; diff --git a/slsDetectorSoftware/tests/test-CmdParser.cpp b/slsDetectorSoftware/tests/test-CmdParser.cpp index 61a64622b..317c7157a 100644 --- a/slsDetectorSoftware/tests/test-CmdParser.cpp +++ b/slsDetectorSoftware/tests/test-CmdParser.cpp @@ -105,16 +105,6 @@ SCENARIO("Parsing a string with the command line parser", "[support]") { REQUIRE(p.arguments()[2] == "7000"); } } - - // WHEN("Cliend id and or detector id cannot be decoded") { - // vs arg{"o:cmd", "-5:cmd", "aedpva:cmd", - // "5-svc:vrf", "asv-5:cmd", "savc-asa:cmd"}; - // THEN("Parsing Throws") { - // for (size_t i = 0; i != arg.size(); ++i) { - // REQUIRE_THROWS(p.Parse(arg[i])); - // } - // } - // } } } @@ -157,6 +147,20 @@ SCENARIO("Parsing strings with -h or --help", "[support]") { } } +TEST_CASE("Parse string with --help"){ + CmdParser p; + p.Parse("list --help"); + REQUIRE(p.isHelp()==true); + REQUIRE(p.command()=="list"); +} + +TEST_CASE("Parse string with -h"){ + CmdParser p; + p.Parse("list -h"); + REQUIRE(p.isHelp()==true); + REQUIRE(p.command()=="list"); +} + TEST_CASE("Parsing consecutive strings resets not found det id") { CmdParser p; p.Parse("1:exptime 0.5"); @@ -266,19 +270,6 @@ TEST_CASE("Double digit id", "[support]") { REQUIRE(p.arguments().empty()); } -// TEST_CASE("Calling with wrong id throws invalid_argument", "[support]") { -// int argc = 2; -// const char *const argv[]{"caller", "asvldkn:vrf"}; -// CmdParser p; -// CHECK_THROWS(p.Parse(argc, argv)); -// } - -// TEST_CASE("Calling with wrong client throws invalid_argument", "[support]") { -// int argc = 2; -// const char *const argv[]{"caller", "lki-3:vrf"}; -// CmdParser p; -// CHECK_THROWS(p.Parse(argc, argv)); -// } TEST_CASE("Build up argv", "[support]") { CmdParser p; @@ -293,8 +284,8 @@ TEST_CASE("Build up argv", "[support]") { TEST_CASE("Allows space between mod id and command"){ CmdParser p; - p.Parse("1: exptime 0.5"); - REQUIRE(p.detector_id() == 8); + p.Parse("7: exptime 0.5"); + REQUIRE(p.detector_id() == 7); REQUIRE(p.command() == "exptime"); REQUIRE(p.arguments().size() == 1); REQUIRE(p.arguments()[0] == "0.5"); @@ -327,4 +318,79 @@ TEST_CASE("Allows space between mod id and command with detector id and :"){ REQUIRE(p.command() == "exptime"); REQUIRE(p.arguments().size() == 1); REQUIRE(p.arguments()[0] == "0.5"); +} + +TEST_CASE("Parse receiver ID"){ + CmdParser p; + p.Parse("2-5:3 flowcontrol10g 1"); + REQUIRE(p.detector_id() == 5); + REQUIRE(p.multi_id() == 2); + REQUIRE(p.command() == "flowcontrol10g"); + REQUIRE(p.arguments().size() == 1); + REQUIRE(p.arguments()[0] == "1"); + REQUIRE(p.receiver_id()==3); +} + +TEST_CASE("Parse receiver ID no det id"){ + CmdParser p; + p.Parse("5:95 flowcontrol10g"); + REQUIRE(p.detector_id() == 5); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == "flowcontrol10g"); + REQUIRE(p.arguments().size() == 0); + REQUIRE(p.receiver_id()==95); +} + + + +TEST_CASE("Det id but no mod id"){ + CmdParser p; + p.Parse("1-exptime"); + REQUIRE(p.detector_id() == -1); //not there + REQUIRE(p.multi_id() == 1); + REQUIRE(p.command() == "exptime"); +} + +TEST_CASE("Det id but no mod id but with space after -"){ + CmdParser p; + p.Parse("1- exptime"); + REQUIRE(p.detector_id() == -1); //not there + REQUIRE(p.multi_id() == 1); + REQUIRE(p.command() == "exptime"); +} + +TEST_CASE("Parse receiver ID no det id no mod"){ + CmdParser p; + p.Parse(":95 flowcontrol10g"); + REQUIRE(p.detector_id() == -1); //not there + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == "flowcontrol10g"); + REQUIRE(p.arguments().size() == 0); + REQUIRE(p.receiver_id()==95); +} + +TEST_CASE("Parse mod and receiver id"){ + CmdParser p; + p.Parse("1:3 exptime"); + REQUIRE(p.detector_id() == 1); + REQUIRE(p.receiver_id()==3); + REQUIRE(p.command() == "exptime"); +} + +TEST_CASE("Det id but no no mod"){ + CmdParser p; + p.Parse("2-:35 exptime"); + REQUIRE(p.detector_id() == -1); + REQUIRE(p.receiver_id()==35); + REQUIRE(p.multi_id() == 2); + REQUIRE(p.command() == "exptime"); +} + +TEST_CASE("All stuff"){ + CmdParser p; + p.Parse("3-4:2 exptime"); + REQUIRE(p.detector_id() == 4); + REQUIRE(p.receiver_id()==2); + REQUIRE(p.multi_id() == 3); + REQUIRE(p.command() == "exptime"); } \ No newline at end of file diff --git a/slsSupportLib/include/sls/string_utils.h b/slsSupportLib/include/sls/string_utils.h index 3b04d70dd..e9135ec05 100644 --- a/slsSupportLib/include/sls/string_utils.h +++ b/slsSupportLib/include/sls/string_utils.h @@ -53,4 +53,7 @@ std::vector split(const std::string &strToSplit, char delimeter); std::string RemoveUnit(std::string &str); bool is_int(const std::string &s); + +bool replace_first(std::string *s, const std::string& substr, const std::string& repl); + } // namespace sls diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 087ad3ce1..94b643363 100755 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -36,4 +36,14 @@ bool is_int(const std::string &s) { }) == s.end(); } +bool replace_first(std::string *s, const std::string& substr, const std::string& repl){ + auto pos = s->find(substr); + if (pos != std::string::npos){ + s->replace(pos, substr.size(), repl); + return true; + } + return false; +} + + }; // namespace sls \ No newline at end of file diff --git a/slsSupportLib/tests/test-string_utils.cpp b/slsSupportLib/tests/test-string_utils.cpp index a9a2c000a..941167701 100644 --- a/slsSupportLib/tests/test-string_utils.cpp +++ b/slsSupportLib/tests/test-string_utils.cpp @@ -76,4 +76,33 @@ TEST_CASE("Check is string is integer") { REQUIRE_FALSE(sls::is_int("")); } + +TEST_CASE("Replace substring in string"){ + std::string s = "this string should be replaced"; + auto r = sls::replace_first(&s, "string ", ""); + REQUIRE(r == true); + REQUIRE(s == "this should be replaced"); +} + +TEST_CASE("Replace --help in command"){ + std::string s = "sls_detector_get --help exptime"; + auto r = sls::replace_first(&s, " --help", ""); + REQUIRE(r == true); + REQUIRE(s == "sls_detector_get exptime"); +} + +TEST_CASE("Replace -h in command"){ + std::string s = "sls_detector_get -h exptime"; + auto r = sls::replace_first(&s, " -h", ""); + REQUIRE(r == true); + REQUIRE(s == "sls_detector_get exptime"); +} + +TEST_CASE("replace --help"){ + std::string s = "list --help"; + auto r = sls::replace_first(&s, " --help", ""); + REQUIRE(r == true); + REQUIRE(s == "list"); +} + // TEST_CASE("concat things not being strings") \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 18cac72b3..15c7e527e 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,21 +7,21 @@ set(SLS_TEST_SOURCES test.cpp ) -add_executable(testclient src/testclient.cpp) -target_link_libraries(testclient slsSupportShared) -set_target_properties(testclient PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +# add_executable(testclient src/testclient.cpp) +# target_link_libraries(testclient slsSupportShared) +# set_target_properties(testclient PROPERTIES +# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -add_executable(testserver src/testserver.cpp) -target_link_libraries(testserver slsSupportShared) -set_target_properties(testserver PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +# add_executable(testserver src/testserver.cpp) +# target_link_libraries(testserver slsSupportShared) +# set_target_properties(testserver PROPERTIES +# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_executable(tests ${SLS_TEST_SOURCES}) target_link_libraries(tests PUBLIC slsProjectOptions - slsSupportShared + slsSupportStatic pthread rt PRIVATE @@ -30,7 +30,7 @@ target_link_libraries(tests if (SLS_USE_DETECTOR) target_link_libraries(tests PUBLIC - slsDetectorShared + slsDetectorStatic ) endif (SLS_USE_DETECTOR)