CVB++ 15.1
Loading...
Searching...
No Matches
detail_network.hpp
1#pragma once
2
3#include "../../global.hpp"
4#include "../../string.hpp"
5#include "../ui.hpp"
6
7#include <sstream>
8
9namespace Cvb
10{
11
12 CVB_BEGIN_INLINE_NS
13
14 namespace UI
15 {
16 namespace Private
17 {
18 inline uint32_t IPV4AddressStringToInt(const std::string &ip)
19 {
20 std::istringstream iss(ip);
21
22 uint32_t ipv4 = 0;
23 for (int i = 0; i < 4; ++i)
24 {
25 uint32_t part;
26 iss >> part;
27 if (iss.fail() || part > 255)
28 throw std::runtime_error("Invalid IP address");
29
30 ipv4 |= part << (8 * (3 - i));
31
32 if (i != 3)
33 {
34 char delimiter;
35 iss >> delimiter;
36 if (iss.fail() || delimiter != '.')
37 throw std::runtime_error("Invalid IP address");
38 }
39 }
40
41 return ipv4;
42 }
43
44 inline std::string IPV4AddressIntToString(uint32_t ip)
45 {
46 std::ostringstream oss;
47 oss << ((ip & 0xFF000000) >> 24) << '.'
48 << ((ip & 0x00FF0000) >> 16) << '.'
49 << ((ip & 0x0000FF00) >> 8) << '.'
50 << (ip & 0x000000FF);
51
52 return oss.str();
53 }
54
55 class MacAddress final
56 {
57
58 public:
59 enum MacDelimiter
60 {
61 Minus,
62 Colon
63 };
64
65 explicit MacAddress(int64_t addr) noexcept
66 {
67 Convert(addr);
68 Check();
69 }
70
71 explicit MacAddress(const String &addr) noexcept
72 : address_(UI::CvbToQt(addr))
73 {
74
75 Check();
76 }
77
78 explicit MacAddress(const QString &addr) noexcept
79 : address_(addr)
80 {
81 Check();
82 }
83
84 bool operator==(const MacAddress &addr) const noexcept
85 {
86 return (address_ == addr.address_);
87 }
88
89 bool operator!=(const MacAddress &addr) const noexcept
90 {
91 return !(*this == addr);
92 }
93
94 String ToString()
95 {
96 return UI::QtToCvb(address_);
97 }
98
99 QString ToQString()
100 {
101 return address_;
102 }
103
104 int64_t ToInt64()
105 {
106 if (address_.isEmpty())
107 return 0;
108
109 return Int64();
110 }
111
112 bool IsValid()
113 {
114 return !(ToInt64() == 0);
115 }
116
117 void SetDelimiter(const MacDelimiter delimiter)
118 {
119 separator_ = delimiter;
120 }
121
122 MacDelimiter Delimiter()
123 {
124 return separator_;
125 }
126
127 private:
128 void Check()
129 {
130 QRegularExpression rx(macRegExp);
131 auto match = rx.match(address_);
132 if (!match.hasMatch())
133 address_ = defaultAddress_;
134
135 ChangeSeparator();
136 }
137
138 void ChangeSeparator()
139 {
140 if (address_.count(Separator()) == 5)
141 return;
142 else
143 {
144 switch (separator_)
145 {
146 case Colon:
147 address_.replace('-', ':');
148 break;
149 case Minus:
150 default:
151 address_.replace(':', '-');
152 break;
153 }
154 }
155 }
156
157 void Convert(int64_t value)
158 {
159 auto tmp = QString("%1").arg(value, 8, 16, QLatin1Char('0'));
160 auto pos = tmp.size() - 2;
161 auto count = 0;
162 while (pos > 0)
163 {
164 tmp.insert(pos, Separator());
165 pos -= 2;
166 ++count;
167 }
168 while (count < 5)
169 {
170 tmp.insert(0, Separator()).insert(0, "00");
171 ++count;
172 }
173 address_ = tmp;
174 }
175
176 int64_t Int64()
177 {
178 auto tmp = address_;
179 tmp.remove(':').remove('-');
180 bool valid = false;
181 return static_cast<int64_t>(tmp.toLongLong(&valid, 16));
182 }
183
184 QChar Separator()
185 {
186 if (separator_ == Colon)
187 return ':';
188 return '-';
189 }
190
191 QString address_;
192 const QString defaultAddress_ = "00-00-00-00-00-00";
193 MacDelimiter separator_ = Minus;
194 const QString macRegExp = "([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})";
195 };
196
197 } /* namespace Private */
198 } /* namespace UI */
199
200 CVB_END_INLINE_NS
201
202} /* namespace Cvb */
T count(T... args)
Namespace for user interface components.
Definition decl_image_scene.hpp:39
Cvb::String QtToCvb(const QString text) noexcept
Convenience converter for strings.
Definition ui.hpp:242
QString CvbToQt(const Cvb::String &text) noexcept
Convenience converter for strings.
Definition ui.hpp:257
Root namespace for the Image Manager interface.
Definition version.hpp:11
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49