File: itbuff.c

package info (click to toggle)
vile 9.8za-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,644 kB
  • sloc: ansic: 120,894; lex: 14,981; sh: 4,478; perl: 3,511; cpp: 3,180; makefile: 1,425; awk: 271
file content (426 lines) | stat: -rw-r--r-- 7,114 bytes parent folder | download
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
/*
 *	tbuff.c
 *
 *	Manage dynamic temporary buffers.
 *	Note that some temp-buffs are never freed, for speed
 *
 *	To do:	add 'itb_ins()' and 'itb_del()' to support cursor-level command
 *		editing.
 *
 * $Id: itbuff.c,v 1.31 2025/01/26 17:03:33 tom Exp $
 */

#include "estruct.h"
#include "edef.h"

#define	NCHUNK	NLINE

/*******(testing)************************************************************/
#if NO_LEAKS
typedef struct _itb_list {
    struct _itb_list *link;
    ITBUFF *buff;
} ITB_LIST;

static ITB_LIST *all_tbuffs;

#define	AllocatedBuffer(q)	itb_remember(q)
#define	FreedBuffer(q)		itb_forget(q)

static void
itb_remember(ITBUFF * p)
{
    ITB_LIST *q;

    beginDisplay();
    if ((q = typealloc(ITB_LIST)) != NULL) {
	q->buff = p;
	q->link = all_tbuffs;
	all_tbuffs = q;
    }
    endofDisplay();
}

static void
itb_forget(ITBUFF * p)
{
    ITB_LIST *q, *r;

    beginDisplay();
    for (q = all_tbuffs, r = NULL; q != NULL; r = q, q = q->link) {
	if (q->buff == p) {
	    if (r != NULL)
		r->link = q->link;
	    else
		all_tbuffs = q->link;
	    free(q);
	    break;
	}
    }
    endofDisplay();
}

void
itb_leaks(void)
{
    while (all_tbuffs != NULL) {
	ITBUFF *q = all_tbuffs->buff;
	FreedBuffer(q);
	itb_free(&q);
    }
}

#else
#define	AllocatedBuffer(q)
#define	FreedBuffer(q)
#endif

/*******(initialization)*****************************************************/

/*
 * ensure that the given temp-buff has as much space as specified
 */
ITBUFF *
itb_alloc(ITBUFF ** p, size_t n)
{
    ITBUFF *q = *p;

    beginDisplay();
    if (q == NULL) {
	if ((q = *p = typealloc(ITBUFF)) != NULL) {
	    if ((q->itb_data = typeallocn(int, q->itb_size = n)) != NULL) {
		q->itb_used = 0;
		q->itb_last = 0;
		q->itb_endc = esc_c;
		AllocatedBuffer(q);
	    } else {
		itb_free(&q);
		*p = NULL;
	    }
	}
    } else if (n >= q->itb_size) {
	safe_typereallocn(int, q->itb_data, q->itb_size = (n * 2));
	if (q->itb_data == NULL) {
	    itb_free(&q);
	    *p = NULL;
	}
    }
    endofDisplay();
    return q;
}

/*
 * (re)initialize a temp-buff
 */
ITBUFF *
itb_init(ITBUFF ** p, int c)
{
    ITBUFF *q = *p;
    if (q == NULL)
	q = itb_alloc(p, (size_t) NCHUNK);
    if (q != NULL) {
	q->itb_used = 0;
	q->itb_last = 0;
	q->itb_endc = c;	/* code to return if no-more-data */
    }
    return (*p = q);
}

/*
 * deallocate a temp-buff
 */
void
itb_free(ITBUFF ** p)
{
    ITBUFF *q = *p;

    beginDisplay();
    if (q != NULL) {
	FreedBuffer(q);
	FreeIfNeeded(q->itb_data);
	free(q);
    }
    *p = NULL;
    endofDisplay();
}

/*******(storage)************************************************************/

/*
 * put a character c at the nth position of the temp-buff.  make it the last.
 */
static ITBUFF *
itb_put(ITBUFF ** p, size_t n, int c)
{
    ITBUFF *q;

    if ((q = itb_alloc(p, n + 1)) != NULL) {
	q->itb_data[n] = c;
	q->itb_used = n + 1;
    }
    return q;
}

/*
 * stuff the nth character into the temp-buff -- assumes space already there
 *  it's sort of the opposite of itb_peek
 */
void
itb_stuff(ITBUFF * p, int c)
{
    if (p != NULL) {
	if (p->itb_last < p->itb_used)
	    p->itb_data[p->itb_last] = c;
	else
	    p->itb_endc = c;
    }
}

/*
 * append a character to the temp-buff
 */
ITBUFF *
itb_append(ITBUFF ** p, int c)
{
    ITBUFF *q = *p;
    size_t n = (q != NULL) ? q->itb_used : 0;

    return itb_put(p, n, c);
}

