File: buffer.c

package info (click to toggle)
xmlindent 0.2.18-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: ansic: 625; yacc: 63; makefile: 29; xml: 22
file content (130 lines) | stat: -rw-r--r-- 3,094 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2002  Pekka Enberg <penberg@iki.fi>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "error.h"
#include "buffer.h"

/* Initialize buffer.  */
void buffer_init(struct buffer * buffer, size_t initial_capacity)
{
    buffer->data     = malloc(initial_capacity * sizeof(unsigned char));
    buffer->length   = 0;
    buffer->capacity = initial_capacity;
}

/* Release buffer.  */
void buffer_release(struct buffer * buffer)
{
    if (buffer->capacity > 0) {
	free(buffer->data);
	buffer->data     = NULL;
	buffer->capacity = 0;
    }
    buffer->length = 0;
}

/* Check if buffer is full.  */
bool buffer_is_full(struct buffer * buffer)
{
    return (buffer->capacity == buffer->length);
}

#define BUFFER_GROWTH_INCREMENT 256

/* Grow buffer by one.  */
void buffer_grow(struct buffer * buffer)
{
    buffer->data =
	realloc(buffer->data,
		(buffer->capacity +
		 BUFFER_GROWTH_INCREMENT) * sizeof(unsigned char));

    if (buffer->data == NULL) {
	error("Could not allocate memory for buffer.");
	exit(1);
    }
    buffer->capacity += BUFFER_GROWTH_INCREMENT;
}

/* Push character at the end of buffer.  */
void buffer_push_char(struct buffer * buffer, int c)
{
    if (buffer_is_full(buffer)) {
	buffer_grow(buffer);
    }
    buffer->data[buffer->length] = c;
    buffer->length++;
}

/* Push string at the end of buffer.  */
void buffer_push_str(struct buffer * buffer, const char * text)
{
    int i;

    for (i = 0; i < strlen(text); i++) {
	buffer_push_char(buffer, (int) text[i]);
    }
}

/* Pop item from back of buffer.  */
int buffer_pop_char(struct buffer * buffer)
{
    int result = -1;

    if (buffer->length > 0) {
	buffer->length--;
	result = buffer->data[buffer->length];
    } else {
	error("Buffer underflow.");
    }
    return result;
}

/* Flush buffer to output stream.  */
void buffer_flush(struct buffer * buffer, FILE * output)
{
    int i;

    for (i = 0; i < buffer->length; i++) {
	fputc(buffer->data[i], output);
    }
    buffer->length = 0;
}

/* Return buffer size.  */
size_t buffer_size(struct buffer * buffer)
{
    return buffer->length;
}

void buffer_copy(struct buffer * dest, struct buffer * src)
{
    int i;

    for (i = 0; i < src->length; i++) {
        buffer_push_char(dest, src->data[i]);
    }
}

void buffer_clear(struct buffer * buf)
{
    buf->length = 0;
}