Browse Source

[2572] added MasterLexer::getTotalSourceSize().

JINMEI Tatuya 12 years ago
parent
commit
e81f84bdab

+ 10 - 0
src/lib/dns/master_lexer.cc

@@ -18,6 +18,7 @@
 #include <dns/master_lexer_inputsource.h>
 #include <dns/master_lexer_inputsource.h>
 #include <dns/master_lexer_state.h>
 #include <dns/master_lexer_state.h>
 
 
+#include <boost/foreach.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/lexical_cast.hpp>
 
 
@@ -172,6 +173,15 @@ MasterLexer::getSourceLine() const {
     return (impl_->sources_.back()->getCurrentLine());
     return (impl_->sources_.back()->getCurrentLine());
 }
 }
 
 
+size_t
+MasterLexer::getTotalSourceSize() const {
+    size_t total_size = 0;
+    BOOST_FOREACH(InputSourcePtr& src, impl_->sources_) {
+        total_size += src->getSize();
+    }
+    return (total_size);
+}
+
 const MasterToken&
 const MasterToken&
 MasterLexer::getNextToken(Options options) {
 MasterLexer::getNextToken(Options options) {
     if (impl_->source_ == NULL) {
     if (impl_->source_ == NULL) {

+ 16 - 0
src/lib/dns/master_lexer.h

@@ -443,6 +443,22 @@ public:
     /// \return The current line number of the source (see the description)
     /// \return The current line number of the source (see the description)
     size_t getSourceLine() const;
     size_t getSourceLine() const;
 
 
+    /// \brief Return the total size of pushed sources.
+    ///
+    /// This method returns the sum of the size of sources that have been
+    /// pushed to the lexer by the time of the call.  It would give the
+    /// caller of some hint about the amount of data the lexer is working on.
+    ///
+    /// The size of a normal file is equal to the file size at the time of
+    /// the source is pushed.  The size of other type of input stream is
+    /// the size of the data available in the stream at the time of the
+    /// source is pushed.
+    ///
+    /// If there is no source pushed in the lexer, it returns 0.
+    ///
+    /// \throw None
+    size_t getTotalSourceSize() const;
+
     /// \brief Parse and return another token from the input.
     /// \brief Parse and return another token from the input.
     ///
     ///
     /// It reads a bit of the last opened source and produces another token
     /// It reads a bit of the last opened source and produces another token

+ 2 - 5
src/lib/dns/master_lexer_inputsource.h

@@ -85,11 +85,8 @@ public:
 
 
     /// \brief Returns the size of the input source in bytes.
     /// \brief Returns the size of the input source in bytes.
     ///
     ///
-    /// If the input source is a normal file, the return value should be
-    /// equal to the file size at the time of the source is created.
-    /// If the input source is other type of input stream, its the size of
-    /// the data available in the stream at the time of the construction of
-    /// the source.
+    /// See \c MasterLexer::getTotalSourceSize() for the definition of
+    /// the size of sources.
     ///
     ///
     /// \throw None
     /// \throw None
     size_t getSize() const { return (input_size_); }
     size_t getSize() const { return (input_size_); }

+ 2 - 1
src/lib/dns/tests/master_lexer_inputsource_unittest.cc

@@ -357,7 +357,8 @@ TEST_F(InputSourceTest, getSize) {
 }
 }
 
 
 TEST_F(InputSourceTest, getPosition) {
 TEST_F(InputSourceTest, getPosition) {
-    // Initially the position is set to 0.
+    // Initially the position is set to 0.  Other cases are tested in tests
+    // for get and unget.
     EXPECT_EQ(0, source_.getPosition());
     EXPECT_EQ(0, source_.getPosition());
     EXPECT_EQ(0, InputSource(TEST_DATA_SRCDIR "/masterload.txt").getPosition());
     EXPECT_EQ(0, InputSource(TEST_DATA_SRCDIR "/masterload.txt").getPosition());
 }
 }

+ 10 - 0
src/lib/dns/tests/master_lexer_unittest.cc

@@ -52,6 +52,7 @@ void
 checkEmptySource(const MasterLexer& lexer) {
 checkEmptySource(const MasterLexer& lexer) {
     EXPECT_TRUE(lexer.getSourceName().empty());
     EXPECT_TRUE(lexer.getSourceName().empty());
     EXPECT_EQ(0, lexer.getSourceLine());
     EXPECT_EQ(0, lexer.getSourceLine());
+    EXPECT_EQ(0, lexer.getTotalSourceSize());
 }
 }
 
 
 TEST_F(MasterLexerTest, preOpen) {
 TEST_F(MasterLexerTest, preOpen) {
@@ -61,9 +62,11 @@ TEST_F(MasterLexerTest, preOpen) {
 
 
 TEST_F(MasterLexerTest, pushStream) {
 TEST_F(MasterLexerTest, pushStream) {
     EXPECT_EQ(0, lexer.getSourceCount());
     EXPECT_EQ(0, lexer.getSourceCount());
+    ss << "test";
     lexer.pushSource(ss);
     lexer.pushSource(ss);
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
     EXPECT_EQ(1, lexer.getSourceCount());
     EXPECT_EQ(1, lexer.getSourceCount());
+    EXPECT_EQ(4, lexer.getTotalSourceSize()); // 4 = len("test")
 
 
     // From the point of view of this test, we only have to check (though
     // From the point of view of this test, we only have to check (though
     // indirectly) getSourceLine calls InputSource::getCurrentLine.  It should
     // indirectly) getSourceLine calls InputSource::getCurrentLine.  It should
@@ -85,6 +88,10 @@ TEST_F(MasterLexerTest, pushFile) {
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
     EXPECT_EQ(1, lexer.getSourceLine());
     EXPECT_EQ(1, lexer.getSourceLine());
 
 
+    // 143 = size of the test zone file.  hardcode it assuming it won't change
+    // too often.
+    EXPECT_EQ(143, lexer.getTotalSourceSize());
+
     lexer.popSource();
     lexer.popSource();
     checkEmptySource(lexer);
     checkEmptySource(lexer);
     EXPECT_EQ(0, lexer.getSourceCount());
     EXPECT_EQ(0, lexer.getSourceCount());
@@ -116,16 +123,19 @@ TEST_F(MasterLexerTest, pushFileFail) {
 }
 }
 
 
 TEST_F(MasterLexerTest, nestedPush) {
 TEST_F(MasterLexerTest, nestedPush) {
+    ss << "test";
     lexer.pushSource(ss);
     lexer.pushSource(ss);
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
 
 
     // We can push another source without popping the previous one.
     // We can push another source without popping the previous one.
     lexer.pushSource(TEST_DATA_SRCDIR "/masterload.txt");
     lexer.pushSource(TEST_DATA_SRCDIR "/masterload.txt");
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
+    EXPECT_EQ(143 + 4, lexer.getTotalSourceSize()); // see above for magic nums
 
 
     // popSource() works on the "topmost" (last-pushed) source
     // popSource() works on the "topmost" (last-pushed) source
     lexer.popSource();
     lexer.popSource();
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
     EXPECT_EQ(expected_stream_name, lexer.getSourceName());
+    EXPECT_EQ(4, lexer.getTotalSourceSize());
 
 
     lexer.popSource();
     lexer.popSource();
     EXPECT_TRUE(lexer.getSourceName().empty());
     EXPECT_TRUE(lexer.getSourceName().empty());