00001
00002
00003
00004
00005
00006
00007 #ifndef BUFFER_H
00008 #define BUFFER_H
00009
00010 #include <stdlib.h>
00011 #include <stdarg.h>
00012 #include <string.h>
00013
00020 #if !(defined(__GNUC__) || defined(__clang__)) && !defined(__attribute__)
00021 #define __attribute__(x)
00022 #endif
00023
00024 #define BUFFER_INISIZ (1<<12)
00025
00026 typedef struct buffer {
00027 char *buf;
00028 char *end;
00029 size_t len;
00030 size_t max;
00031 int abort_on_failure;
00032
00033
00034 struct {
00035 char *buf;
00036 size_t len;
00037 } ubuf;
00038 char initial[BUFFER_INISIZ];
00039 } buffer_t;
00040
00050 void buffer_init(buffer_t * b);
00051
00062 void buffer_ubuf(buffer_t * b, char *buf, size_t len);
00063
00069 void buffer_max(buffer_t * b, size_t max);
00070
00076 void buffer_abortonfailure(buffer_t * b, int abortonfailure);
00077
00081 void buffer_free(buffer_t * b);
00082
00089 int buffer_dupl(buffer_t *b, char **buf, size_t *l);
00090
00096 #define buffer_dup(b,buf) (buffer_dupl(b,buf,NULL))
00097
00107 int buffer_putvfstring(buffer_t * b, const char *format, va_list ap);
00108 #define buffer_vprintf buffer_putvfstring
00109
00117 int buffer_putfstring(buffer_t * b, const char *format, ...)
00118 __attribute__ (( format(printf,2,3) )) ;
00119 #define buffer_printf buffer_putfstring
00120
00127 int buffer_putlstring(buffer_t * b, const char *str, size_t len);
00128
00134 #define buffer_putstring(b,s) (buffer_putlstring(b,s,strlen(s)))
00135
00141 #define buffer_putliteral(b,l) (buffer_putlstring(b,l "",sizeof(l)-1))
00142
00150 const char *buffer_tolstring(buffer_t * b, size_t * size);
00151
00158 #define buffer_tostring(b) buffer_tolstring(b, NULL)
00159
00165 void buffer_rewind(buffer_t * b, size_t n);
00166
00172 size_t buffer_pos(buffer_t * b);
00173
00184 #define BUFFER_STACK(name,size) \
00185 buffer_t name[1];\
00186 char name##_ubuf[size > BUFFER_INISIZ ? size : 1]; \
00187 buffer_init(name);\
00188 buffer_max(name, size);\
00189 buffer_ubuf(name, name##_ubuf, size);
00190
00198 #define BUFFER_STACK_ABORT(name,size) \
00199 BUFFER_STACK(name,size);\
00200 buffer_abortonfailure(name, 1);
00201
00209 #define BUFFER_STACK_PRINT(name,size,...) \
00210 BUFFER_STACK(name,size);\
00211 buffer_putfstring(name, __VA_ARGS__);
00212
00213 #endif