]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2369] Make InputSource manage opening files internally
authorMukund Sivaraman <muks@isc.org>
Tue, 30 Oct 2012 07:09:38 +0000 (12:39 +0530)
committerMukund Sivaraman <muks@isc.org>
Tue, 30 Oct 2012 07:12:22 +0000 (12:42 +0530)
src/lib/dns/inputsource.cc
src/lib/dns/inputsource.h
src/lib/dns/tests/inputsource_unittest.cc

index 5bc7947e46f4d787c4905708df15245c382f3415..1da98b0b8de49cef5e985a6390e1540e41abe41d 100644 (file)
 
 #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()) {
index 7c46b9395b56a37b8dcf79875c5deeb657acbdf4..a4522d3023cb6e35e8c36c3d5b8d8ef34a0d1451 100644 (file)
@@ -18,6 +18,7 @@
 #include <exceptions/exceptions.h>
 
 #include <iostream>
+#include <fstream>
 #include <string>
 #include <vector>
 
@@ -27,16 +28,12 @@ namespace master_lexer_internal {
 
 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() {
+    InputSource(std::istream& input_stream);
+    InputSource(const char* filename);
+
+    ~InputSource();
+
+    const std::string& getName() const {
         return (name_);
     }
 
@@ -65,14 +62,16 @@ public:
     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
index 4c7bf5c334724d63a0f21a58b4e622206bba47c0..097adedf9b3e612356b216ccbeb0e7fb4d60c694 100644 (file)
@@ -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) {