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

[libvirt] [libvirt-glib 01/23] Add helpers in libvirt-gconfig-helpers.[ch]



These helper functions will be useful later on.
---
 examples/Makefile.am                      |    1 +
 libvirt-gconfig/Makefile.am               |    3 +
 libvirt-gconfig/libvirt-gconfig-helpers.c |  179 +++++++++++++++++++++++++++++
 libvirt-gconfig/libvirt-gconfig-helpers.h |   48 ++++++++
 libvirt-gconfig/libvirt-gconfig-object.c  |   52 ---------
 libvirt-gconfig/libvirt-gconfig.h         |    2 +
 libvirt-gconfig/libvirt-gconfig.sym       |    1 +
 libvirt-gobject/Makefile.am               |    2 +
 8 files changed, 236 insertions(+), 52 deletions(-)
 create mode 100644 libvirt-gconfig/libvirt-gconfig-helpers.c
 create mode 100644 libvirt-gconfig/libvirt-gconfig-helpers.h

diff --git a/examples/Makefile.am b/examples/Makefile.am
index 0964597..d88c0e1 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -19,6 +19,7 @@ conn_test_CFLAGS = \
 		$(WARN_CFLAGS) \
 		$(GLIB2_CFLAGS) \
 		$(LIBVIRT_CFLAGS) \
+		$(LIBXML2_CFLAGS) \
 		-I$(top_srcdir)
 conn_test_SOURCES = \
 		conn-test.c
diff --git a/libvirt-gconfig/Makefile.am b/libvirt-gconfig/Makefile.am
index 180d5ee..634ad5a 100644
--- a/libvirt-gconfig/Makefile.am
+++ b/libvirt-gconfig/Makefile.am
@@ -9,6 +9,7 @@ GCONFIG_HEADER_FILES = \
 			libvirt-gconfig-capabilities.h \
 			libvirt-gconfig-domain.h \
 			libvirt-gconfig-domain-snapshot.h \
+			libvirt-gconfig-helpers.h \
 			libvirt-gconfig-interface.h \
 			libvirt-gconfig-network.h \
 			libvirt-gconfig-network-filter.h \
@@ -21,6 +22,7 @@ GCONFIG_SOURCE_FILES = \
 			libvirt-gconfig-capabilities.c \
 			libvirt-gconfig-domain.c \
 			libvirt-gconfig-domain-snapshot.c \
+			libvirt-gconfig-helpers.c \
 			libvirt-gconfig-interface.c \
 			libvirt-gconfig-network.c \
 			libvirt-gconfig-network-filter.c \
@@ -70,6 +72,7 @@ LibvirtGConfig-1.0.gir: libvirt-gconfig-1.0.la $(G_IR_SCANNER) Makefile.am
                 --output $@ \
                 -I$(top_srcdir) \
                 -I$(top_builddir) \
+                $(LIBXML2_CFLAGS) \
                 --verbose \
                 --pkg=gobject-2.0 \
                 --c-include="libvirt-gconfig/libvirt-gconfig.h" \
