[8f243ad] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple XML (stream) parse tree handling code (Jabber/XMPP, mainly) * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[8f243ad] | 7 | * * |
---|
[4f7255d] | 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. * |
---|
[8f243ad] | 12 | * * |
---|
[4f7255d] | 13 | * This program is distributed in the hope that it will be useful, * |
---|
[8f243ad] | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
[4f7255d] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
[8f243ad] | 17 | * * |
---|
[4f7255d] | 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
[8f243ad] | 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
[f06894d] | 24 | #ifndef _XMLTREE_H |
---|
| 25 | #define _XMLTREE_H |
---|
| 26 | |
---|
[5ebff60] | 27 | typedef enum { |
---|
| 28 | XT_COMPLETE = 1, /* </tag> reached */ |
---|
| 29 | XT_SEEN = 2, /* Handler called (or not defined) */ |
---|
[8f243ad] | 30 | } xt_flags; |
---|
| 31 | |
---|
[5ebff60] | 32 | typedef enum { |
---|
| 33 | XT_ABORT, /* Abort, don't handle the rest anymore */ |
---|
| 34 | XT_HANDLED, /* Handled this tag properly, go to the next one */ |
---|
| 35 | XT_NEXT /* Try if there's another matching handler */ |
---|
[8f243ad] | 36 | } xt_status; |
---|
| 37 | |
---|
[5ebff60] | 38 | struct xt_attr { |
---|
[8f243ad] | 39 | char *key, *value; |
---|
| 40 | }; |
---|
| 41 | |
---|
[5ebff60] | 42 | struct xt_node { |
---|
[8f243ad] | 43 | struct xt_node *parent; |
---|
| 44 | struct xt_node *children; |
---|
[5ebff60] | 45 | |
---|
[8f243ad] | 46 | char *name; |
---|
| 47 | struct xt_attr *attr; |
---|
| 48 | char *text; |
---|
| 49 | int text_len; |
---|
[5ebff60] | 50 | |
---|
[8f243ad] | 51 | struct xt_node *next; |
---|
| 52 | xt_flags flags; |
---|
| 53 | }; |
---|
| 54 | |
---|
[5ebff60] | 55 | typedef xt_status (*xt_handler_func) (struct xt_node *node, gpointer data); |
---|
[8f243ad] | 56 | |
---|
[5ebff60] | 57 | struct xt_handler_entry { |
---|
[8f243ad] | 58 | char *name, *parent; |
---|
| 59 | xt_handler_func func; |
---|
| 60 | }; |
---|
| 61 | |
---|
[5ebff60] | 62 | struct xt_parser { |
---|
[8f243ad] | 63 | GMarkupParseContext *parser; |
---|
| 64 | struct xt_node *root; |
---|
| 65 | struct xt_node *cur; |
---|
[5ebff60] | 66 | |
---|
[4bbcba3] | 67 | const struct xt_handler_entry *handlers; |
---|
[8f243ad] | 68 | gpointer data; |
---|
[5ebff60] | 69 | |
---|
[8f243ad] | 70 | GError *gerr; |
---|
| 71 | }; |
---|
| 72 | |
---|
[5ebff60] | 73 | struct xt_parser *xt_new(const struct xt_handler_entry *handlers, gpointer data); |
---|
| 74 | void xt_reset(struct xt_parser *xt); |
---|
| 75 | int xt_feed(struct xt_parser *xt, const char *text, int text_len); |
---|
| 76 | int xt_handle(struct xt_parser *xt, struct xt_node *node, int depth); |
---|
| 77 | void xt_cleanup(struct xt_parser *xt, struct xt_node *node, int depth); |
---|
| 78 | struct xt_node *xt_from_string(const char *in, int text_len); |
---|
| 79 | char *xt_to_string(struct xt_node *node); |
---|
| 80 | char *xt_to_string_i(struct xt_node *node); |
---|
| 81 | void xt_print(struct xt_node *node); |
---|
| 82 | struct xt_node *xt_dup(struct xt_node *node); |
---|
| 83 | void xt_free_node(struct xt_node *node); |
---|
| 84 | void xt_free(struct xt_parser *xt); |
---|
| 85 | struct xt_node *xt_find_node(struct xt_node *node, const char *name); |
---|
| 86 | struct xt_node *xt_find_path(struct xt_node *node, const char *name); |
---|
| 87 | char *xt_find_attr(struct xt_node *node, const char *key); |
---|
| 88 | struct xt_node *xt_find_node_by_attr(struct xt_node *xt, const char *tag, const char *key, const char *value); |
---|
[8f243ad] | 89 | |
---|
[5ebff60] | 90 | struct xt_node *xt_new_node(char *name, const char *text, struct xt_node *children); |
---|
| 91 | void xt_add_child(struct xt_node *parent, struct xt_node *child); |
---|
| 92 | void xt_insert_child(struct xt_node *parent, struct xt_node *child); |
---|
| 93 | void xt_add_attr(struct xt_node *node, const char *key, const char *value); |
---|
| 94 | int xt_remove_attr(struct xt_node *node, const char *key); |
---|
[f06894d] | 95 | |
---|
| 96 | #endif |
---|