diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-09-18 18:16:31 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-09-18 18:16:31 +0200 |
commit | c17cc9a2bcf393b8e6af553dd14a129ec9cc9462 (patch) | |
tree | 1645b014c46a2dd01087ff2e74abb918cc7b5efe /src/HtmlTree.c | |
parent | 534c441fd8172322ff5eaad54a1d26b9d8492c39 (diff) |
Remove unecessary malloced node for child nodes (the child node item with the next item included is malloced instead)
Diffstat (limited to 'src/HtmlTree.c')
-rw-r--r-- | src/HtmlTree.c | 57 |
1 files changed, 25 insertions, 32 deletions
diff --git a/src/HtmlTree.c b/src/HtmlTree.c index f4deb88..faef08a 100644 --- a/src/HtmlTree.c +++ b/src/HtmlTree.c @@ -1,5 +1,6 @@ #include "../include/HtmlTree.h" #include <stdlib.h> +#include <assert.h> static void html_node_deinit(HtmlNode *self); @@ -13,45 +14,35 @@ static void html_node_init(HtmlNode *self) { self->node_type = HTML_NODE_NODE; } +/* |parent| can't be NULL */ static HtmlNode* html_node_create(HtmlStringView name_or_value, HtmlNode *parent, HtmlNodeType node_type) { - HtmlNode *new_node = malloc(sizeof(HtmlNode)); - if(!new_node) + HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild)); + assert(parent); + if(!node_child) return NULL; - new_node->name_or_value = name_or_value; - new_node->parent = parent; - new_node->first_child = NULL; - new_node->last_child = NULL; - new_node->first_attr = NULL; - new_node->node_type = node_type; - - if(parent) { - HtmlNodeChild *node_child = malloc(sizeof(HtmlNodeChild)); - if(!node_child) { - free(new_node); - return NULL; - } - - node_child->node = new_node; - node_child->next = NULL; - - if(!parent->first_child) { - parent->first_child = node_child; - parent->last_child = node_child; - } else { - parent->last_child->next = node_child; - parent->last_child = node_child; - } + node_child->node.name_or_value = name_or_value; + node_child->node.parent = parent; + node_child->node.first_child = NULL; + node_child->node.last_child = NULL; + node_child->node.first_attr = NULL; + node_child->node.node_type = node_type; + + node_child->next = NULL; + + if(!parent->first_child) { + parent->first_child = node_child; + parent->last_child = node_child; + } else { + parent->last_child->next = node_child; + parent->last_child = node_child; } - return new_node; + return &node_child->node; } static void html_node_child_deinit(HtmlNodeChild *self) { - html_node_deinit(self->node); - free(self->node); - self->node = NULL; - + html_node_deinit(&self->node); if(self->next) { html_node_child_deinit(self->next); free(self->next); @@ -87,6 +78,7 @@ void html_node_deinit(HtmlNode *self) { } typedef struct { + HtmlNode *root_node; HtmlNode *current_node; HtmlAttribute *current_node_last_attribute; } ParseUserdata; @@ -106,7 +98,7 @@ static int parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, voi } case HTML_PARSE_TAG_END: { HtmlNode *parent_node = parse_userdata->current_node->parent; - if(parent_node) { + if(parent_node && parent_node != parse_userdata->root_node) { parse_userdata->current_node = parent_node; parse_userdata->current_node_last_attribute = NULL; } @@ -148,6 +140,7 @@ int html_parse_to_tree(HtmlTree *self, const char *html_source, size_t len) { ParseUserdata parse_userdata; html_node_init(&self->root_node); + parse_userdata.root_node = &self->root_node; parse_userdata.current_node = &self->root_node; parse_userdata.current_node_last_attribute = NULL; result = html_parser_parse(html_source, len, parse_callback, &parse_userdata); |