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.
|
|
|
|
|
|
|
|
#ifndef DNS_INPUTSOURCE_H
|
|
|
|
#define DNS_INPUTSOURCE_H 1
|
|
|
|
|
2012-10-29 08:35:20 +05:30
|
|
|
#include <exceptions/exceptions.h>
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
#include <iostream>
|
2012-10-30 12:39:38 +05:30
|
|
|
#include <fstream>
|
2012-10-26 10:30:09 +05:30
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace dns {
|
|
|
|
namespace master_lexer_internal {
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief An input source that is used internally by MasterLexer.
|
|
|
|
///
|
|
|
|
/// This is a helper internal class for MasterLexer, and represents
|
|
|
|
/// state of a single source of the entire zone data to be
|
|
|
|
/// parsed. Normally this means the master zone file, but MasterLexer
|
|
|
|
/// can have multiple InputSources if $INCLUDE is used. The source can
|
|
|
|
/// also be generic input stream (std::istream).
|
|
|
|
///
|
|
|
|
/// This class is not meant for public use.
|
2012-10-26 10:30:09 +05:30
|
|
|
class InputSource {
|
|
|
|
public:
|
2012-11-02 09:59:16 +05:30
|
|
|
/// \brief Returned by getChar() when end of stream is reached.
|
|
|
|
static const int END_OF_STREAM;
|
|
|
|
|
|
|
|
/// \brief Exception thrown when ungetChar() is made to go before
|
|
|
|
/// the start of buffer.
|
|
|
|
struct UngetBeforeBeginning : public OutOfRange {
|
|
|
|
UngetBeforeBeginning(const char* file, size_t line, const char* what) :
|
|
|
|
OutOfRange(file, line, what)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Exception thrown when we fail to read from the input
|
|
|
|
/// stream or file.
|
|
|
|
struct ReadError : public Unexpected {
|
|
|
|
ReadError(const char* file, size_t line, const char* what) :
|
|
|
|
Unexpected(file, line, what)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Exception thrown when we fail to open the input file.
|
|
|
|
struct OpenError : public Unexpected {
|
|
|
|
OpenError(const char* file, size_t line, const char* what) :
|
|
|
|
Unexpected(file, line, what)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Constructor which takes an input stream. The stream is
|
|
|
|
/// read-from, but it is not closed.
|
2012-10-30 12:39:38 +05:30
|
|
|
InputSource(std::istream& input_stream);
|
2012-10-30 12:53:46 +05:30
|
|
|
|
|
|
|
/// \brief Constructor which takes a filename to read from. The
|
|
|
|
/// associated file stream is managed internally.
|
2012-11-02 09:57:22 +05:30
|
|
|
///
|
|
|
|
/// \throws OpenError when opening the input file fails.
|
2012-10-30 12:39:38 +05:30
|
|
|
InputSource(const char* filename);
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Destructor
|
2012-10-30 12:39:38 +05:30
|
|
|
~InputSource();
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Returns a name for the InputSource. Typically this is the
|
|
|
|
/// filename, but if the InputSource was constructed for an
|
|
|
|
/// \c std::istream, it returns a name in the format "stream-%p".
|
2012-10-30 12:39:38 +05:30
|
|
|
const std::string& getName() const {
|
2012-10-26 10:30:09 +05:30
|
|
|
return (name_);
|
|
|
|
}
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Returns if the input source is at end of file.
|
2012-10-26 10:30:09 +05:30
|
|
|
bool atEOF() const {
|
|
|
|
return (at_eof_);
|
|
|
|
}
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Returns the current line number being read.
|
2012-10-26 10:30:09 +05:30
|
|
|
size_t getCurrentLine() const {
|
|
|
|
return (line_);
|
|
|
|
}
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Saves the current line being read. Later, when
|
|
|
|
/// \c ungetAll() is called, it skips back to the last-saved line.
|
2012-10-26 10:30:09 +05:30
|
|
|
void saveLine() {
|
|
|
|
saved_line_ = line_;
|
|
|
|
}
|
|
|
|
|
2012-10-30 12:53:46 +05:30
|
|
|
/// \brief Returns a single character from the input source. If end
|
2012-11-01 10:20:10 +05:30
|
|
|
/// of file is reached, \c END_OF_STREAM is returned.
|
2012-11-01 10:35:17 +05:30
|
|
|
///
|
|
|
|
/// \throws ReadError when reading from the input stream or file
|
|
|
|
/// fails.
|
2012-10-26 10:30:09 +05:30
|
|
|
int getChar();
|
2012-10-30 12:53:46 +05:30
|
|
|
|
|
|
|
/// \brief Skips backward a single character in the input
|
|
|
|
/// source. The last-read character is unget.
|
2012-11-01 09:46:40 +05:30
|
|
|
///
|
|
|
|
/// \throws UngetBeforeBeginning if we go backwards past the start
|
|
|
|
/// of reading, or backwards past the last time compact() was
|
|
|
|
/// called.
|
2012-10-26 10:30:09 +05:30
|
|
|
void ungetChar();
|
2012-10-30 12:53:46 +05:30
|
|
|
|
2012-11-01 10:40:00 +05:30
|
|
|
/// Forgets what was read, and skips back to the position where
|
|
|
|
/// \c compact() was last called. If \c compact() was not called, it
|
|
|
|
/// skips back to where reading started. If \c saveLine() was called
|
|
|
|
/// previously, it sets the current line number to the line number
|
|
|
|
/// saved.
|
2012-10-26 10:30:09 +05:30
|
|
|
void ungetAll();
|
|
|
|
|
2012-10-31 10:53:31 +05:30
|
|
|
/// Removes buffered content before the current location in the
|
|
|
|
/// \c InputSource. It's not possible to \c ungetChar() after this,
|
|
|
|
/// unless we read more data using \c getChar().
|
|
|
|
void compact();
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
private:
|
|
|
|
bool at_eof_;
|
|
|
|
size_t line_;
|
|
|
|
size_t saved_line_;
|
|
|
|
|
|
|
|
std::vector<char> buffer_;
|
|
|
|
size_t buffer_pos_;
|
2012-10-30 12:39:38 +05:30
|
|
|
|
2012-11-01 09:48:55 +05:30
|
|
|
const std::string name_;
|
2012-11-04 21:40:33 +05:30
|
|
|
std::ifstream file_stream_;
|
2012-10-30 12:39:38 +05:30
|
|
|
std::istream& input_;
|
2012-10-26 10:30:09 +05:30
|
|
|
};
|
|
|
|
|
2012-11-01 10:20:10 +05:30
|
|
|
const int InputSource::END_OF_STREAM = -1;
|
|
|
|
|
2012-10-26 10:30:09 +05:30
|
|
|
} // namespace master_lexer_internal
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace isc
|
|
|
|
|
|
|
|
#endif // DNS_INPUTSOURCE_H
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: c++
|
|
|
|
// End:
|