-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgapBuffer.cpp
138 lines (121 loc) · 2.9 KB
/
gapBuffer.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
130
131
132
133
134
135
136
137
138
#include "gapBuffer.h"
using namespace std;
// initialize a new empty buffer gap
GapBuffer::GapBuffer(int size) {
this->size = size;
this->buffer = new char[size];
this->buffer[0] = '\0';
this->gapStart = 0;
this->gapEnd = size-1;
this->dirty = false;
}
// returns whether the buffer gap is valid
bool GapBuffer::is_valid() {
if (this->size>0 &&
sizeof(this->buffer)==this->size &&
this->gapStart >= 0 &&
this->gapStart < this->size &&
this->gapEnd >= 0 &&
this->gapEnd < this->size &&
this->gapStart <= this->gapEnd)
return true;
return false;
}
// returns whether the buffer gap is empty
bool GapBuffer::is_empty() {
if (this->gapStart == 0 &&
this->gapEnd == this->size-1)
return true;
return false;
}
// returns whether the buffer gap is full
bool GapBuffer::is_full() {
if (this->gapStart > this->gapEnd)
return true;
return false;
}
// returns whether the buffer gap is dirty
bool GapBuffer::is_dirty() {
if (this->dirty == true)
return true;
return false;
}
// returns whether the buffer gap is at left
bool GapBuffer::is_at_left() {
if(this->gapStart == 0)
return true;
return false;
}
// returns whether the buffer gap is at right
bool GapBuffer::is_at_right() {
if (this->gapEnd == this->size-1)
return true;
return false;
}
// returns whether two buffer gaps are the same
bool GapBuffer::equals(GapBuffer b) {
if (this->blockOffset == b.blockOffset && this->id == b.id)
return true;
return false;
}
// moves the cursor to the right
void GapBuffer::move_forward() {
if (!this->is_at_right()) {
this->buffer[gapStart] = this->buffer[gapEnd+1];
(this->gapStart)++;
(this->gapEnd)++;
}
}
// moves the cursor to the left
void GapBuffer::move_backward() {
if (!this->is_at_left()) {
this->buffer[gapEnd] = this->buffer[gapStart-1];
(this->gapStart)--;
(this->gapEnd)--;
}
}
// inserts the character c before the gap
void GapBuffer::insert_char(char c) {
if (!this->is_full()) {
//this->buffer[gapStart] = c;
//(this->gapStart)++;
this->buffer[gapEnd] = c;
(this->gapEnd)--;
this->dirty = true;
}
}
// deletes the character before the gap
void GapBuffer::delete_char() {
if (!this->is_empty()) {
//(this->gapStart)--;
this->buffer[this->gapStart-1] = this->buffer[this->gapEnd+1];
(this->gapEnd)++;
this->dirty = true;
}
}
// returns the buffer value
char* GapBuffer::get_buffer() {
return buffer;
}
// sets the buffer value
int GapBuffer::set_buffer(char* buffer, int size) {
if (this->gapEnd+1+size == this->size) {
strncpy(this->buffer+this->gapEnd+1, buffer, size);
return 0;
}
return 1;
}
// returns the current character just before cursor position
char GapBuffer::current_char() {
if (this->is_at_left()) {
return '\0';
}
return this->buffer[gapStart-1];
}
// returns the next character just after cursor position
char GapBuffer::next_char() {
if (this->is_at_right()) {
return '\0';
}
return this->buffer[gapEnd+1];
}