cctools
buffer.h
Go to the documentation of this file.
1/*
2Copyright (C) 2022 The University of Notre Dame
3This software is distributed under the GNU General Public License.
4See the file COPYING for details.
5*/
6
7#ifndef BUFFER_H
8#define BUFFER_H
9
10#include <stdlib.h>
11#include <stdarg.h>
12#include <string.h>
13
20#if !(defined(__GNUC__) || defined(__clang__)) && !defined(__attribute__)
21#define __attribute__(x) /* do nothing */
22#endif
23
24#define BUFFER_INISIZ (1<<12)
25
26typedef struct buffer {
27 char *buf; /* buf points to the start of the buffer, which may be a buffer on the stack or heap */
28 char *end; /* the current end of the buffer */
29 size_t len; /* current size of buffer */
30 size_t max; /* maximum size of buffer */
31 int abort_on_failure; /* call debug.c fatal(...) on error instead of returning */
32
33 /* a user provided buffer which replaces initial if larger */
34 struct {
35 char *buf;
36 size_t len;
37 } ubuf;
38 char initial[BUFFER_INISIZ]; /* a reasonably sized buffer to use initially so we avoid (numerous) heap allocations */
39} buffer_t;
40
51
62void buffer_ubuf(buffer_t * b, char *buf, size_t len);
63
69void buffer_max(buffer_t * b, size_t max);
70
76void buffer_abortonfailure(buffer_t * b, int abortonfailure);
77
82
89int buffer_dupl(buffer_t *b, char **buf, size_t *l);
90
96#define buffer_dup(b,buf) (buffer_dupl(b,buf,NULL))
97
107int buffer_putvfstring(buffer_t * b, const char *format, va_list ap);
108#define buffer_vprintf buffer_putvfstring
109
117int buffer_putfstring(buffer_t * b, const char *format, ...)
118__attribute__ (( format(printf,2,3) )) ;
119#define buffer_printf buffer_putfstring
120
127int buffer_putlstring(buffer_t * b, const char *str, size_t len);
128
134#define buffer_putstring(b,s) (buffer_putlstring(b,s,strlen(s)))
135
141#define buffer_putliteral(b,l) (buffer_putlstring(b,l "",sizeof(l)-1))
142
150const char *buffer_tolstring(buffer_t * b, size_t * size);
151
158#define buffer_tostring(b) buffer_tolstring(b, NULL)
159
165void buffer_rewind(buffer_t * b, size_t n);
166
173
180int buffer_grow(buffer_t * b, size_t n);
181
191int buffer_seek(buffer_t * b, size_t pos);
192
203#define BUFFER_STACK(name,size) \
204 buffer_t name[1];\
205 char name##_ubuf[size > BUFFER_INISIZ ? size : 1]; /* Unfortunately, we can't conditionally allocate this ubuf array. Use char[1] if less than BUFFER_INISIZ */\
206 buffer_init(name);\
207 buffer_max(name, size);\
208 buffer_ubuf(name, name##_ubuf, size); /* if this is less than BUFFER_INISIZ, then B->initial is still used. */
209
217#define BUFFER_STACK_ABORT(name,size) \
218 BUFFER_STACK(name,size);\
219 buffer_abortonfailure(name, 1);
220
228#define BUFFER_STACK_PRINT(name,size,...) \
229 BUFFER_STACK(name,size);\
230 buffer_putfstring(name, __VA_ARGS__);
231
232#endif /* BUFFER_H */
void buffer_abortonfailure(buffer_t *b, int abortonfailure)
Set the buffer to call fatal(...) on error instead of returning an error code.
const char * buffer_tolstring(buffer_t *b, size_t *size)
Returns the buffer as a string.
void buffer_rewind(buffer_t *b, size_t n)
Rewinds the buffer to position n.
int buffer_grow(buffer_t *b, size_t n)
Make room for at least n additional chars.
size_t buffer_pos(buffer_t *b)
Get the current position in the buffer.
int buffer_putfstring(buffer_t *b, const char *format,...) __attribute__((format(printf
Appends the formatted output to the buffer.
int buffer_putlstring(buffer_t *b, const char *str, size_t len)
Appends the string to the end of the buffer.
int buffer_seek(buffer_t *b, size_t pos)
Seek to a position.
void buffer_max(buffer_t *b, size_t max)
Set the maximum size of the buffer.
void buffer_ubuf(buffer_t *b, char *buf, size_t len)
Use the provided buffer as a starting buffer.
void buffer_init(buffer_t *b)
Initialize a buffer.
int buffer_dupl(buffer_t *b, char **buf, size_t *l)
Make a heap allocated copy of the buffer.
int buffer_putvfstring(buffer_t *b, const char *format, va_list ap)
Print the formatted output to the buffer.
void buffer_free(buffer_t *b)
Free any resources and memory in use by a buffer.
Definition buffer.h:26