Merge branch 'progfpga' into somefixes

This commit is contained in:
2021-09-15 11:41:52 +02:00
8 changed files with 269 additions and 134 deletions

View File

@ -1,28 +1,13 @@
#include "CmdParser.h"
#include "sls/sls_detector_defs.h"
#include <cstdio>
#include <cstring>
#include <iostream>
#include "sls/string_utils.h"
#include <algorithm>
#include <iterator>
#include <sstream>
namespace sls {
void CmdParser::Print() {
std::cout << "\nCmdParser::Print()\n";
std::cout << "\tmulti_id: " << multi_id_
<< ", detector_id: " << detector_id_ << std::endl;
std::cout << "\texecutable: " << executable_ << '\n';
std::cout << "\tcommand: " << command_ << '\n';
std::cout << "\tn_arguments: " << n_arguments() << '\n';
std::cout << "\targuments: ";
for (const auto &argument : arguments_) {
std::cout << argument << " ";
}
std::cout << "\n\n";
};
void CmdParser::Parse(int argc, const char *const argv[]) {
Reset();
executable_ = argv[0]; // first arg is calling binary
@ -36,88 +21,82 @@ void CmdParser::Parse(int argc, const char *const argv[]) {
}
}
void CmdParser::Parse(const std::string &s) {
void CmdParser::Parse(std::string s) {
// taking s by value we can modify it.
Reset();
// Are we looking at -h --help? avoid removing h from command starting
// with h when combined with detector id (ex, 1-hostname)
bool h = replace_first(&s, "--help", " ");
h = h || replace_first(&s, " -h", " ");
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);
pre.erase(std::remove(pre.begin(), pre.end(), ' '), pre.end());
s.erase(0, pos);
DecodeIdAndPosition(pre.c_str());
}
// 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_){
// 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;
char tmp[100];
void CmdParser::DecodeIdAndPosition(std::string pre) {
if (pre.empty())
return;
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;
// 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 {
command_ = c;
// the : is somewhere in the middle
detector_id_ = std::stoi(pre);
pre.erase(0, pos + 1);
}
}
std::vector<const char *> CmdParser::argv() const {
std::vector<const char *> vec;
if (!command_.empty()) {
vec.push_back(&command_.front());
// now if the string is not empty we also have a receiver id
if (!pre.empty()) {
receiver_id_ = std::stoi(pre);
}
for (auto &arg : arguments_) {
vec.push_back(&arg.front());
}
return vec;
}
std::string CmdParser::cli_line() const {
std::ostringstream os;
os << command_;
for (const auto &arg : arguments_)
os << " " << arg;
return os.str();
}
void CmdParser::Reset() {
multi_id_ = 0;
detector_id_ = -1;
receiver_id_ = -1;
help_ = false;
command_.clear();
executable_.clear();

View File

@ -22,11 +22,11 @@ namespace sls {
class CmdParser {
public:
void Parse(int argc, const char *const argv[]);
void Parse(const std::string &s);
void Print();
void Parse(std::string s);
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; }
@ -36,14 +36,13 @@ class CmdParser {
const std::vector<std::string> &arguments() const noexcept {
return arguments_;
};
std::vector<const char *> argv() const;
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_;