-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
90 lines (71 loc) · 2.05 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
/*
Copyright (C) 2010-2011, Bruce Ediger
This file is part of acl.
acl 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 2 of the License, or
(at your option) any later version.
acl 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 acl; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: buffer.c,v 1.6 2011/06/12 18:22:01 bediger Exp $ */
/*
* Self-resizing buffer.
* The usual life-cycle:
* b = new_buffer(some_size);
* buffer_append(b, ptr, strlen(ptr));
* buffer_append(b, "/", 1);
* buffer_append(b, ptr2, strlen(ptr2));
* char *string = b->buffer;
* ... use string
* delete_buffer(b);
*/
#include <stdio.h> /* NULL manifest constant */
#include <stdlib.h> /* malloc(), free(), realloc() */
#include <string.h> /* memcpy() */
#include <buffer.h>
struct buffer *
new_buffer(int desired_size)
{
struct buffer *r = malloc(sizeof *r);
r->size = 0;
r->buffer = malloc(desired_size);
r->size = desired_size;
r->offset = 0;
return r;
}
void
delete_buffer(struct buffer *b)
{
if (b->buffer)
{
free(b->buffer);
b->buffer = NULL;
}
b->offset = b->size = 0;
free(b);
b = NULL;
}
void
resize_buffer(struct buffer *b, int increment)
{
char *reallocated_buffer;
int new_size = b->size + increment;
reallocated_buffer = realloc(b->buffer, new_size);
b->buffer = reallocated_buffer;
b->size = new_size;
}
void
buffer_append(struct buffer *b, const char *bytes, int length)
{
if (length >= (b->size - b->offset))
resize_buffer(b, length);
/* XXX - assumes resize_buffer() always succeeds. */
memcpy(&b->buffer[b->offset], bytes, length);
b->offset += length;
}