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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Copyright (C) 2010- The University of Notre Dame
* This software is distributed under the GNU General Public License.
* See the file COPYING for details.
*/
#include "buffer.h"
#include "debug.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#define inuse(b) ((size_t)(b->end - b->buf))
#define avail(b) (b->len - inuse(b))
#define checkerror(b,err,expr) \
do {\
if ((err) == (expr)) {\
if (b->abort_on_failure) {\
fatal("[%s:%d]: %s", __FILE__, __LINE__, strerror(errno));\
} else {\
return -1;\
}\
}\
} while (0)
void buffer_init(buffer_t * b)
{
bzero(b->initial, sizeof(b->initial));
b->buf = b->end = b->initial;
b->len = sizeof(b->initial);
b->ubuf.buf = NULL;
b->ubuf.len = 0;
b->end[0] = '\0'; /* initialize with empty string */
b->max = 0;
b->abort_on_failure = 0;
}
void buffer_ubuf(buffer_t * b, char *buf, size_t len)
{
assert(b->buf == b->initial && b->buf == b->end);
if (buf && len > sizeof(b->initial)) {
b->buf = b->end = b->ubuf.buf = buf;
b->len = b->ubuf.len = len;
b->end[0] = '\0'; /* initialize with empty string */
}
}
void buffer_max(buffer_t * b, size_t max)
{
b->max = max;
}
void buffer_abortonfailure(buffer_t * b, int abortonfailure)
{
b->abort_on_failure = abortonfailure;
}
void buffer_free(buffer_t * b)
{
if (!(b->buf == b->ubuf.buf || b->buf == b->initial)) {
free(b->buf);
}
}
int buffer_seek(buffer_t * b, size_t pos) {
assert(b);
if (pos >= inuse(b)) {
int rc = buffer_grow(b, pos - inuse(b) + 1);
if (rc < 0) return rc;
b->end = b->buf + pos;
b->end[0] = '\0';
} else {
buffer_rewind(b, pos);
}
return 0;
}
int buffer_grow(buffer_t * b, size_t n)
{
size_t used = inuse(b);
size_t newlen = sizeof(b->initial); /* current buf is always at least as big as b->initial */
/* simple solution to find next power of 2 */
while (newlen < used+n) newlen <<= 1;
/* too big? */
if (0 < b->max && b->max < newlen) {
if (used+n <= b->max) {
/* This handles the case where b->max is not a power of 2. */
newlen = b->max;
} else {
errno = ENOBUFS;
checkerror(b, 0, 0);
}
}
/* Keep using the initial buffer if possible */
if (newlen <= b->len) return 0;
if (b->buf == b->ubuf.buf || b->buf == b->initial) {
char *new = malloc(newlen);
checkerror(b, NULL, new);
memcpy(new, b->buf, used);
b->buf = new;
} else {
char *new = realloc(b->buf, newlen);
checkerror(b, NULL, new);
b->buf = new;
}
b->end = b->buf+used;
b->end[0] = '\0';
b->len = newlen;
assert(avail(b) >= n);
return 0;
}
int buffer_putvfstring(buffer_t * b, const char *format, va_list va)
{
int rc;
va_list va2;
size_t used = inuse(b);
va_copy(va2, va);
rc = vsnprintf(b->end, avail(b), format, va2);
va_end(va2);
checkerror(b, -1, rc);
/* N.B. vsnprintf rc does not include NUL byte */
if (avail(b) <= (size_t)rc) {
rc = buffer_grow(b, rc+1);
if (rc == -1) return -1;
} else {
b->end += rc;
assert(rc+used == inuse(b));
assert(inuse(b) < b->len);
return rc;
}
va_copy(va2, va);
rc = vsnprintf(b->end, avail(b), format, va2);
assert(rc >= 0);
b->end += rc;
assert(rc+used == inuse(b));
assert(inuse(b) < b->len);
va_end(va2);
return rc;
}
int buffer_putfstring(buffer_t * b, const char *format, ...)
{
va_list va;
va_start(va, format);
int rc = buffer_vprintf(b, format, va);
va_end(va);
return rc;
}
int buffer_putlstring(buffer_t * b, const char *str, size_t len)
{
if (avail(b) <= len && buffer_grow(b, len+1) == -1) {
return -1;
}
memcpy(b->end, str, len);
b->end += len;
b->end[0] = '\0';
return (int)len;
}
const char *buffer_tolstring(buffer_t * b, size_t * size)
{
if(size != NULL)
*size = inuse(b);
return b->buf;
}
size_t buffer_pos(buffer_t * b)
{
return inuse(b);
}
void buffer_rewind(buffer_t * b, size_t n)
{
assert(inuse(b) >= n);
b->end = b->buf+n;
b->end[0] = '\0';
}
int buffer_dupl(buffer_t *b, char **buf, size_t *l)
{
size_t n = inuse(b);
*buf = malloc(n+1); /* include NUL */
checkerror(b, NULL, *buf);
if (l)
*l = n;
memcpy(*buf, b->buf, n+1); /* include NUL */
return 0;
}
/* vim: set noexpandtab tabstop=4: */
|