util: buffer: Split getting of effective indent out of virBufferGetIndent

The function basically does two very distinct things depending on a
bool. As a first step of conversion split out the case when @dynamic is
true and implement it as a new function and convert all callers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-10-24 12:29:12 +02:00
parent 32ff9baf68
commit 673f5e04da
4 changed files with 29 additions and 11 deletions

View File

@ -1600,6 +1600,7 @@ virBufferEscapeShell;
virBufferEscapeSQL;
virBufferEscapeString;
virBufferFreeAndReset;
virBufferGetEffectiveIndent;
virBufferGetIndent;
virBufferSetIndent;
virBufferStrcat;

View File

@ -117,6 +117,24 @@ virBufferGetIndent(const virBuffer *buf, bool dynamic)
return buf->indent;
}
/**
* virBufferGetEffectiveIndent:
* @buf: the buffer
*
* Returns the number of spaces that need to be appended to @buf to honour
* auto-indentation.
*/
size_t
virBufferGetEffectiveIndent(const virBuffer *buf)
{
if (buf->use && buf->content[buf->use - 1] != '\n')
return 0;
return buf->indent;
}
/**
* virBufferGrow:
* @buf: the buffer
@ -161,14 +179,12 @@ void
virBufferAdd(virBufferPtr buf, const char *str, int len)
{
unsigned int needSize;
int indent;
size_t indent;
if (!str || !buf || (len == 0 && buf->indent == 0))
if (!str || !buf || buf->error || (len == 0 && buf->indent == 0))
return;
indent = virBufferGetIndent(buf, true);
if (indent < 0)
return;
indent = virBufferGetEffectiveIndent(buf);
if (len < 0)
len = strlen(str);

View File

@ -110,6 +110,7 @@ void virBufferSetIndent(virBufferPtr, int indent);
virBufferSetIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2)
int virBufferGetIndent(const virBuffer *buf, bool dynamic);
size_t virBufferGetEffectiveIndent(const virBuffer *buf);
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
void virBufferAddStr(virBufferPtr buf, const char *str);

View File

@ -19,7 +19,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
int ret = 0;
if (virBufferGetIndent(buf, false) != 0 ||
virBufferGetIndent(buf, true) != 0) {
virBufferGetEffectiveIndent(buf) != 0) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}
@ -29,28 +29,28 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
ret = -1;
}
if (virBufferGetIndent(buf, false) != 3 ||
virBufferGetIndent(buf, true) != 3 ||
virBufferGetEffectiveIndent(buf) != 3 ||
virBufferError(buf)) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}
virBufferAdjustIndent(buf, -2);
if (virBufferGetIndent(buf, false) != 1 ||
virBufferGetIndent(buf, true) != 1 ||
virBufferGetEffectiveIndent(buf) != 1 ||
virBufferError(buf)) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}
virBufferAdjustIndent(buf, -3);
if (virBufferGetIndent(buf, false) != 0 ||
virBufferGetIndent(buf, true) != 0) {
virBufferGetEffectiveIndent(buf) != 0) {
VIR_TEST_DEBUG("Indentation level not truncated");
ret = -1;
}
virBufferAdjustIndent(buf, 3);
virBufferFreeAndReset(buf);
if (virBufferGetIndent(buf, false) != 0 ||
virBufferGetIndent(buf, true) != 0 ||
virBufferGetEffectiveIndent(buf) != 0 ||
virBufferError(buf)) {
VIR_TEST_DEBUG("Reset didn't clear indentation");
ret = -1;
@ -66,7 +66,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
ret = -1;
}
if (virBufferGetIndent(buf, false) != 2 ||
virBufferGetIndent(buf, true) != 0) {
virBufferGetEffectiveIndent(buf) != 0) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}