[libvirt] [PATCHv2 10/13] snapshot: simplify indentation of nwfilter

Eric Blake eblake at redhat.com
Thu Sep 29 16:22:30 UTC 2011


Fixing this involved some refactoring of common code out of
domain_conf and nwfilter_conf into nwfilter_params.

* src/conf/nwfilter_params.h (virNWFilterFormatParamAttributes):
Adjust signature.
* src/conf/nwfilter_params.c (_formatParameterAttrs)
(virNWFilterFormatParamAttributes): Adjust indentation handling,
and handle filterref here.
(formatterParam): Delete unused struct.
* src/conf/domain_conf.c (virDomainNetDefFormat): Adjust caller.
* src/conf/nwfilter_conf.c (virNWFilterIncludeDefFormat): Likewise.
---
 src/conf/domain_conf.c     |   15 ++++---------
 src/conf/nwfilter_conf.c   |   18 ++++++----------
 src/conf/nwfilter_params.c |   45 +++++++++++++++++++------------------------
 src/conf/nwfilter_params.h |    7 ++++-
 4 files changed, 37 insertions(+), 48 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 70201af..efdf914 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -9441,7 +9441,6 @@ virDomainNetDefFormat(virBufferPtr buf,
                       unsigned int flags)
 {
     const char *type = virDomainNetTypeToString(def->type);
-    char *attrs;

     if (!type) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
@@ -9562,15 +9561,11 @@ virDomainNetDefFormat(virBufferPtr buf,
         }
     }
     if (def->filter) {
-        virBufferEscapeString(buf, "      <filterref filter='%s'",
-                              def->filter);
-        attrs = virNWFilterFormatParamAttributes(def->filterparams,
-                                                 "        ");
-        if (!attrs || strlen(attrs) <= 1)
-            virBufferAddLit(buf, "/>\n");
-        else
-            virBufferAsprintf(buf, ">\n%s      </filterref>\n", attrs);
-        VIR_FREE(attrs);
+        virBufferAdjustIndent(buf, 4);
+        if (virNWFilterFormatParamAttributes(buf, def->filterparams,
+                                             def->filter) < 0)
+            return -1;
+        virBufferAdjustIndent(buf, -4);
     }
     if (def->bootIndex)
         virBufferAsprintf(buf, "      <boot order='%d'/>\n", def->bootIndex);
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 08ede48..5ab4c60 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2849,19 +2849,15 @@ no_memory:
 static char *
 virNWFilterIncludeDefFormat(virNWFilterIncludeDefPtr inc)
 {
-    char *attrs;
     virBuffer buf = VIR_BUFFER_INITIALIZER;

-    virBufferAsprintf(&buf,"  <filterref filter='%s'",
-                      inc->filterref);
-
-    attrs = virNWFilterFormatParamAttributes(inc->params, "    ");
-
-    if (!attrs || strlen(attrs) <= 1)
-        virBufferAddLit(&buf, "/>\n");
-    else
-        virBufferAsprintf(&buf, ">\n%s  </filterref>\n", attrs);
-
+    virBufferAdjustIndent(&buf, 2);
+    if (virNWFilterFormatParamAttributes(&buf, inc->params,
+                                         inc->filterref) < 0) {
+        virBufferFreeAndReset(&buf);
+        return NULL;
+    }
+    virBufferAdjustIndent(&buf, -2);
     if (virBufferError(&buf)) {
         virReportOOMError();
         virBufferFreeAndReset(&buf);
diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
index ee10b21..871aca9 100644
--- a/src/conf/nwfilter_params.c
+++ b/src/conf/nwfilter_params.c
@@ -258,41 +258,36 @@ skip_entry:
 }


-struct formatterParam {
-    virBufferPtr buf;
-    const char *indent;
-};
-
-
 static void
 _formatParameterAttrs(void *payload, const void *name, void *data)
 {
-    struct formatterParam *fp = (struct formatterParam *)data;
+    virBufferPtr buf = data;

-    virBufferAsprintf(fp->buf, "%s<parameter name='%s' value='%s'/>\n",
-                      fp->indent,
+    virBufferAsprintf(buf, "  <parameter name='%s' value='%s'/>\n",
                       (const char *)name,
                       (char *)payload);
 }


-char *
-virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
-                                 const char *indent)
+int
+virNWFilterFormatParamAttributes(virBufferPtr buf,
+                                 virNWFilterHashTablePtr table,
+                                 const char *filterref)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    struct formatterParam fp = {
-        .buf = &buf,
-        .indent = indent,
-    };
-
-    virHashForEach(table->hashTable, _formatParameterAttrs, &fp);
+    int count = virHashSize(table->hashTable);

-    if (virBufferError(&buf)) {
-        virReportOOMError();
-        virBufferFreeAndReset(&buf);
-        return NULL;
+    if (count < 0) {
+        virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("missing filter parameter table"));
+        return -1;
     }
-
-    return virBufferContentAndReset(&buf);
+    virBufferAsprintf(buf, "<filterref filter='%s'", filterref);
+    if (count) {
+        virBufferAddLit(buf, ">\n");
+        virHashForEach(table->hashTable, _formatParameterAttrs, buf);
+        virBufferAddLit(buf, "</filterref>\n");
+    } else {
+        virBufferAddLit(buf, "/>\n");
+    }
+    return 0;
 }
diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h
index 012d6a1..4345229 100644
--- a/src/conf/nwfilter_params.h
+++ b/src/conf/nwfilter_params.h
@@ -1,6 +1,7 @@
 /*
  * nwfilter_params.h: parsing and data maintenance of filter parameters
  *
+ * Copyright (C) 2011 Red Hat, Inc.
  * Copyright (C) 2010 IBM Corporation
  *
  * This library is free software; you can redistribute it and/or
@@ -23,6 +24,7 @@
 # define NWFILTER_PARAMS_H

 # include "hash.h"
+# include "buf.h"

 typedef struct _virNWFilterHashTable virNWFilterHashTable;
 typedef virNWFilterHashTable *virNWFilterHashTablePtr;
@@ -35,8 +37,9 @@ struct _virNWFilterHashTable {


 virNWFilterHashTablePtr virNWFilterParseParamAttributes(xmlNodePtr cur);
-char * virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
-                                        const char *indent);
+int virNWFilterFormatParamAttributes(virBufferPtr buf,
+                                     virNWFilterHashTablePtr table,
+                                     const char *filterref);

 virNWFilterHashTablePtr virNWFilterHashTableCreate(int n);
 void virNWFilterHashTableFree(virNWFilterHashTablePtr table);
-- 
1.7.4.4




More information about the libvir-list mailing list