forked from hughbarney/femto
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgap.c
More file actions
234 lines (202 loc) · 5.94 KB
/
Copy pathgap.c
File metadata and controls
234 lines (202 loc) · 5.94 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
228
229
230
231
232
233
234
/*
* gap.c, femto, Hugh Barney, Public Domain, 2017
* Derived from: Anthony's Editor January 93, (Public Domain 1991, 1993 by Anthony Howe)
*
* Buffer content handling.
*/
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <assert.h>
#include "femto.h"
#include "undo.h"
#include "buffer.h"
#include "gap.h"
#include "key.h"
/** growgap() - Enlarge gap by at least n chars.
*
* @param bp .. buffer.
* @param n .. number of characters.
*
* @returns TRUE on success. FALSE if buffer has content but cannot grow.
*
* Exits with fatal error when no memory can be allocated the first time.
*
* The position of the gap is not changed after enlargment.
*
*/
/* Note: The fatal exit should be left to the caller, though these
* mostly don't care.
*/
bool growgap(buffer_t *bp, point_t n)
{
char_t *new;
point_t buflen, newlen, xgap, xegap;
assert(bp->b_buf <= bp->b_gap);
assert(bp->b_gap <= bp->b_egap);
assert(bp->b_egap <= bp->b_ebuf);
xgap = bp->b_gap - bp->b_buf;
xegap = bp->b_egap - bp->b_buf;
buflen = bp->b_ebuf - bp->b_buf;
/* reduce number of reallocs by growing by a minimum amount */
n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
newlen = buflen + n * sizeof (char_t);
if (buflen == 0) {
if (newlen < 0 || MAX_SIZE_T < newlen)
fatal(f_alloc);
new = (char_t*) malloc((size_t) newlen);
if (new == NULL)
fatal(f_alloc); /* Cannot edit a file without a buffer. */
} else {
if (newlen < 0 || MAX_SIZE_T < newlen) {
msg(m_alloc);
return false;
}
new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
if (new == NULL) {
msg(m_alloc); /* Report non-fatal error. */
return false;
}
}
/* Relocate pointers in new buffer and append the new
* extension to the end of the gap.
*/
bp->b_buf = new;
bp->b_gap = bp->b_buf + xgap;
bp->b_ebuf = bp->b_buf + buflen;
bp->b_egap = bp->b_buf + newlen;
while (xegap < buflen--)
*--bp->b_egap = *--bp->b_ebuf;
bp->b_ebuf = bp->b_buf + newlen;
assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
assert(bp->b_buf <= bp->b_gap);
assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
assert(bp->b_egap <= bp->b_ebuf);
return true;
}
point_t movegap(buffer_t *bp, point_t offset)
{
char_t *p = ptr(bp, offset);
while (p < bp->b_gap)
*--bp->b_egap = *--bp->b_gap;
while (bp->b_egap < p)
*bp->b_gap++ = *bp->b_egap++;
assert(bp->b_gap <= bp->b_egap);
assert(bp->b_buf <= bp->b_gap);
assert(bp->b_egap <= bp->b_ebuf);
return (pos(bp, bp->b_egap));
}
/* Given a buffer offset, convert it to a pointer into the buffer */
char_t * ptr(buffer_t *bp, register point_t offset)
{
if (offset < 0)
return (bp->b_buf);
#if VALGRIND
return (bp->b_buf+offset +
(
bp->b_buf +
offset < bp->b_gap ?
0 : bp->b_egap-bp->b_gap - 1
)
);
#else
return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
#endif
}
/* Given a pointer into the buffer, convert it to a buffer offset */
point_t pos(buffer_t *bp, register char_t *cp)
{
assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
}
/** buffer_fwrite() - write buffer content to stream
* @param stream
* @param size
*
* Writes size bytes starting from point to stream, but at most
* the bytes from point to the end of the buffer.
*
*/
size_t buffer_fwrite(buffer_t *buffer, FILE *stream, size_t size)
{
size_t len;
if (size == 0)
return 0;
buffer->b_point = movegap(buffer, buffer->b_point);
len = buffer->b_ebuf - buffer->b_egap;
if (size > len)
size = len;
return fwrite(buffer->b_egap, sizeof (char), size, stream);
}
void zero_buffer(buffer_t *bp)
{
/* reset the gap, make it the whole buffer */
bp->b_gap = bp->b_buf;
bp->b_egap = bp->b_ebuf;
bp->b_point = 0; /* goto start of buffer */
bp->b_mark = NOMARK;
}
/* get the size of the document in the buffer */
point_t document_size(buffer_t *bp)
{
return (bp->b_ebuf - bp->b_buf) - (bp->b_egap - bp->b_gap);
}
bool buffer_is_empty(buffer_t *bp)
{
return (bp->b_gap == bp->b_buf && bp->b_egap == bp->b_ebuf);
}
/** Read size bytes from stream into buffer starting at point
@returns: number of bytes read or zero if the buffer cannot be grown by size
*/
size_t buffer_fread(buffer_t *buffer, FILE *stream, size_t size)
{
size_t len;
if (size == 0)
return 0;
if (buffer->b_egap - buffer->b_gap < size * sizeof (char_t) && !growgap(buffer, size))
return -1;
buffer->b_point = movegap(buffer, buffer->b_point);
len = fread(buffer->b_gap, sizeof (char), size, stream);
buffer->b_gap += len;
return len;
}
/* find the point for start of line ln */
point_t line_to_point(int ln)
{
point_t end_p = pos(curbp, curbp->b_ebuf);
point_t p, start;
for (p=0, start=0; p < end_p; p++) {
if ( *(ptr(curbp, p)) == '\n') {
if (--ln == 0)
return start;
if (p + 1 < end_p)
start = p + 1;
}
}
return -1;
}
/* scan buffer and fill in curline and lastline */
void get_line_stats(int *curline, int *lastline)
{
point_t end_p = pos(curbp, curbp->b_ebuf);
point_t p;
int line;
*curline = -1;
for (p=0, line=0; p < end_p; p++) {
line += (*(ptr(curbp,p)) == '\n') ? 1 : 0;
*lastline = line;
if (*curline == -1 && p == curbp->b_point) {
*curline = (*(ptr(curbp,p)) == '\n') ? line : line + 1;
}
}
*lastline = *lastline + 1;
if (curbp->b_point == end_p)
*curline = *lastline;
}
/*
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/