mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-08-31 14:05:33 +00:00
[2369] Make InputSource manage opening files internally
This commit is contained in:
@@ -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())
|
||||
{}
|
||||
InputSource(std::istream& input_stream);
|
||||
InputSource(const char* filename);
|
||||
|
||||
const std::string& getName() {
|
||||
~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) {
|
||||
|
Reference in New Issue
Block a user