sisi4s
Loading...
Searching...
No Matches
LineNumberStream.hpp
Go to the documentation of this file.
1#ifndef LINE_NUMBER_STREAM_DEFINED
2#define LINE_NUMBER_STREAM_DEFINED
3
4#include <string>
5
6namespace sisi4s {
12public:
17 LineNumberStream(std::istream *stream_,
18 std::string const &source_,
19 int const tabWidth_ = 2)
20 : line(1)
21 , column(1)
22 , tabWidth(tabWidth_)
23 , stream(stream_)
24 , source(source_) {}
32 int peek() { return stream->peek(); }
37 int get() {
38 int character(stream->get());
39 switch (character) {
40 case '\n':
41 ++line;
42 column = 1;
43 break;
44 case '\t': column += tabWidth; break;
45 default: ++column;
46 }
47 return character;
48 }
52 std::istream *getStream() { return stream; }
57 std::string getSource() { return source; }
61 int getLine() { return line; }
65 int getColumn() { return column; }
66
67protected:
69 std::istream *stream;
70 std::string source;
71};
72} // namespace sisi4s
73
74#endif
Wrapper for an input stream providing tracking of the current line and the current column during stre...
Definition LineNumberStream.hpp:11
int column
Definition LineNumberStream.hpp:68
std::string getSource()
Returns the source name of the underlying stream, usually its file name.
Definition LineNumberStream.hpp:57
int line
Definition LineNumberStream.hpp:68
~LineNumberStream()
Destroys this stream and delete the underlying stream.
Definition LineNumberStream.hpp:28
int getLine()
Returns the line of the next character to be read.
Definition LineNumberStream.hpp:61
int getColumn()
Returns the column of the next character to be read.
Definition LineNumberStream.hpp:65
int peek()
Peeks one character from the underlying stream.
Definition LineNumberStream.hpp:32
std::string source
Definition LineNumberStream.hpp:70
std::istream * getStream()
Returns the underlying std::istream.
Definition LineNumberStream.hpp:52
LineNumberStream(std::istream *stream_, std::string const &source_, int const tabWidth_=2)
Creates a wrapper for the given stream. The wrapper takes ownership of the given pointer.
Definition LineNumberStream.hpp:17
int get()
Reads and returns one character from the underlying stream while keeping track of the line and column...
Definition LineNumberStream.hpp:37
int tabWidth
Definition LineNumberStream.hpp:68
std::istream * stream
Definition LineNumberStream.hpp:69
Definition Algorithm.hpp:10