]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: buffer: Split getting of effective indent out of virBufferGetIndent
authorPeter Krempa <pkrempa@redhat.com>
Thu, 24 Oct 2019 10:29:12 +0000 (12:29 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 24 Oct 2019 17:35:34 +0000 (19:35 +0200)
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>
src/libvirt_private.syms
src/util/virbuffer.c
src/util/virbuffer.h
tests/virbuftest.c

index 12cb3b5bf75437c51c6e5484785a606363cf522f..94c2e4ef6acafa81b6c92f1bf5f7e997c70c7cb5 100644 (file)
@@ -1600,6 +1600,7 @@ virBufferEscapeShell;
 virBufferEscapeSQL;
 virBufferEscapeString;
 virBufferFreeAndReset;
+virBufferGetEffectiveIndent;
 virBufferGetIndent;
 virBufferSetIndent;
 virBufferStrcat;
index 69fcf946f559464ab484a58977855264833e77ea..0d2721b118dbdc30f4797c1755ad5419a5c9f26a 100644 (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);
index ff24ab10196c12f2032e70941b5ec8dc3640dd8e..7156d9d0d8aeeee70de34a05568b18fb7e702d8a 100644 (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);
index 064b3e96b447cd9d6daa461cb21eff4ea8ad18fe..0c806908e19acc20d683c18bfa7c13eb02bd313b 100644 (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;
     }