Add GLprobs and MSAprobs to binaries
[jabaws.git] / binaries / src / GLProbs-1.0 / FileBuffer.h
1 /////////////////////////////////////////////////////////////////
2 // FileBuffer.h
3 //
4 // Buffered file reading.
5 /////////////////////////////////////////////////////////////////
6
7 #ifndef FILEBUFFER_H
8 #define FILEBUFFER_H
9
10 #include <string>
11 #include <fstream>
12 #include <iostream>
13
14 using namespace std;
15
16 const int BufferSize = 1000;
17
18 /////////////////////////////////////////////////////////////////
19 // FileBuffer
20 //
21 // Class for buffering file reading.
22 /////////////////////////////////////////////////////////////////
23
24 class FileBuffer {
25         ifstream file;
26         char buffer[BufferSize];
27         int currPos;
28         int size;
29         bool isEOF;
30         bool isValid;
31         bool canUnget;
32
33 public:
34
35         // Some common routines
36
37         FileBuffer(const char *filename) :
38                         file(filename), currPos(0), size(0), isEOF(false), isValid(
39                                         !file.fail()), canUnget(false) {
40         }
41         ~FileBuffer() {
42                 close();
43         }
44         bool fail() const {
45                 return !isValid;
46         }
47         bool eof() const {
48                 return (!isValid || isEOF);
49         }
50         void close() {
51                 file.close();
52                 isValid = false;
53         }
54
55         /////////////////////////////////////////////////////////////////
56         // FileBuffer::Get()
57         //
58         // Retrieve a character from the file buffer.  Returns true if
59         // and only if a character is read.
60         /////////////////////////////////////////////////////////////////
61
62         bool Get(char &ch) {
63
64                 // check to make sure that there's more stuff in the file
65                 if (!isValid || isEOF)
66                         return false;
67
68                 // if the buffer is empty, it's time to reload it
69                 if (currPos == size) {
70                         file.read(buffer, BufferSize);
71                         size = file.gcount();
72                         isEOF = (size == 0);
73                         currPos = 0;
74                         if (isEOF)
75                                 return false;
76                 }
77
78                 // store the read character
79                 ch = buffer[currPos++];
80                 canUnget = true;
81                 return true;
82         }
83
84         /////////////////////////////////////////////////////////////////
85         // FileBuffer::UnGet()
86         //
87         // Unretrieve the most recently read character from the file
88         // buffer.  Note that this allows only a one-level undo.
89         /////////////////////////////////////////////////////////////////
90
91         void UnGet() {
92                 assert(canUnget);
93                 assert(isValid);
94                 assert(currPos > 0);
95                 currPos--;
96                 assert(currPos < size);
97                 isEOF = false;
98                 canUnget = false;
99         }
100
101         /////////////////////////////////////////////////////////////////
102         // FileBuffer::GetLine()
103         //
104         // Retrieve characters of text until a newline character is
105         // encountered.  Terminates properly on end-of-file condition.
106         /////////////////////////////////////////////////////////////////
107
108         void GetLine(string &s) {
109                 char ch;
110                 s = "";
111                 while (Get(ch) && ch != '\n')
112                         s += ch;
113         }
114
115 };
116
117 #endif