-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.cpp
More file actions
84 lines (63 loc) · 1.62 KB
/
ip.cpp
File metadata and controls
84 lines (63 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "ip.hh"
namespace updater {
ip::ip()
{
}
ip::ip(std::string dotted_decimal)
{
std::vector<unsigned char> decimals = {};
const std::string delimiter = ".";
size_t pos = 0;
unsigned d;
while ((pos = dotted_decimal.find(delimiter)) != std::string::npos)
{
std::string token = dotted_decimal.substr(0, pos);
d = std::stoul(token);
decimals.push_back(d);
dotted_decimal.erase(0, pos + delimiter.length());
}
d = std::stoul(dotted_decimal);
decimals.push_back(d);
if (decimals.size() != 4) throw new std::exception;
_a = decimals[0];
_b = decimals[1];
_c = decimals[2];
_d = decimals[3];
}
ip::ip(const unsigned char &a, const unsigned char &b, const unsigned char &c, const unsigned char &d)
{
_a = a;
_b = b;
_c = c;
_d = d;
}
std::string ip::toString() const
{
unsigned int a = static_cast<unsigned>(_a);
unsigned int b = static_cast<unsigned>(_b);
unsigned int c = static_cast<unsigned>(_c);
unsigned int d = static_cast<unsigned>(_d);
std::string ip = "";
ip.append(std::to_string(a));
ip.append(".");
ip.append(std::to_string(b));
ip.append(".");
ip.append(std::to_string(c));
ip.append(".");
ip.append(std::to_string(d));
return ip;
}
std::vector<unsigned char> ip::getDecimals() const
{
return std::vector<unsigned char> {_a, _b, _c, _d};
}
bool ip::operator==(const ip &other) const
{
bool a = _a == other._a;
bool b = _b == other._b;
bool c = _c == other._c;
bool d = _d == other._d;
if (a && b && c && d) return true;
return false;
}
}