accordion.cpp
6.13 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "accordion.h"
#include <assert.h>
#include <string.h>
Accordion::Accordion(char * buffer, int bufferSize) :
m_historySize(bufferSize),
m_history(buffer),
m_numberOfBuffers(0),
m_startOfLastEditedBuffer(0)
{
for (int i = 0; i < m_historySize; i ++) {
m_history[i] = k_freeSpaceMarker;
}
}
const char * Accordion::bufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
int startOfBuffer = startOfBufferAtIndex(index);
return &m_history[startOfBuffer];
}
char * Accordion::editableBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
moveFreeSpaceAtEndOfBufferAtIndex(index);
int startOfBuffer = startOfBufferAtIndex(index);
return &m_history[startOfBuffer];
}
int Accordion::sizeOfEditableBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
moveFreeSpaceAtEndOfBufferAtIndex(index);
int length = 0;
for (int i = startOfBufferAtIndex(index); i < m_historySize-1; i++) {
if (m_history[i] == k_freeSpaceMarker && m_history[i+1] != k_freeSpaceMarker) {
break;
}
length++;
// We do not count one Free Space Marker, in order to always have at a Free
// Space of size at least one.
}
return length;
}
bool Accordion::appendBuffer(const char * buffer) {
cleanFreeSpace();
moveFreeSpaceAtEndOfHistory();
int len = strlen(buffer);
if (len + 1 > freeSpaceSize() - 1) { // We keep at keast one Free char.
return false;
}
m_startOfLastEditedBuffer = startOfFreeSpace();
memcpy(&m_history[m_startOfLastEditedBuffer], buffer, len+1);
m_numberOfBuffers++;
return true;
}
bool Accordion::replaceBufferAtIndex(int index, const char * buffer) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
int len = strlen(buffer);
if (len < sizeOfEditableBufferAtIndex(index)) {
int startOfOldBuffer = startOfBufferAtIndex(index);
memcpy(&m_history[startOfOldBuffer], buffer, len+1);
m_startOfLastEditedBuffer = startOfOldBuffer;
return true;
}
return false;
}
void Accordion::deleteBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
moveFreeSpaceAtEndOfBufferAtIndex(index);
int i = startOfBufferAtIndex(index);
while (i < m_historySize && m_history[i] != k_freeSpaceMarker) {
m_history[i] = k_freeSpaceMarker;
i++;
}
m_numberOfBuffers--;
}
void Accordion::deleteLastBuffer() {
cleanFreeSpace();
if (m_numberOfBuffers > 0) {
deleteBufferAtIndex(m_numberOfBuffers-1);
}
}
void Accordion::deleteAll() {
cleanFreeSpace();
for (int i = 0; i < m_historySize; i++){
m_history[i] = k_freeSpaceMarker;
}
m_numberOfBuffers = 0;
}
int Accordion::freeSpaceSize() {
cleanFreeSpace();
int sizeOfFreeSpace = 0;
int freeSpaceStart = startOfFreeSpace();
for (int i = freeSpaceStart; i < m_historySize; i++) {
if (m_history[i] == k_freeSpaceMarker) {
sizeOfFreeSpace++;
} else {
return sizeOfFreeSpace;
}
}
return sizeOfFreeSpace;
}
int Accordion::startOfBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
int bufferCount = 0;
int startOfBuffer = 0;
while (m_history[startOfBuffer] == k_freeSpaceMarker && startOfBuffer < m_historySize) {
startOfBuffer++;
}
for (int i = startOfBuffer; i < m_historySize; i++) {
if (bufferCount == index) {
while (m_history[i] == k_freeSpaceMarker && i < m_historySize) {
i++;
}
return i;
}
if (m_history[i] == 0) {
bufferCount++;
}
}
assert(false);
return 0;
}
int Accordion::endOfBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
int startOfBuffer = startOfBufferAtIndex(index);
for (int i = startOfBuffer; i < m_historySize; i++) {
if (m_history[i] == 0) {
return i;
}
}
assert(false);
return 0;
}
int Accordion::startOfFreeSpace() {
cleanFreeSpace();
for (int i = 0; i < m_historySize; i++) {
if (m_history[i] == k_freeSpaceMarker) {
return i;
}
}
assert(false);
return 0;
}
void Accordion::cleanFreeSpace() {
if (m_history[m_startOfLastEditedBuffer] == k_freeSpaceMarker) {
return;
}
int indexOfCharToChangeIntoFreeSpaceMarker = m_startOfLastEditedBuffer
+ strlen(&m_history[m_startOfLastEditedBuffer]) + 1;
while (m_history[indexOfCharToChangeIntoFreeSpaceMarker] != k_freeSpaceMarker
&& indexOfCharToChangeIntoFreeSpaceMarker < m_historySize)
{
m_history[indexOfCharToChangeIntoFreeSpaceMarker] = k_freeSpaceMarker;
indexOfCharToChangeIntoFreeSpaceMarker++;
}
}
void Accordion::moveFreeSpaceAtPosition(int i) {
assert(i >= 0 && i <= m_historySize);
cleanFreeSpace();
int freeSpaceStart = startOfFreeSpace();
int newFreeSpaceStart = freeSpaceStart;
if (freeSpaceStart != i){
// First, move the chars that would be overriden by the free space.
// The indexes depend on the relative positions of the free space and the
// new destination.
int sizeFreeSpace = freeSpaceSize();
int len = 0;
int src = 0;
int dst = 0;
if (freeSpaceStart > i) {
len = freeSpaceStart - i;
src = i;
dst = i + sizeFreeSpace;
newFreeSpaceStart = i;
} else {
src = freeSpaceStart + sizeFreeSpace;
len = i - src;
dst = freeSpaceStart;
newFreeSpaceStart = i-sizeFreeSpace;
}
memmove(&m_history[dst], &m_history[src], len);
// Then move the free space.
for (int j = newFreeSpaceStart ; j < newFreeSpaceStart+sizeFreeSpace; j++) {
m_history[j] = k_freeSpaceMarker;
}
}
m_startOfLastEditedBuffer = newFreeSpaceStart-1;
while (m_startOfLastEditedBuffer > 0 && m_history[m_startOfLastEditedBuffer-1] != 0 ) {
m_startOfLastEditedBuffer--;
}
}
void Accordion::moveFreeSpaceAtEndOfBufferAtIndex(int index) {
assert(index >= 0 && index < numberOfBuffers());
cleanFreeSpace();
int endOfBuffer = endOfBufferAtIndex(index);
moveFreeSpaceAtPosition(endOfBuffer+1);
}
void Accordion::moveFreeSpaceAtEndOfHistory() {
cleanFreeSpace();
if (m_numberOfBuffers > 0) {
moveFreeSpaceAtEndOfBufferAtIndex(m_numberOfBuffers-1);
}
}