-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgap_buffer.h
158 lines (127 loc) · 3.76 KB
/
gap_buffer.h
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
/*
* gap_buffer.h
*
* Author: Hsin Tsao ([email protected])
* Version: 1.0 (June 12, 2003)
*
* A text buffer class using the buffer-gap technique for managing
* the text stored in the buffer.
*
* 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://www.lazyhacker.com
*/
#include <stdio.h>
class GapBuffer {
char *point; // location pointer into buffer
char *buffer; // start of text buffer
char *bufend; // first location outside buffer
char *gapstart; // start of gap
char *gapend; // first location after end of gap
unsigned int GAP_SIZE; // expand GAP by this value
int InitBuffer(unsigned int size);
/*
* 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 CopyBytes(char *destination, char *source, unsigned int length);
/*
* Expand the size of the buffer.
*/
void ExpandBuffer(unsigned int size);
/*
* Expand the size of the gap.
*/
void ExpandGap(unsigned int size);
public:
static const int DEFAULT_GAP_SIZE=20;
/* Constructor with default gap size. */
GapBuffer(int gsize=DEFAULT_GAP_SIZE);
/* Constructor with instantiating with an existing file. */
GapBuffer(FILE *file, int gsize=DEFAULT_GAP_SIZE);
/* Copy constructor to deal with our pointer members. */
GapBuffer(const GapBuffer& tb);
~GapBuffer();
/*
* Returns the size of the buffer minus the gap.
*/
int BufferSize();
/*
* Move the gap to the current position of the point.
*/
void MoveGapToPoint();
/*
* Set point to offset from start of buffer.
*/
void SetPoint(unsigned int offset);
/*
* Returns the current size of the gap.
*/
int SizeOfGap();
/*
* Returns offset from point to start of buffer.
*/
unsigned int PointOffset();
/*
* Return character that point is pointing to.
* If point is inside the gap, then return the
* the first character outside the gap.
*/
char GetChar();
/*
* Return the previous character and
* move point back one position.
*/
char PreviousChar();
/*
* Replace the character of point. Does
* not move the gap.
*/
void ReplaceChar(char ch);
/*
* Get the next character and increment point.
*/
char NextChar();
/*
* Inserts a character at point location
* and advance the point.
*/
void PutChar(char ch);
/*
* Insert character at point position, but
* does NOT advance the point.
*/
void InsertChar(char ch);
/*
* Delete "size" number of characters.
*/
void DeleteChars(unsigned int size);
/*
* Inserts a length size string
* at point.
*/
void InsertString(const char *string, unsigned int length);
/*
* Prints out the current buffer from start
* to end.
*/
void PrintBuffer();
/*
* Saves to file the number of bytes starting from
* the point.
*/
int SaveBufferToFile(FILE *file, unsigned int bytes);
};