mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-04 00:50:42 +02:00
WIP
This commit is contained in:
parent
abf56ad643
commit
adaf56ca2e
@ -1,6 +1,7 @@
|
||||
|
||||
#include "CmdParser.h"
|
||||
#include "sls/sls_detector_defs.h"
|
||||
#include "sls/string_utils.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
@ -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<std::string>(iss);
|
||||
command_ = *it++; //First arg is the comand to run
|
||||
|
||||
arguments_ =
|
||||
std::vector<std::string>(it, std::istream_iterator<std::string>());
|
||||
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<const char *> CmdParser::argv() const {
|
||||
std::vector<const char *> vec;
|
||||
if (!command_.empty()) {
|
||||
|
@ -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_;
|
||||
|
@ -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");
|
||||
}
|
@ -53,4 +53,7 @@ std::vector<std::string> 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
|
||||
|
@ -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
|
@ -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")
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user