summaryrefslogtreecommitdiff
path: root/libdleyna/server/xml-util.c
blob: ec11575386b0f47ca777238340a63374cf65c70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * dLeyna
 *
 * Copyright (C) 2012-2017 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Christophe Guiraud <christophe.guiraud@intel.com>
 *
 */

#include <string.h>
#include <stdlib.h>

#include "xml-util.h"

static xmlNode *prv_get_child_node(xmlNode *node, va_list args)
{
	const gchar *name;

	name = va_arg(args, const gchar *);
	while (name != NULL) {
		node = node->children;
		while (node != NULL) {
			if (node->name != NULL &&
			    !strcmp(name, (char *)node->name))
				break;

			node = node->next;
		}

		if (node == NULL)
			break;

		name = va_arg(args, const gchar *);
	}

	return node;
}

static GList *prv_get_children_list(xmlNode *node, const gchar *name)
{
	GList *child_list = NULL;

	node = node->children;
	while (node != NULL) {
		if (node->name != NULL &&
		    !strcmp(name, (char *)node->name))
			child_list = g_list_prepend(child_list, node);

		node = node->next;
	}

	return child_list;
}

GList *xml_util_get_child_string_list_content_by_name(xmlNode *node, ...)
{
	xmlChar *content;
	va_list args;
	GList *child_list = NULL;
	GList *next;
	GList *str_list = NULL;
	xmlNode *child_list_node;
	xmlNode *child_node;

	va_start(args, node);

	child_node = prv_get_child_node(node, args);

	va_end(args);

	if (child_node != NULL) {
		child_list = prv_get_children_list(child_node->parent,
					(const gchar *)child_node->name);
		next = child_list;
		while (next) {
			child_list_node = (xmlNode *)next->data;

			content = xmlNodeGetContent(child_list_node);

			if (content != NULL) {
				str_list = g_list_prepend(str_list,
						  g_strdup((gchar *)content));

				xmlFree(content);
			}

			next = g_list_next(next);
		}

		g_list_free(child_list);
	}

	return str_list;
}

gchar *xml_util_get_child_string_content_by_name(xmlNode *node, ...)
{
	xmlChar *content;
	va_list args;
	gchar *str = NULL;
	xmlNode *child_node;

	va_start(args, node);

	child_node = prv_get_child_node(node, args);

	va_end(args);

	if (child_node != NULL) {
		content = xmlNodeGetContent(child_node);

		if (content != NULL) {
			str = g_strdup((gchar *)content);

			xmlFree(content);
		}
	}

	return str;
}