📄 xinputstream.cpp
字号:
/* $Id: XInputStream.cpp,v 1.10 1997/04/14 12:31:42 matt Exp $ Extended C++ input streams class. (c) June 96 Matt Phillips. */#include <util/checks.h>#include "XInputStream.h"void XInputStream::BacktrackMark::mark (XInputStream &stream){ TextMark::mark (stream); line = stream.line; column = stream.column; stream.markSet.set (index);}int XInputStream::TextMark::isValid (const XInputStream &stream) const{ if (index <= stream.indexLast && index > stream.indexCurrent - stream.buffSize ()) return 1; else return 0;}void XInputStream::BacktrackMark::jump (XInputStream &stream) const{ // sanity check CHECK (isValid (stream), "invalid mark used in jump"); stream.indexCurrent = index; stream.current = *(stream.indexToPtr (index)); stream.line = line; stream.column = column;}// create an XInputStream with an associated file name.XInputStream::XInputStream (istream &s, const string &fname) : input (s), filename (fname){ init ();}XInputStream::XInputStream (istream &s) : input (s){ init ();}int XInputStream::buffSize () const{ if (indexCurrent < MaxBuffSize) return indexCurrent + 1; else return MaxBuffSize - (indexLast - indexCurrent);}void XInputStream::init (){ // setup buffer buff = new char [MaxBuffSize]; indexCurrent = indexLast = 0; line = column = 0; buff [MaxBuffSize - 1] = '\0'; // for peekPrev () int chr = input.get (); current = buff [0] = ((chr == EOF) ? '\0' : chr);}XInputStream::~XInputStream (){ delete buff;}char XInputStream::peekPrev () const{ if (indexCurrent % MaxBuffSize == 0) return buff [MaxBuffSize - 1]; else return buff [indexCurrent - 1];}char XInputStream::get (){ if (!eof ()) { char prev = current; if (indexCurrent < indexLast) { indexCurrent++; current = *(indexToPtr (indexCurrent)); } else { int chr = input.get (); indexCurrent++; indexLast = indexCurrent; if (chr == EOF) chr = 0; current = *(indexToPtr (indexCurrent)) = (char)chr; if (indexCurrent >= MaxBuffSize) markSet.slide (); } // do line/column tracking if (prev == '\n') { line++; column = 0; } else if (prev == '\t') column += 8 - (column % 8); else column++; } return peek ();}void XInputStream::getText (string &text, const XInputStream::TextMark &start, const XInputStream::TextMark &end) const{ CHECK (start.isValid (*this) && end.isValid (*this), "invalid mark(s) used in getText"); if (start.index <= end.index) text.assign (indexToPtr (start.index), end.index - start.index); else text.assign (indexToPtr (end.index), start.index - end.index);}unsigned XInputStream::getState () const{ unsigned state = stClear; // word boundary if ((isalnum (peekPrev ())) != (!eof () && isalnum (peek ()) != 0)) state |= stWordEdge; // end of line if (peek () == '\n' || eof ()) state |= stEndLine; // beginning of line if (column == 0) state |= stStartLine; // eof if (eof ()) state |= stEOF; return state;}template class SlidingBitset<XInputStream::MaxBuffSize>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -