2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-01 14:35:29 +00:00

[2369] Make InputSource manage opening files internally

This commit is contained in:
Mukund Sivaraman
2012-10-30 12:39:38 +05:30
parent 2c8d3ac2d8
commit b5ca6ac342
3 changed files with 55 additions and 16 deletions

View File

@@ -14,10 +14,44 @@
#include <dns/inputsource.h> #include <dns/inputsource.h>
#include <cstdio>
using namespace std;
namespace isc { namespace isc {
namespace dns { namespace dns {
namespace master_lexer_internal { 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 int
InputSource::getChar() { InputSource::getChar() {
if (buffer_pos_ == buffer_.size()) { if (buffer_pos_ == buffer_.size()) {

View File

@@ -18,6 +18,7 @@
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <iostream> #include <iostream>
#include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -27,16 +28,12 @@ namespace master_lexer_internal {
class InputSource { class InputSource {
public: public:
InputSource(std::istream& input, const std::string& name) : InputSource(std::istream& input_stream);
input_(input), InputSource(const char* filename);
name_(name),
at_eof_(false),
line_(1),
saved_line_(line_),
buffer_pos_(buffer_.size())
{}
const std::string& getName() { ~InputSource();
const std::string& getName() const {
return (name_); return (name_);
} }
@@ -65,14 +62,16 @@ public:
void ungetAll(); void ungetAll();
private: private:
std::istream& input_;
const std::string name_;
bool at_eof_; bool at_eof_;
size_t line_; size_t line_;
size_t saved_line_; size_t saved_line_;
std::vector<char> buffer_; std::vector<char> buffer_;
size_t buffer_pos_; size_t buffer_pos_;
std::string name_;
std::fstream file_stream_;
std::istream& input_;
}; };
} // namespace master_lexer_internal } // namespace master_lexer_internal

View File

@@ -32,27 +32,33 @@ namespace {
class InputSourceTest : public ::testing::Test { class InputSourceTest : public ::testing::Test {
protected: protected:
InputSourceTest() : InputSourceTest() :
name_("a90wjer"),
str_("Line1 to scan.\nLine2 to scan.\nLine3 to scan.\n"), str_("Line1 to scan.\nLine2 to scan.\nLine3 to scan.\n"),
str_length_(strlen(str_)), str_length_(strlen(str_)),
iss_(str_), iss_(str_),
source_(iss_, name_) source_(iss_)
{} {}
string name_;
const char* str_; const char* str_;
size_t str_length_; const size_t str_length_;
stringstream iss_; stringstream iss_;
InputSource source_; InputSource source_;
}; };
// Test the default return values set during InputSource construction. // Test the default return values set during InputSource construction.
TEST_F(InputSourceTest, defaults) { TEST_F(InputSourceTest, defaults) {
EXPECT_EQ(name_, source_.getName());
EXPECT_EQ(1, source_.getCurrentLine()); EXPECT_EQ(1, source_.getCurrentLine());
EXPECT_FALSE(source_.atEOF()); 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 // getChar() should return characters from the input stream in
// sequence. ungetChar() should skip backwards. // sequence. ungetChar() should skip backwards.
TEST_F(InputSourceTest, getAndUngetChar) { TEST_F(InputSourceTest, getAndUngetChar) {