Merge branch 'developer' into mysocket

This commit is contained in:
Erik Frojdh
2019-05-27 09:12:10 +02:00
36 changed files with 670 additions and 1108 deletions

View File

@ -5,10 +5,11 @@
#include <iostream>
#include <iterator>
#include <sstream>
//printing function for debugging
void CmdLineParser::Print() {
std::cout << "\nCmdLineParser::Print()\n";
std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl;
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';
@ -19,27 +20,22 @@ void CmdLineParser::Print() {
std::cout << "\n\n";
};
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];
void CmdLineParser::Parse(int argc, const char * const argv[]) {
executable_ = argv[0]; //first arg is calling binary
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_.emplace_back(std::string(argv[i]));
arguments_.emplace_back(argv[i]);
}
}
};
}
void CmdLineParser::Parse(const std::string &s) {
std::istringstream iss(s);
auto it = std::istream_iterator<std::string>(iss);
//read the first element and increment
command_ = *it++;
arguments_ = std::vector<std::string>(it, std::istream_iterator<std::string>());
;
arguments_ =
std::vector<std::string>(it, std::istream_iterator<std::string>());
DecodeIdAndPosition(command_.c_str());
}
@ -51,19 +47,23 @@ void CmdLineParser::DecodeIdAndPosition(const char *c) {
if (contains_id && contains_pos) {
int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp);
if (r != 3) {
throw(std::invalid_argument("Cannot decode client or detector id from: \"" + std::string(c) + "\"\n"));
throw(std::invalid_argument(
"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(std::invalid_argument("Cannot decode client id from: \"" + std::string(c) + "\"\n"));
throw(std::invalid_argument("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(std::invalid_argument("Cannot decode detector id from: \"" + std::string(c) + "\"\n"));
throw(std::invalid_argument("Cannot decode detector id from: \"" +
std::string(c) + "\"\n"));
}
command_ = tmp;
} else {
@ -71,12 +71,11 @@ void CmdLineParser::DecodeIdAndPosition(const char *c) {
}
}
std::vector<char *> CmdLineParser::argv() {
std::vector<char *> vec;
if (command_.empty()!=true){
std::vector<const char *> CmdLineParser::argv() const {
std::vector<const char *> vec;
if (command_.empty() != true) {
vec.push_back(&command_.front());
}
for (auto &arg : arguments_) {
vec.push_back(&arg.front());
}

View File

@ -24,9 +24,7 @@ IpAddr::IpAddr(const std::string &address) {
inet_pton(AF_INET, address.c_str(), &addr_);
}
IpAddr::IpAddr(const char *address) {
inet_pton(AF_INET, address, &addr_);
}
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
std::string IpAddr::str() const {
char ipstring[INET_ADDRSTRLEN]{};
@ -35,9 +33,9 @@ std::string IpAddr::str() const {
}
std::string IpAddr::hex() const {
std::ostringstream ss;
ss << std::hex << std::setfill('0');
for (int i = 0; i != 4; ++i) {
ss << std::hex << std::setfill('0') << std::setw(2)
<< ((addr_ >> i * 8) & 0xFF);
ss << std::setw(2) << ((addr_ >> i * 8) & 0xFF);
}
return ss.str();
}
@ -60,18 +58,14 @@ std::string MacAddr::to_hex(const char delimiter) const {
for (int i = 32; i >= 0; i -= 8) {
if (delimiter)
ss << delimiter;
ss << ((addr_ >> i) & 0xFF);
ss << std::setw(2) << ((addr_ >> i) & 0xFF);
}
return ss.str();
}
std::string MacAddr::str() const {
return to_hex(':');
}
std::string MacAddr::str() const { return to_hex(':'); }
std::string MacAddr::hex() const {
return to_hex();
}
std::string MacAddr::hex() const { return to_hex(); }
std::ostream &operator<<(std::ostream &out, const IpAddr &addr) {
return out << addr.str();