-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cpp
86 lines (62 loc) · 1.92 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
#include "Buffer.h"
Buffer::Buffer(const string& file_path) {
f.open(file_path);
buffer = new char[2 * BUFFER_SIZE];
f.readsome(buffer, 2 * BUFFER_SIZE);
int n_char_read = f.gcount();
size_left = (n_char_read < BUFFER_SIZE) ? n_char_read : BUFFER_SIZE;
size_right = (n_char_read > BUFFER_SIZE) ? n_char_read - BUFFER_SIZE : 0;
ptr_begin = 0;
ptr_end = -1;
}
Buffer::Buffer(const Buffer& orig) {}
Buffer::~Buffer() {
f.close();
}
void Buffer::print() {
for (int i = 0 ; i < size_left ; i++) {
if (i == ptr_begin) cout << "[";
if (i == ptr_end) cout << "]";
if (buffer[i] == '\n') cout << "\\n";
else cout << buffer[i];
}
cout << "|";
for (int i = BUFFER_SIZE ; i < BUFFER_SIZE + size_right ; i++) {
if (i == ptr_begin) cout << "[";
if (i == ptr_end) cout << "]";
if (buffer[i] == '\n') cout << "\\n";
else cout << buffer[i];
}
cout << endl;
}
char Buffer::current() const {
return buffer[ptr_end];
}
void Buffer::settle() {
if (ptr_begin <= BUFFER_SIZE && ptr_end > BUFFER_SIZE)
load_buffer_left();
if (ptr_begin >= BUFFER_SIZE && ptr_end <= BUFFER_SIZE)
load_buffer_right();
ptr_begin = ptr_end;
}
char Buffer::next() {
ptr_end = (ptr_end + 1) % (2 * BUFFER_SIZE);
return current();
}
bool Buffer::done() const {
return (size_left == 0 && ptr_begin >= BUFFER_SIZE + size_right) ||
(size_right == 0 && ptr_begin >= size_left);
}
void Buffer::load_buffer_left() {
f.readsome(buffer, BUFFER_SIZE);
size_left = f.gcount();
}
void Buffer::load_buffer_right() {
f.readsome(buffer + BUFFER_SIZE, BUFFER_SIZE);
size_right = f.gcount();
}
char Buffer::retract(int n) {
ptr_end -= n;
// if (ptr_end <= ptr_begin) throw std::invalid_argument("Cannot retract beyond");
return current();
}