2012-10-26 10:30:09 +05:30
|
|
|
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2012-11-01 09:39:35 +05:30
|
|
|
#include <dns/master_lexer_inputsource.h>
|
2012-10-26 10:30:09 +05:30
|
|
|
|
2012-11-06 09:57:39 +05:30
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
namespace isc {
|
|
|
|
namespace dns {
|
|
|
|
namespace master_lexer_internal {
|
|
|
|
|
2012-10-31 13:04:03 +05:30
|
|
|
namespace { // unnamed namespace
|
|
|
|
|
|
|
|
std::string
|
2012-11-04 22:30:16 +05:30
|
|
|
createStreamName(const std::istream& input_stream) {
|
2012-10-31 13:04:03 +05:30
|
|
|
std::stringstream ss;
|
|
|
|
ss << "stream-" << &input_stream;
|
|
|
|
return (ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of unnamed namespace
|
|
|
|
|
2012-11-06 08:56:50 -08:00
|
|
|
// Explicit definition of class static constant. The value is given in the
|
|
|
|
// declaration so it's not needed here.
|
|
|
|
const int InputSource::END_OF_STREAM;
|
|
|
|
|
2012-10-30 12:39:38 +05:30
|
|
|
InputSource::InputSource(std::istream& input_stream) :
|
|
|
|
at_eof_(false),
|
|
|
|
line_(1),
|
|
|
|
saved_line_(line_),
|
2012-11-01 09:51:49 +05:30
|
|
|
buffer_pos_(0),
|
2012-10-31 13:04:03 +05:30
|
|
|
name_(createStreamName(input_stream)),
|
2012-10-30 12:39:38 +05:30
|
|
|
input_(input_stream)
|
2012-10-31 13:04:03 +05:30
|
|
|
{}
|
2012-10-30 12:39:38 +05:30
|
|
|
|
|
|
|
InputSource::InputSource(const char* filename) :
|
|
|
|
at_eof_(false),
|
|
|
|
line_(1),
|
|
|
|
saved_line_(line_),
|
2012-11-01 09:51:49 +05:30
|
|
|
buffer_pos_(0),
|
2012-10-30 12:39:38 +05:30
|
|
|
name_(filename),
|
|
|
|
input_(file_stream_)
|
|
|
|
{
|
2012-11-06 09:57:39 +05:30
|
|
|
errno = 0;
|
2012-11-04 21:40:33 +05:30
|
|
|
file_stream_.open(filename);
|
2012-11-02 09:57:22 +05:30
|
|
|
if (file_stream_.fail()) {
|
2012-11-06 09:57:39 +05:30
|
|
|
std::string error_txt("Error opening the input source file: ");
|
|
|
|
error_txt += filename;
|
|
|
|
if (errno != 0) {
|
|
|
|
error_txt += "; possible cause: ";
|
|
|
|
error_txt += std::strerror(errno);
|
|
|
|
}
|
|
|
|
isc_throw(OpenError, error_txt);
|
2012-11-02 09:57:22 +05:30
|
|
|
}
|
2012-10-30 12:39:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
InputSource::~InputSource()
|
|
|
|
{
|
|
|
|
if (file_stream_.is_open()) {
|
|
|
|
file_stream_.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
int
|
|
|
|
InputSource::getChar() {
|
|
|
|
if (buffer_pos_ == buffer_.size()) {
|
|
|
|
// We may have reached EOF at the last call to
|
|
|
|
// getChar(). at_eof_ will be set then. We then simply return
|
|
|
|
// early.
|
|
|
|
if (at_eof_) {
|
2012-11-01 10:20:10 +05:30
|
|
|
return (END_OF_STREAM);
|
2012-10-26 10:30:09 +05:30
|
|
|
}
|
|
|
|
// We are not yet at EOF. Read from the stream.
|
2012-11-04 22:30:16 +05:30
|
|
|
const int c = input_.get();
|
2012-10-26 10:30:09 +05:30
|
|
|
// Have we reached EOF now? If so, set at_eof_ and return early,
|
|
|
|
// but don't modify buffer_pos_ (which should still be equal to
|
|
|
|
// the size of buffer_).
|
2012-11-01 10:35:17 +05:30
|
|
|
if (input_.eof()) {
|
2012-10-26 10:30:09 +05:30
|
|
|
at_eof_ = true;
|
2012-11-01 10:20:10 +05:30
|
|
|
return (END_OF_STREAM);
|
2012-10-26 10:30:09 +05:30
|
|
|
}
|
2012-11-01 10:35:17 +05:30
|
|
|
// This has to come after the .eof() check as some
|
|
|
|
// implementations seem to check the eofbit also in .fail().
|
|
|
|
if (input_.fail()) {
|
|
|
|
isc_throw(ReadError,
|
|
|
|
"Error reading from the input stream: " << getName());
|
|
|
|
}
|
2012-10-26 10:30:09 +05:30
|
|
|
buffer_.push_back(c);
|
|
|
|
}
|
|
|
|
|
2012-11-07 12:31:30 +05:30
|
|
|
const int c = buffer_[buffer_pos_];
|
|
|
|
++buffer_pos_;
|
2012-10-26 10:30:09 +05:30
|
|
|
if (c == '\n') {
|
2012-11-07 12:31:30 +05:30
|
|
|
++line_;
|
2012-10-26 10:30:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputSource::ungetChar() {
|
|
|
|
if (at_eof_) {
|
|
|
|
at_eof_ = false;
|
|
|
|
} else if (buffer_pos_ == 0) {
|
2012-11-01 09:46:40 +05:30
|
|
|
isc_throw(UngetBeforeBeginning,
|
|
|
|
"Cannot skip before the start of buffer");
|
2012-10-26 10:30:09 +05:30
|
|
|
} else {
|
2012-11-07 12:31:30 +05:30
|
|
|
--buffer_pos_;
|
2012-10-26 10:30:09 +05:30
|
|
|
if (buffer_[buffer_pos_] == '\n') {
|
2012-11-07 12:31:30 +05:30
|
|
|
--line_;
|
2012-10-26 10:30:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InputSource::ungetAll() {
|
|
|
|
buffer_pos_ = 0;
|
|
|
|
line_ = saved_line_;
|
|
|
|
at_eof_ = false;
|
|
|
|
}
|
|
|
|
|
2012-11-04 22:44:08 +05:30
|
|
|
void
|
|
|
|
InputSource::saveLine() {
|
|
|
|
saved_line_ = line_;
|
|
|
|
}
|
|
|
|
|
2012-10-31 10:53:31 +05:30
|
|
|
void
|
|
|
|
InputSource::compact() {
|
|
|
|
if (buffer_pos_ == buffer_.size()) {
|
|
|
|
buffer_.clear();
|
|
|
|
} else {
|
2012-10-31 12:42:02 +05:30
|
|
|
buffer_.erase(buffer_.begin(), buffer_.begin() + buffer_pos_);
|
2012-10-31 10:53:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
buffer_pos_ = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-04 22:44:08 +05:30
|
|
|
void
|
|
|
|
InputSource::mark() {
|
|
|
|
saveLine();
|
|
|
|
compact();
|
|
|
|
}
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
} // namespace master_lexer_internal
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace isc
|