Fri, 05 Dec 2003 08:45:05 +0000
[gaim-migrate @ 8403]
Set the buttons in the account editor to be insensitive when nothing is
selected.
| 7131 | 1 | /** |
| 2 | * @file xmlnode.c XML DOM functions | |
| 3 | * | |
| 4 | * gaim | |
| 5 | * | |
| 6 | * Copyright (C) 2003 Nathan Walp <faceprint@faceprint.com> | |
| 7 | * | |
| 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. | |
| 12 | * | |
| 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 | */ | |
| 22 | ||
| 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 */ | |
| 27 | ||
| 28 | #include "internal.h" | |
| 29 | ||
| 30 | #include <string.h> | |
| 31 | #include <glib.h> | |
| 32 | ||
| 33 | #include "xmlnode.h" | |
| 34 | ||
| 35 | static xmlnode* | |
| 36 | new_node(const char *name, NodeType type) | |
| 37 | { | |
| 38 | xmlnode *node = g_new0(xmlnode, 1); | |
| 39 | if(name) | |
| 40 | node->name = g_strdup(name); | |
| 41 | node->type = type; | |
| 42 | ||
| 43 | return node; | |
| 44 | } | |
| 45 | ||
| 46 | xmlnode* | |
| 47 | xmlnode_new(const char *name) | |
| 48 | { | |
| 49 | g_return_val_if_fail(name != NULL, NULL); | |
| 50 | ||
| 51 | return new_node(name, NODE_TYPE_TAG); | |
| 52 | } | |
| 53 | ||
| 54 | xmlnode *xmlnode_new_child(xmlnode *parent, const char *name) | |
| 55 | { | |
| 56 | xmlnode *node; | |
| 57 | ||
| 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; | |
| 66 | } | |
| 67 | ||
| 68 | void | |
| 69 | xmlnode_insert_child(xmlnode *parent, xmlnode *child) | |
| 70 | { | |
| 71 | g_return_if_fail(parent != NULL); | |
| 72 | g_return_if_fail(child != NULL); | |
| 73 | ||
| 74 | child->parent = parent; | |
| 75 | ||
| 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 | } | |
| 83 | } | |
| 84 | ||
| 85 | void | |
| 86 | xmlnode_insert_data(xmlnode *parent, const char *data, size_t size) | |
| 87 | { | |
| 88 | xmlnode *node; | |
| 89 | size_t real_size; | |
| 90 | ||
| 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); | |
| 103 | } | |
| 104 | ||
| 105 | void | |
| 106 | xmlnode_remove_attrib(xmlnode *node, const char *attr) | |
| 107 | { | |
| 108 | xmlnode *attr_node, *sibling = NULL; | |
| 109 | ||
| 110 | g_return_if_fail(node != NULL); | |
| 111 | g_return_if_fail(attr != NULL); | |
| 112 | ||
| 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 | } | |
| 127 | } | |
| 128 | ||
| 129 | void | |
| 130 | xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value) | |
| 131 | { | |
| 132 | xmlnode *attrib_node; | |
| 133 | ||
| 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); | |
| 145 | } | |
| 146 | ||
| 147 | const char* | |
| 148 | xmlnode_get_attrib(xmlnode *node, const char *attr) | |
| 149 | { | |
| 150 | xmlnode *x; | |
| 151 | ||
| 152 | g_return_val_if_fail(node != NULL, NULL); | |
| 153 | ||
| 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; | |
| 161 | } | |
| 162 | ||
| 163 | void xmlnode_free(xmlnode *node) | |
| 164 | { | |
| 165 | xmlnode *x, *y; | |
| 166 | ||
| 167 | g_return_if_fail(node != NULL); | |
| 168 | ||
| 169 | x = node->child; | |
| 170 | while(x) { | |
| 171 | y = x->next; | |
| 172 | xmlnode_free(x); | |
| 173 | x = y; | |
| 174 | } | |
| 175 | ||
| 176 | if(node->name) | |
| 177 | g_free(node->name); | |
| 178 | if(node->data) | |
| 179 | g_free(node->data); | |
| 180 | g_free(node); | |
| 181 | } | |
| 182 | ||
| 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; | |
| 189 | ||
| 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]; | |
| 195 | ||
| 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 | } | |
| 202 | ||
| 203 | if(child_name && ret) | |
| 204 | ret = xmlnode_get_child(x, child_name); | |
| 205 | ||
| 206 | g_strfreev(names); | |
| 207 | return ret; | |
| 208 | } | |
| 209 | ||
| 210 | char * | |
| 211 | xmlnode_get_data(xmlnode *node) | |
| 212 | { | |
| 213 | GString *str = NULL; | |
| 214 | char *ret = NULL; | |
| 215 | xmlnode *c; | |
| 216 | ||
| 217 | g_return_val_if_fail(node != NULL, NULL); | |
| 218 | ||
| 219 | ||
| 220 | for(c = node->child; c; c = c->next) { | |
| 221 | if(c->type == NODE_TYPE_DATA) { | |
| 222 | if(!str) | |
| 223 | str = g_string_new(""); | |
| 224 | str = g_string_append_len(str, c->data, c->data_sz); | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | if(str) { | |
| 229 | ret = str->str; | |
| 230 | g_string_free(str, FALSE); | |
| 231 | } | |
| 232 | ||
| 233 | return ret; | |
| 234 | } | |
| 235 | ||
| 7642 | 236 | char *xmlnode_to_str(xmlnode *node, int *len) |
| 7131 | 237 | { |
| 238 | char *ret; | |
| 239 | GString *text = g_string_new(""); | |
| 240 | xmlnode *c; | |
| 241 | char *node_name, *esc, *esc2; | |
| 242 | gboolean need_end = FALSE; | |
| 243 | ||
| 244 | node_name = g_markup_escape_text(node->name, -1); | |
| 245 | g_string_append_printf(text, "<%s", node_name); | |
| 246 | ||
| 247 | ||
| 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 | } | |
| 260 | ||
| 261 | if(need_end) { | |
| 262 | text = g_string_append_c(text, '>'); | |
| 263 | ||
| 264 | for(c = node->child; c; c = c->next) | |
| 265 | { | |
| 266 | if(c->type == NODE_TYPE_TAG) { | |
| 7642 | 267 | int esc_len; |
| 268 | esc = xmlnode_to_str(c, &esc_len); | |
| 269 | text = g_string_append_len(text, esc, esc_len); | |
| 7131 | 270 | g_free(esc); |
| 271 | } else if(c->type == NODE_TYPE_DATA) { | |
| 272 | esc = g_markup_escape_text(c->data, c->data_sz); | |
| 7642 | 273 | text = g_string_append(text, esc); |
| 7131 | 274 | g_free(esc); |
| 275 | } | |
| 276 | } | |
| 277 | ||
| 278 | g_string_append_printf(text, "</%s>", node_name); | |
| 279 | } else { | |
| 280 | g_string_append_printf(text, "/>"); | |
| 281 | } | |
| 282 | ||
| 283 | g_free(node_name); | |
| 284 | ||
| 285 | ret = text->str; | |
| 7642 | 286 | if(len) |
| 287 | *len = text->len; | |
| 7131 | 288 | g_string_free(text, FALSE); |
| 289 | return ret; | |
| 290 | } | |
| 291 | ||
| 292 | struct _xmlnode_parser_data { | |
| 293 | xmlnode *current; | |
| 294 | }; | |
| 295 | ||
| 296 | static void | |
| 297 | xmlnode_parser_element_start(GMarkupParseContext *context, | |
| 298 | const char *element_name, const char **attrib_names, | |
| 299 | const char **attrib_values, gpointer user_data, GError **error) | |
| 300 | { | |
| 301 | struct _xmlnode_parser_data *xpd = user_data; | |
| 302 | xmlnode *node; | |
| 303 | int i; | |
| 304 | ||
| 305 | if(!element_name) { | |
| 306 | return; | |
| 307 | } else { | |
| 308 | if(xpd->current) | |
| 309 | node = xmlnode_new_child(xpd->current, element_name); | |
| 310 | else | |
| 311 | node = xmlnode_new(element_name); | |
| 312 | ||
| 313 | for(i=0; attrib_names[i]; i++) | |
| 314 | xmlnode_set_attrib(node, attrib_names[i], attrib_values[i]); | |
| 315 | ||
| 316 | xpd->current = node; | |
| 317 | } | |
| 318 | } | |
| 319 | ||
| 320 | static void | |
| 321 | xmlnode_parser_element_end(GMarkupParseContext *context, | |
| 322 | const char *element_name, gpointer user_data, GError **error) | |
| 323 | { | |
| 324 | struct _xmlnode_parser_data *xpd = user_data; | |
| 325 | ||
| 326 | if(!element_name || !xpd->current) | |
| 327 | return; | |
| 328 | ||
| 329 | if(xpd->current->parent) { | |
| 330 | if(!strcmp(xpd->current->name, element_name)) | |
| 331 | xpd->current = xpd->current->parent; | |
| 332 | } | |
| 333 | } | |
| 334 | ||
| 335 | static void | |
| 336 | xmlnode_parser_element_text(GMarkupParseContext *context, const char *text, | |
| 337 | gsize text_len, gpointer user_data, GError **error) | |
| 338 | { | |
| 339 | struct _xmlnode_parser_data *xpd = user_data; | |
| 340 | ||
| 341 | if(!xpd->current) | |
| 342 | return; | |
| 343 | ||
| 344 | if(!text || !text_len) | |
| 345 | return; | |
| 346 | ||
| 347 | xmlnode_insert_data(xpd->current, text, text_len); | |
| 348 | } | |
| 349 | ||
| 350 | static GMarkupParser xmlnode_parser = { | |
| 351 | xmlnode_parser_element_start, | |
| 352 | xmlnode_parser_element_end, | |
| 353 | xmlnode_parser_element_text, | |
| 354 | NULL, | |
| 355 | NULL | |
| 356 | }; | |
| 357 | ||
| 358 | ||
| 359 | xmlnode *xmlnode_from_str(const char *str, size_t size) | |
| 360 | { | |
| 361 | struct _xmlnode_parser_data *xpd = g_new0(struct _xmlnode_parser_data, 1); | |
| 362 | xmlnode *ret; | |
| 363 | GMarkupParseContext *context; | |
| 364 | size_t real_size = size == -1 ? strlen(str) : size; | |
| 365 | ||
| 366 | context = g_markup_parse_context_new(&xmlnode_parser, 0, xpd, NULL); | |
| 367 | ||
| 368 | if(!g_markup_parse_context_parse(context, str, real_size, NULL)) { | |
| 369 | while(xpd->current && xpd->current->parent) | |
| 370 | xpd->current = xpd->current->parent; | |
| 371 | if(xpd->current) | |
| 372 | xmlnode_free(xpd->current); | |
| 373 | xpd->current = NULL; | |
| 374 | } | |
| 375 | g_markup_parse_context_free(context); | |
| 376 | ||
| 377 | ret = xpd->current; | |
| 378 | g_free(xpd); | |
| 379 | return ret; | |
| 380 | } |