mirror of https://gitee.com/openkylin/libvirt.git
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:
parent
32ff9baf68
commit
673f5e04da
|
@ -1600,6 +1600,7 @@ virBufferEscapeShell;
|
||||||
virBufferEscapeSQL;
|
virBufferEscapeSQL;
|
||||||
virBufferEscapeString;
|
virBufferEscapeString;
|
||||||
virBufferFreeAndReset;
|
virBufferFreeAndReset;
|
||||||
|
virBufferGetEffectiveIndent;
|
||||||
virBufferGetIndent;
|
virBufferGetIndent;
|
||||||
virBufferSetIndent;
|
virBufferSetIndent;
|
||||||
virBufferStrcat;
|
virBufferStrcat;
|
||||||
|
|
|
@ -117,6 +117,24 @@ virBufferGetIndent(const virBuffer *buf, bool dynamic)
|
||||||
return buf->indent;
|
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:
|
* virBufferGrow:
|
||||||
* @buf: the buffer
|
* @buf: the buffer
|
||||||
|
@ -161,14 +179,12 @@ void
|
||||||
virBufferAdd(virBufferPtr buf, const char *str, int len)
|
virBufferAdd(virBufferPtr buf, const char *str, int len)
|
||||||
{
|
{
|
||||||
unsigned int needSize;
|
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;
|
return;
|
||||||
|
|
||||||
indent = virBufferGetIndent(buf, true);
|
indent = virBufferGetEffectiveIndent(buf);
|
||||||
if (indent < 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
|
|
|
@ -110,6 +110,7 @@ void virBufferSetIndent(virBufferPtr, int indent);
|
||||||
virBufferSetIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2)
|
virBufferSetIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2)
|
||||||
|
|
||||||
int virBufferGetIndent(const virBuffer *buf, bool dynamic);
|
int virBufferGetIndent(const virBuffer *buf, bool dynamic);
|
||||||
|
size_t virBufferGetEffectiveIndent(const virBuffer *buf);
|
||||||
|
|
||||||
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
||||||
void virBufferAddStr(virBufferPtr buf, const char *str);
|
void virBufferAddStr(virBufferPtr buf, const char *str);
|
||||||
|
|
|
@ -19,7 +19,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (virBufferGetIndent(buf, false) != 0 ||
|
if (virBufferGetIndent(buf, false) != 0 ||
|
||||||
virBufferGetIndent(buf, true) != 0) {
|
virBufferGetEffectiveIndent(buf) != 0) {
|
||||||
VIR_TEST_DEBUG("Wrong indentation");
|
VIR_TEST_DEBUG("Wrong indentation");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
@ -29,28 +29,28 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
if (virBufferGetIndent(buf, false) != 3 ||
|
if (virBufferGetIndent(buf, false) != 3 ||
|
||||||
virBufferGetIndent(buf, true) != 3 ||
|
virBufferGetEffectiveIndent(buf) != 3 ||
|
||||||
virBufferError(buf)) {
|
virBufferError(buf)) {
|
||||||
VIR_TEST_DEBUG("Wrong indentation");
|
VIR_TEST_DEBUG("Wrong indentation");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
virBufferAdjustIndent(buf, -2);
|
virBufferAdjustIndent(buf, -2);
|
||||||
if (virBufferGetIndent(buf, false) != 1 ||
|
if (virBufferGetIndent(buf, false) != 1 ||
|
||||||
virBufferGetIndent(buf, true) != 1 ||
|
virBufferGetEffectiveIndent(buf) != 1 ||
|
||||||
virBufferError(buf)) {
|
virBufferError(buf)) {
|
||||||
VIR_TEST_DEBUG("Wrong indentation");
|
VIR_TEST_DEBUG("Wrong indentation");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
virBufferAdjustIndent(buf, -3);
|
virBufferAdjustIndent(buf, -3);
|
||||||
if (virBufferGetIndent(buf, false) != 0 ||
|
if (virBufferGetIndent(buf, false) != 0 ||
|
||||||
virBufferGetIndent(buf, true) != 0) {
|
virBufferGetEffectiveIndent(buf) != 0) {
|
||||||
VIR_TEST_DEBUG("Indentation level not truncated");
|
VIR_TEST_DEBUG("Indentation level not truncated");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
virBufferAdjustIndent(buf, 3);
|
virBufferAdjustIndent(buf, 3);
|
||||||
virBufferFreeAndReset(buf);
|
virBufferFreeAndReset(buf);
|
||||||
if (virBufferGetIndent(buf, false) != 0 ||
|
if (virBufferGetIndent(buf, false) != 0 ||
|
||||||
virBufferGetIndent(buf, true) != 0 ||
|
virBufferGetEffectiveIndent(buf) != 0 ||
|
||||||
virBufferError(buf)) {
|
virBufferError(buf)) {
|
||||||
VIR_TEST_DEBUG("Reset didn't clear indentation");
|
VIR_TEST_DEBUG("Reset didn't clear indentation");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
@ -66,7 +66,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
if (virBufferGetIndent(buf, false) != 2 ||
|
if (virBufferGetIndent(buf, false) != 2 ||
|
||||||
virBufferGetIndent(buf, true) != 0) {
|
virBufferGetEffectiveIndent(buf) != 0) {
|
||||||
VIR_TEST_DEBUG("Wrong indentation");
|
VIR_TEST_DEBUG("Wrong indentation");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue