Kea  1.5.0
master_lexer_inputsource.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef DNS_INPUTSOURCE_H
8 #define DNS_INPUTSOURCE_H 1
9 
10 #include <exceptions/exceptions.h>
11 
12 #include <boost/noncopyable.hpp>
13 
14 #include <iostream>
15 #include <fstream>
16 #include <string>
17 #include <vector>
18 
19 namespace isc {
20 namespace dns {
21 namespace master_lexer_internal {
22 
33 class InputSource : boost::noncopyable {
34 public:
41  static const int END_OF_STREAM = -1;
42 
45  struct UngetBeforeBeginning : public OutOfRange {
46  UngetBeforeBeginning(const char* file, size_t line, const char* what) :
47  OutOfRange(file, line, what)
48  {}
49  };
50 
52  struct OpenError : public Unexpected {
53  OpenError(const char* file, size_t line, const char* what) :
54  Unexpected(file, line, what)
55  {}
56  };
57 
63  explicit InputSource(std::istream& input_stream);
64 
70  explicit InputSource(const char* filename);
71 
73  ~InputSource();
74 
78  const std::string& getName() const {
79  return (name_);
80  }
81 
90  size_t getSize() const { return (input_size_); }
91 
108  size_t getPosition() const { return (total_pos_); }
109 
111  bool atEOF() const {
112  return (at_eof_);
113  }
114 
116  size_t getCurrentLine() const {
117  return (line_);
118  }
119 
126  void saveLine();
127 
135  void compact();
136 
138  void mark();
139 
145  int getChar();
146 
153  void ungetChar();
154 
160  void ungetAll();
161 
162 private:
163  bool at_eof_;
164  size_t line_;
165  size_t saved_line_;
166 
167  std::vector<char> buffer_;
168  size_t buffer_pos_;
169  size_t total_pos_;
170 
171  const std::string name_;
172  std::ifstream file_stream_;
173  std::istream& input_;
174  const size_t input_size_;
175 };
176 
177 } // namespace master_lexer_internal
178 } // namespace dns
179 } // namespace isc
180 
181 #endif // DNS_INPUTSOURCE_H
182 
183 // Local Variables:
184 // mode: c++
185 // End:
isc::dns::master_lexer_internal::InputSource::getSize
size_t getSize() const
Returns the size of the input source in bytes.
Definition: master_lexer_inputsource.h:90
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
isc::dns::master_lexer_internal::InputSource::InputSource
InputSource(std::istream &input_stream)
Constructor which takes an input stream.
Definition: master_lexer_inputsource.cc:85
isc::dns::master_lexer_internal::InputSource::OpenError
Exception thrown when we fail to open the input file.
Definition: master_lexer_inputsource.h:52
isc::dns::master_lexer_internal::InputSource::OpenError::OpenError
OpenError(const char *file, size_t line, const char *what)
Definition: master_lexer_inputsource.h:53
isc::dns::master_lexer_internal::InputSource::getName
const std::string & getName() const
Returns a name for the InputSource.
Definition: master_lexer_inputsource.h:78
isc::dns::master_lexer_internal::InputSource
An input source that is used internally by MasterLexer.
Definition: master_lexer_inputsource.h:33
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::dns::master_lexer_internal::InputSource::UngetBeforeBeginning::UngetBeforeBeginning
UngetBeforeBeginning(const char *file, size_t line, const char *what)
Definition: master_lexer_inputsource.h:46
isc::dns::master_lexer_internal::InputSource::ungetChar
void ungetChar()
Skips backward a single character in the input source.
Definition: master_lexer_inputsource.cc:173
isc::dns::master_lexer_internal::InputSource::ungetAll
void ungetAll()
Forgets what was read, and skips back to the position where compact() was last called.
Definition: master_lexer_inputsource.cc:189
isc::dns::master_lexer_internal::InputSource::UngetBeforeBeginning
Exception thrown when ungetChar() is made to go before the start of buffer.
Definition: master_lexer_inputsource.h:45
isc::dns::master_lexer_internal::InputSource::mark
void mark()
Calls saveLine() and compact() in sequence.
Definition: master_lexer_inputsource.cc:214
isc::dns::master_lexer_internal::InputSource::atEOF
bool atEOF() const
Returns if the input source is at end of file.
Definition: master_lexer_inputsource.h:111
isc::dns::master_lexer_internal::InputSource::getPosition
size_t getPosition() const
Returns the current read position in the input source.
Definition: master_lexer_inputsource.h:108
isc::dns::master_lexer_internal::InputSource::getChar
int getChar()
Returns a single character from the input source.
Definition: master_lexer_inputsource.cc:136
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition: exceptions/exceptions.h:115
exceptions.h
isc::dns::master_lexer_internal::InputSource::END_OF_STREAM
static const int END_OF_STREAM
Returned by getChar() when end of stream is reached.
Definition: master_lexer_inputsource.h:41
isc::dns::master_lexer_internal::InputSource::~InputSource
~InputSource()
Destructor.
Definition: master_lexer_inputsource.cc:128
isc::dns::master_lexer_internal::InputSource::saveLine
void saveLine()
Saves the current line being read.
Definition: master_lexer_inputsource.cc:198
isc::dns::master_lexer_internal::InputSource::compact
void compact()
Removes buffered content before the current location in the InputSource.
Definition: master_lexer_inputsource.cc:203
isc::dns::master_lexer_internal::InputSource::getCurrentLine
size_t getCurrentLine() const
Returns the current line number being read.
Definition: master_lexer_inputsource.h:116