From a4704dfd3c31a7dee955a2be9dd4b0fc79848884 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 19 Sep 2013 16:34:02 +0930 Subject: [PATCH] Add strbuf_va_vprintf() convenience macro --- strbuf.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/strbuf.h b/strbuf.h index 234285ac..4152119a 100644 --- a/strbuf.h +++ b/strbuf.h @@ -132,7 +132,7 @@ typedef const struct strbuf *const_strbuf; /** Convenience macro for filling a strbuf from the calling function's - * printf(3)-like variadic arguments. See alloca(3) for more information. + * printf(3)-like variadic arguments. * * #include * @@ -147,7 +147,27 @@ typedef const struct strbuf *const_strbuf; #define strbuf_va_printf(sb,fmt) do { \ va_list __strbuf_ap; \ va_start(__strbuf_ap, fmt); \ - strbuf_vsprintf(sb, fmt, __strbuf_ap); \ + strbuf_vsprintf(sb, (fmt), __strbuf_ap); \ + va_end(__strbuf_ap); \ + } while (0) + +/** Convenience macro for filling a strbuf from the calling function's va_list + * variadic argument pointer. + * + * #include + * + * void funcf(const char *format, va_list ap) { + * strbuf b = strbuf_alloca(1024); + * strbuf_va_vprintf(b, format, ap); + * ... + * } + * + * @author Andrew Bettison + */ +#define strbuf_va_vprintf(sb,fmt,ap) do { \ + va_list __strbuf_ap; \ + va_copy(__strbuf_ap, (ap)); \ + strbuf_vsprintf(sb, (fmt), __strbuf_ap); \ va_end(__strbuf_ap); \ } while (0) @@ -314,8 +334,7 @@ strbuf strbuf_putc(strbuf sb, char ch); * * @author Andrew Bettison */ -int strbuf_sprintf(strbuf sb, const char *fmt, ...) -__attribute__ (( format(printf,2,3) )); +int strbuf_sprintf(strbuf sb, const char *fmt, ...) __attribute__((format(printf, 2, 3))); int strbuf_vsprintf(strbuf sb, const char *fmt, va_list ap);