forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cpp
50 lines (39 loc) · 767 Bytes
/
file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// file.cpp
#include "file.h"
#define FILE_BUFFER_SIZE 1024
void CFileBuffer::Open(const char *fileName)
{
Close();
f = fopen(fileName, "rt");
if (f == 0) throw int(ERROR_FILE_NOT_OPEN);
buffer = new char[FILE_BUFFER_SIZE];
pos = n = 0;
lastChar = ' ';
}
void CFileBuffer::Close()
{
if (IsOpen())
{
fclose(f);
f = 0;
delete[] buffer;
lastChar = 0;
}
}
char CFileBuffer::FillBuffer()
{
if (!IsOpen()) throw int(ERROR_FILE_NOT_OPEN);
n = fread(buffer, sizeof(char), FILE_BUFFER_SIZE, f);
pos = 0;
if (n == 0)
{
if (feof(f)) throw int(ERROR_FILE_END_OF_FILE);
else throw int(ERROR_FILE_READ_FAILED);
}
return buffer[pos++];
}
char CFileBuffer::GetNext()
{
lastChar = (pos < n) ? buffer[pos++] : FillBuffer();
return lastChar;
}