mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-19 08:17:13 +02:00
Merge branch 'refactor' of github.com:slsdetectorgroup/slsDetectorPackage into refactor
This commit is contained in:
45
slsSupportLib/include/network_utils.h
Normal file
45
slsSupportLib/include/network_utils.h
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace sls {
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
|
||||
class IpAddr {
|
||||
private:
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
constexpr IpAddr(uint32_t address) noexcept : addr_{address} {}
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
constexpr bool operator==(const IpAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||
constexpr bool operator!=(const IpAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||
constexpr bool operator==(const uint32_t other) const noexcept { return addr_ == other; }
|
||||
constexpr bool operator!=(const uint32_t other) const noexcept { return addr_ != other; }
|
||||
};
|
||||
|
||||
class MacAddr {
|
||||
private:
|
||||
uint64_t addr_{0};
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
constexpr MacAddr(uint64_t mac) noexcept : addr_{mac} {}
|
||||
MacAddr(std::string mac);
|
||||
MacAddr(const char *address);
|
||||
std::string str() const;
|
||||
std::string hex() const;
|
||||
constexpr bool operator==(const MacAddr &other) const noexcept { return addr_ == other.addr_; }
|
||||
constexpr bool operator!=(const MacAddr &other) const noexcept { return addr_ != other.addr_; }
|
||||
constexpr bool operator==(const uint64_t other) const noexcept { return addr_ == other; }
|
||||
constexpr bool operator!=(const uint64_t other) const noexcept { return addr_ != other; }
|
||||
};
|
||||
|
||||
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)
|
||||
*/
|
||||
|
Reference in New Issue
Block a user