mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 18:17:59 +02:00
class for Ip and Mac
This commit is contained in:
@ -16,29 +16,35 @@ class IpAddr {
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
explicit IpAddr(uint32_t address);
|
||||
IpAddr(uint32_t address);
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
bool operator==(const IpAddr &other) const { return addr_ == other.addr_; }
|
||||
bool operator!=(const IpAddr &other) const { return addr_ != other.addr_; }
|
||||
bool operator==(const uint32_t other) const { return addr_ == other; }
|
||||
bool operator!=(const uint32_t other) const { return addr_ != other; }
|
||||
};
|
||||
|
||||
class MacAddr {
|
||||
private:
|
||||
uint64_t addr_{0};
|
||||
std::string to_hex(const char delimiter = 0);
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
MacAddr(uint64_t mac);
|
||||
MacAddr(std::string mac);
|
||||
std::string str() { return to_hex(':'); }
|
||||
std::string hex() { return to_hex(); }
|
||||
MacAddr(const char *address);
|
||||
std::string str() const { return to_hex(':'); }
|
||||
std::string hex() const { return to_hex(); }
|
||||
bool operator==(const MacAddr &other) const { return addr_ == other.addr_; }
|
||||
bool operator!=(const MacAddr &other) const { return addr_ != other.addr_; }
|
||||
bool operator==(const uint64_t other) const { return addr_ == other; }
|
||||
bool operator!=(const uint64_t other) const { return addr_ != other; }
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr);
|
||||
|
||||
} // namespace sls
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -14,10 +16,18 @@ Still this is better than strcpy and a buffer overflow...
|
||||
*/
|
||||
template <size_t array_size>
|
||||
void strcpy_safe(char (&destination)[array_size], const char *source) {
|
||||
assert(array_size > strlen(source));
|
||||
strncpy(destination, source, array_size-1);
|
||||
destination[array_size - 1] = '\0';
|
||||
}
|
||||
|
||||
template <size_t array_size>
|
||||
void strcpy_safe(char (&destination)[array_size], const std::string& source) {
|
||||
assert(array_size > source.size());
|
||||
strncpy(destination, source.c_str(), array_size-1);
|
||||
destination[array_size - 1] = '\0';
|
||||
}
|
||||
|
||||
/*
|
||||
Removes all occurrences of the specified char from a c string
|
||||
Templated on array size to ensure no access after buffer limits.
|
||||
@ -51,6 +61,12 @@ Concatenate strings using + if the strings are different
|
||||
*/
|
||||
std::string concatenateIfDifferent(const std::vector<std::string> &container);
|
||||
|
||||
/*
|
||||
Concatenate vector of things with str method using + if the strings are different
|
||||
*/
|
||||
template<typename T>
|
||||
std::string concatenateIfDifferent(const std::vector<T> &container);
|
||||
|
||||
/*
|
||||
Convert an ip address string to a string in hex format. (removing dots)
|
||||
*/
|
||||
|
@ -17,6 +17,7 @@ namespace sls {
|
||||
|
||||
IpAddr::IpAddr(uint32_t address) : addr_{address} {}
|
||||
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_); }
|
||||
std::string IpAddr::str() const {
|
||||
char ipstring[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &addr_, ipstring, INET_ADDRSTRLEN);
|
||||
@ -31,6 +32,7 @@ std::string IpAddr::hex() const {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
MacAddr::MacAddr(uint64_t mac) : addr_{mac} {}
|
||||
MacAddr::MacAddr(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
@ -39,8 +41,9 @@ MacAddr::MacAddr(std::string mac) {
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::stol(mac, nullptr, 16);
|
||||
}
|
||||
MacAddr::MacAddr(const char *address) : MacAddr(std::string(address)) {}
|
||||
|
||||
std::string MacAddr::to_hex(const char delimiter) {
|
||||
std::string MacAddr::to_hex(const char delimiter) const{
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((addr_ >> 40) & 0xFF);
|
||||
@ -52,6 +55,14 @@ std::string MacAddr::to_hex(const char delimiter) {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
std::string MacAddrToString(uint64_t mac) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
#include "string_utils.h"
|
||||
#include "container_utils.h"
|
||||
#include "network_utils.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
@ -38,6 +39,22 @@ std::string concatenateIfDifferent(const std::vector<std::string>& container)
|
||||
return result;
|
||||
}
|
||||
}
|
||||
template<typename T>
|
||||
std::string concatenateIfDifferent(const std::vector<T>& container)
|
||||
{
|
||||
if (allEqual(container)) {
|
||||
return container.front().str();
|
||||
} else {
|
||||
std::string result;
|
||||
for (const auto& s : container)
|
||||
result += s.str() + '+';
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
template std::string concatenateIfDifferent(const std::vector<IpAddr>&);
|
||||
template std::string concatenateIfDifferent(const std::vector<MacAddr>&);
|
||||
|
||||
|
||||
std::string stringIpToHex(const std::string& ip)
|
||||
{
|
||||
|
@ -21,6 +21,23 @@ TEST_CASE("Convert mac address") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert mac address using classes") {
|
||||
|
||||
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
|
||||
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", "ee:50:22:46:d9:f4"};
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto mac0 = MacAddr(vec_addr[i]);
|
||||
auto mac1 = MacAddr(vec_ans[i]);
|
||||
|
||||
CHECK(mac0 == vec_addr[i]);
|
||||
CHECK(mac1 == vec_addr[i]);
|
||||
CHECK(mac0 == vec_ans[i]);
|
||||
CHECK(mac1 == vec_ans[i]);
|
||||
CHECK(mac0.str() == vec_ans[i]);
|
||||
CHECK(mac1.str() == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
|
||||
@ -35,6 +52,24 @@ TEST_CASE("Convert IP") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP using classes ") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
|
||||
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto ip0 = IpAddr(vec_addr[i]);
|
||||
auto ip1 = IpAddr(vec_ans[i]);
|
||||
|
||||
CHECK(ip0 == ip1);
|
||||
CHECK(ip0 == vec_addr[i]);
|
||||
CHECK(ip1 == vec_addr[i]);
|
||||
CHECK(ip0 == vec_ans[i]);
|
||||
CHECK(ip1 == vec_ans[i]);
|
||||
CHECK(ip0.str() == vec_ans[i]);
|
||||
CHECK(ip1.str() == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("IP not valid") {
|
||||
|
||||
CHECK(IpStringToUint("hej") == 0);
|
||||
|
@ -110,4 +110,6 @@ TEST_CASE("Many characters in a row"){
|
||||
char str[] = "someeequitellll::ongstring";
|
||||
sls::removeChar(str, 'l');
|
||||
REQUIRE(std::string(str) == "someeequite::ongstring");
|
||||
}
|
||||
}
|
||||
|
||||
// TEST_CASE("concat things not being strings")
|
Reference in New Issue
Block a user