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 
19 #if !(defined(__GNUC__) || defined(__clang__)) && !defined(__attribute__)
20 #define __attribute__(x) /* do nothing */
21 #endif
22 
23 typedef struct buffer {
24  char *buf; /* buf points to the start of the buffer, which may be a buffer on the stack or heap */
25  char *end; /* the current end of the buffer */
26  size_t len; /* current size of buffer */
27  size_t max; /* maximum size of buffer */
28  int abort_on_failure; /* call debug.c fatal(...) on error instead of returning */
29 
30  char initial[1<<12]; /* a reasonably sized buffer to use initially so we avoid (numerous) heap allocations */
31 
32  /* a user provided buffer which replaces initial if larger */
33  struct {
34  char *buf;
35  size_t len;
36  } ubuf;
37 } buffer_t;
38 
48 void buffer_init(buffer_t * b);
49 
60 void buffer_ubuf(buffer_t * b, char *buf, size_t len);
61 
67 void buffer_max(buffer_t * b, size_t max);
68 
74 void buffer_abortonfailure(buffer_t * b, int abortonfailure);
75 
79 void buffer_free(buffer_t * b);
80 
90 int buffer_putvfstring(buffer_t * b, const char *format, va_list ap);
91 #define buffer_vprintf buffer_putvfstring
92 
100 int buffer_putfstring(buffer_t * b, const char *format, ...)
101 __attribute__ (( format(printf,2,3) )) ;
102 #define buffer_printf buffer_putfstring
103 
110 int buffer_putlstring(buffer_t * b, const char *str, size_t len);
111 
117 #define buffer_putstring(b,s) (buffer_putlstring(b,s,strlen(s)))
118 
124 #define buffer_putliteral(b,l) (buffer_putlstring(b,l "",sizeof(l)-1))
125 
133 const char *buffer_tostring(buffer_t * b, size_t * size);
134 
140 void buffer_rewind(buffer_t * b, size_t n);
141 
147 size_t buffer_pos(buffer_t * b);
148 
149 #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.
void buffer_rewind(buffer_t *b, size_t n)
Rewinds the buffer to position n.
const char * buffer_tostring(buffer_t *b, size_t *size)
Returns the buffer as a string.
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.
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:23
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.