提交 91e7862c 编写于 作者: P Peter Krempa

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.
上级 ff9ed72b
......@@ -1315,6 +1315,7 @@ virBufferEscapeShell;
virBufferEscapeString;
virBufferFreeAndReset;
virBufferGetIndent;
virBufferSetIndent;
virBufferStrcat;
virBufferTrim;
virBufferURIEncodeString;
......
......@@ -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
......
......@@ -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);
......
......@@ -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 { \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册