From 91e7862c153ae7ff1d2a4f0db900c891f6442303 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 9 Mar 2017 17:02:19 +0100 Subject: [PATCH] util: buffer: Add API to set indentation level to a given value 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 | 1 + src/util/virbuffer.c | 19 +++++++++++++++++++ src/util/virbuffer.h | 2 ++ tests/virbuftest.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e99bdb0289..98628a4238 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1315,6 +1315,7 @@ virBufferEscapeShell; virBufferEscapeString; virBufferFreeAndReset; virBufferGetIndent; +virBufferSetIndent; virBufferStrcat; virBufferTrim; virBufferURIEncodeString; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 41d541b321..80c8e289d4 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -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 diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 94f14b5b16..d1b64ca3a3 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -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); diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 34160e6b28..8ec6ce51ed 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -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 { \ -- 2.47.2