File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
43
43
memcpy (_address.bytes , address, sizeof (_address.bytes ));
44
44
}
45
45
46
+ bool IPAddress::fromString (const char *address)
47
+ {
48
+ // TODO: add support for "a", "a.b", "a.b.c" formats
49
+
50
+ uint16_t acc = 0 ; // Accumulator
51
+ uint8_t dots = 0 ;
52
+
53
+ while (*address)
54
+ {
55
+ char c = *address++;
56
+ if (c >= ' 0' && c <= ' 9' )
57
+ {
58
+ acc = acc * 10 + (c - ' 0' );
59
+ if (acc > 255 ) {
60
+ // Value out of [0..255] range
61
+ return false ;
62
+ }
63
+ }
64
+ else if (c == ' .' )
65
+ {
66
+ if (dots == 3 ) {
67
+ // Too much dots (there must be 3 dots)
68
+ return false ;
69
+ }
70
+ _address.bytes [dots++] = acc;
71
+ acc = 0 ;
72
+ }
73
+ else
74
+ {
75
+ // Invalid char
76
+ return false ;
77
+ }
78
+ }
79
+
80
+ if (dots != 3 ) {
81
+ // Too few dots (there must be 3 dots)
82
+ return false ;
83
+ }
84
+ _address.bytes [3 ] = acc;
85
+ return true ;
86
+ }
87
+
46
88
IPAddress& IPAddress::operator =(const uint8_t *address)
47
89
{
48
90
memcpy (_address.bytes , address, sizeof (_address.bytes ));
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ class IPAddress : public Printable {
45
45
IPAddress (uint32_t address);
46
46
IPAddress (const uint8_t *address);
47
47
48
+ bool fromString (const char *address);
49
+ bool fromString (const String &address) { return fromString (address.c_str ()); }
50
+
48
51
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
49
52
// to a four-byte uint8_t array is expected
50
53
operator uint32_t () const { return _address.dword ; };
@@ -71,5 +74,4 @@ class IPAddress : public Printable {
71
74
72
75
const IPAddress INADDR_NONE (0 ,0 ,0 ,0 );
73
76
74
-
75
77
#endif
You can’t perform that action at this time.
0 commit comments