buf: add virBufferVasprintf

Match the fact that we have virAsprintf and virVasprintf.

* src/util/buf.h (virBufferVasprintf): New prototype.
* src/util/buf.c (virBufferAsprintf): Move guts...
(virBufferVasprintf): ...to new function.
* src/libvirt_private.syms (buf.h): Export it.
* bootstrap.conf (gnulib_modules): Add stdarg, for va_copy.
This commit is contained in:
Eric Blake 2011-04-30 10:44:42 -06:00
parent 68ea80cfdd
commit f09acccfd7
4 changed files with 34 additions and 15 deletions

View File

@ -70,6 +70,7 @@ sigaction
sigpipe
snprintf
socket
stdarg
stpcpy
strchrnul
strndup

View File

@ -22,6 +22,7 @@ virBitmapString;
# buf.h
virBufferAdd;
virBufferAddChar;
virBufferAsprintf;
virBufferContentAndReset;
virBufferError;
virBufferEscapeSexpr;
@ -30,7 +31,7 @@ virBufferFreeAndReset;
virBufferStrcat;
virBufferURIEncodeString;
virBufferUse;
virBufferAsprintf;
virBufferVasprintf;
# caps.h

View File

@ -1,7 +1,7 @@
/*
* buf.c: buffers for libvirt
*
* Copyright (C) 2005-2008, 2010 Red Hat, Inc.
* Copyright (C) 2005-2008, 2010-2011 Red Hat, Inc.
*
* See COPYING.LIB for the License of this software
*
@ -223,8 +223,25 @@ virBufferUse(const virBufferPtr buf)
void
virBufferAsprintf(const virBufferPtr buf, const char *format, ...)
{
int size, count, grow_size;
va_list argptr;
va_start(argptr, format);
virBufferVasprintf(buf, format, argptr);
va_end(argptr);
}
/**
* virBufferVasprintf:
* @buf: the buffer to dump
* @format: the format
* @argptr: the variable list of arguments
*
* Do a formatted print to an XML buffer.
*/
void
virBufferVasprintf(const virBufferPtr buf, const char *format, va_list argptr)
{
int size, count, grow_size;
va_list copy;
if ((format == NULL) || (buf == NULL))
return;
@ -236,38 +253,34 @@ virBufferAsprintf(const virBufferPtr buf, const char *format, ...)
virBufferGrow(buf, 100) < 0)
return;
va_start(argptr, format);
va_copy(copy, argptr);
size = buf->size - buf->use;
if ((count = vsnprintf(&buf->content[buf->use],
size, format, argptr)) < 0) {
size, format, copy)) < 0) {
virBufferSetError(buf);
goto err;
va_end(copy);
return;
}
va_end(copy);
/* Grow buffer if necessary and retry */
if (count >= size) {
buf->content[buf->use] = 0;
va_end(argptr);
va_start(argptr, format);
grow_size = (count + 1 > 1000) ? count + 1 : 1000;
if (virBufferGrow(buf, grow_size) < 0) {
goto err;
return;
}
size = buf->size - buf->use;
if ((count = vsnprintf(&buf->content[buf->use],
size, format, argptr)) < 0) {
virBufferSetError(buf);
goto err;
return;
}
}
buf->use += count;
err:
va_end(argptr);
return;
}
/**

View File

@ -1,7 +1,7 @@
/*
* buf.h: buffers for libvirt
*
* Copyright (C) 2005-2008 Red Hat, Inc.
* Copyright (C) 2005-2008, 2011 Red Hat, Inc.
*
* See COPYING.LIB for the License of this software
*
@ -13,6 +13,8 @@
# include "internal.h"
# include <stdarg.h>
/**
* virBuffer:
*
@ -42,6 +44,8 @@ void virBufferAdd(const virBufferPtr buf, const char *str, int len);
void virBufferAddChar(const virBufferPtr buf, char c);
void virBufferAsprintf(const virBufferPtr buf, const char *format, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
void virBufferVasprintf(const virBufferPtr buf, const char *format, va_list ap)
ATTRIBUTE_FMT_PRINTF(2, 0);
void virBufferStrcat(const virBufferPtr buf, ...)
ATTRIBUTE_SENTINEL;
void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str);