| return 4; | return 4; | ||||
| } | } | ||||
| bool IPAddress::fromString(const char *address) | |||||
| { | |||||
| 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; | |||||
| } | |||||
| #endif | #endif |
| 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() { return _address.dword; }; | operator uint32_t() { return _address.dword; }; |
| } | } | ||||
| } | } | ||||
| bool IPAddress::fromString(const char *address) | |||||
| { | |||||
| unsigned int acc = 0; // Accumulator | |||||
| unsigned int 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; | |||||
| } | |||||
| #define IPAddress_h | #define IPAddress_h | ||||
| #include <Printable.h> | #include <Printable.h> | ||||
| #include <WString.h> | |||||
| // A class to make it easier to handle and pass around IP addresses | // A class to make it easier to handle and pass around IP addresses | ||||
| _address.bytes[3] = *address++; | _address.bytes[3] = *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 () { | operator uint32_t () { |