On 10/19/2011 11:51 AM, Peter Krempa wrote:
On 09/29/2011 06:22 PM, Eric Blake wrote:
> Rather than having to adjust all callers in a chain to deal with
> indentation, it is nicer to have virBuffer do auto-indentation.
>
> virBufferAdd(const virBufferPtr buf, const char *str, int len)
> {
> unsigned int needSize;
> + int indent;
/* context for a later comment */
> - if ((str == NULL) || (buf == NULL) || (len == 0))
> + if (!str || !buf || (len == 0&& buf->indent == 0))
> return;
> @@ -249,6 +284,9 @@ virBufferVasprintf(virBufferPtr buf, const
char
> *format, va_list argptr)
> if (buf->error)
> return;
>
> + if (buf->indent)
> + virBufferAdd(buf, "", -1);
> +
virBufferAdd is a no-op, if this expression is true (see context above):
" if (!str || !buf || (len == 0 && buf->indent == 0))". You could
save a
call to strlen and the if clause if you call virBufferAdd(buf, "", 0)
The len=0 will be automaticaly used (as the only way to accept len==0 as
a correct value is if indentation is requested). You can also save a
line with the if clause and call it directly. ( Looks like you've
prepared it for this to happen :D)
Okay, I simplified these two lines down to one.
> diff --git a/tests/virbuftest.c b/tests/virbuftest.c
>
The tests look fine to me and it's nice to have them.
Yet they missed out on the new stuff in the meantime.
ACK,
Here's what I squashed before pushing:
diff --git i/src/util/buf.c w/src/util/buf.c
index 3dd9a72..ac5360c 100644
--- i/src/util/buf.c
+++ w/src/util/buf.c
@@ -284,8 +284,7 @@ virBufferVasprintf(virBufferPtr buf, const char
*format, va_list argptr)
if (buf->error)
return;
- if (buf->indent)
- virBufferAddLit(buf, "");
+ virBufferAddLit(buf, ""); /* auto-indent */
if (buf->size == 0 &&
virBufferGrow(buf, 100) < 0)
@@ -500,8 +499,7 @@ virBufferURIEncodeString(virBufferPtr buf, const
char *str)
if (buf->error)
return;
- if (buf->indent)
- virBufferAddLit(buf, "");
+ virBufferAddLit(buf, ""); /* auto-indent */
for (p = str; *p; ++p) {
if (c_isalnum(*p))
diff --git i/tests/virbuftest.c w/tests/virbuftest.c
index 2f94e1c..bc53959 100644
--- i/tests/virbuftest.c
+++ w/tests/virbuftest.c
@@ -63,7 +63,7 @@ static int testBufAutoIndent(const void *data
ATTRIBUTE_UNUSED)
virBuffer bufinit = VIR_BUFFER_INITIALIZER;
virBufferPtr buf = &bufinit;
const char expected[] =
- " 1\n 2\n 3\n 4\n 5\n 6\n 7\n &\n 8\n 9\n";
+ " 1\n 2\n 3\n 4\n 5\n 6\n 7\n &\n 8\n 9\n 10\n
' 11'\n";
char *result = NULL;
int ret = 0;
@@ -86,7 +86,7 @@ static int testBufAutoIndent(const void *data
ATTRIBUTE_UNUSED)
TEST_ERROR("Wrong indentation");
ret = -1;
}
- virBufferAdjustIndent(buf, -2);
+ virBufferAdjustIndent(buf, -3);
if (virBufferGetIndent(buf, false) != -1 ||
virBufferGetIndent(buf, true) != -1 ||
virBufferError(buf) != -1) {
@@ -119,6 +119,10 @@ static int testBufAutoIndent(const void *data
ATTRIBUTE_UNUSED)
virBufferEscapeSexpr(buf, "%s", "8\n");
virBufferURIEncodeString(buf, "9");
virBufferAddChar(buf, '\n');
+ virBufferEscapeShell(buf, "10");
+ virBufferAddChar(buf, '\n');
+ virBufferEscapeShell(buf, " 11");
+ virBufferAddChar(buf, '\n');
result = virBufferContentAndReset(buf);
if (!result || STRNEQ(result, expected)) {
--
Eric Blake eblake(a)redhat.com +1-801-349-2682
Libvirt virtualization library
http://libvirt.org