36 #ifndef RCDISCOVER_UTILS_H 37 #define RCDISCOVER_UTILS_H 47 std::ostringstream out;
49 out << std::hex << std::setfill(
'0');
50 out << std::setw(2) << ((mac>>40)&0xff) <<
':' << std::setw(2) << ((mac>>32)&0xff) <<
':' 51 << std::setw(2) << ((mac>>24)&0xff) <<
':' << std::setw(2) << ((mac>>16)&0xff) <<
':' 52 << std::setw(2) << ((mac>>8)&0xff) <<
':' << std::setw(2) << (mac&0xff);
59 std::ostringstream out;
61 out << ((ip>>24)&0xff) <<
'.' << ((ip>>16)&0xff) <<
'.' 62 << ((ip>>8)&0xff) <<
'.' << (ip&0xff);
68 std::array<std::string, n>
split(
const std::string& s,
const char sep)
70 std::array<std::string, n> result;
74 if (s.front() == sep || s.back() == sep)
76 throw std::invalid_argument(
"strings starts or ends with separator");
80 std::istringstream iss(s);
81 for (uint32_t i = 0; i < n; ++i)
83 if (!std::getline(iss, result[i], sep))
85 throw std::out_of_range(
"n");
90 if (std::getline(iss, tmp, sep))
92 throw std::out_of_range(
"n");
103 const auto splitted = split<n>(s, sep);
105 std::array<uint8_t, n> result;
107 std::transform(std::begin(splitted),
110 [&base](
const std::string& s) -> std::uint8_t
112 const auto v = std::stoul(s,
nullptr, base);
115 throw std::out_of_range(
"number is larger than 255");
117 return static_cast<std::uint8_t
>(v);
123 inline std::array<uint8_t, 6>
string2mac(
const std::string& mac)
125 return string2byte<6>(mac, 16,
':');
128 inline std::array<uint8_t, 4>
string2ip(
const std::string& ip)
130 return string2byte<4>(ip, 10,
'.');
143 template<std::
size_t N>
149 for (std::size_t i = 0; i < N; ++i)
151 result |= (
static_cast<ReturnType
>(a[i]) << ((N - 1 - i) * 8));
157 std::string::const_iterator str_last,
158 std::string::const_iterator p_first,
159 std::string::const_iterator p_last)
161 if (str_first == str_last && p_first == p_last)
164 if (str_first == str_last)
169 return std::next(p_first) == p_last;
173 if (p_first == p_last)
178 if (*p_first ==
'?' || std::tolower(*p_first) == std::tolower(*str_first))
181 std::next(p_first), p_last);
186 return wildcardMatch(std::next(str_first), str_last, p_first, p_last) ||
187 wildcardMatch(str_first, str_last, std::next(p_first), p_last);
193 #endif // RCDISCOVER_UTILS_H
std::array< uint8_t, 6 > string2mac(const std::string &mac)
MinFittingType< N >::type byteArrayToInt(const std::array< std::uint8_t, N > &a)
bool wildcardMatch(std::string::const_iterator str_first, std::string::const_iterator str_last, std::string::const_iterator p_first, std::string::const_iterator p_last)
std::string ip2string(const uint32_t ip)
std::array< uint8_t, n > string2byte(const std::string &s, const int base, const char sep)
std::string mac2string(const uint64_t mac)
std::array< std::string, n > split(const std::string &s, const char sep)
std::array< uint8_t, 4 > string2ip(const std::string &ip)