annotate src/xmlnode.c @ 7305:fa25f82dda21

[gaim-migrate @ 7889] choice is a good thing committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Mon, 20 Oct 2003 05:08:05 +0000
parents af889fd531d0
children 9008b5be4275
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7131
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
1 /**
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
2 * @file xmlnode.c XML DOM functions
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
3 *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
4 * gaim
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
5 *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
6 * Copyright (C) 2003 Nathan Walp <faceprint@faceprint.com>
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
7 *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
11 * (at your option) any later version.
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
12 *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
16 * GNU General Public License for more details.
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
17 *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
21 */
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
22
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
23 /* A lot of this code at least resembles the code in libxode, but since
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
24 * libxode uses memory pools that we simply have no need for, I decided to
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
25 * write my own stuff. Also, re-writing this lets me be as lightweight
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
26 * as I want to be. Thank you libxode for giving me a good starting point */
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
27
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
28 #include "internal.h"
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
29
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
30 #include <string.h>
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
31 #include <glib.h>
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
32
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
33 #include "xmlnode.h"
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
34
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
35 static xmlnode*
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
36 new_node(const char *name, NodeType type)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
37 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
38 xmlnode *node = g_new0(xmlnode, 1);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
39 if(name)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
40 node->name = g_strdup(name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
41 node->type = type;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
42
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
43 return node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
44 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
45
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
46 xmlnode*
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
47 xmlnode_new(const char *name)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
48 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
49 g_return_val_if_fail(name != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
50
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
51 return new_node(name, NODE_TYPE_TAG);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
52 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
53
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
54 xmlnode *xmlnode_new_child(xmlnode *parent, const char *name)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
55 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
56 xmlnode *node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
57
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
58 g_return_val_if_fail(parent != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
59 g_return_val_if_fail(name != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
60
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
61 node = new_node(name, NODE_TYPE_TAG);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
62
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
63 xmlnode_insert_child(parent, node);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
64
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
65 return node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
66 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
67
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
68 void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
69 xmlnode_insert_child(xmlnode *parent, xmlnode *child)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
70 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
71 g_return_if_fail(parent != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
72 g_return_if_fail(child != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
73
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
74 child->parent = parent;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
75
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
76 if(parent->child) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
77 xmlnode *x;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
78 for(x = parent->child; x->next; x = x->next);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
79 x->next = child;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
80 } else {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
81 parent->child = child;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
82 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
83 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
84
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
85 void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
86 xmlnode_insert_data(xmlnode *parent, const char *data, size_t size)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
87 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
88 xmlnode *node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
89 size_t real_size;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
90
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
91 g_return_if_fail(parent != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
92 g_return_if_fail(data != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
93 g_return_if_fail(size != 0);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
94
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
95 real_size = size == -1 ? strlen(data) : size;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
96
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
97 node = new_node(NULL, NODE_TYPE_DATA);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
98
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
99 node->data = g_memdup(data, real_size);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
100 node->data_sz = real_size;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
101
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
102 xmlnode_insert_child(parent, node);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
103 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
104
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
105 void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
106 xmlnode_remove_attrib(xmlnode *node, const char *attr)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
107 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
108 xmlnode *attr_node, *sibling = NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
109
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
110 g_return_if_fail(node != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
111 g_return_if_fail(attr != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
112
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
113 for(attr_node = node->child; attr_node; attr_node = attr_node->next)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
114 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
115 if(attr_node->type == NODE_TYPE_ATTRIB &&
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
116 !strcmp(attr_node->name, attr)) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
117 if(node->child == attr_node) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
118 node->child = attr_node->next;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
119 } else {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
120 sibling->next = attr_node->next;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
121 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
122 xmlnode_free(attr_node);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
123 return;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
124 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
125 sibling = attr_node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
126 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
127 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
128
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
129 void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
130 xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
131 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
132 xmlnode *attrib_node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
133
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
134 g_return_if_fail(node != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
135 g_return_if_fail(attr != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
136 g_return_if_fail(value != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
137
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
138 xmlnode_remove_attrib(node, attr);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
139
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
140 attrib_node = new_node(attr, NODE_TYPE_ATTRIB);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
141
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
142 attrib_node->data = g_strdup(value);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
143
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
144 xmlnode_insert_child(node, attrib_node);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
145 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
146
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
147 const char*
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
148 xmlnode_get_attrib(xmlnode *node, const char *attr)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
149 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
150 xmlnode *x;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
151
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
152 g_return_val_if_fail(node != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
153
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
154 for(x = node->child; x; x = x->next) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
155 if(x->type == NODE_TYPE_ATTRIB && !strcmp(attr, x->name)) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
156 return x->data;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
157 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
158 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
159
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
160 return NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
161 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
162
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
163 void xmlnode_free(xmlnode *node)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
164 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
165 xmlnode *x, *y;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
166
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
167 g_return_if_fail(node != NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
168
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
169 x = node->child;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
170 while(x) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
171 y = x->next;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
172 xmlnode_free(x);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
173 x = y;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
174 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
175
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
176 if(node->name)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
177 g_free(node->name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
178 if(node->data)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
179 g_free(node->data);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
180 g_free(node);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
181 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
182
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
183 xmlnode*
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
184 xmlnode_get_child(xmlnode *parent, const char *name)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
185 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
186 xmlnode *x, *ret = NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
187 char **names;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
188 char *parent_name, *child_name;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
189
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
190 g_return_val_if_fail(parent != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
191
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
192 names = g_strsplit(name, "/", 2);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
193 parent_name = names[0];
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
194 child_name = names[1];
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
195
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
196 for(x = parent->child; x; x = x->next) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
197 if(x->type == NODE_TYPE_TAG && name && !strcmp(parent_name, x->name)) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
198 ret = x;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
199 break;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
200 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
201 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
202
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
203 if(child_name && ret)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
204 ret = xmlnode_get_child(x, child_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
205
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
206 g_strfreev(names);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
207 return ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
208 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
209
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
210 char *
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
211 xmlnode_get_data(xmlnode *node)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
212 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
213 GString *str = NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
214 char *ret = NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
215 xmlnode *c;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
216
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
217 g_return_val_if_fail(node != NULL, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
218
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
219
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
220 for(c = node->child; c; c = c->next) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
221 if(c->type == NODE_TYPE_DATA) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
222 if(!str)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
223 str = g_string_new("");
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
224 str = g_string_append_len(str, c->data, c->data_sz);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
225 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
226 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
227
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
228 if(str) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
229 ret = str->str;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
230 g_string_free(str, FALSE);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
231 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
232
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
233 return ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
234 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
235
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
236 char *xmlnode_to_str(xmlnode *node)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
237 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
238 char *ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
239 GString *text = g_string_new("");
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
240 xmlnode *c;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
241 char *node_name, *esc, *esc2;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
242 gboolean need_end = FALSE;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
243
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
244 node_name = g_markup_escape_text(node->name, -1);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
245 g_string_append_printf(text, "<%s", node_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
246
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
247
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
248 for(c = node->child; c; c = c->next)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
249 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
250 if(c->type == NODE_TYPE_ATTRIB) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
251 esc = g_markup_escape_text(c->name, -1);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
252 esc2 = g_markup_escape_text(c->data, -1);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
253 g_string_append_printf(text, " %s='%s'", esc, esc2);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
254 g_free(esc);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
255 g_free(esc2);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
256 } else if(c->type == NODE_TYPE_TAG || c->type == NODE_TYPE_DATA) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
257 need_end = TRUE;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
258 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
259 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
260
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
261 if(need_end) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
262 text = g_string_append_c(text, '>');
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
263
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
264 for(c = node->child; c; c = c->next)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
265 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
266 if(c->type == NODE_TYPE_TAG) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
267 esc = xmlnode_to_str(c);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
268 g_string_append_printf(text, "%s", esc);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
269 g_free(esc);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
270 } else if(c->type == NODE_TYPE_DATA) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
271 esc = g_markup_escape_text(c->data, c->data_sz);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
272 g_string_append_printf(text, "%s", esc);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
273 g_free(esc);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
274 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
275 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
276
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
277 g_string_append_printf(text, "</%s>", node_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
278 } else {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
279 g_string_append_printf(text, "/>");
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
280 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
281
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
282 g_free(node_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
283
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
284 ret = text->str;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
285 g_string_free(text, FALSE);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
286 return ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
287 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
288
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
289 struct _xmlnode_parser_data {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
290 xmlnode *current;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
291 };
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
292
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
293 static void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
294 xmlnode_parser_element_start(GMarkupParseContext *context,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
295 const char *element_name, const char **attrib_names,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
296 const char **attrib_values, gpointer user_data, GError **error)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
297 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
298 struct _xmlnode_parser_data *xpd = user_data;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
299 xmlnode *node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
300 int i;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
301
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
302 if(!element_name) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
303 return;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
304 } else {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
305 if(xpd->current)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
306 node = xmlnode_new_child(xpd->current, element_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
307 else
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
308 node = xmlnode_new(element_name);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
309
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
310 for(i=0; attrib_names[i]; i++)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
311 xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
312
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
313 xpd->current = node;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
314 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
315 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
316
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
317 static void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
318 xmlnode_parser_element_end(GMarkupParseContext *context,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
319 const char *element_name, gpointer user_data, GError **error)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
320 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
321 struct _xmlnode_parser_data *xpd = user_data;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
322
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
323 if(!element_name || !xpd->current)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
324 return;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
325
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
326 if(xpd->current->parent) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
327 if(!strcmp(xpd->current->name, element_name))
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
328 xpd->current = xpd->current->parent;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
329 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
330 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
331
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
332 static void
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
333 xmlnode_parser_element_text(GMarkupParseContext *context, const char *text,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
334 gsize text_len, gpointer user_data, GError **error)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
335 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
336 struct _xmlnode_parser_data *xpd = user_data;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
337
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
338 if(!xpd->current)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
339 return;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
340
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
341 if(!text || !text_len)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
342 return;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
343
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
344 xmlnode_insert_data(xpd->current, text, text_len);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
345 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
346
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
347 static GMarkupParser xmlnode_parser = {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
348 xmlnode_parser_element_start,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
349 xmlnode_parser_element_end,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
350 xmlnode_parser_element_text,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
351 NULL,
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
352 NULL
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
353 };
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
354
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
355
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
356 xmlnode *xmlnode_from_str(const char *str, size_t size)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
357 {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
358 struct _xmlnode_parser_data *xpd = g_new0(struct _xmlnode_parser_data, 1);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
359 xmlnode *ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
360 GMarkupParseContext *context;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
361 size_t real_size = size == -1 ? strlen(str) : size;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
362
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
363 context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
364
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
365 if(!g_markup_parse_context_parse(context, str, real_size, NULL)) {
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
366 while(xpd->current && xpd->current->parent)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
367 xpd->current = xpd->current->parent;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
368 if(xpd->current)
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
369 xmlnode_free(xpd->current);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
370 xpd->current = NULL;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
371 }
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
372 g_markup_parse_context_free(context);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
373
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
374 ret = xpd->current;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
375 g_free(xpd);
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
376 return ret;
af889fd531d0 [gaim-migrate @ 7698]
Nathan Walp <nwalp@pidgin.im>
parents:
diff changeset
377 }