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