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
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);
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);
int ret = 0;
if (virBufferGetIndent(buf, false) != 0 ||
- virBufferGetIndent(buf, true) != 0) {
+ virBufferGetEffectiveIndent(buf) != 0) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}
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;
ret = -1;
}
if (virBufferGetIndent(buf, false) != 2 ||
- virBufferGetIndent(buf, true) != 0) {
+ virBufferGetEffectiveIndent(buf) != 0) {
VIR_TEST_DEBUG("Wrong indentation");
ret = -1;
}