diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-31 18:39:13 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-31 18:39:13 (GMT) |
commit | 67d69026c64a96b4e0dc64391b8bdcf43a187fab (patch) | |
tree | 4dce2be022cc052c7744cb6af8d0f1eeb56850ce /Swiften/Network | |
parent | b3e6f21953d5d4b55ab9ffde78e1a1ff2a59dad1 (diff) | |
download | swift-67d69026c64a96b4e0dc64391b8bdcf43a187fab.zip swift-67d69026c64a96b4e0dc64391b8bdcf43a187fab.tar.bz2 |
HostAddress: Add constructor from String.
Diffstat (limited to 'Swiften/Network')
-rw-r--r-- | Swiften/Network/HostAddress.cpp | 12 | ||||
-rw-r--r-- | Swiften/Network/HostAddress.h | 8 | ||||
-rw-r--r-- | Swiften/Network/UnitTest/HostAddressTest.cpp | 11 |
3 files changed, 24 insertions, 7 deletions
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 <boost/numeric/conversion/cast.hpp> +#include <boost/lexical_cast.hpp> #include <cassert> #include <sstream> #include <iomanip> +#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<String> components = address.split('.'); + assert(components.size() == 4); + foreach(const String& component, components) { + address_.push_back(boost::lexical_cast<int>(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 <string> #include <vector> namespace Swift { + class String; + class HostAddress { public: HostAddress(); + HostAddress(const String&); HostAddress(const unsigned char* address, int length); const std::vector<unsigned char>& getRawAddress() const { @@ -20,5 +22,3 @@ namespace Swift { std::vector<unsigned char> 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 <cppunit/extensions/TestFactoryRegistry.h> #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}; |