-
Notifications
You must be signed in to change notification settings - Fork 4
/
gap_buffer.cpp
481 lines (370 loc) · 9.53 KB
/
gap_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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* gap_buffer.cpp
*
* Author: Hsin Tsao ([email protected])
* Version: 1.0 (June 12, 2003)
*
* This provides the implementation to the GapBuffer class defined
* in text_buffer.h.
*
* Portions of this work derived from Joseph Allen's usenet
* postings on comp.editor that was released to the public
* domain.
*
*
* There are no restrictions on the use of this code other
* than to include my name in any derived work. There are
* no warranty for this obviously, but you are welcomed
* to modify, correct, or adapt the code to your needs. The
* author appreciates if you are willing to submit corrections
* or suggestions for improvement.
*
*
* http:https://www.lazyhacker.com
*/
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "gap_buffer.h"
using namespace std;
GapBuffer::GapBuffer(int gsize) : GAP_SIZE(gsize)
{
InitBuffer(GAP_SIZE);
};
GapBuffer::GapBuffer(FILE *file, int gsize) : GAP_SIZE(gsize)
{
// determine the size of the file then create
// a buffer of size + GAP_SIZE
struct stat buf;
fstat(fileno(file), &buf);
long file_size = buf.st_size;
InitBuffer(file_size + GAP_SIZE);
MoveGapToPoint();
ExpandGap( (int)file_size );
unsigned int amount = fread(gapstart, 1, file_size, file);
gapstart += amount;
};
/* Copy Constructor - Since we have pointers
* as member data, we need to provide our own
* copy constructor because the default will
* only cause copies to all point to the same
* location.
*/
GapBuffer::GapBuffer(const GapBuffer& tb)
{
GAP_SIZE = tb.GAP_SIZE;
buffer = (char *) malloc(tb.bufend - tb.buffer);
strcpy(buffer, tb.buffer);
bufend = buffer + (tb.bufend - tb.buffer);
gapstart = buffer + (tb.gapstart - tb.buffer);
gapend = gapstart + (tb.gapend - tb.gapstart);
point = buffer + (tb.point - tb.buffer);
}
GapBuffer::~GapBuffer()
{
if (buffer) {
free(buffer);
}
};
/*
* Copy the characters from one location to another. We have
* to write our own instead of using memcopy because we are
* working within a single linear buffer and thus can have
* overlap between the source and destination.
*/
int GapBuffer::CopyBytes(char *destination, char *source, unsigned int length)
{
if ( (destination == source) || (length == 0) ) {
return 1;
}
// if we're moving the character toward the front of the buffer
if (source > destination) {
// check to make sure that we don't go beyond the buffer
if ( (source + length) >= bufend ) {
return 0;
}
for (; length > 0; length--) {
*(destination++) = *(source++);
}
} else {
// To prevent overwriting characters we still
// need to move, go to the back and copy forward.
source += length;
destination += length;
for (; length > 0; length--) {
// decrement first 'cause we start one byte beyond where we want
*(--destination) = *(--source);
}
}
return 1;
}
/*
* Expand the buffer to new size + GAP_SIZE.
*
*/
void GapBuffer::ExpandBuffer(unsigned int size)
{
// Check to see that we actually need to increase the buffer
// since BufferSize doesn't include the gap.
if ( ( (bufend - buffer) + size) > BufferSize() ) {
char *origbuffer = buffer;
int NewBufferSize = (bufend - buffer) + size + GAP_SIZE;
buffer = (char *) realloc(buffer, NewBufferSize);
point += buffer - origbuffer;
bufend += buffer - origbuffer;
gapstart += buffer - origbuffer;
gapend += buffer - origbuffer;
}
}
/*
* Move the gap to the current position of the point.
* The point should end in same location as gapstart.
*/
void GapBuffer::MoveGapToPoint()
{
if (point == gapstart) {
return;
}
if (point == gapend) {
point = gapstart;
return;
}
// Move gap towards the left
if (point < gapstart) {
// Move the point over by gapsize.
CopyBytes(point + (gapend-gapstart), point, gapstart - point);
gapend -= (gapstart - point);
gapstart = point;
} else {
// Since point is after the gap, find distance
// between gapend and point and that's how
// much we move from gapend to gapstart.
CopyBytes(gapstart, gapend, point - gapend);
gapstart += point - gapend;
gapend = point;
point = gapstart;
}
}
/*
* Expand the size of the gap. If the required
* size is less then the current gap size, do
* nothing. If the size is greater than the
* current size, increase the gap to the default
* gap size + size.
*/
void GapBuffer::ExpandGap(unsigned int size)
{
if (size > SizeOfGap()) {
size += GAP_SIZE;
ExpandBuffer(size);
CopyBytes(gapend+size, gapend, bufend - gapend);
gapend += size;
bufend += size;
}
}
/*
* Set point to offset from start of buffer.
*/
void GapBuffer::SetPoint(unsigned int offset)
{
point = buffer + offset;
if (point > gapstart) {
point += gapend - gapstart;
}
}
/*
* Returns the current size of the gap.
*/
int GapBuffer::SizeOfGap()
{
return gapend - gapstart;
}
/*
* Returns offset from point to start of buffer.
*/
unsigned int GapBuffer::PointOffset()
{
if (point > gapend) {
return ((point - buffer) - (gapend - gapstart));
} else {
return (point - buffer);
}
}
/*
* Return character that point is pointing to.
* If point is inside the gap, then return the
* the first character outside the gap.
*/
char GapBuffer::GetChar()
{
// If the point is anywhere in the gap, then
// it should always be at the start of the gap.
if (point == gapstart) {
point = gapend;
}
return *point;
}
/*
* Return the previous character and
* move point back one position.
*/
char GapBuffer::PreviousChar()
{
if (point == gapend) {
point = gapstart;
}
return *(--point);
}
/*
* Replace the character of point.
*/
void GapBuffer::ReplaceChar(char ch)
{
// Since we're just replacing the current character,
// we don't need to move or modify the gap.
if (point == gapstart) {
point = gapend;
}
if (point == bufend) {
ExpandBuffer(1);
bufend++;
}
*point = ch;
}
/*
* Increment pointer. Returns next character.
*/
char GapBuffer::NextChar()
{
// point should not be in the gap.
if (point == gapstart) {
point = gapend;
return *point;
}
return *(++point);
}
void GapBuffer::PutChar(char ch)
{
InsertChar(ch);
*point++;
}
/*
* Insert character at point position. Note
* that repeatedly calling this function will
* keep calling MoveGapToPoint since this function
* doesn't advance the point. The result is the
* text appears reverse of key strokes.
*/
void GapBuffer::InsertChar(char ch)
{
// Here we do need to move the gap if the point
// is not already at the start of the gap.
if (point != gapstart) {
MoveGapToPoint();
}
// check to make sure that the gap has room
if (gapstart == gapend) {
ExpandGap(1);
}
*(gapstart++) = ch;
}
/*
* Delete "size" number of characters.
*/
void GapBuffer::DeleteChars(unsigned int size)
{
if (point != gapstart) {
MoveGapToPoint();
}
// We shifted the gap so that gapend points to the location
// where we want to start deleting so extend it
// to cover all the characters.
gapend += size;
}
void GapBuffer::InsertString(char *string, unsigned int length)
{
MoveGapToPoint();
if (length > SizeOfGap()) {
ExpandGap(length);
}
do {
PutChar(*(string++));
} while ( length-- );
}
/*
* Here we initilize the buffer and set
* the pointers to the correct position.
*/
int GapBuffer::InitBuffer(unsigned int size)
{
if (buffer) {
free(buffer);
}
buffer = (char *) malloc(size);
if (!buffer) {
return 0;
}
point = buffer;
gapstart = buffer;
// initially gapend is outside of buffer
gapend = buffer + size;
bufend = gapend;
return 1;
}
int GapBuffer::BufferSize()
{
return (bufend - buffer) - (gapend - gapstart);
}
/*
char* GapBuffer::GetBuffer()
{
return buffer;
}
*/
void GapBuffer::PrintBuffer()
{
/*
char ch;
cout << "Printing the buffer: " << endl;
SetPoint(0);
while (point < bufend) {
cout << GetCharMovePoint();
}
cout << "Printing the buffer in reverse: " << endl;
while (point >= buffer) {
cout << GetPrevCharMovePoint();
}
*/
char *temp = buffer;
while (temp < bufend) {
if ( (temp >= gapstart) && (temp < gapend) ) {
cout << "_";
temp++;
} else {
cout << *(temp++);
}
}
cout << endl;
}
int GapBuffer::SaveBufferToFile(FILE *file, unsigned int bytes)
{
if (!bytes) {
return 1;
}
if (point == gapstart) {
point = gapend;
}
if ( (gapstart > point) && (gapstart < (point + bytes)) && (gapstart != gapend) ) {
if ( gapstart - point != fwrite(point, 1, gapstart-point, file) ) {
return 0;
}
if ( (bytes - (gapstart - point)) != fwrite(gapend, 1, bytes-(gapstart - point), file) ) {
return 1;
}
return 1;
} else {
return bytes == fwrite(point, 1, bytes, file);
}
}