-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.cpp
129 lines (96 loc) · 2.81 KB
/
Buffer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "Buffer.h"
Buffer::Buffer(std::string filePath) {
fileStr = filePath;
fileStream.open(fileStr); //Open fstream
buffer1[SIZE - 1] = EOF; //Set sentinel
fileStream.read(buffer1, SIZE - 1); //Read chars
lexemeBegin = forward = &buffer1[0]; //Get memory address
}
Buffer::Buffer() {
buffer1[0] = EOF;
buffer2[0] = EOF;
}
Buffer::~Buffer() noexcept {
if (fileStream) {
fileStream.close();
}
}
std::string Buffer::getLexeme() {
char* current = lexemeBegin;
std::stringstream ss;
while (current != forward) {
if (*current == EOF) { //If current is EOF, transfer buffer
if (activitiedBuffer == Tag::First) {
current = &buffer1[0];
}
else if (activitiedBuffer == Tag::Second) {
current = &buffer2[0];
}
}
ss << *current;
if (current == forward) break;
current++;
}
return ss.str();
}
char Buffer::next() {
char tmp = *forward;
forward++;
if (tmp == '\0') { //'\0' means out of original code or not load yet, end the task
isEnd = true;
return '\0';
}
if (*forward == EOF && (forward == &buffer1[SIZE - 1] || forward == &buffer2[SIZE - 1])) { //EOF means current buffer has been scanned, load new buffer
read();
}
else if (*forward == EOF && forward != &buffer1[SIZE - 1] && forward != &buffer2[SIZE - 1]) {
isEnd = true;
}
return *forward;
}
char Buffer::cur() {
return *forward;
}
char Buffer::pre() {
char tmp = *forward;
if (forward == &buffer2[0]) { //If forward is at the beginning of the buffer2, which means buffer1 has been loaded and scanned, so go back to the end of buffer1
forward = &buffer1[SIZE - 1];
return tmp;
}
else if (forward == &buffer1[0] && buffer2[0] != '\0') { //If forward is at the beginning of the buffer1, which means either buffer2 has been scanned or not loaded yet
forward = &buffer2[0];
return tmp;
}
else if (forward == &buffer1[0] && buffer2[0] == '\0') {
return tmp;
}
forward--;
return tmp;
}
void Buffer::confirmLexeme() {
lexemeBegin = forward;
}
void Buffer::changeFilePath(std::string filePath) {
fileStr = filePath;
fileStream.open(fileStr, std::ios::binary); //Open fstream
buffer1[SIZE - 1] = EOF; //Set sentinel
fileStream.read(buffer1, SIZE - 1); //Read chars
lexemeBegin = forward = &buffer1[0]; //Get memory address
}
void Buffer::read() {
if (!fileStream) return;
if (activitiedBuffer == Tag::First) {
memset(buffer2, -1, sizeof(buffer2));
buffer2[SIZE - 1] = EOF;
fileStream.read(buffer2, SIZE - 1);
activitiedBuffer = Tag::Second;
forward = &buffer2[0];
}
else if (activitiedBuffer == Tag::Second) {
memset(buffer1, -1, sizeof(buffer1));
buffer1[SIZE - 1] = EOF;
fileStream.read(buffer1, SIZE - 1);
activitiedBuffer = Tag::First;
forward = &buffer1[0];
}
}