File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
4343 memcpy (_address.bytes , address, sizeof (_address.bytes ));
4444}
4545
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+
4688IPAddress& IPAddress::operator =(const uint8_t *address)
4789{
4890 memcpy (_address.bytes , address, sizeof (_address.bytes ));
Original file line number Diff line number Diff line change 2121#define IPAddress_h
2222
2323#include < stdint.h>
24- #include < Printable.h>
24+ #include " Printable.h"
25+ #include " WString.h"
2526
2627// A class to make it easier to handle and pass around IP addresses
2728
@@ -45,6 +46,9 @@ class IPAddress : public Printable {
4546 IPAddress (uint32_t address);
4647 IPAddress (const uint8_t *address);
4748
49+ bool fromString (const char *address);
50+ bool fromString (const String &address) { return fromString (address.c_str ()); }
51+
4852 // Overloaded cast operator to allow IPAddress objects to be used where a pointer
4953 // to a four-byte uint8_t array is expected
5054 operator uint32_t () const { return _address.dword ; };
@@ -71,5 +75,4 @@ class IPAddress : public Printable {
7175
7276const IPAddress INADDR_NONE (0 ,0 ,0 ,0 );
7377
74-
7578#endif
You can’t perform that action at this time.
0 commit comments