buffer.h
Go to the documentation of this file.
/*
Copyright (C) 2022 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#ifndef BUFFER_H
#define BUFFER_H
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#if !(defined(__GNUC__) || defined(__clang__)) && !defined(__attribute__)
#define __attribute__(x) /* do nothing */
#endif
#define BUFFER_INISIZ (1<<12)
typedef struct buffer {
char *buf; /* buf points to the start of the buffer, which may be a buffer on the stack or heap */
char *end; /* the current end of the buffer */
size_t len; /* current size of buffer */
size_t max; /* maximum size of buffer */
int abort_on_failure; /* call debug.c fatal(...) on error instead of returning */
/* a user provided buffer which replaces initial if larger */
struct {
char *buf;
size_t len;
} ubuf;
char initial[BUFFER_INISIZ]; /* a reasonably sized buffer to use initially so we avoid (numerous) heap allocations */
} buffer_t;
void buffer_init(buffer_t * b);
void buffer_ubuf(buffer_t * b, char *buf, size_t len);
void buffer_max(buffer_t * b, size_t max);
void buffer_abortonfailure(buffer_t * b, int abortonfailure);
void buffer_free(buffer_t * b);
int buffer_dupl(buffer_t *b, char **buf, size_t *l);
#define buffer_dup(b,buf) (buffer_dupl(b,buf,NULL))
int buffer_putvfstring(buffer_t * b, const char *format, va_list ap);
#define buffer_vprintf buffer_putvfstring
int buffer_putfstring(buffer_t * b, const char *format, ...)
__attribute__ (( format(printf,2,3) )) ;
#define buffer_printf buffer_putfstring
int buffer_putlstring(buffer_t * b, const char *str, size_t len);
#define buffer_putstring(b,s) (buffer_putlstring(b,s,strlen(s)))
#define buffer_putliteral(b,l) (buffer_putlstring(b,l "",sizeof(l)-1))
const char *buffer_tolstring(buffer_t * b, size_t * size);
#define buffer_tostring(b) buffer_tolstring(b, NULL)
void buffer_rewind(buffer_t * b, size_t n);
size_t buffer_pos(buffer_t * b);
int buffer_grow(buffer_t * b, size_t n);
int buffer_seek(buffer_t * b, size_t pos);
#define BUFFER_STACK(name,size) \
buffer_t name[1];\
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 */\
buffer_init(name);\
buffer_max(name, size);\
buffer_ubuf(name, name##_ubuf, size); /* if this is less than BUFFER_INISIZ, then B->initial is still used. */
#define BUFFER_STACK_ABORT(name,size) \
BUFFER_STACK(name,size);\
buffer_abortonfailure(name, 1);
#define BUFFER_STACK_PRINT(name,size,...) \
BUFFER_STACK(name,size);\
buffer_putfstring(name, __VA_ARGS__);
#endif /* BUFFER_H */
buffer_abortonfailure
void buffer_abortonfailure(buffer_t *b, int abortonfailure)
Set the buffer to call fatal(...) on error instead of returning an error code.
buffer_tolstring
const char * buffer_tolstring(buffer_t *b, size_t *size)
Returns the buffer as a string.
buffer_rewind
void buffer_rewind(buffer_t *b, size_t n)
Rewinds the buffer to position n.
buffer_grow
int buffer_grow(buffer_t *b, size_t n)
Make room for at least n additional chars.
buffer_pos
size_t buffer_pos(buffer_t *b)
Get the current position in the buffer.
buffer_putfstring
int buffer_putfstring(buffer_t *b, const char *format,...) __attribute__((format(printf
Appends the formatted output to the buffer.
buffer_putlstring
int buffer_putlstring(buffer_t *b, const char *str, size_t len)
Appends the string to the end of the buffer.
buffer_seek
int buffer_seek(buffer_t *b, size_t pos)
Seek to a position.
buffer_max
void buffer_max(buffer_t *b, size_t max)
Set the maximum size of the buffer.
buffer_ubuf
void buffer_ubuf(buffer_t *b, char *buf, size_t len)
Use the provided buffer as a starting buffer.
buffer_init
void buffer_init(buffer_t *b)
Initialize a buffer.
buffer_dupl
int buffer_dupl(buffer_t *b, char **buf, size_t *l)
Make a heap allocated copy of the buffer.
buffer_putvfstring
int buffer_putvfstring(buffer_t *b, const char *format, va_list ap)
Print the formatted output to the buffer.
buffer_free
void buffer_free(buffer_t *b)
Free any resources and memory in use by a buffer.
buffer
Definition buffer.h:26