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 @@ -79,3 +79,44 @@ String IPAddress::toString()
7979 return String (szRet);
8080}
8181
82+ bool IPAddress::fromString (const char *address)
83+ {
84+ // TODO: add support for "a", "a.b", "a.b.c" formats
85+
86+ uint16_t acc = 0 ; // Accumulator
87+ uint8_t dots = 0 ;
88+
89+ while (*address)
90+ {
91+ char c = *address++;
92+ if (c >= ' 0' && c <= ' 9' )
93+ {
94+ acc = acc * 10 + (c - ' 0' );
95+ if (acc > 255 ) {
96+ // Value out of [0..255] range
97+ return false ;
98+ }
99+ }
100+ else if (c == ' .' )
101+ {
102+ if (dots == 3 ) {
103+ // Too much dots (there must be 3 dots)
104+ return false ;
105+ }
106+ _address.bytes [dots++] = acc;
107+ acc = 0 ;
108+ }
109+ else
110+ {
111+ // Invalid char
112+ return false ;
113+ }
114+ }
115+
116+ if (dots != 3 ) {
117+ // Too few dots (there must be 3 dots)
118+ return false ;
119+ }
120+ _address.bytes [3 ] = acc;
121+ return true ;
122+ }
Original file line number Diff line number Diff line change @@ -51,6 +51,9 @@ class IPAddress: public Printable
5151 IPAddress (const uint8_t *address);
5252 virtual ~IPAddress () {}
5353
54+ bool fromString (const char *address);
55+ bool fromString (const String &address) { return fromString (address.c_str ()); }
56+
5457 // Overloaded cast operator to allow IPAddress objects to be used where a pointer
5558 // to a four-byte uint8_t array is expected
5659 operator uint32_t () const
You can’t perform that action at this time.
0 commit comments