[Libvirt-cim] [PATCH 20/47] Add basic functions about converting others link list to xml

Xu Wang gesaint at linux.vnet.ibm.com
Tue Oct 8 06:13:54 UTC 2013


Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/xmlgen.c |  133 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/libxkutil/xmlgen.c b/libxkutil/xmlgen.c
index 4287d42..72a0904 100644
--- a/libxkutil/xmlgen.c
+++ b/libxkutil/xmlgen.c
@@ -42,6 +42,139 @@ typedef const char *(*devfn_t)(xmlNodePtr node, struct domain *dominfo);
 typedef const char *(*poolfn_t)(xmlNodePtr node, struct virt_pool *pool);
 typedef const char *(*resfn_t)(xmlNodePtr node, struct virt_pool_res *res);
 
+static struct others *props_to_xml(xmlNodePtr node,
+                                   struct others *head,
+                                   const char *parent)
+{
+        struct others *tmp = head;
+        struct others *last = NULL;
+        struct others *del = NULL;
+
+        if (head == NULL) {
+                CU_DEBUG("others is null.");
+                goto out;
+        }
+
+        while (tmp) {
+                if (compare_parent(tmp->parent, parent) &&
+                    tmp->type == TYPE_PROP) {
+                        xmlNewProp(node, BAD_CAST tmp->name, BAD_CAST tmp->value);
+                        if (tmp == head) {
+                                head = head->next;
+                        } else {
+                                last->next = tmp->next;
+                        }
+
+                        del = tmp;
+                        tmp = tmp->next;
+                        cleanup_node_of_others(del);
+                } else {
+                        last = tmp;
+                        tmp = tmp->next;
+                }
+        }
+
+out:
+        return head;
+}
+
+static struct others *others_to_xml(xmlNodePtr root,
+                         struct others *head,
+                         const char *parent)
+{
+        struct others *tmp = head;
+        struct others *last = NULL;
+        xmlNodePtr new_node = NULL;
+
+        if (head == NULL) {
+                CU_DEBUG("others is null.");
+                goto out;
+        }
+
+        /* fetch all node items from others to build structure of xml */
+        while (tmp) {
+                if (compare_parent(tmp->parent, parent) &&
+                    tmp->type == TYPE_NODE) {
+                        new_node = xmlNewChild(root, NULL,
+                                               BAD_CAST tmp->name,
+                                               BAD_CAST tmp->value);
+
+                        if (new_node == NULL) {
+                                 CU_DEBUG("xmlNewChild failed.");
+                                 goto out;
+                        }
+
+                        if (tmp == head) {
+                                head = head->next;
+                        } else {
+                                last->next = tmp->next;
+                        }
+
+                        cleanup_node_of_others(tmp);
+
+                        /* find all properties of this node and build new tag in xml */
+                        head = props_to_xml(new_node,
+                                            head,
+                                            (char *)new_node->name);
+
+                        /* recursive build sub node of tmp */
+                        head = others_to_xml(new_node,
+                                             head,
+                                             (char *)new_node->name);
+                        tmp = head;
+                } else {
+                        last = tmp;
+                        tmp = tmp->next;
+                }
+        }
+
+out:
+        return head;
+}
+
+static struct others *add_node_to_others(struct others *head,
+                                         const char *name,
+                                         const char *value,
+                                         enum others_type type,
+                                         const char *parent)
+{
+        struct others *new_node = NULL;
+
+        new_node = calloc(1, sizeof(*new_node));
+        if (new_node == NULL) {
+                CU_DEBUG("calloc failed.");
+                return NULL;
+        }
+
+        if (name == NULL) {
+                CU_DEBUG("name is null");
+                return NULL;
+        }
+        new_node->name = strdup(name);
+
+        if (value) {
+                new_node->value = strdup(value);
+        } else {
+                new_node->value = NULL;
+        }
+
+        new_node->type = type;
+
+        if (parent) {
+                new_node->parent = strdup(parent);
+        } else {
+                new_node->parent = NULL;
+        }
+
+        if (head == NULL) {
+                new_node->next = NULL;
+        } else {
+                new_node->next = head;
+        }
+
+        return new_node;
+}
+
 static char *disk_block_xml(xmlNodePtr root, struct disk_device *dev)
 {
         xmlNodePtr disk;
-- 
1.7.1




More information about the Libvirt-cim mailing list