From: Pierre Habouzit Date: Sun, 27 May 2007 17:07:51 +0000 (+0200) Subject: add printf-like primitive to the buffers. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=e1ffef197d9dc7fdc79f9149c123f4cd0809ab63;p=apps%2Fmadmutt.git add printf-like primitive to the buffers. Signed-off-by: Pierre Habouzit --- diff --git a/lib-lib/buffer.c b/lib-lib/buffer.c index b442721..71f31b4 100644 --- a/lib-lib/buffer.c +++ b/lib-lib/buffer.c @@ -107,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) { + len = 0; + } + 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; +} diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index dd9d936..6d62f2a 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -93,6 +93,12 @@ static inline void buffer_reset(buffer_t *buf) { } } +ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list) + __attribute__((format(printf, 2, 0))); + +ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); + /****** LEGACY BUFFERS *******/