diff --git a/libvirt-gconfig/libvirt-gconfig-helpers.c b/libvirt-gconfig/libvirt-gconfig-helpers.c
new file mode 100644
index 0000000..2fbfa9e
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-helpers.c
@@ -0,0 +1,179 @@
+/*
+ * libvirt-gconfig-helpers.c: various GVirConfig helpers
+ *
+ * Copyright (C) 2010, 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors: Daniel P. Berrange <berrange redhat com>
+ *          Christophe Fergeau <cfergeau gmail com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <libxml/xmlerror.h>
+
+#include "libvirt-gconfig/libvirt-gconfig.h"
+
+GQuark
+gvir_config_object_error_quark(void)
+{
+    return g_quark_from_static_string("gvir-config-object");
+}
+
+static GError *gvir_xml_error_new_literal(GQuark domain,
+                                          gint code,
+                                          const gchar *message)
+{
+    xmlErrorPtr xerr = xmlGetLastError();
+
+    if (!xerr)
+        return NULL;
+
+    if (message)
+        return g_error_new(domain,
+                           code,
+                           "%s: %s",
+                           message,
+                           xerr->message);
+    else
+        return g_error_new(domain,
+                           code,
+                           "%s",
+                           xerr->message);
+}
+
+
+GError *gvir_xml_error_new(GQuark domain,
+                           gint code,
+                           const gchar *format,
+                           ...)
+{
+    GError *err;
+    va_list args;
+    gchar *message;
+
+    va_start(args, format);
+    message = g_strdup_vprintf(format, args);
+    va_end(args);
+
+    err = gvir_xml_error_new_literal(domain, code, message);
+
+    g_free(message);
+
+    return err;
+}
+
+xmlNodePtr
+gvir_config_xml_parse(const char *xml, const char *root_node, GError **err)
+{
+    xmlDocPtr doc;
+
+    if (!xml) {
+        *err = g_error_new(GVIR_CONFIG_OBJECT_ERROR,
+                           0,
+                           "%s",
+                           "No XML document to parse");
+        return NULL;
+    }
+
+    doc = xmlParseMemory(xml, strlen(xml));
+    if (!doc) {
+        *err = gvir_xml_error_new(GVIR_CONFIG_OBJECT_ERROR,
+                                  0,
+                                  "%s",
+                                  "Unable to parse configuration");
+        return NULL;
+    }
+    if ((!doc->children) || (strcmp((char *)doc->children->name, root_node) != 0)) {
+        *err = g_error_new(GVIR_CONFIG_OBJECT_ERROR,
+                           0,
+                           "XML data has no '%s' node",
+                           root_node);
+        xmlFreeDoc(doc);
+        return NULL;
+    }
+
+    return doc->children;
+}
+
+
+/*
+ * gvir_config_xml_get_element, gvir_config_xml_get_child_element_content
+ * and gvir_config_xml_get_child_element_content_glib are:
+ *
+ * Copyright (C) 2006, 2007 OpenedHand Ltd.
+ *
+ * Author: Jorn Baayen <jorn openedhand com>
+ */
+xmlNode *
+gvir_config_xml_get_element (xmlNode *node, ...)
+{
+        va_list var_args;
+
+        va_start (var_args, node);
+
+        while (TRUE) {
+                const char *arg;
+
+                arg = va_arg (var_args, const char *);
+                if (!arg)
+                        break;
+
+                for (node = node->children; node; node = node->next)
+                        if (!g_strcmp0 (arg, (char *) node->name))
+                                break;
+
+                if (!node)
+                        break;
+        }
+
+        va_end (var_args);
+
+        return node;
+}
+
+xmlChar *
+gvir_config_xml_get_child_element_content (xmlNode    *node,
+                                           const char *child_name)
+{
+        xmlNode *child_node;
+
+        child_node = gvir_config_xml_get_element (node, child_name, NULL);
+        if (!child_node)
+                return NULL;
+
+        return xmlNodeGetContent (child_node);
+}
+
+char *
+gvir_config_xml_get_child_element_content_glib (xmlNode    *node,
+                                                const char *child_name)
+{
+        xmlChar *content;
+        char *copy;
+
+        content = gvir_config_xml_get_child_element_content (node, child_name);
+        if (!content)
+                return NULL;
+
+        copy = g_strdup ((char *) content);
+
+        xmlFree (content);
+
+        return copy;
+}
diff --git a/libvirt-gconfig/libvirt-gconfig-helpers.h b/libvirt-gconfig/libvirt-gconfig-helpers.h
new file mode 100644
index 0000000..1bdf2d0
--- /dev/null
+++ b/libvirt-gconfig/libvirt-gconfig-helpers.h
@@ -0,0 +1,48 @@
+/*
+ * libvirt-gconfig-helpers.h: various GVirConfig helpers
+ *
+ * Copyright (C) 2010, 2011 Red Hat
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors: Daniel P. Berrange <berrange redhat com>
+ *          Christophe Fergeau <cfergeau gmail com>
+ */
+
+#if !defined(__LIBVIRT_GCONFIG_H__) && !defined(LIBVIRT_GCONFIG_BUILD)
+#error "Only <libvirt-gconfig/libvirt-gconfig.h> can be included directly."
+#endif
+
+#ifndef __LIBVIRT_GCONFIG_HELPERS_H__
+#define __LIBVIRT_GCONFIG_HELPERS_H__
+
+G_BEGIN_DECLS
+
+GQuark gvir_config_object_error_quark(void);
+#define GVIR_CONFIG_OBJECT_ERROR gvir_config_object_error_quark()
+
+GError *gvir_xml_error_new(GQuark domain, gint code,
+                           const gchar *format, ...);
+xmlNodePtr gvir_config_xml_parse(const char *xml,
+                                 const char *root_node,
+                                 GError **err);
+xmlNode * gvir_config_xml_get_element (xmlNode *node, ...);
+xmlChar * gvir_config_xml_get_child_element_content (xmlNode    *node,
+                                                     const char *child_name);
+char *gvir_config_xml_get_child_element_content_glib (xmlNode    *node,
+                                                      const char *child_name);
+G_END_DECLS
+
+#endif /* __LIBVIRT_GCONFIG_HELPERS_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig-object.c b/libvirt-gconfig/libvirt-gconfig-object.c
index 95b147f..4a58323 100644
--- a/libvirt-gconfig/libvirt-gconfig-object.c
+++ b/libvirt-gconfig/libvirt-gconfig-object.c
@@ -55,58 +55,6 @@ enum {
 };
 
 
