Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3909b4

Browse files
committed
Added IPAddress::fromString(....) function
1 parent 306eaa1 commit e3909b4

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

‎hardware/arduino/avr/cores/arduino/IPAddress.cpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
4343
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

46+
bool IPAddress::fromString(const char *address)
47+
{
48+
// TODO: add support for "a", "a.b", "a.b.c" formats
49+
50+
uint16_t acc = 0; // Accumulator
51+
uint8_t dots = 0;
52+
53+
while (*address)
54+
{
55+
char c = *address++;
56+
if (c >= '0' && c <= '9')
57+
{
58+
acc = acc * 10 + (c - '0');
59+
if (acc > 255) {
60+
// Value out of [0..255] range
61+
return false;
62+
}
63+
}
64+
else if (c == '.')
65+
{
66+
if (dots == 3) {
67+
// Too much dots (there must be 3 dots)
68+
return false;
69+
}
70+
_address.bytes[dots++] = acc;
71+
acc = 0;
72+
}
73+
else
74+
{
75+
// Invalid char
76+
return false;
77+
}
78+
}
79+
80+
if (dots != 3) {
81+
// Too few dots (there must be 3 dots)
82+
return false;
83+
}
84+
_address.bytes[3] = acc;
85+
return true;
86+
}
87+
4688
IPAddress& IPAddress::operator=(const uint8_t *address)
4789
{
4890
memcpy(_address.bytes, address, sizeof(_address.bytes));

‎hardware/arduino/avr/cores/arduino/IPAddress.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class IPAddress : public Printable {
4545
IPAddress(uint32_t address);
4646
IPAddress(const uint8_t *address);
4747

48+
bool fromString(const char *address);
49+
bool fromString(const String &address) { return fromString(address.c_str()); }
50+
4851
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4952
// to a four-byte uint8_t array is expected
5053
operator uint32_t() const { return _address.dword; };
@@ -71,5 +74,4 @@ class IPAddress : public Printable {
7174

7275
const IPAddress INADDR_NONE(0,0,0,0);
7376

74-
7577
#endif

‎hardware/arduino/sam/cores/arduino/IPAddress.cpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
4343
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

46+
bool IPAddress::fromString(const char *address)
47+
{
48+
// TODO: add support for "a", "a.b", "a.b.c" formats
49+
50+
uint16_t acc = 0; // Accumulator
51+
uint8_t dots = 0;
52+
53+
while (*address)
54+
{
55+
char c = *address++;
56+
if (c >= '0' && c <= '9')
57+
{
58+
acc = acc * 10 + (c - '0');
59+
if (acc > 255) {
60+
// Value out of [0..255] range
61+
return false;
62+
}
63+
}
64+
else if (c == '.')
65+
{
66+
if (dots == 3) {
67+
// Too much dots (there must be 3 dots)
68+
return false;
69+
}
70+
_address.bytes[dots++] = acc;
71+
acc = 0;
72+
}
73+
else
74+
{
75+
// Invalid char
76+
return false;
77+
}
78+
}
79+
80+
if (dots != 3) {
81+
// Too few dots (there must be 3 dots)
82+
return false;
83+
}
84+
_address.bytes[3] = acc;
85+
return true;
86+
}
87+
4688
IPAddress& IPAddress::operator=(const uint8_t *address)
4789
{
4890
memcpy(_address.bytes, address, sizeof(_address.bytes));

‎hardware/arduino/sam/cores/arduino/IPAddress.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class IPAddress : public Printable {
4545
IPAddress(uint32_t address);
4646
IPAddress(const uint8_t *address);
4747

48+
bool fromString(const char *address);
49+
bool fromString(const String &address) { return fromString(address.c_str()); }
50+
4851
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4952
// to a four-byte uint8_t array is expected
5053
operator uint32_t() const { return _address.dword; };
@@ -71,5 +74,4 @@ class IPAddress : public Printable {
7174

7275
const IPAddress INADDR_NONE(0,0,0,0);
7376

74-
7577
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /