-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
87 lines (75 loc) · 1.77 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
#include "buffer.h"
Buffer::Buffer(std::string filename) :
length(20), buff1(), buff2(), beginLex(0), forward(0)
{
buff1.resize(length);
buff2.resize(length);
file.open(filename, std::ios::in);
int length = this->length;
if (file) {
file.read(&buff1[0], length);
if (file.eof()) {
int p = file.gcount();
buff1[p] = -1;
file.close();
}
transform(buff1.begin(), buff1.end(), buff1.begin(), ::tolower);
} else {
std::cout << "ERRO:: Falha ao abrir o arquivo: >> " << filename
<< " << " << std::endl;
buff1[0] = -1;
}
curBuff = &buff1;
}
std::string* Buffer::getOtherBuffer(){
if(curBuff == &buff1){
return &buff2;
}
else if(curBuff == &buff2){
return &buff1;
}
printf("error:: Buffer::getOtherBuffer() falhou. (in file >> buffer.cpp <<)\n");
return NULL;
}
void Buffer::load(){
int length = this->length;
if (file) {
file.read(&curBuff->operator [](0), length);
transform(curBuff->begin(), curBuff->end(), curBuff->begin(), ::tolower);
if(file.eof()){
int howManyLeft = file.gcount();
(*curBuff)[howManyLeft] = -1;
file.close();
}
}
}
char Buffer::next(){
int x = forward;
forward++;
if(forward==length){
forward = 0;
curBuff = this->getOtherBuffer();
this->load();
return (*this->getOtherBuffer())[x];
}
return (*curBuff)[x];
}
void Buffer::retract(){
forward--;
}
void Buffer::fail(){
forward=beginLex;
}
char Buffer::lookAhead(){
return (*curBuff)[forward];
}
void Buffer::beginLexeme(){
beginLex = forward;
}
std::string Buffer::getLexeme(){
if(forward<=beginLex){
return (this->getOtherBuffer())->substr(beginLex,length-beginLex)+
curBuff->substr(0, forward);
}
else return curBuff->substr(beginLex, forward-beginLex);
}