1 /////////////////////////////////////////////////////////////////
4 // Buffered file reading.
5 /////////////////////////////////////////////////////////////////
17 const int BufferSize = 1000;
19 /////////////////////////////////////////////////////////////////
22 // Class for buffering file reading.
23 /////////////////////////////////////////////////////////////////
27 char buffer[BufferSize];
36 // Some common routines
38 FileBuffer (const char *filename) : file (filename), currPos (0), size (0), isEOF (false), isValid (!file.fail()), canUnget (false){}
39 ~FileBuffer (){ close(); }
40 bool fail () const { return !isValid; }
41 bool eof () const { return (!isValid || isEOF); }
42 void close(){ file.close(); isValid = false; }
44 /////////////////////////////////////////////////////////////////
47 // Retrieve a character from the file buffer. Returns true if
48 // and only if a character is read.
49 /////////////////////////////////////////////////////////////////
53 // check to make sure that there's more stuff in the file
54 if (!isValid || isEOF) return false;
56 // if the buffer is empty, it's time to reload it
58 file.read (buffer, BufferSize);
62 if (isEOF) return false;
65 // store the read character
66 ch = buffer[currPos++];
71 /////////////////////////////////////////////////////////////////
72 // FileBuffer::UnGet()
74 // Unretrieve the most recently read character from the file
75 // buffer. Note that this allows only a one-level undo.
76 /////////////////////////////////////////////////////////////////
83 assert (currPos < size);
88 /////////////////////////////////////////////////////////////////
89 // FileBuffer::GetLine()
91 // Retrieve characters of text until a newline character is
92 // encountered. Terminates properly on end-of-file condition.
93 /////////////////////////////////////////////////////////////////
95 void GetLine (string &s){
98 while (Get (ch) && ch != '\n')