diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 08ae54eb1..d953ea5b1 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -805,9 +805,6 @@ int multiSlsDetector::execCommand(const std::string& cmd, int detPos) { int multiSlsDetector::readConfigurationFile(const std::string &fname) { freeSharedMemory(); setupMultiDetector(); - - char *args[100]; - char myargs[100][1000]; FILE_LOG(logINFO) << "Loading configuration file: " << fname; std::ifstream input_file; @@ -820,16 +817,7 @@ int multiSlsDetector::readConfigurationFile(const std::string &fname) { current_line.erase(current_line.find('#')); FILE_LOG(logDEBUG1) << "current_line after removing comments:\n\t" << current_line; if (current_line.length() > 1) { - std::istringstream line_stream(current_line); - int n_arguments = 0; - std::string current_argument; - while (line_stream.good()) { - line_stream >> current_argument; - sls::strcpy_safe(myargs[n_arguments], current_argument.c_str()); - args[n_arguments] = myargs[n_arguments]; - ++n_arguments; - } - multiSlsDetectorClient(n_arguments, args, PUT_ACTION, this); + multiSlsDetectorClient(current_line, PUT_ACTION, this); } } input_file.close(); diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index 93b3fb9d0..69c927f3d 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -4,6 +4,7 @@ #include "CmdLineParser.h" #include "container_utils.h" +#include "string_utils.h" #include "multiSlsDetector.h" #include "multiSlsDetectorCommand.h" #include "sls_detector_exceptions.h" @@ -18,100 +19,84 @@ inline int dummyCallback(detectorData *d, int p, void *) { class multiSlsDetectorClient { public: - multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr) { - int id = -1, pos = -1, iv = 0; - bool verify = true, update = true; - char cmd[100] = ""; + multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr): + action_(action), + detPtr(myDetector){ + parser.Parse(argc, argv); + runCommand(); - if (action == slsDetectorDefs::PUT_ACTION && argc < 2) { - std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl; + } + multiSlsDetectorClient(const std::string& args, int action, multiSlsDetector *myDetector = nullptr): + action_(action), + detPtr(myDetector){ + parser.Parse(args); + runCommand(); + } + private: + int action_; + CmdLineParser parser; + multiSlsDetector* detPtr = nullptr; + + void runCommand(){ + bool verify = true; + bool update = true; + if (action_ == slsDetectorDefs::PUT_ACTION && parser.n_arguments() == 0) { + std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl; std::cout << std::endl; return; }; - if (action == slsDetectorDefs::GET_ACTION && argc < 1) { - std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl; + if (action_ == slsDetectorDefs::GET_ACTION && parser.command().empty()) { + std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl; std::cout << std::endl; return; }; - if (action == slsDetectorDefs::READOUT_ACTION) { - id = 0; - pos = -1; - if (argc) { // multi id scanned - if (strchr(argv[0], '-')) { - iv = sscanf(argv[0], "%d-%s", &id, cmd); //%s needn't be there (if not 1:), so 1 or 2 arguments scanned - if (iv >= 1 && id >= 0) { - argv[0] = cmd; - std::cout << id << "-"; - } else { - id = 0; - } - } // single id scanned - if (strchr(argv[0], ':')) { - iv = sscanf(argv[0], "%d:", &pos); - if (iv == 1 && pos >= 0) { - std::cout << "pos " << pos << " is not allowed for readout!" << std::endl; - return; - } - } - } - } else { // multi id scanned - iv = sscanf(argv[0], "%d-%s", &id, cmd); // scan success - if (iv == 2 && id >= 0) { - argv[0] = cmd; - std::cout << id << "-"; - } else { - id = 0; - } // sls pos scanned - iv = sscanf(argv[0], "%d:%s", &pos, cmd); // scan success - if (iv == 2 && pos >= 0) { - argv[0] = cmd; - std::cout << pos << ":"; - } - if (iv != 2) { - pos = -1; - } // remove the %d- and %d: - if (strlen(cmd) == 0u) { - strcpy(cmd, argv[0]); - } - - // special commands - std::string scmd = cmd; // free without calling multiSlsDetector constructor - if (scmd == "free") { - multiSlsDetector::freeSharedMemory(id, pos); - return; - } // get user details without verify sharedMultiSlsDetector version - else if ((scmd == "user") && (action == slsDetectorDefs::GET_ACTION)) { - verify = false; - update = false; - } + if (action_ == slsDetectorDefs::READOUT_ACTION && parser.detector_id() != -1) { + std::cout << "detector_id: " << parser.detector_id() << " ,readout of individual detectors is not allowed!" << std::endl; + return; } + + // special commands + if (parser.command() == "free") { + multiSlsDetector::freeSharedMemory(parser.multi_id(), parser.detector_id()); + return; + } // get user details without verify sharedMultiSlsDetector version + else if ((parser.command() == "user") && (action_ == slsDetectorDefs::GET_ACTION)) { + verify = false; + update = false; + } + //std::cout<<"id:"< localDet; - if (myDetector == nullptr) { + if (detPtr == nullptr) { try { - localDet = sls::make_unique(id, verify, update); - myDetector = localDet.get(); + localDet = sls::make_unique(parser.multi_id(), verify, update); + detPtr = localDet.get(); } catch (const SlsDetectorPackageExceptions &e) { /*std::cout << e.GetMessage() << std::endl;*/ return; } catch (...) { - std::cout << " caught exception" << std::endl; + std::cout << " caught exception\n"; return; } } - if (pos >= myDetector->getNumberOfDetectors()) { - std::cout << "position is out of bounds." << std::endl; + if (parser.detector_id() >= detPtr->getNumberOfDetectors()) { + std::cout << "position is out of bounds.\n"; return; } // call multi detector command line - multiSlsDetectorCommand myCmd(myDetector); - std::string answer = myCmd.executeLine(argc, argv, action, pos); + multiSlsDetectorCommand myCmd(detPtr); + std::string answer = myCmd.executeLine(parser.n_arguments()+1, parser.argv().data(), action_, parser.detector_id()); - if (action != slsDetectorDefs::READOUT_ACTION) { - std::cout << argv[0] << " "; + if (parser.multi_id()!=0) + std::cout << parser.multi_id() << '-'; + if (parser.detector_id() != -1) + std::cout << parser.detector_id() << ':'; + + if (action_ != slsDetectorDefs::READOUT_ACTION) { + std::cout << parser.command() << " "; } std::cout << answer << std::endl; } diff --git a/slsDetectorSoftware/slsDetectorClient/sls_detector_client.cpp b/slsDetectorSoftware/slsDetectorClient/sls_detector_client.cpp index 469803064..0f31d2e1d 100644 --- a/slsDetectorSoftware/slsDetectorClient/sls_detector_client.cpp +++ b/slsDetectorSoftware/slsDetectorClient/sls_detector_client.cpp @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) { int action = slsDetectorDefs::HELP_ACTION; #endif - if (argc > 1) - argv++; - multiSlsDetectorClient(argc - 1, argv, action); + // if (argc > 1) + // argv++; + multiSlsDetectorClient(argc, argv, action); } diff --git a/slsSupportLib/include/CmdLineParser.h b/slsSupportLib/include/CmdLineParser.h index c241d6666..d9f57cca4 100644 --- a/slsSupportLib/include/CmdLineParser.h +++ b/slsSupportLib/include/CmdLineParser.h @@ -13,14 +13,18 @@ public: //getters int multi_id() const { return multi_id_; }; int detector_id() const { return detector_id_; }; - std::string command() const { return command_; } - const std::vector& arguments() { return arguments_; }; + int n_arguments() const {return arguments_.size();} + const std::string& command() const { return command_; } + const std::string& executable() const { return executable_;} + const std::vector& arguments() const{ return arguments_; }; + std::vector argv(); private: void DecodeIdAndPosition(const char* c); int multi_id_ = 0; int detector_id_ = -1; std::string command_; + std::string executable_; std::vector arguments_; }; diff --git a/slsSupportLib/src/CmdLineParser.cpp b/slsSupportLib/src/CmdLineParser.cpp index 51a5a682a..356800947 100644 --- a/slsSupportLib/src/CmdLineParser.cpp +++ b/slsSupportLib/src/CmdLineParser.cpp @@ -10,7 +10,9 @@ void CmdLineParser::Print() { std::cout << "\nCmdLineParser::Print()\n"; std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl; - std::cout << "\tcommand: " << command_ << std::endl; + std::cout << "\texecutable: " << executable_ << '\n'; + std::cout << "\tcommand: " << command_ << '\n'; + std::cout << "\tn_arguments: " << n_arguments() << '\n'; std::cout << "\targuments: "; for (size_t i = 0; i < arguments_.size(); ++i) { std::cout << arguments_[i] << " "; @@ -22,12 +24,13 @@ void CmdLineParser::Parse(int argc, char* argv[]) { //first element of argv is the command used to call the executable ->skipping //and if this is the only command skip all + executable_ = argv[0]; if (argc > 1) { //second element is cmd string that needs to be decoded DecodeIdAndPosition(argv[1]); //The rest of the arguments goes into a vector for later processing for (int i = 2; i < argc; ++i) - arguments_.push_back(std::string(argv[i])); + arguments_.emplace_back(std::string(argv[i])); } }; @@ -65,3 +68,11 @@ void CmdLineParser::DecodeIdAndPosition(const char* c) command_ = c; } } + +std::vector CmdLineParser::argv(){ + std::vector vec; + vec.push_back(&command_.front()); + for (auto& arg: arguments_) + vec.push_back(&arg.front()); + return vec; +}