-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuffer.c
193 lines (156 loc) · 4.47 KB
/
buffer.c
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
/*
Copyright 2014 Dario Hamidi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "buffer.h"
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct buffer {
size_t capacity;
size_t gapstart;
size_t gapend;
char* data;
};
static BUFFER bufnew(size_t hint) {
BUFFER buf = NULL;
if (hint < 1024) {
hint = 1024;
}
buf = must_malloc(sizeof(*buf));
buf->capacity = hint;
buf->data = must_malloc(hint);
buf->gapstart = 1;
buf->gapend = hint - 1;
return buf;
}
static const char* bufbefore(BUFFER self) {
return self->data;
}
static const char* bufafter(BUFFER self) {
return self->data + self->gapend - 1;
}
static void bufdestroy(BUFFER* self) {
if (self == NULL) {
return;
}
free( (*self)->data );
free( *self );
*self = NULL;
}
static int bufisfull(BUFFER self) {
return (self->gapstart + 1) == self->gapend;
}
static void bufexpand(BUFFER self) {
char *new_data;
char *old_data = self->data;
size_t new_capacity = self->capacity + BUFFER_GROW_BY;
if (new_capacity <= self->capacity) {
fprintf(stderr, "%s: cannot expand buffer: integer overflow\n",
PROGRAM_NAME);
abort();
}
new_data = must_malloc(new_capacity);
memcpy(new_data, bufbefore(self), self->gapstart);
memcpy(new_data + (new_capacity - self->gapend),
bufafter(self),
self->capacity - self->gapend);
self->data = new_data;
self->gapend = new_capacity - self->gapend;
free(old_data);
}
static void bufcput(BUFFER self, int c) {
unsigned char byte = (unsigned char)c;
if (bufisfull(self)) {
bufexpand(self);
}
self->data[self->gapstart - 1] = byte;
self->data[self->gapstart] = 0;
self->gapstart++;
}
static void bufsput(BUFFER self, const char* str) {
while (*str) {
bufcput(self, *str++);
}
}
static char* bufstring(BUFFER self, char** buf, size_t* bufsize) {
size_t new_size = self->gapstart + (self->capacity - self->gapend);
if ( (*bufsize) < new_size || (*buf) == NULL ) {
*bufsize = new_size;
free(*buf);
*buf = must_malloc(new_size);
}
strcpy( (*buf), bufbefore(self) );
strcpy( (*buf) + self->gapstart - 1 , bufafter(self) );
return *buf;
}
static void bufforward(BUFFER self, int by) {
while (by --> 0 && self->gapend < self->capacity - 1) {
self->data[self->gapstart - 1] = self->data[self->gapend - 1];
self->data[self->gapend - 1] = 0;
self->gapstart++;
self->gapend++;
}
}
static void bufbackward(BUFFER self, int by) {
while (by --> 0 && self->gapstart > 1) {
self->data[self->gapend - 2] = self->data[self->gapstart - 2];
self->data[self->gapstart - 2] = 0;
self->gapstart--;
self->gapend--;
}
}
static size_t bufpoint(BUFFER self) {
return self->gapstart;
}
static void bufdelete(BUFFER self) {
if ( self->gapend < self->capacity - 1 ) {
self->data[self->gapend - 1] = 0;
self->gapend++;
}
}
static void bufclear(BUFFER self) {
memset(self->data, 0, self->capacity);
self->gapstart = 1;
self->gapend = self->capacity - 1;
}
static void bufdeletetoend(BUFFER self) {
char* after = (char*)bufafter(self);
memset(after, 0, strlen(after));
self->gapend = self->capacity - 1;
}
static void bufdeletetobeginning(BUFFER self) {
char* before = (char*)bufbefore(self);
memset(before, 0, strlen(before));
self->gapstart = 1;
}
static size_t buflen(BUFFER self) {
return self->capacity - (self->gapend - self->gapstart);
}
struct buffer_interface Buffer = {
.new = bufnew,
.destroy = bufdestroy,
.length = buflen,
.cput = bufcput,
.sput = bufsput,
.string = bufstring,
.forward = bufforward,
.backward = bufbackward,
.before = bufbefore,
.after = bufafter,
.point = bufpoint,
.delete = bufdelete,
.clear = bufclear,
.delete_to_end = bufdeletetoend,
.delete_to_beginning = bufdeletetobeginning,
};