File: buffer.c

package info (click to toggle)
imapfilter 1%3A1.2.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 300 kB
  • ctags: 315
  • sloc: ansic: 3,392; sh: 182; makefile: 103
file content (60 lines) | stat: -rw-r--r-- 737 bytes parent folder | download | duplicates (10)
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
#include <stdio.h>

#include "imapfilter.h"
#include "buffer.h"


/*
 * Initialize buffer.
 */
void
buffer_init(buffer *buf, size_t n)
{

	buf->data = (char *)xmalloc((n + 1) * sizeof(char));
	*buf->data = '\0';
	buf->len = 0;
	buf->size = n;
}


/*
 * Free allocated memory of buffer.
 */
void
buffer_free(buffer *buf)
{

	if (!buf->data)
		return;

	xfree(buf->data);
	buf->data = NULL;
}


/*
 * Reset buffer.
 */
void
buffer_reset(buffer *buf)
{

	*buf->data = '\0';
	buf->len = 0;
}


/*
 * Check if the buffer has enough space to store data and reallocate memory if
 * needed.
 */
void
buffer_check(buffer *buf, size_t n)
{

	while (n > buf->size) {
		buf->size *= 2;
		buf->data = (char *)xrealloc(buf->data, buf->size + 1);
	}
}