[PATCH 2/3] buf: add virBufferVasprintf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.
---
 bootstrap.conf           |    1 +
 src/libvirt_private.syms |    3 ++-
 src/util/buf.c           |   39 ++++++++++++++++++++++++++-------------
 src/util/buf.h           |    6 +++++-
 4 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index fde00da..09e8218 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -70,6 +70,7 @@ sigaction
 sigpipe
 snprintf
 socket
+stdarg
 stpcpy
 strchrnul
 strndup
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index fc8edb4..00f7e08 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -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
diff --git a/src/util/buf.c b/src/util/buf.c
index 7814e8e..750e277 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -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;
 }

 /**
diff --git a/src/util/buf.h b/src/util/buf.h
index 00f2043..06d01ba 100644
--- a/src/util/buf.h
+++ b/src/util/buf.h
@@ -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);
-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]