-#define GVIR_CONFIG_OBJECT_ERROR gvir_config_object_error_quark()
-
-
-static GQuark
-gvir_config_object_error_quark(void)
-{
-    return g_quark_from_static_string("gvir-config-object");
-}
-
-static GError *gvir_xml_error_new_literal(GQuark domain,
-                                          gint code,
-                                          const gchar *message)
-{
-    xmlErrorPtr xerr = xmlGetLastError();
-
-    if (!xerr)
-        return NULL;
-
-    if (message)
-        return g_error_new(domain,
-                           code,
-                           "%s: %s",
-                           message,
-                           xerr->message);
-    else
-        return g_error_new(domain,
-                           code,
-                           "%s",
-                           xerr->message);
-}
-
-
-static GError *gvir_xml_error_new(GQuark domain,
-                                  gint code,
-                                  const gchar *format,
-                                  ...)
-{
-    GError *err;
-    va_list args;
-    gchar *message;
-
-    va_start(args, format);
-    message = g_strdup_vprintf(format, args);
-    va_end(args);
-
-    err = gvir_xml_error_new_literal(domain, code, message);
-
-    g_free(message);
-
-    return err;
-}
-
 static void gvir_xml_generic_error_nop(void *userData G_GNUC_UNUSED,
                                        const char *msg G_GNUC_UNUSED,
                                        ...)
diff --git a/libvirt-gconfig/libvirt-gconfig.h b/libvirt-gconfig/libvirt-gconfig.h
index c561804..fdc78a4 100644
--- a/libvirt-gconfig/libvirt-gconfig.h
+++ b/libvirt-gconfig/libvirt-gconfig.h
@@ -24,7 +24,9 @@
 #define __LIBVIRT_GCONFIG_H__
 
 #include <glib-object.h>
+#include <libxml/tree.h>
 
+#include <libvirt-gconfig/libvirt-gconfig-helpers.h>
 #include <libvirt-gconfig/libvirt-gconfig-object.h>
 #include <libvirt-gconfig/libvirt-gconfig-capabilities.h>
 #include <libvirt-gconfig/libvirt-gconfig-domain.h>
diff --git a/libvirt-gconfig/libvirt-gconfig.sym b/libvirt-gconfig/libvirt-gconfig.sym
index 2d09c37..24407c3 100644
--- a/libvirt-gconfig/libvirt-gconfig.sym
+++ b/libvirt-gconfig/libvirt-gconfig.sym
@@ -22,6 +22,7 @@ LIBVIRT_GOBJECT_0.0.1 {
 	gvir_config_node_device_new;
 
 	gvir_config_object_get_type;
+	gvir_config_object_error_quark;
 	gvir_config_object_new;
 	gvir_config_object_get_doc;
 	gvir_config_object_get_schema;
diff --git a/libvirt-gobject/Makefile.am b/libvirt-gobject/Makefile.am
index 7013675..b482aa5 100644
--- a/libvirt-gobject/Makefile.am
+++ b/libvirt-gobject/Makefile.am
@@ -51,6 +51,7 @@ libvirt_gobject_1_0_la_CFLAGS = \
 			$(LIBVIRT_CFLAGS) \
 			$(GOBJECT2_CFLAGS) \
 			$(GIO2_CFLAGS) \
+			$(LIBXML2_CFLAGS) \
 			$(WARN_CFLAGS)
 libvirt_gobject_1_0_la_LIBADD = \
 			$(LIBVIRT_LIBS) \
@@ -116,6 +117,7 @@ LibvirtGObject-1.0.gir: libvirt-gobject-1.0.la $(G_IR_SCANNER) Makefile.am
                 --output $@ \
                 -I$(top_srcdir) \
                 -I$(top_builddir) \
+                $(LIBXML2_CFLAGS) \
                 --verbose \
                 --pkg=gobject-2.0 \
                 --pkg=gthread-2.0 \
-- 
1.7.6.4


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