-
Notifications
You must be signed in to change notification settings - Fork 85
/
xbuf.c
88 lines (80 loc) · 1.35 KB
/
xbuf.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
#include "less.h"
#include "xbuf.h"
/*
* Initialize an expandable text buffer.
*/
public void
xbuf_init(xbuf)
struct xbuffer *xbuf;
{
xbuf->data = NULL;
xbuf->size = xbuf->end = 0;
}
public void
xbuf_deinit(xbuf)
struct xbuffer *xbuf;
{
if (xbuf->data != NULL)
free(xbuf->data);
xbuf_init(xbuf);
}
public void
xbuf_reset(xbuf)
struct xbuffer *xbuf;
{
xbuf->end = 0;
}
/*
* Add a byte to an expandable text buffer.
*/
public void
xbuf_add_byte(xbuf, b)
struct xbuffer *xbuf;
unsigned char b;
{
if (xbuf->end >= xbuf->size)
{
unsigned char *data;
xbuf->size = (xbuf->size == 0) ? 16 : xbuf->size * 2;
data = (unsigned char *) ecalloc(xbuf->size, sizeof(unsigned char));
if (xbuf->data != NULL)
{
memcpy(data, xbuf->data, xbuf->end);
free(xbuf->data);
}
xbuf->data = data;
}
xbuf->data[xbuf->end++] = b;
}
public void
xbuf_add_data(xbuf, data, len)
struct xbuffer *xbuf;
unsigned char *data;
int len;
{
int i;
for (i = 0; i < len; i++)
xbuf_add_byte(xbuf, data[i]);
}
public int
xbuf_pop(buf)
struct xbuffer *buf;
{
if (buf->end == 0)
return -1;
return (int) buf->data[--(buf->end)];
}
public void
xbuf_set(dst, src)
struct xbuffer *dst;
struct xbuffer *src;
{
xbuf_reset(dst);
xbuf_add_data(dst, src->data, src->end);
}
public char *
xbuf_char_data(xbuf)
struct xbuffer *xbuf;
{
return (char *)(xbuf->data);
}