[libvirt-glib] Add gvir_xml_set_error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This mirrors g_set_error, but uses gvir_xml_error_new. The main
benefit of using gvxr_xml_set_error over gvir_xml_error_new is that
it handles NULL GError **.
---
 libvirt-gconfig/libvirt-gconfig-helpers-private.h |    2 +
 libvirt-gconfig/libvirt-gconfig-helpers.c         |   40 ++++++++++++++++-----
 libvirt-gconfig/libvirt-gconfig-object.c          |   32 ++++++----------
 3 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/libvirt-gconfig/libvirt-gconfig-helpers-private.h b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
index 0a35595..b5b4878 100644
--- a/libvirt-gconfig/libvirt-gconfig-helpers-private.h
+++ b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
@@ -32,6 +32,8 @@ G_BEGIN_DECLS
 
 GError *gvir_xml_error_new(GQuark domain, gint code,
                            const gchar *format, ...);
+void gvir_xml_set_error(GError **error, GQuark domain, gint code,
+                        const gchar *format, ...);
 xmlNodePtr gvir_config_xml_parse(const char *xml,
                                  const char *root_node,
                                  GError **err);
diff --git a/libvirt-gconfig/libvirt-gconfig-helpers.c b/libvirt-gconfig/libvirt-gconfig-helpers.c
index 722fe3d..bfdcfc3 100644
--- a/libvirt-gconfig/libvirt-gconfig-helpers.c
+++ b/libvirt-gconfig/libvirt-gconfig-helpers.c
@@ -59,6 +59,22 @@ static GError *gvir_xml_error_new_literal(GQuark domain,
 }
 
 
+static GError *gvir_xml_error_new_va(GQuark domain,
+                                     gint code,
+                                     const gchar *format,
+                                     va_list args)
+{
+    GError *err;
+    gchar *message;
+
+    message = g_strdup_vprintf(format, args);
+    err = gvir_xml_error_new_literal(domain, code, message);
+
+    g_free(message);
+
+    return err;
+}
+
 GError *gvir_xml_error_new(GQuark domain,
                            gint code,
                            const gchar *format,
@@ -66,17 +82,25 @@ GError *gvir_xml_error_new(GQuark domain,
 {
     GError *err;
     va_list args;
-    gchar *message;
 
     va_start(args, format);
-    message = g_strdup_vprintf(format, args);
+    err = gvir_xml_error_new_va(domain, code, format, args);
     va_end(args);
 
-    err = gvir_xml_error_new_literal(domain, code, message);
+    return err;
+}
 
-    g_free(message);
+void gvir_xml_set_error(GError **error, GQuark domain, gint code,
+                        const gchar *format, ...)
+{
+    va_list args;
 
-    return err;
+    if (error == NULL)
+        return;
+
+    va_start(args, format);
+    *error = gvir_xml_error_new_va(domain, code, format, args);
+    va_end(args);
 }
 
 xmlNodePtr
@@ -94,10 +118,8 @@ gvir_config_xml_parse(const char *xml, const char *root_node, GError **err)
 
     doc = xmlParseMemory(xml, strlen(xml));
     if (!doc) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "%s",
-                                  "Unable to parse configuration");
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR,
+                           0, "%s", "Unable to parse configuration");
         return NULL;
     }
     if ((!doc->children) || (strcmp((char *)doc->children->name, root_node) != 0)) {
diff --git a/libvirt-gconfig/libvirt-gconfig-object.c b/libvirt-gconfig/libvirt-gconfig-object.c
index 03c7d5a..e2597f5 100644
--- a/libvirt-gconfig/libvirt-gconfig-object.c
+++ b/libvirt-gconfig/libvirt-gconfig-object.c
@@ -202,28 +202,23 @@ void gvir_config_object_validate(GVirConfigObject *config,
     xmlSetStructuredErrorFunc(NULL, gvir_xml_structured_error_nop);
 
     if (!priv->node) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "%s",
-                                  "No XML document associated with this config object");
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR, 0, "%s",
+                           "No XML document associated with this config object");
         return;
     }
 
     rngParser = xmlRelaxNGNewParserCtxt(priv->schema);
     if (!rngParser) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "Unable to create RNG parser for %s",
-                                  priv->schema);
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR, 0,
+                           "Unable to create RNG parser for %s",
+                           priv->schema);
         return;
     }
 
     rng = xmlRelaxNGParse(rngParser);
     if (!rng) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "Unable to parse RNG %s",
-                                  priv->schema);
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR, 0,
+                           "Unable to parse RNG %s", priv->schema);
         xmlRelaxNGFreeParserCtxt(rngParser);
         return;
     }
@@ -231,19 +226,16 @@ void gvir_config_object_validate(GVirConfigObject *config,
 
     rngValid = xmlRelaxNGNewValidCtxt(rng);
     if (!rngValid) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "Unable to create RNG validation context %s",
-                                  priv->schema);
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR, 0,
+                           "Unable to create RNG validation context %s",
+                           priv->schema);
         xmlRelaxNGFree(rng);
         return;
     }
 
     if (xmlRelaxNGValidateDoc(rngValid, priv->node->doc) != 0) {
-        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
-                                  0,
-                                  "%s",
-                                  "Unable to validate doc");
+        gvir_xml_set_error(err, GVIR_CONFIG_OBJECT_ERROR, 0,
+                           "%s", "Unable to validate doc");
         xmlRelaxNGFreeValidCtxt(rngValid);
         xmlRelaxNGFree(rng);
         return;
-- 
1.7.7.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]