Kea 2.5.8
master_lexer_inputsource.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2024 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#include <config.h>
8
11#include <dns/master_lexer.h>
12
13#include <istream>
14#include <iostream>
15#include <cerrno>
16#include <cstring>
17
18namespace isc {
19namespace dns {
20namespace master_lexer_internal {
21
22namespace { // unnamed namespace
23
24std::string
25createStreamName(const std::istream& input_stream) {
26 std::stringstream ss;
27 ss << "stream-" << &input_stream;
28 return (ss.str());
29}
30
31size_t
32getStreamSize(std::istream& is) {
33 errno = 0; // see below
34 is.seekg(0, std::ios_base::end);
35 if (is.bad()) {
36 // This means the istream has an integrity error. It doesn't make
37 // sense to continue from this point, so we treat it as a fatal error.
38 isc_throw(InputSource::OpenError,
39 "failed to seek end of input source");
40 } else if (is.fail() || errno != 0) {
41 // This is an error specific to seekg(). There can be several
42 // reasons, but the most likely cause in this context is that the
43 // stream is associated with a special type of file such as a pipe.
44 // In this case, it's more likely that other main operations of
45 // the input source work fine, so we continue with just setting
46 // the stream size to "unknown".
47 //
48 // (At least some versions of) Solaris + SunStudio shows deviant
49 // behavior here: seekg() apparently calls lseek(2) internally, but
50 // even if it fails it doesn't set the error bits of istream. That will
51 // confuse the rest of this function, so, as a heuristic workaround
52 // we check errno and handle any non 0 value as fail().
53 is.clear(); // clear this error not to confuse later ops.
55 }
56 const std::streampos len = is.tellg();
57 size_t ret = len;
58 if (len == static_cast<std::streampos>(-1)) { // cast for some compilers
59 if (!is.fail()) {
60 // tellg() returns -1 if istream::fail() would be true, but it's
61 // not guaranteed that it shouldn't be returned in other cases.
62 // In fact, with the combination of SunStudio and stlport,
63 // a stringstream created by the default constructor showed that
64 // behavior. We treat such cases as an unknown size.
66 } else {
67 isc_throw(InputSource::OpenError, "failed to get input size");
68 }
69 }
70 is.seekg(0, std::ios::beg);
71 if (is.fail()) {
72 isc_throw(InputSource::OpenError,
73 "failed to seek beginning of input source");
74 }
76 return (ret);
77}
78
79} // end of unnamed namespace
80
81// Explicit definition of class static constant. The value is given in the
82// declaration so it's not needed here.
84
85InputSource::InputSource(std::istream& input_stream) :
86 at_eof_(false),
87 line_(1),
88 saved_line_(line_),
89 buffer_pos_(0),
90 total_pos_(0),
91 name_(createStreamName(input_stream)),
92 input_(input_stream),
93 input_size_(getStreamSize(input_)) {
94}
95
96namespace {
97// A helper to initialize InputSource::input_ in the member initialization
98// list.
99std::istream&
100openFileStream(std::ifstream& file_stream, const char* filename) {
101 errno = 0;
102 file_stream.open(filename);
103 if (file_stream.fail()) {
104 std::string error_txt("Error opening the input source file: ");
105 error_txt += filename;
106 if (errno != 0) {
107 error_txt += "; possible cause: ";
108 error_txt += std::strerror(errno);
109 }
110 isc_throw(InputSource::OpenError, error_txt);
111 }
112
113 return (file_stream);
114}
115}
116
117InputSource::InputSource(const char* filename) :
118 at_eof_(false),
119 line_(1),
120 saved_line_(line_),
121 buffer_pos_(0),
122 total_pos_(0),
123 name_(filename),
124 input_(openFileStream(file_stream_, filename)),
125 input_size_(getStreamSize(input_)) {
126}
127
129 if (file_stream_.is_open()) {
130 file_stream_.close();
131 }
132}
133
134int
136 if (buffer_pos_ == buffer_.size()) {
137 // We may have reached EOF at the last call to
138 // getChar(). at_eof_ will be set then. We then simply return
139 // early.
140 if (at_eof_) {
141 return (END_OF_STREAM);
142 }
143 // We are not yet at EOF. Read from the stream.
144 const int c = input_.get();
145 // Have we reached EOF now? If so, set at_eof_ and return early,
146 // but don't modify buffer_pos_ (which should still be equal to
147 // the size of buffer_).
148 if (input_.eof()) {
149 at_eof_ = true;
150 return (END_OF_STREAM);
151 }
152 // This has to come after the .eof() check as some
153 // implementations seem to check the eofbit also in .fail().
154 if (input_.fail()) {
156 "Error reading from the input stream: " << getName());
157 }
158 buffer_.push_back(c);
159 }
160
161 const int c = buffer_[buffer_pos_];
162 ++buffer_pos_;
163 ++total_pos_;
164 if (c == '\n') {
165 ++line_;
166 }
167
168 return (c);
169}
170
171void
173 if (at_eof_) {
174 at_eof_ = false;
175 } else if (buffer_pos_ == 0) {
177 "Cannot skip before the start of buffer");
178 } else {
179 --buffer_pos_;
180 --total_pos_;
181 if (buffer_[buffer_pos_] == '\n') {
182 --line_;
183 }
184 }
185}
186
187void
189 isc_throw_assert(total_pos_ >= buffer_pos_);
190 total_pos_ -= buffer_pos_;
191 buffer_pos_ = 0;
192 line_ = saved_line_;
193 at_eof_ = false;
194}
195
196void
198 saved_line_ = line_;
199}
200
201void
203 if (buffer_pos_ == buffer_.size()) {
204 buffer_.clear();
205 } else {
206 buffer_.erase(buffer_.begin(), buffer_.begin() + buffer_pos_);
207 }
208
209 buffer_pos_ = 0;
210}
211
212void
214 saveLine();
215 compact();
216}
217
218} // namespace master_lexer_internal
219} // namespace dns
220} // namespace isc
Exception thrown when we fail to read from the input stream or file.
Definition: master_lexer.h:308
static const size_t SOURCE_SIZE_UNKNOWN
Special value for input source size meaning "unknown".
Definition: master_lexer.h:339
void ungetAll()
Forgets what was read, and skips back to the position where compact() was last called.
void ungetChar()
Skips backward a single character in the input source.
void compact()
Removes buffered content before the current location in the InputSource.
void mark()
Calls saveLine() and compact() in sequence.
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.
const std::string & getName() const
Returns a name for the InputSource.
void saveLine()
Saves the current line being read.
InputSource(std::istream &input_stream)
Constructor which takes an input stream.
const Name & name_
Definition: dns/message.cc:696
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition: isc_assert.h:18
Defines the logger used by the top-level component of kea-lfc.
Exception thrown when ungetChar() is made to go before the start of buffer.