fix buffer module.
[apps/pfixtools.git] / buffer.h
index e3075ba..24bc590 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -1,5 +1,5 @@
 /******************************************************************************/
-/*          postlicyd: a postfix policy daemon with a lot of features         */
+/*          pfixtools: a collection of postfix related tools                  */
 /*          ~~~~~~~~~                                                         */
 /*  ________________________________________________________________________  */
 /*                                                                            */
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
-#ifndef MUTT_LIB_LIB_BUFFER_H
-#define MUTT_LIB_LIB_BUFFER_H
+#ifndef PFIXTOOLS_BUFFER_H
+#define PFIXTOOLS_BUFFER_H
 
+#include <stdarg.h>
 #include "mem.h"
 #include "str.h"
 
@@ -45,6 +46,8 @@ typedef struct buffer_t {
     ssize_t size;
 } buffer_t;
 
+#define BUFFER_INIT {NULL, 0, 0}
+
 DO_INIT(buffer_t, buffer);
 static inline void buffer_wipe(buffer_t *buf) {
     p_delete(&buf->data);
@@ -52,6 +55,10 @@ static inline void buffer_wipe(buffer_t *buf) {
 DO_NEW(buffer_t, buffer);
 DO_DELETE(buffer_t, buffer);
 
+static inline void buffer_reset(buffer_t *buf) {
+    buf->data[buf->len = 0] = '\0';
+}
+
 static inline char *buffer_unwrap(buffer_t **buf) {
     char *res = (*buf)->data;
     (*buf)->data = NULL;
@@ -96,4 +103,23 @@ static inline void buffer_addch(buffer_t *buf, int c) {
     buffer_extendch(buf, 1, c);
 }
 
-#endif /* MUTT_LIB_LIB_BUFFER_H */
+__attribute__((format(printf,2,0)))
+ssize_t buffer_addvf(buffer_t *, const char *fmt, va_list);
+
+static inline __attribute__((format(printf,2,3)))
+ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...)
+{
+    ssize_t res;
+    va_list args;
+    va_start(args, fmt);
+    res = buffer_addvf(buf, fmt, args);
+    va_end(args);
+    return res;
+}
+
+void buffer_consume(buffer_t *buf, ssize_t len);
+
+ssize_t buffer_read(buffer_t *buf, int fd, ssize_t count);
+ssize_t buffer_write(buffer_t *buf, int fd);
+
+#endif /* PFIXTOOLS_BUFFER_H */