Commit b5ca6ac3 authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

[2369] Make InputSource manage opening files internally

parent 2c8d3ac2
......@@ -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()) {
......
......@@ -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
......
......@@ -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) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment