cctools
buffer.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2005- The University of Notre Dame
3 This software is distributed under the GNU General Public License.
4 See 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 
26 typedef 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 
50 void buffer_init(buffer_t * b);
51 
62 void buffer_ubuf(buffer_t * b, char *buf, size_t len);
63 
69 void buffer_max(buffer_t * b, size_t max);
70 
76 void buffer_abortonfailure(buffer_t * b, int abortonfailure);
77 
81 void buffer_free(buffer_t * b);
82 
89 int buffer_dupl(buffer_t *b, char **buf, size_t *l);
90 
96 #define buffer_dup(b,buf) (buffer_dupl(b,buf,NULL))
97 
107 int buffer_putvfstring(buffer_t * b, const char *format, va_list ap);
108 #define buffer_vprintf buffer_putvfstring
109 
117 int buffer_putfstring(buffer_t * b, const char *format, ...)
118 __attribute__ (( format(printf,2,3) )) ;
119 #define buffer_printf buffer_putfstring
120 
127 int 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 
150 const char *buffer_tolstring(buffer_t * b, size_t * size);
151 
158 #define buffer_tostring(b) buffer_tolstring(b, NULL)
159 
165 void buffer_rewind(buffer_t * b, size_t n);
166 
172 size_t buffer_pos(buffer_t * b);
173 
180 int buffer_grow(buffer_t * b, size_t n);
181 
191 int 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 */
int buffer_putvfstring(buffer_t *b, const char *format, va_list ap)
Print the formatted output to the buffer.
int buffer_putfstring(buffer_t *b, const char *format,...) __attribute__((format(printf
Appends the formatted output to the buffer.
int buffer_seek(buffer_t *b, size_t pos)
Seek to a position.
void buffer_rewind(buffer_t *b, size_t n)
Rewinds the buffer to position n.
void buffer_max(buffer_t *b, size_t max)
Set the maximum size of the buffer.
void buffer_init(buffer_t *b)
Initialize a buffer.
int buffer_putlstring(buffer_t *b, const char *str, size_t len)
Appends the string to the end of the buffer.
void buffer_free(buffer_t *b)
Free any resources and memory in use by a buffer.
int buffer_grow(buffer_t *b, size_t n)
Make room for at least n additional chars.
void buffer_abortonfailure(buffer_t *b, int abortonfailure)
Set the buffer to call fatal(...) on error instead of returning an error code.
Definition: buffer.h:26
const char * buffer_tolstring(buffer_t *b, size_t *size)
Returns the buffer as a string.
size_t buffer_pos(buffer_t *b)
Get the current position in the buffer.
void buffer_ubuf(buffer_t *b, char *buf, size_t len)
Use the provided buffer as a starting buffer.
int buffer_dupl(buffer_t *b, char **buf, size_t *l)
Make a heap allocated copy of the buffer.