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

[libvirt] [PATCH 2/3] Routine to truncate virBuffer



Routine to truncate virBuffer

From: Bharata B Rao <bharata linux vnet ibm com>

Add a helper to truncate virBuffer.

/**
 * virBufferTruncate:
 * @buf:  the buffer
 * @len:  number of bytes by which the buffer is truncated
 *
 * Truncate the buffer by @len bytes.
 *
 * Returns zero on success or -1 on error
 */
int virBufferTruncate(virBufferPtr buf, unsigned int len)

This doesn't reduce the buffer size, but instead reduces
the in-use buffer.

Signed-off-by: Bharata B Rao <bharata linux vnet ibm com>
---

 src/libvirt_private.syms |    1 +
 src/util/buf.c           |   25 +++++++++++++++++++++++++
 src/util/buf.h           |    1 +
 tests/virbuftest.c       |    3 ++-
 4 files changed, 29 insertions(+), 1 deletions(-)


diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6a1562e..3a2ae86 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -33,6 +33,7 @@ virBufferEscapeString;
 virBufferFreeAndReset;
 virBufferGetIndent;
 virBufferStrcat;
+virBufferTruncate;
 virBufferURIEncodeString;
 virBufferUse;
 virBufferVasprintf;
diff --git a/src/util/buf.c b/src/util/buf.c
index 5043128..4bba350 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -123,6 +123,31 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
 }
 
 /**
+ * virBufferTruncate:
+ * @buf:  the buffer
+ * @len:  number of bytes by which the buffer is truncated
+ *
+ * Truncate the buffer by @len bytes.
+ *
+ * Returns zero on success or -1 on error
+ */
+int
+virBufferTruncate(virBufferPtr buf, unsigned int len)
+{
+    if (buf->error)
+        return -1;
+
+    if ((signed)(buf->use - len) < 0) {
+        virBufferSetError(buf, -1);
+        return -1;
+    }
+
+    buf->use -= len;
+    buf->content[buf->use] = '\0';
+    return 0;
+}
+
+/**
  * virBufferAdd:
  * @buf: the buffer to append to
  * @str: the string
diff --git a/src/util/buf.h b/src/util/buf.h
index 1a8ebfb..76f48e0 100644
--- a/src/util/buf.h
+++ b/src/util/buf.h
@@ -63,5 +63,6 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str);
 
 void virBufferAdjustIndent(virBufferPtr buf, int indent);
 int virBufferGetIndent(const virBufferPtr buf, bool dynamic);
+int virBufferTruncate(virBufferPtr buf, unsigned int len);
 
 #endif /* __VIR_BUFFER_H__ */
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
index 51a62fe..2230cb0 100644
--- a/tests/virbuftest.c
+++ b/tests/virbuftest.c
@@ -123,7 +123,8 @@ static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED)
     virBufferAddChar(buf, '\n');
     virBufferEscapeShell(buf, " 11");
     virBufferAddChar(buf, '\n');
-
+    virBufferAsprintf(buf, "%d", 12);
+    virBufferTruncate(buf, 4);
     result = virBufferContentAndReset(buf);
     if (!result || STRNEQ(result, expected)) {
         virtTestDifference(stderr, expected, result);


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