📄 xinputstream.h
字号:
/* $Id: XInputStream.h,v 1.10 1997/04/14 12:31:47 matt Exp $ Extended C++ input streams class. Provides higher-level operations on an istream-derived object, including line, column information and meta-state information (word boundary, line end, etc.) Also allows mark and backtrack operations. Mark and backtrack are currently bounded to backing up to MaxBuffSize chars into the past, but this may eventually become unbounded (probably by using the GNU streambuf mark operations). (c) June 96 Matt Phillips. */#ifndef _X_INPUT_STREAM_H#define _X_INPUT_STREAM_H#include <iostream.h>#include <std/string.h>#include <contain/SlidingBitset.h>class XInputStream{public: // this will go away when the backtracking becomes unbounded. enum {MaxBuffSize = 4096}; // bit masks for stream state. enum {stClear = 0x00, stWordEdge = 0x01, stStartLine = 0x02, stEndLine = 0x04, stEOF = 0x08}; // a mark that is used to read substrings from the backtrack buffer. class TextMark { public: TextMark (const XInputStream &s) {mark (s);} TextMark (const TextMark &m) {index = m.index;} TextMark () {index = -1;} // creates an invalid mark. // place mark at current point in s. void mark (const XInputStream &s) {index = s.indexCurrent;} // unmark stream (mark becomes invalid). void unmark () {index = -1;} // true if mark is valid. int isValid (const XInputStream &stream) const; // get the logical index of this mark (see XInputStream::getIndex ()). int getIndex () const {return index;} // move mark i places forward or backward in the stream. this may // make mark invalid. void inc (int i) {index += i;} TextMark operator - (int i) const {return TextMark (index - i);} TextMark operator + (int i) const {return TextMark (index + i);} int operator > (const TextMark &m) const {return index > m.index;} protected: TextMark (int i) {index = i;} int index; friend class XInputStream; }; // a mark that allows backtracking. class BacktrackMark : public TextMark { public: BacktrackMark () {} BacktrackMark (XInputStream &s) {mark (s);} // place mark at current point in s. void mark (XInputStream &s); // sets stream to be at this mark. void jump (XInputStream &stream) const; int getLine () const {return line;} int getColumn () const {return column;} protected: int line, column; friend class XInputStream; }; // these may be fiddled with as necessary, but note that column is // used to calculate state info. string filename; int line, column; // create an XInputStream with an associated file name. XInputStream (istream &s, const string &fname); XInputStream (istream &s); // copy constructor not available yet. virtual ~XInputStream (); // copies the text contained between start and end marks into text. // the order of the marks is not important. void getText (string &text, const TextMark &start, const TextMark &end) const; // true if stream is at end of input. int eof () const {return indexCurrent == indexLast && input.eof ();} // return the current character. char peek () const {return current;} // return the previous character. char peekPrev () const; // get next character from input stream and return it. no effect if // stream at eof (). char get (); // returns the current logical index into the stream. int getIndex () const {return indexCurrent;} // get the current state as a bitmask (see stXXX enum). unsigned getState () const; // true if state (see getState ()) has flag set. static int inState (unsigned state, unsigned flag) {return state & flag;} // true if index has ever been marked for backtrack. int isMarked (int index) const {return markSet.get (index);}protected: istream &input; /* buff: pointer to start of backtrack buffer. current: (cached) current input character. indexCurrent: the logical index of the current input character. indexLast: the logical index of the last character read from the actual input stream. indexCurrent may be < indexLast (which indicates a backtrack has occurred) or equal, but never greater. */ int indexCurrent, indexLast; char *buff; char current; // cached copy of buff [indexCurrent % MaxBuffSize] // a sliding bitset for tracking where this stream has been marked. SlidingBitset<MaxBuffSize> markSet; // return the number of bytes logically in the backtrack buffer. // this can never be greater than MaxBuffSize. int buffSize () const; // turn logical index into a pointer into the backtrack buffer. char *indexToPtr (int index) const {return buff + (index % MaxBuffSize);}private: void init (); friend class XInputStream::BacktrackMark; friend class XInputStream::TextMark;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -