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 { \