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

[libvirt] [libvirt-glib 2/4] Add helpers to parse xml trees



This will be helpful to implement GVirConfig getters.
---
 libvirt-gconfig/libvirt-gconfig-helpers-private.h |    5 ++
 libvirt-gconfig/libvirt-gconfig-helpers.c         |   52 ++++++++++++++++++--
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/libvirt-gconfig/libvirt-gconfig-helpers-private.h b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
index 4c4a539..5c98a58 100644
--- a/libvirt-gconfig/libvirt-gconfig-helpers-private.h
+++ b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
@@ -50,7 +50,12 @@ 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);
+xmlChar *gvir_config_xml_get_attribute_content(xmlNodePtr node,
+                                               const char *attr_name);
+char *gvir_config_xml_get_attribute_content_glib(xmlNodePtr node,
+                                                 const char *attr_name);
 const char *gvir_config_genum_get_nick (GType enum_type, gint value);
+int gvir_config_genum_get_value (GType enum_type, const char *nick);
 G_END_DECLS
 
 #endif /* __LIBVIRT_GCONFIG_HELPERS_PRIVATE_H__ */
diff --git a/libvirt-gconfig/libvirt-gconfig-helpers.c b/libvirt-gconfig/libvirt-gconfig-helpers.c
index c1c23da..75ad2b2 100644
--- a/libvirt-gconfig/libvirt-gconfig-helpers.c
+++ b/libvirt-gconfig/libvirt-gconfig-helpers.c
@@ -210,22 +210,43 @@ gvir_config_xml_get_child_element_content (xmlNode    *node,
         return xmlNodeGetContent (child_node);
 }
 
+static char *libxml_to_glib(xmlChar *str)
+{
+    char *g_str;
+
+    if (str == NULL)
+        return NULL;
+    g_str = g_strdup((char *)str);
+    xmlFree(str);
+
+    return g_str;
+}
+
 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);
+        return libxml_to_glib(content);
+}
+
+G_GNUC_INTERNAL xmlChar *
+gvir_config_xml_get_attribute_content(xmlNodePtr node, const char *attr_name)
+{
+    return xmlGetProp(node, (const xmlChar*)attr_name);
+}
+
+G_GNUC_INTERNAL char *
+gvir_config_xml_get_attribute_content_glib(xmlNodePtr node, const char *attr_name)
+{
+    xmlChar *attr;
 
-        xmlFree (content);
+    attr = gvir_config_xml_get_attribute_content(node, attr_name);
 
-        return copy;
+    return libxml_to_glib(attr);
 }
 
 const char *gvir_config_genum_get_nick (GType enum_type, gint value)
@@ -244,3 +265,22 @@ const char *gvir_config_genum_get_nick (GType enum_type, gint value)
 
     g_return_val_if_reached(NULL);
 }
+
+G_GNUC_INTERNAL int
+gvir_config_genum_get_value (GType enum_type, const char *nick)
+{
+    GEnumClass *enum_class;
+    GEnumValue *enum_value;
+
+    g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), -1);
+    g_return_val_if_fail(nick != NULL, -1);
+
+    enum_class = g_type_class_ref(enum_type);
+    enum_value = g_enum_get_value_by_nick(enum_class, nick);
+    g_type_class_unref(enum_class);
+
+    if (enum_value != NULL)
+        return enum_value->value;
+
+    g_return_val_if_reached(-1);
+}
-- 
1.7.7.5


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