Kea  1.9.9-git
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:
size_t getPosition() const
Returns the current read position in the input source.
Exception thrown when we fail to open the input file.
Exception thrown when ungetChar() is made to go before the start of buffer.
An input source that is used internally by MasterLexer.
size_t getCurrentLine() const
Returns the current line number being read.
void compact()
Removes buffered content before the current location in the InputSource.
static const int END_OF_STREAM
Returned by getChar() when end of stream is reached.
int getChar()
Returns a single character from the input source.
void saveLine()
Saves the current line being read.
UngetBeforeBeginning(const char *file, size_t line, const char *what)
OpenError(const char *file, size_t line, const char *what)
A generic exception that is thrown when an unexpected error condition occurs.
bool atEOF() const
Returns if the input source is at end of file.
InputSource(std::istream &input_stream)
Constructor which takes an input stream.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
const std::string & getName() const
Returns a name for the InputSource.
size_t getSize() const
Returns the size of the input source in bytes.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
void ungetChar()
Skips backward a single character in the input source.
void mark()
Calls saveLine() and compact() in sequence.
void ungetAll()
Forgets what was read, and skips back to the position where compact() was last called.