diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fdf21866ff64a70235075df52c41b0a63b4ea8d0..ef8047d75ddcc68308a84e380a44792b1946d6d5 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -20,6 +20,7 @@ virBufferAddChar; virBufferAdjustIndent; virBufferAsprintf; virBufferContentAndReset; +virBufferCurrentContent; virBufferError; virBufferEscape; virBufferEscapeSexpr; diff --git a/src/util/buf.c b/src/util/buf.c index 630e4c9c2fd7bd5d9b607ba915b945d47c0dd62e..6c7c50103c3c95b9855ab8612343c45f10e85a43 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -174,13 +174,33 @@ virBufferAddChar(virBufferPtr buf, char c) virBufferAdd(buf, &c, 1); } +/** + * virBufferCurrentContent: + * @buf: Buffer + * + * Get the current content from the buffer. The content is only valid + * until the next operation on @buf, and an empty string is returned if + * no content is present yet. + * + * Returns the buffer content or NULL in case of error. + */ +const char * +virBufferCurrentContent(virBufferPtr buf) +{ + if (!buf || buf->error) + return NULL; + return buf->use ? buf->content : ""; +} + /** * virBufferContentAndReset: * @buf: Buffer * * Get the content from the buffer and free (only) the buffer structure. * The caller owns the returned string & should free it when no longer - * required. The buffer object is reset to its initial state. + * required. The buffer object is reset to its initial state. This + * interface intentionally returns NULL instead of an empty string if + * there is no content. * * Returns the buffer content or NULL in case of error. */ diff --git a/src/util/buf.h b/src/util/buf.h index a8e2eb5fcc6b9542daae732706eb262c3f2a81fe..2750b17c1d4059c90a3bda8a471daa88dd78c15e 100644 --- a/src/util/buf.h +++ b/src/util/buf.h @@ -37,6 +37,7 @@ struct _virBuffer { }; # endif +const char *virBufferCurrentContent(virBufferPtr buf); char *virBufferContentAndReset(virBufferPtr buf); void virBufferFreeAndReset(virBufferPtr buf); int virBufferError(const virBufferPtr buf); diff --git a/tests/virbuftest.c b/tests/virbuftest.c index cd02db12f3281d9c2e04c0c21f8ed4c48bff43a7..35ba9978744b2f8366ffe96fc2f3f5b1078e93ca 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -73,6 +73,10 @@ static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED) ret = -1; } virBufferAdjustIndent(buf, 3); + if (STRNEQ(virBufferCurrentContent(buf), "")) { + TEST_ERROR("Wrong content"); + ret = -1; + } if (virBufferGetIndent(buf, false) != 3 || virBufferGetIndent(buf, true) != 3 || virBufferError(buf)) { @@ -102,6 +106,10 @@ static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED) } virBufferAdjustIndent(buf, 2); virBufferAddLit(buf, "1"); + if (STRNEQ(virBufferCurrentContent(buf), " 1")) { + TEST_ERROR("Wrong content"); + ret = -1; + } if (virBufferGetIndent(buf, false) != 2 || virBufferGetIndent(buf, true) != 0) { TEST_ERROR("Wrong indentation");