aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/IPAddress.cpp
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@arduino.cc>2015-09-18 11:30:45 +0200
committerCristian Maglie <c.maglie@arduino.cc>2015-09-18 11:30:45 +0200
commite42c3ccbdb22dd1544c0d834e381c5966bfe9e69 (patch)
treeade64a8ca680eeefbabb09ebc33d605a123c234a /cores/arduino/IPAddress.cpp
parentb5911b2bc58e2cee759bd8bcd0c78fdc2d76e5db (diff)
parent5a46ff0b10ff0204e1ccbf5881d8d37afaaf418f (diff)
Merge branch 'ethernet-dns-fix' of https://github.com/cmaglie/Arduino
Diffstat (limited to 'cores/arduino/IPAddress.cpp')
-rw-r--r--cores/arduino/IPAddress.cpp42
1 files changed, 42 insertions, 0 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));