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