1 /////////////////////////////////////////////////////////////////
4 // Buffered file reading.
5 /////////////////////////////////////////////////////////////////
16 const int BufferSize = 1000;
18 /////////////////////////////////////////////////////////////////
21 // Class for buffering file reading.
22 /////////////////////////////////////////////////////////////////
26 char buffer[BufferSize];
35 // Some common routines
37 FileBuffer(const char *filename) :
38 file(filename), currPos(0), size(0), isEOF(false), isValid(
39 !file.fail()), canUnget(false) {
48 return (!isValid || isEOF);
55 /////////////////////////////////////////////////////////////////
58 // Retrieve a character from the file buffer. Returns true if
59 // and only if a character is read.
60 /////////////////////////////////////////////////////////////////
64 // check to make sure that there's more stuff in the file
65 if (!isValid || isEOF)
68 // if the buffer is empty, it's time to reload it
69 if (currPos == size) {
70 file.read(buffer, BufferSize);
78 // store the read character
79 ch = buffer[currPos++];
84 /////////////////////////////////////////////////////////////////
85 // FileBuffer::UnGet()
87 // Unretrieve the most recently read character from the file
88 // buffer. Note that this allows only a one-level undo.
89 /////////////////////////////////////////////////////////////////
96 assert(currPos < size);
101 /////////////////////////////////////////////////////////////////
102 // FileBuffer::GetLine()
104 // Retrieve characters of text until a newline character is
105 // encountered. Terminates properly on end-of-file condition.
106 /////////////////////////////////////////////////////////////////
108 void GetLine(string &s) {
111 while (Get(ch) && ch != '\n')