From e7ff9a46e35d79ecc8a33c6be949673d52c5e34d Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 29 Jun 2011 21:24:06 +1000 Subject: [PATCH] Implement parse in terms of string constructor. --- ip.cpp.rl | 25 +++++++++++++++++-------- ip.hpp | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ip.cpp.rl b/ip.cpp.rl index e22de767..0eb397f6 100644 --- a/ip.cpp.rl +++ b/ip.cpp.rl @@ -27,8 +27,12 @@ using namespace std; -const ipv4::ip ipv4::ip::LOOPBACK ({ 127, 0, 0, 1 }); -const ipv4::ip ipv4::ip::ANY ({ 0, 0, 0, 0 }); +const ipv4::ip ipv4::ip::LOOPBACK (127, 0, 0, 1); +const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0); + +const range ipv4::WELL_KNOWN_PORT ( 0, 1023), + ipv4::REGISTERED_PORT ( 1024, 49151), + ipv4::PRIVATE_PORT (49152, 65535); ipv4::ip::ip (uint8_t a, uint8_t b, uint8_t c, uint8_t d): @@ -86,8 +90,7 @@ ipv4::ip::operator == (const ipv4::ip &rhs) const { %%write data; -ipv4::ip -ipv4::ip::parse (const string &data) { +ipv4::ip::ip (const std::string &data) { bool __success = true; uint8_t __octets[4]; const char *octetstart, *octetend; @@ -104,8 +107,14 @@ ipv4::ip::parse (const string &data) { if (!__success) throw invalid_argument(data); - return ipv4::ip(__octets[0], - __octets[1], - __octets[2], - __octets[3]); + m_octets[0] = __octets[0]; + m_octets[1] = __octets[1]; + m_octets[2] = __octets[2]; + m_octets[3] = __octets[3]; } + + +ipv4::ip +ipv4::ip::parse (const string &data) + { return ipv4::ip (data); } + diff --git a/ip.hpp b/ip.hpp index 1078e767..7e069227 100644 --- a/ip.hpp +++ b/ip.hpp @@ -31,6 +31,7 @@ namespace ipv4 { uint32_t m_integer; }; + ip (const std::string &); ip (uint8_t a, uint8_t b, uint8_t c, uint8_t d); ip& operator = (const ip &);