-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.cpp
63 lines (52 loc) · 1.21 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
#include "./include/stringbuffer.hpp"
#include "./include/filebuffer.hpp"
#include <vector>
using namespace std;
StringBuffer::StringBuffer() {
}
void StringBuffer::init(string str) {
buffer = str;
npos = 0;
eosChar = '$';
}
void StringBuffer::loadstring(string str) {
init(str);
}
char StringBuffer::getChar() {
char k = npos < buffer.length() ? buffer[npos]:eosChar;
return k;
}
char StringBuffer::nextChar() {
if (npos+1 < buffer.length()) {
npos++;
return buffer[npos];
}
npos++;
return eosChar;
}
char StringBuffer::prevChar() {
if (npos-1 > -1) {
npos--;
return buffer[npos];
}
return eosChar;
}
bool StringBuffer::done() {
return npos >= buffer.length();
}
FileBuffer::FileBuffer() {
}
vector<string> FileBuffer::readFile(string filename) {
infile.open(filename, ios::in);
if (!infile.is_open()) {
cout<<"Error: couldn't open file "<<filename<<endl;
return lines;
}
string current_line;
while (infile.good()) {
getline(infile, current_line);
lines.push_back(current_line);
}
infile.close();
return lines;
}