diff options
author | John-Mark Bell <jmb@netsurf-browser.org> | 2012-11-09 23:22:19 +0000 |
---|---|---|
committer | John-Mark Bell <jmb@netsurf-browser.org> | 2012-11-09 23:23:04 +0000 |
commit | 4d58ed656248d975e0394fbfec66215a45e74dfa (patch) | |
tree | 5d554b8481badddabd7f9667961ec481bd655091 /utils | |
parent | 79bd44fcb4bb88c7fe23ef0c7fb62786eb340ca5 (diff) | |
download | netsurf-4d58ed656248d975e0394fbfec66215a45e74dfa.tar.gz netsurf-4d58ed656248d975e0394fbfec66215a45e74dfa.tar.bz2 |
Port hotlist load/save to libdom.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/domutils.c | 94 | ||||
-rw-r--r-- | utils/domutils.h | 15 |
2 files changed, 108 insertions, 1 deletions
diff --git a/utils/domutils.c b/utils/domutils.c index 20d76cdcc..741148913 100644 --- a/utils/domutils.c +++ b/utils/domutils.c @@ -17,6 +17,7 @@ */ #include <dom/dom.h> +#include <dom/bindings/hubbub/parser.h> #include "utils/config.h" #include "utils/log.h" @@ -64,3 +65,96 @@ dom_node *find_first_named_dom_element(dom_node *parent, lwc_string *element_nam return element; } + +void domutils_iterate_child_elements(dom_node *parent, + domutils_iterate_cb cb, void *ctx) +{ + dom_nodelist *children; + uint32_t index, num_children; + dom_exception error; + + error = dom_node_get_child_nodes(parent, &children); + if (error != DOM_NO_ERR || children == NULL) + return; + + error = dom_nodelist_get_length(children, &num_children); + if (error != DOM_NO_ERR) { + dom_nodelist_unref(children); + return; + } + + for (index = 0; index < num_children; index++) { + dom_node *child; + dom_node_type type; + + error = dom_nodelist_item(children, index, &child); + if (error != DOM_NO_ERR) { + dom_nodelist_unref(children); + return; + } + + error = dom_node_get_node_type(child, &type); + if (error == DOM_NO_ERR && type == DOM_ELEMENT_NODE) { + if (cb(child, ctx) == false) { + dom_node_unref(child); + dom_nodelist_unref(children); + return; + } + } + + dom_node_unref(child); + } + + dom_nodelist_unref(children); +} + +static void ignore_dom_msg(uint32_t severity, void *ctx, const char *msg, ...) +{ +} + +dom_document *domutils_parse_file(const char *filename, const char *encoding) +{ + dom_hubbub_error error; + dom_hubbub_parser *parser; + dom_document *document; + FILE *fp = NULL; +#define BUF_SIZE 512 + uint8_t buf[BUF_SIZE]; + + fp = fopen(filename, "r"); + if (fp == NULL) { + return NULL; + } + + parser = dom_hubbub_parser_create(encoding, false, false, + ignore_dom_msg, NULL, NULL, &document); + if (parser == NULL) { + fclose(fp); + return NULL; + } + + while (feof(fp) == 0) { + size_t read = fread(buf, sizeof(buf[0]), BUF_SIZE, fp); + + error = dom_hubbub_parser_parse_chunk(parser, buf, read); + if (error != DOM_HUBBUB_OK) { + dom_node_unref(document); + dom_hubbub_parser_destroy(parser); + fclose(fp); + return NULL; + } + } + + error = dom_hubbub_parser_completed(parser); + if (error != DOM_HUBBUB_OK) { + dom_node_unref(document); + dom_hubbub_parser_destroy(parser); + fclose(fp); + return NULL; + } + + dom_hubbub_parser_destroy(parser); + + return document; +} + diff --git a/utils/domutils.h b/utils/domutils.h index ecdf2bc4a..6dd62fc8a 100644 --- a/utils/domutils.h +++ b/utils/domutils.h @@ -19,6 +19,19 @@ #ifndef _NETSURF_UTILS_DOMUTILS_H_ #define _NETSURF_UTILS_DOMUTILS_H_ -dom_node *find_first_named_dom_element(dom_node *parent, lwc_string *element_name); +#include <stdbool.h> + +#include <dom/dom.h> + +typedef bool (*domutils_iterate_cb)(dom_node *node, void *ctx); + +dom_node *find_first_named_dom_element(dom_node *parent, + lwc_string *element_name); + +void domutils_iterate_child_elements(dom_node *parent, + domutils_iterate_cb cb, void *ctx); + +dom_document *domutils_parse_file(const char *filename, + const char *encoding); #endif |