[libvirt] [PATCH 2/4] util: buffer: Add support for adding text blocks with indentation

Martin Kletzander mkletzan at redhat.com
Wed Mar 25 09:13:39 UTC 2015


On Tue, Mar 24, 2015 at 03:03:21PM +0100, Peter Krempa wrote:
>The current auto-indentation buffer code applies indentation only on
>complete strings. To allow adding a string containing newlines and

s/strings/lines/ ? :)

>having it properly indented this patch adds virBufferAddStr.
>---
> src/libvirt_private.syms |  1 +
> src/util/virbuffer.c     | 38 ++++++++++++++++++++++++++++++++++++
> src/util/virbuffer.h     |  1 +
> tests/virbuftest.c       | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 90 insertions(+)
>
>diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
>index 33222f0..0beb44f 100644
>--- a/src/libvirt_private.syms
>+++ b/src/libvirt_private.syms
>@@ -1099,6 +1099,7 @@ virBitmapToData;
> virBufferAdd;
> virBufferAddBuffer;
> virBufferAddChar;
>+virBufferAddStr;
> virBufferAdjustIndent;
> virBufferAsprintf;
> virBufferCheckErrorInternal;
>diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
>index 0089d1b..50d953e 100644
>--- a/src/util/virbuffer.c
>+++ b/src/util/virbuffer.c
>@@ -756,3 +756,41 @@ virBufferTrim(virBufferPtr buf, const char *str, int len)
>     buf->use -= len < 0 ? len2 : len;
>     buf->content[buf->use] = '\0';
> }
>+
>+
>+/**
>+ * virBufferAddStr:
>+ * @buf: the buffer to append to
>+ * @str: string to append
>+ *
>+ * Appends @str to @buffer. Applies autoindentation on the separate lines of
>+ * @str.
>+ */
>+void
>+virBufferAddStr(virBufferPtr buf,
>+                const char *str)
>+{
>+    size_t len = 0;
>+    const char *start = str;
>+
>+    if (!buf || !str || buf->error)
>+        return;
>+
>+    while (*str) {
>+        len++;
>+
>+        if (*str == '\n') {
>+            virBufferAdd(buf, start, len);
>+            str++;
>+            len = 0;
>+            start = str;
>+
>+            continue;
>+        }
>+
>+        str++;
>+    }
>+

strchr() might've been more readable here, but not worth changing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150325/c670498a/attachment-0001.sig>


More information about the libvir-list mailing list