Skip to content

Commit 68701bf

Browse files
committed
Merge pull request esp8266#1457 from martinayotte/master
manual merge of IPAddress from Arduino.cc
2 parents 371c788 + c1e91c7 commit 68701bf

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

cores/esp8266/IPAddress.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
4384
IPAddress& IPAddress::operator=(const uint8_t *address) {
4485
memcpy(_address.bytes, address, sizeof(_address.bytes));
4586
return *this;

cores/esp8266/IPAddress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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 {

0 commit comments

Comments
 (0)