-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdyn_buffer.c
More file actions
150 lines (122 loc) · 2.71 KB
/
dyn_buffer.c
File metadata and controls
150 lines (122 loc) · 2.71 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <assert.h>
#include "dyn_buffer.h"
BUF_ERR
buf_init(buffer *buf, size_t initial_size)
{
assert(buf);
assert(initial_size > 0);
buf->data = malloc(sizeof *buf->data * initial_size);
if (!buf->data) {
return BUF_ERR_MEM;
}
buf->size = initial_size;
buf->used = 1;
buf->data[0] = '\0';
return BUF_SUCCESS;
}
bool
buf_can_fit(buffer *buf, size_t len)
{
return((buf->size - buf->used) > len);
}
BUF_ERR
buf_concat(buffer *buf, char *data, size_t len)
{
assert(buf);
if (len == 0) {
len = strlen(data);
}
while (!buf_can_fit(buf, len)) {
if (buf_enlarge(buf) != BUF_SUCCESS) {
return BUF_ERR_MEM;
}
}
strncat(buf->data, data, len);
buf->used += len;
return BUF_SUCCESS;
}
BUF_ERR
buf_append(buffer *buf, char c)
{
assert(buf);
if (!buf_can_fit(buf, 1)) {
if (buf_enlarge(buf) != BUF_SUCCESS) {
return BUF_ERR_MEM;
}
}
buf->data[buf->used - 1] = c;
buf->data[buf->used] = '\0';
buf->used++;
return BUF_SUCCESS;
}
BUF_ERR
buf_enlarge(buffer *buf)
{
assert(buf);
char *new_data = realloc(buf->data, buf->size * 2);
if (!new_data) {
return BUF_ERR_MEM;
}
buf->size *= 2;
buf->data = new_data;
return BUF_SUCCESS;
}
BUF_ERR
buf_printf(buffer *buf, char *fstring, ...)
{
int printed = 0, free_size = 0;
BUF_ERR ret = BUF_SUCCESS;
assert(buf);
va_list arguments;
va_start(arguments, fstring);
va_end(arguments);
if (!buf) {
vfprintf(stdout, fstring, arguments);
return BUF_SUCCESS;
}
/* enlarge buffer if it's really full */
if (!buf_can_fit(buf, 1)) {
ret = buf_enlarge(buf);
IF_RETURN(ret != BUF_SUCCESS, ret);
}
/* try to write whole string to buffer */
free_size = buf->size - buf->used + 1;
printed = vsnprintf(&buf->data[buf->used - 1], free_size, fstring, arguments);
/* if limit of bufer was reached enlarge and try to print again */
while (printed + 1 > free_size) {
ret = buf_enlarge(buf);
IF_RETURN(ret != BUF_SUCCESS, ret);
free_size = buf->size - buf->used + 1;
va_start(arguments, fstring);
va_end(arguments);
printed = vsnprintf(&buf->data[buf->used - 1], free_size, fstring, arguments);
}
buf->used += printed;
return BUF_SUCCESS;
}
void
buf_flush(buffer *buf)
{
buf->data[0] = '\0';
buf->used = 1;
}
void
buf_destroy(buffer *buf)
{
free(buf->data);
}
size_t
buf_get_len(buffer *buf)
{
return buf->used - 1;
}
char
*buf_get_data(buffer *buf)
{
return buf->data;
}