/*
 * Copy one temp-buff to another
 */
ITBUFF *
itb_copy(ITBUFF ** d, ITBUFF * s)
{
    ITBUFF *p;

    if (s != NULL) {
	if ((p = itb_init(d, s->itb_endc)) != NULL) {
	    int *ptr = s->itb_data;
	    size_t len = s->itb_used;
	    while ((len-- != 0) && (p = itb_append(&p, *ptr++)) != NULL) {
		;
	    }
	}
    } else
	p = itb_init(d, esc_c);
    return p;
}

#if VILE_NEEDED
/*
 * append a binary data to the temp-buff
 */
ITBUFF *
itb_bappend(ITBUFF ** p, const char *s, size_t len)
{
    while ((len-- != 0) && itb_append(p, (int) (*s++)) != 0) {
	;
    }
    return *p;
}

/*
 * append a string to the temp-buff
 */
ITBUFF *
itb_sappend(ITBUFF ** p, const char *s)
{
    if (!s)
	return *p;
    while (*s && itb_append(p, (int) (*s++)) != 0) {
	;
    }
    return *p;
}

void
itb_delete(ITBUFF * p, size_t cnt)
{
    int *from, *to, *used;

    if (p != 0) {
	to = &p->itb_data[p->itb_last];
	from = to + cnt;

	used = &p->itb_data[p->itb_used];

	if (from >= used) {
	    p->itb_used = p->itb_last;
	    return;
	}

	while (from < used) {
	    *to++ = *from++;
	}

	p->itb_used -= cnt;
    }
}

ITBUFF *
itb_insert(ITBUFF ** p, int c)
{
    ITBUFF *q = *p;
    int *last, *to;

    /* force enough room for another character */
    q = itb_put(p, q->itb_used, 0 /* any value */ );

    /* copy up */
    to = &q->itb_data[q->itb_used - 1];
    last = &q->itb_data[q->itb_last];
    while (to > last) {
	*to = *(to - 1);
	to--;
    }

    /* put in the new one */
    itb_stuff(q, c);

    return *p;
}
#endif /* NEEDED */

/*******(retrieval)************************************************************/

/*
 * get the nth character from the temp-buff
 */
int
itb_get(ITBUFF * p, size_t n)
{
    int c = esc_c;

    if (p != NULL)
	c = (n < p->itb_used) ? p->itb_data[n] : p->itb_endc;

    return c;
}

#if VILE_NEEDED
/*
 * undo the last 'itb_put'
 */
void
itb_unput(ITBUFF * p)
{
    if (p != 0
	&& p->itb_used != 0)
	p->itb_used -= 1;
}
#endif

/*******(iterators)************************************************************/

/*
 * Reset the iteration-count
 */
static size_t
itb_seek(ITBUFF * p, size_t seekto, int whence)
{
    size_t olast;

    if (p == NULL)
	return 0;

    olast = p->itb_last;

    if (whence == 0)
	p->itb_last = seekto;
    else if (whence == 1)
	p->itb_last += seekto;
    else if (whence == 2)
	p->itb_last = p->itb_used - seekto;
    return olast;
}

void
itb_first(ITBUFF * p)
{
    (void) itb_seek(p, (size_t) 0, 0);
}

/*
 * Returns true iff the iteration-count has not gone past the end of temp-buff.
 */
int
itb_more(ITBUFF * p)
{
    return (p != NULL) ? (p->itb_last < p->itb_used) : FALSE;
}

/*
 * get the next character from the temp-buff
 */
int
itb_next(ITBUFF * p)
{
    if (p != NULL)
	return itb_get(p, p->itb_last++);
    return esc_c;
}

/*
 * get the last character from the temp-buff, and shorten
 * (opposite of itb_append)
 */
int
itb_last(ITBUFF * p)
{
    int c;

    if (p != NULL && p->itb_used > 0) {
	c = itb_get(p, p->itb_used - 1);
	p->itb_used--;
	return c;
    }
    return esc_c;
}

#if VILE_NEEDED
/*
 * undo a itb_next
 */
void
itb_unnext(ITBUFF * p)
{
    if (p == 0)
	return;
    if (p->itb_last > 0)
	p->itb_last--;
}
#endif

/*
 * get the next character from the temp-buff w/o incrementing index
 */
int
itb_peek(ITBUFF * p)
{
    if (p != NULL)
	return itb_get(p, p->itb_last);
    return esc_c;
}

/*******(bulk-data)************************************************************/

/*
 * returns a pointer to data, assumes it is one long string
 */
int *
itb_values(ITBUFF * p)
{
    return (p != NULL) ? p->itb_data : NULL;
}

/*
 * returns the length of the data
 */
size_t
itb_length(ITBUFF * p)
{
    return (p != NULL && p->itb_data != NULL) ? p->itb_used : 0;
}