X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fbuffer.c;h=25f930dddaa4b6af97998b0f004805ed96616402;hp=e1327c8e04463e58b34ae232a4a17ed84bdb9435;hb=03c8a86d85fc313b6772a1cdfc07e635583f9ed0;hpb=230399f9632c37b66c1c117a17e8327eae6b3235 diff --git a/lib-lib/buffer.c b/lib-lib/buffer.c index e1327c8..25f930d 100644 --- a/lib-lib/buffer.c +++ b/lib-lib/buffer.c @@ -26,15 +26,23 @@ * please see the file GPL in the top level source directory. */ -#include -#include -#include +#include "lib-lib.h" -#include "mem.h" -#include "str.h" -#include "ascii.h" -#include "buffer.h" -#include "file.h" +#define BUFSIZ_INCREMENT 1024 + +void buffer_resize(buffer_t *buf, ssize_t newsize) +{ + if (newsize >= buf->size) { + /* rounds newsize to the 1024 multiple just after newsize+1 */ + newsize = (newsize + BUFSIZ_INCREMENT) & ~(BUFSIZ_INCREMENT - 1); + p_realloc(&buf->data, newsize); + } +} + + + + +/****** LEGACY BUFFERS *******/ /* * Creates and initializes a BUFFER*. If passed an existing BUFFER*, @@ -99,3 +107,33 @@ void mutt_buffer_add(BUFFER *buf, const char *s, ssize_t len) *buf->dptr = '\0'; } +ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list args) +{ + ssize_t len; + va_list ap; + + va_copy(ap, args); + buffer_ensure(buf, BUFSIZ); + + len = vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, args); + if (len < 0) + return len; + if (len >= buf->size - buf->len) { + buffer_ensure(buf, len); + vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, ap); + } + buf->len += len; + buf->data[buf->len] = '\0'; + + return len; +} + +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; +}