From 67d69026c64a96b4e0dc64391b8bdcf43a187fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Fri, 31 Jul 2009 20:39:13 +0200 Subject: HostAddress: Add constructor from String. diff --git a/Swiften/Network/HostAddress.cpp b/Swiften/Network/HostAddress.cpp index 84a0012..ea324cb 100644 --- a/Swiften/Network/HostAddress.cpp +++ b/Swiften/Network/HostAddress.cpp @@ -1,10 +1,14 @@ #include "Swiften/Network/HostAddress.h" #include +#include #include #include #include +#include "Swiften/Base/foreach.h" +#include "Swiften/Base/String.h" + namespace Swift { HostAddress::HostAddress() { @@ -13,6 +17,14 @@ HostAddress::HostAddress() { } } +HostAddress::HostAddress(const String& address) { + std::vector components = address.split('.'); + assert(components.size() == 4); + foreach(const String& component, components) { + address_.push_back(boost::lexical_cast(component.getUTF8String())); + } +} + HostAddress::HostAddress(const unsigned char* address, int length) { assert(length == 4 || length == 16); address_.reserve(length); diff --git a/Swiften/Network/HostAddress.h b/Swiften/Network/HostAddress.h index 2c9760d..fa34df4 100644 --- a/Swiften/Network/HostAddress.h +++ b/Swiften/Network/HostAddress.h @@ -1,13 +1,15 @@ -#ifndef SWIFTEN_HOSTADDRESS -#define SWIFTEN_HOSTADDRESS +#pragma once #include #include namespace Swift { + class String; + class HostAddress { public: HostAddress(); + HostAddress(const String&); HostAddress(const unsigned char* address, int length); const std::vector& getRawAddress() const { @@ -20,5 +22,3 @@ namespace Swift { std::vector address_; }; } - -#endif diff --git a/Swiften/Network/UnitTest/HostAddressTest.cpp b/Swiften/Network/UnitTest/HostAddressTest.cpp index b805647..50e9198 100644 --- a/Swiften/Network/UnitTest/HostAddressTest.cpp +++ b/Swiften/Network/UnitTest/HostAddressTest.cpp @@ -2,18 +2,23 @@ #include #include "Swiften/Network/HostAddress.h" +#include "Swiften/Base/String.h" using namespace Swift; -class HostAddressTest : public CppUnit::TestFixture -{ +class HostAddressTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(HostAddressTest); + CPPUNIT_TEST(testConstructor); CPPUNIT_TEST(testToString); CPPUNIT_TEST(testToString_IPv6); CPPUNIT_TEST_SUITE_END(); public: - HostAddressTest() {} + void testConstructor() { + HostAddress testling("192.168.1.254"); + + CPPUNIT_ASSERT_EQUAL(std::string("192.168.1.254"), testling.toString()); + } void testToString() { unsigned char address[4] = {10, 0, 1, 253}; -- cgit v0.10.2-6-g49f6