BIND 10 trac2369, updated. 9843e33b58ce12f13fc34fe27c7ef0e4042bd506 [2369] Add API documentation for InputSource
BIND 10 source code commits
bind10-changes at lists.isc.org
Tue Oct 30 07:25:09 UTC 2012
The branch, trac2369 has been updated
via 9843e33b58ce12f13fc34fe27c7ef0e4042bd506 (commit)
via b5ca6ac342e49edb73ab75938de20c8fd3f6e8b8 (commit)
from 2c8d3ac2d8d62ef77c0f888a7c334689ebcd9b5b (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 9843e33b58ce12f13fc34fe27c7ef0e4042bd506
Author: Mukund Sivaraman <muks at isc.org>
Date: Tue Oct 30 12:53:46 2012 +0530
[2369] Add API documentation for InputSource
commit b5ca6ac342e49edb73ab75938de20c8fd3f6e8b8
Author: Mukund Sivaraman <muks at isc.org>
Date: Tue Oct 30 12:39:38 2012 +0530
[2369] Make InputSource manage opening files internally
-----------------------------------------------------------------------
Summary of changes:
src/lib/dns/inputsource.cc | 34 ++++++++++++++++++
src/lib/dns/inputsource.h | 54 ++++++++++++++++++++++-------
src/lib/dns/tests/inputsource_unittest.cc | 16 ++++++---
3 files changed, 87 insertions(+), 17 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/dns/inputsource.cc b/src/lib/dns/inputsource.cc
index 5bc7947..1da98b0 100644
--- a/src/lib/dns/inputsource.cc
+++ b/src/lib/dns/inputsource.cc
@@ -14,10 +14,44 @@
#include <dns/inputsource.h>
+#include <cstdio>
+
+using namespace std;
+
namespace isc {
namespace dns {
namespace master_lexer_internal {
+InputSource::InputSource(std::istream& input_stream) :
+ at_eof_(false),
+ line_(1),
+ saved_line_(line_),
+ buffer_pos_(buffer_.size()),
+ input_(input_stream)
+{
+ char buf[FILENAME_MAX];
+ snprintf(buf, sizeof(buf), "stream-%p", &input_stream);
+ name_ = buf;
+}
+
+InputSource::InputSource(const char* filename) :
+ at_eof_(false),
+ line_(1),
+ saved_line_(line_),
+ buffer_pos_(buffer_.size()),
+ name_(filename),
+ input_(file_stream_)
+{
+ file_stream_.open(filename, fstream::in);
+}
+
+InputSource::~InputSource()
+{
+ if (file_stream_.is_open()) {
+ file_stream_.close();
+ }
+}
+
int
InputSource::getChar() {
if (buffer_pos_ == buffer_.size()) {
diff --git a/src/lib/dns/inputsource.h b/src/lib/dns/inputsource.h
index 7c46b93..d45fa28 100644
--- a/src/lib/dns/inputsource.h
+++ b/src/lib/dns/inputsource.h
@@ -18,6 +18,7 @@
#include <exceptions/exceptions.h>
#include <iostream>
+#include <fstream>
#include <string>
#include <vector>
@@ -25,29 +26,47 @@ namespace isc {
namespace dns {
namespace master_lexer_internal {
+/// \brief An input source that is used internally by MasterLexer.
+///
+/// This is a helper internal class for MasterLexer, and represents
+/// state of a single source of the entire zone data to be
+/// parsed. Normally this means the master zone file, but MasterLexer
+/// can have multiple InputSources if $INCLUDE is used. The source can
+/// also be generic input stream (std::istream).
+///
+/// This class is not meant for public use.
class InputSource {
public:
- InputSource(std::istream& input, const std::string& name) :
- input_(input),
- name_(name),
- at_eof_(false),
- line_(1),
- saved_line_(line_),
- buffer_pos_(buffer_.size())
- {}
-
- const std::string& getName() {
+ /// \brief Constructor which takes an input stream. The stream is
+ /// read-from, but it is not closed.
+ InputSource(std::istream& input_stream);
+
+ /// \brief Constructor which takes a filename to read from. The
+ /// associated file stream is managed internally.
+ InputSource(const char* filename);
+
+ /// \brief Destructor
+ ~InputSource();
+
+ /// \brief Returns a name for the InputSource. Typically this is the
+ /// filename, but if the InputSource was constructed for an
+ /// \c std::istream, it returns a name in the format "stream-%p".
+ const std::string& getName() const {
return (name_);
}
+ /// \brief Returns if the input source is at end of file.
bool atEOF() const {
return (at_eof_);
}
+ /// \brief Returns the current line number being read.
size_t getCurrentLine() const {
return (line_);
}
+ /// \brief Saves the current line being read. Later, when
+ /// \c ungetAll() is called, it skips back to the last-saved line.
void saveLine() {
saved_line_ = line_;
}
@@ -60,19 +79,30 @@ public:
{}
};
+ /// \brief Returns a single character from the input source. If end
+ /// of file is reached, -1 is returned.
int getChar();
+
+ /// \brief Skips backward a single character in the input
+ /// source. The last-read character is unget.
void ungetChar();
+
+ /// Forgets everything read so far, and skips back to the position
+ /// where reading started. If \c saveLine() was called previously,
+ /// it sets the current line number to the line number saved then.
void ungetAll();
private:
- std::istream& input_;
- const std::string name_;
bool at_eof_;
size_t line_;
size_t saved_line_;
std::vector<char> buffer_;
size_t buffer_pos_;
+
+ std::string name_;
+ std::fstream file_stream_;
+ std::istream& input_;
};
} // namespace master_lexer_internal
diff --git a/src/lib/dns/tests/inputsource_unittest.cc b/src/lib/dns/tests/inputsource_unittest.cc
index 4c7bf5c..097aded 100644
--- a/src/lib/dns/tests/inputsource_unittest.cc
+++ b/src/lib/dns/tests/inputsource_unittest.cc
@@ -32,27 +32,33 @@ namespace {
class InputSourceTest : public ::testing::Test {
protected:
InputSourceTest() :
- name_("a90wjer"),
str_("Line1 to scan.\nLine2 to scan.\nLine3 to scan.\n"),
str_length_(strlen(str_)),
iss_(str_),
- source_(iss_, name_)
+ source_(iss_)
{}
- string name_;
const char* str_;
- size_t str_length_;
+ const size_t str_length_;
stringstream iss_;
InputSource source_;
};
// Test the default return values set during InputSource construction.
TEST_F(InputSourceTest, defaults) {
- EXPECT_EQ(name_, source_.getName());
EXPECT_EQ(1, source_.getCurrentLine());
EXPECT_FALSE(source_.atEOF());
}
+// getName() on file and stream sources
+TEST_F(InputSourceTest, getName) {
+ EXPECT_EQ(0, source_.getName().find("stream-"));
+
+ // Use some file; doesn't really matter what.
+ InputSource source2(TEST_DATA_SRCDIR "/masterload.txt");
+ EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", source2.getName());
+}
+
// getChar() should return characters from the input stream in
// sequence. ungetChar() should skip backwards.
TEST_F(InputSourceTest, getAndUngetChar) {
More information about the bind10-changes
mailing list