]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: buffer: Add API to set indentation level to a given value
authorPeter Krempa <pkrempa@redhat.com>
Thu, 9 Mar 2017 16:02:19 +0000 (17:02 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 27 Mar 2017 07:29:57 +0000 (09:29 +0200)
It will be useful to set indentation level to 0 after formatting a
nested structure rather than having to track the depth.

src/libvirt_private.syms
src/util/virbuffer.c
src/util/virbuffer.h
tests/virbuftest.c

index e99bdb0289debab7d3e6777ce4d58fb95aaf5ee9..98628a4238fe536358804ed4d345232e70571e12 100644 (file)
@@ -1315,6 +1315,7 @@ virBufferEscapeShell;
 virBufferEscapeString;
 virBufferFreeAndReset;
 virBufferGetIndent;
+virBufferSetIndent;
 virBufferStrcat;
 virBufferTrim;
 virBufferURIEncodeString;
index 41d541b32188c871c9f91970b4837c1e56e29d19..80c8e289d45785855c3c6d6f39759a166a588e78 100644 (file)
@@ -88,6 +88,25 @@ virBufferAdjustIndent(virBufferPtr buf, int indent)
     buf->indent += indent;
 }
 
+
+/**
+ * virBufferSetIndent:
+ * @buf: the buffer
+ * @indent: new indentation size.
+ *
+ * Set the auto-indent value to @indent. See virBufferAdjustIndent on how auto
+ * indentation is applied.
+ */
+void
+virBufferSetIndent(virBufferPtr buf, int indent)
+{
+    if (!buf || buf->error)
+        return;
+
+    buf->indent = indent;
+}
+
+
 /**
  * virBufferGetIndent:
  * @buf: the buffer
index 94f14b5b16b7f94a31b34f782d78739958d7461d..d1b64ca3a3decb565c1af33eb66fed62fd22227e 100644 (file)
@@ -95,6 +95,8 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str);
     virBufferAdd(buf_, "" literal_string_ "", sizeof(literal_string_) - 1)
 
 void virBufferAdjustIndent(virBufferPtr buf, int indent);
+void virBufferSetIndent(virBufferPtr, int indent);
+
 int virBufferGetIndent(const virBuffer *buf, bool dynamic);
 
 void virBufferTrim(virBufferPtr buf, const char *trim, int len);
index 34160e6b28b3fbd36706c156917b98474d17d556..8ec6ce51ed3216f97ae7841bca02db4a5e70c64a 100644 (file)
@@ -404,6 +404,34 @@ testBufEscapeN(const void *opaque)
 }
 
 
+static int
+testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *actual;
+    int ret = -1;
+
+    virBufferSetIndent(&buf, 11);
+    virBufferAddLit(&buf, "test\n");
+    virBufferSetIndent(&buf, 2);
+    virBufferAddLit(&buf, "test2\n");
+
+    if (!(actual = virBufferContentAndReset(&buf)))
+        goto cleanup;
+
+    if (STRNEQ(actual, "           test\n  test2\n")) {
+        VIR_TEST_DEBUG("testBufSetIndent: expected indent not set\n");
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(actual);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -422,6 +450,7 @@ mymain(void)
     DO_TEST("Auto-indentation", testBufAutoIndent, 0);
     DO_TEST("Trim", testBufTrim, 0);
     DO_TEST("AddBuffer", testBufAddBuffer, 0);
+    DO_TEST("set indent", testBufSetIndent, 0);
 
 #define DO_TEST_ADD_STR(DATA, EXPECT)                                  \
     do {                                                               \