aboutsummaryrefslogtreecommitdiff
path: root/cores
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@arduino.cc>2015-09-09 11:52:33 +0200
committerCristian Maglie <c.maglie@arduino.cc>2015-09-09 12:03:29 +0200
commit5a46ff0b10ff0204e1ccbf5881d8d37afaaf418f (patch)
treec82c91f25c604506d1f78f79bc3d00277de34083 /cores
parentf3bacff7eea629769601295233c79ff34e834aaf (diff)
Added IPAddress::fromString(....) function
Diffstat (limited to 'cores')
-rw-r--r--cores/arduino/IPAddress.cpp42
-rw-r--r--cores/arduino/IPAddress.h4
2 files changed, 45 insertions, 1 deletions
diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp
index 899cbd4..76aefa8 100644
--- a/cores/arduino/IPAddress.cpp
+++ b/cores/arduino/IPAddress.cpp
@@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
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)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h
index 94acdc4..b20ab58 100644
--- a/cores/arduino/IPAddress.h
+++ b/cores/arduino/IPAddress.h
@@ -45,6 +45,9 @@ public:
IPAddress(uint32_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
// to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; };
@@ -71,5 +74,4 @@ public:
const IPAddress INADDR_NONE(0,0,0,0);
-
#endif