Skip to content

Commit e7f2ade

Browse files
committed
SF 3558085: Add formatter to MACAddress object
1 parent 84ca64e commit e7f2ade

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎CHANGELOG‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Release 1.5.0 (2012-08-??)
2626
- UTF portability improvements
2727
- fixed SF#3556186: Linux shouldn't use <net/if.h> in Net/SocketDefs.h
2828
- added IPAddress RFC 4291 compatible site-local prefix support
29+
- fixed SF# 3012166: IPv6 patch
30+
- added SF# 3558085: Add formatter to MACAddress object
2931

3032
Release 1.4.4 (2012-08-??)
3133
==========================

‎Net/include/Poco/Net/NetworkInterface.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class Net_API NetworkInterface
104104
};
105105

106106
static const unsigned NO_INDEX = ~0;
107+
#if defined(POCO_OS_FAMILY_WINDOWS)
108+
static const char MAC_SEPARATOR = '-';
109+
#else
110+
static const char MAC_SEPARATOR = ':';
111+
#endif
107112

108113
NetworkInterface(unsigned index = NO_INDEX);
109114
/// Creates a NetworkInterface representing the
@@ -318,4 +323,7 @@ inline bool NetworkInterface::operator == (const NetworkInterface& other) const
318323
} } // namespace Poco::Net
319324

320325

326+
Net_API std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac);
327+
328+
321329
#endif // Net_NetworkInterface_INCLUDED

‎Net/src/NetworkInterface.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,29 @@
4848
#include <ipifcons.h>
4949
#endif
5050
#include <cstring>
51+
#include <iostream>
52+
#include <iomanip>
5153

5254

5355
using Poco::NumberFormatter;
5456
using Poco::FastMutex;
5557
using Poco::format;
5658

5759

60+
std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac)
61+
{
62+
std::ios state(0);
63+
state.copyfmt(os);
64+
for (unsigned i = 0; i < mac.size(); ++i)
65+
{
66+
if (i > 0) os << Poco::Net::NetworkInterface::MAC_SEPARATOR;
67+
os << std::hex << std::setw(2) << std::setfill('0') << (unsigned) mac[i];
68+
}
69+
os.copyfmt(state);
70+
return os;
71+
}
72+
73+
5874
namespace Poco {
5975
namespace Net {
6076

‎Net/testsuite/src/NetworkInterfaceTest.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ void NetworkInterfaceTest::testMap()
6969

7070
NetworkInterface::MACAddress mac(it->second.macAddress());
7171
if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK))
72-
{
73-
std::cout << "MAC Address: ";
74-
for (unsigned i = 0; i < mac.size(); ++i)
75-
std::cout << ((i == 0) ? ' ' : ':') << std::hex << std::setw(2) << std::setfill('0') << (unsigned) mac[i];
76-
std::cout << std::dec << std::endl;
77-
}
72+
std::cout << "MAC Address: " << mac << std::endl;
7873

7974
typedef NetworkInterface::AddressList List;
8075
const List& ipList = it->second.addressList();
@@ -108,6 +103,11 @@ void NetworkInterfaceTest::testList()
108103
std::cout << "Index: " << it->index() << std::endl;
109104
std::cout << "Name: " << it->name() << std::endl;
110105
std::cout << "DisplayName: " << it->displayName() << std::endl;
106+
std::cout << "Status: " << (it->isUp() ? "Up" : "Down") << std::endl;
107+
108+
NetworkInterface::MACAddress mac(it->macAddress());
109+
if (!mac.empty() && (it->type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK))
110+
std::cout << "MAC Address: " << mac << std::endl;
111111

112112
typedef NetworkInterface::AddressList List;
113113
const List& ipList = it->addressList();

0 commit comments

Comments
 (0)