mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-29 18:52:13 +01:00
Added IPAddress::fromString(....) function
This commit is contained in:
parent
306eaa13c9
commit
e3909b4e2c
@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
|
|||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IPAddress::fromString(const char *address)
|
||||||
|
{
|
||||||
|
// TODO: add support for "a", "a.b", "a.b.c" formats
|
||||||
|
|
||||||
|
uint16_t acc = 0; // Accumulator
|
||||||
|
uint8_t dots = 0;
|
||||||
|
|
||||||
|
while (*address)
|
||||||
|
{
|
||||||
|
char c = *address++;
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
acc = acc * 10 + (c - '0');
|
||||||
|
if (acc > 255) {
|
||||||
|
// Value out of [0..255] range
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '.')
|
||||||
|
{
|
||||||
|
if (dots == 3) {
|
||||||
|
// Too much dots (there must be 3 dots)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_address.bytes[dots++] = acc;
|
||||||
|
acc = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Invalid char
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dots != 3) {
|
||||||
|
// Too few dots (there must be 3 dots)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_address.bytes[3] = acc;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
IPAddress& IPAddress::operator=(const uint8_t *address)
|
IPAddress& IPAddress::operator=(const uint8_t *address)
|
||||||
{
|
{
|
||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
||||||
|
@ -45,6 +45,9 @@ public:
|
|||||||
IPAddress(uint32_t address);
|
IPAddress(uint32_t address);
|
||||||
IPAddress(const uint8_t *address);
|
IPAddress(const uint8_t *address);
|
||||||
|
|
||||||
|
bool fromString(const char *address);
|
||||||
|
bool fromString(const String &address) { return fromString(address.c_str()); }
|
||||||
|
|
||||||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
||||||
// to a four-byte uint8_t array is expected
|
// to a four-byte uint8_t array is expected
|
||||||
operator uint32_t() const { return _address.dword; };
|
operator uint32_t() const { return _address.dword; };
|
||||||
@ -71,5 +74,4 @@ public:
|
|||||||
|
|
||||||
const IPAddress INADDR_NONE(0,0,0,0);
|
const IPAddress INADDR_NONE(0,0,0,0);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
|
|||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IPAddress::fromString(const char *address)
|
||||||
|
{
|
||||||
|
// TODO: add support for "a", "a.b", "a.b.c" formats
|
||||||
|
|
||||||
|
uint16_t acc = 0; // Accumulator
|
||||||
|
uint8_t dots = 0;
|
||||||
|
|
||||||
|
while (*address)
|
||||||
|
{
|
||||||
|
char c = *address++;
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
acc = acc * 10 + (c - '0');
|
||||||
|
if (acc > 255) {
|
||||||
|
// Value out of [0..255] range
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '.')
|
||||||
|
{
|
||||||
|
if (dots == 3) {
|
||||||
|
// Too much dots (there must be 3 dots)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_address.bytes[dots++] = acc;
|
||||||
|
acc = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Invalid char
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dots != 3) {
|
||||||
|
// Too few dots (there must be 3 dots)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_address.bytes[3] = acc;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
IPAddress& IPAddress::operator=(const uint8_t *address)
|
IPAddress& IPAddress::operator=(const uint8_t *address)
|
||||||
{
|
{
|
||||||
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
memcpy(_address.bytes, address, sizeof(_address.bytes));
|
||||||
|
@ -45,6 +45,9 @@ public:
|
|||||||
IPAddress(uint32_t address);
|
IPAddress(uint32_t address);
|
||||||
IPAddress(const uint8_t *address);
|
IPAddress(const uint8_t *address);
|
||||||
|
|
||||||
|
bool fromString(const char *address);
|
||||||
|
bool fromString(const String &address) { return fromString(address.c_str()); }
|
||||||
|
|
||||||
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
||||||
// to a four-byte uint8_t array is expected
|
// to a four-byte uint8_t array is expected
|
||||||
operator uint32_t() const { return _address.dword; };
|
operator uint32_t() const { return _address.dword; };
|
||||||
@ -71,5 +74,4 @@ public:
|
|||||||
|
|
||||||
const IPAddress INADDR_NONE(0,0,0,0);
|
const IPAddress INADDR_NONE(0,0,0,0);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user