-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.c
144 lines (110 loc) · 2.51 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
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
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "buffer.h"
#include "common.h"
#define BUFFER_MINSIZE 128
#define BUFFER_MAXSIZE 10240
static int enlarge_buffer(struct buffer * const buffer, size_t new_size)
{
if (buffer->size >= BUFFER_MAXSIZE)
return -1;
new_size = MIN(new_size, BUFFER_MAXSIZE);
new_size = MAX(new_size, BUFFER_MINSIZE);
void *ptr;
if (!(ptr = realloc(buffer->buf, new_size)))
return -1;
buffer->buf = ptr;
buffer->size = new_size;
return 0;
}
int read_into_buffer(const int fd, struct buffer * const buffer)
{
assert(buffer);
assert(fd >= 0);
if (!buffer->size) {
buffer->buf = malloc(BUFFER_MINSIZE);
if (!buffer->buf)
return -1;
buffer->size = BUFFER_MINSIZE;
}
ssize_t r;
r = read(fd, buffer->buf + buffer->idx, buffer->size - buffer->idx);
if (r < 0)
return r;
buffer->idx += r;
if ((buffer->idx >= buffer->size) && (buffer->buf[buffer->idx - 1] != '\n')) {
if (enlarge_buffer(buffer, buffer->size * 2))
return -1;
}
return r;
}
int write_buffer_into_fd(const int fd, struct buffer * const buffer)
{
assert(buffer);
assert(fd >= 0);
ssize_t r;
size_t sb = 0;
if (!buffer->idx)
return 0;
do {
r = write(fd, buffer->buf + sb, buffer->idx - sb);
if (r < 1)
return r;
sb += r;
} while (sb < buffer->idx);
buffer->idx = 0;
return sb;
}
__attribute__((format(printf, 2, 0)))
int vbufprintf(struct buffer * const buffer, const char *fmt, va_list ap)
{
va_list _ap;
size_t size, len;
assert(buffer);
do {
size = buffer->size - buffer->idx;
va_copy(_ap, ap);
len = vsnprintf(buffer->buf + buffer->idx, size, fmt, _ap);
va_end(_ap);
if (len >= size && enlarge_buffer(buffer, buffer->size * 2))
return -1;
} while (len >= size);
buffer->idx += len;
return 0;
}
__attribute__((format(printf, 2, 3)))
int bufprintf(struct buffer * const buffer, char *format, ...)
{
va_list ap;
int r;
va_start(ap, format);
r = vbufprintf(buffer, format, ap);
va_end(ap);
return r;
}
int buffer_terminate_line(struct buffer * const buffer)
{
assert(buffer);
if (buffer->idx == 0 || buffer->buf[buffer->idx - 1] != '\n')
return -1;
buffer->buf[buffer->idx - 1] = '\0';
chomp(buffer->buf);
return 0;
}
void buffer_reset(struct buffer * const buffer)
{
buffer->idx = 0;
}
void buffer_init(struct buffer * const buffer)
{
memset(buffer, 0, sizeof(*buffer));
}
void buffer_free(struct buffer * const buffer)
{
free(buffer->buf);
memset(buffer, 0, sizeof(*buffer));
}