36 #ifndef RCDISCOVER_UTILS_H
37 #define RCDISCOVER_UTILS_H
48 std::ostringstream out;
50 out << std::hex << std::setfill(
'0');
51 out << std::setw(2) << ((mac>>40)&0xff) <<
':' << std::setw(2) << ((mac>>32)&0xff) <<
':'
52 << std::setw(2) << ((mac>>24)&0xff) <<
':' << std::setw(2) << ((mac>>16)&0xff) <<
':'
53 << std::setw(2) << ((mac>>8)&0xff) <<
':' << std::setw(2) << (mac&0xff);
60 std::ostringstream out;
62 out << ((ip>>24)&0xff) <<
'.' << ((ip>>16)&0xff) <<
'.'
63 << ((ip>>8)&0xff) <<
'.' << (ip&0xff);
69 std::array<std::string, n>
split(
const std::string& s,
const char sep)
71 std::array<std::string, n> result;
75 if (s.front() == sep || s.back() == sep)
77 throw std::invalid_argument(
"strings starts or ends with separator");
81 std::istringstream iss(s);
82 for (uint32_t i = 0; i < n; ++i)
84 if (!std::getline(iss, result[i], sep))
86 throw std::out_of_range(
"n");
91 if (std::getline(iss, tmp, sep))
93 throw std::out_of_range(
"n");
104 const auto splitted = split<n>(s, sep);
106 std::array<uint8_t, n> result;
108 std::transform(std::begin(splitted),
111 [&base](
const std::string& s) -> std::uint8_t
113 const auto v = std::stoul(s,
nullptr, base);
116 throw std::out_of_range(
"number is larger than 255");
118 return static_cast<std::uint8_t
>(v);
124 inline std::array<uint8_t, 6>
string2mac(
const std::string& mac)
126 return string2byte<6>(mac, 16,
':');
129 inline std::array<uint8_t, 4>
string2ip(
const std::string& ip)
131 return string2byte<4>(ip, 10,
'.');
144 template<std::
size_t N>
150 for (std::size_t i = 0; i < N; ++i)
152 result |= (
static_cast<ReturnType
>(a[i]) << ((N - 1 - i) * 8));
158 std::string::const_iterator str_last,
159 std::string::const_iterator p_first,
160 std::string::const_iterator p_last)
162 if (str_first == str_last && p_first == p_last)
165 if (str_first == str_last)
170 return std::next(p_first) == p_last;
174 if (p_first == p_last)
179 if (*p_first ==
'?' || std::tolower(*p_first) == std::tolower(*str_first))
182 std::next(p_first), p_last);
187 return wildcardMatch(std::next(str_first), str_last, p_first, p_last) ||
188 wildcardMatch(str_first, str_last, std::next(p_first), p_last);
194 #endif // RCDISCOVER_UTILS_H