From d6c1f9aaed7e869efc2be38924ef3dc2d309ff22 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 11 Apr 2008 16:51:58 -0700 Subject: [PATCH] implement operator >> --- src/node/mac48-address.cc | 35 +++++++++++++++++++++++++++++++++-- src/node/mac48-address.h | 3 ++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/node/mac48-address.cc b/src/node/mac48-address.cc index a65941470..9683f80d1 100644 --- a/src/node/mac48-address.cc +++ b/src/node/mac48-address.cc @@ -200,9 +200,40 @@ std::ostream& operator<< (std::ostream& os, const Mac48Address & address) return os; } -std::istream& operator>> (std::istream& is, const Mac48Address & address) +static uint8_t +AsInt (std::string v) { - // XXX ! + std::istringstream iss; + iss.str (v); + uint32_t retval; + iss >> std::hex >> retval >> std::dec; + return retval; +} + +std::istream& operator>> (std::istream& is, Mac48Address & address) +{ + std::string v; + is >> v; + + std::string::size_type col = 0; + for (uint8_t i = 0; i < 6; ++i) + { + std::string tmp; + std::string::size_type next; + next = v.find (":", col); + if (next == std::string::npos) + { + tmp = v.substr (col, v.size ()-col); + address.m_address[i] = AsInt (tmp); + break; + } + else + { + tmp = v.substr (col, next-col); + address.m_address[i] = AsInt (tmp); + col = next + 1; + } + } return is; } diff --git a/src/node/mac48-address.h b/src/node/mac48-address.h index d7077eb88..8e11b739b 100644 --- a/src/node/mac48-address.h +++ b/src/node/mac48-address.h @@ -107,6 +107,7 @@ private: static uint8_t GetType (void); friend bool operator < (const Mac48Address &a, const Mac48Address &b); friend bool operator == (const Mac48Address &a, const Mac48Address &b); + friend std::istream& operator>> (std::istream& is, Mac48Address & address); uint8_t m_address[6]; }; @@ -117,7 +118,7 @@ bool operator == (const Mac48Address &a, const Mac48Address &b); bool operator != (const Mac48Address &a, const Mac48Address &b); bool operator < (const Mac48Address &a, const Mac48Address &b); std::ostream& operator<< (std::ostream& os, const Mac48Address & address); -std::istream& operator>> (std::istream& is, const Mac48Address & address); +std::istream& operator>> (std::istream& is, Mac48Address & address); } // namespace ns3