Browse Source

[2432] Add a RRsetCollection constructor that takes an std::istream

Mukund Sivaraman 12 years ago
parent
commit
98d515d5e6

+ 13 - 0
src/lib/dns/rrset_collection.cc

@@ -68,6 +68,19 @@ RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
     loader.load();
 }
 
+RRsetCollection::RRsetCollection(std::istream& input_stream, const Name& origin,
+                                 const RRClass& rrclass)
+{
+    MasterLoaderCallbacks callbacks
+        (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
+         boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
+    MasterLoader loader(input_stream, origin, rrclass, callbacks,
+                        boost::bind(&RRsetCollection::addRRset,
+                                    this, _1, _2, _3, _4, _5),
+                        MasterLoader::DEFAULT);
+    loader.load();
+}
+
 const AbstractRRset*
 RRsetCollection::find(const Name& name, const RRType& rrtype,
                       const RRClass& rrclass) const {

+ 15 - 0
src/lib/dns/rrset_collection.h

@@ -43,9 +43,24 @@ public:
     ///
     /// \param filename Name of a file containing a collection of RRs in
     /// the master file format (which may or may not form a valid zone).
+    /// \param origin The zone origin.
+    /// \param rrclass The zone class.
     RRsetCollection(const char* filename, const isc::dns::Name& origin,
                     const isc::dns::RRClass& rrclass);
 
+    /// \brief Constructor.
+    ///
+    /// This constructor is similar to the previous one, but instead of
+    /// taking a filename to load a zone from, it takes an input
+    /// stream. The constructor throws MasterLoaderError if there is an
+    /// error during loading.
+    ///
+    /// \param input_stream The input stream to load from.
+    /// \param origin The zone origin.
+    /// \param rrclass The zone class.
+    RRsetCollection(std::istream& input_stream, const isc::dns::Name& origin,
+                    const isc::dns::RRClass& rrclass);
+
     /// \brief Destructor
     virtual ~RRsetCollection() {}
 

+ 16 - 0
src/lib/dns/tests/rrset_collection_unittest.cc

@@ -19,6 +19,7 @@
 #include <gtest/gtest.h>
 
 #include <list>
+#include <fstream>
 
 using namespace isc::dns;
 using namespace isc::dns::rdata;
@@ -39,6 +40,21 @@ public:
     RRsetCollection collection;
 };
 
+TEST_F(RRsetCollectionTest, istreamConstructor) {
+    std::ifstream fs(TEST_DATA_SRCDIR "/example.org");
+    RRsetCollection collection2(fs, origin, rrclass);
+
+    RRsetCollectionBase::iterator iter = collection.begin();
+    RRsetCollectionBase::iterator iter2 = collection2.begin();
+    while (iter != collection.end()) {
+         EXPECT_TRUE(iter2 != collection2.end());
+         EXPECT_EQ((*iter).toText(), (*iter2).toText());
+         ++iter;
+         ++iter2;
+    }
+    EXPECT_TRUE(iter2 == collection2.end());
+}
+
 TEST_F(RRsetCollectionTest, findBase) {
     // Test the find() that returns isc::dns::AbstractRRset*
     const AbstractRRset* rrset = collection.find(Name("www.example.org"),